src/size-list.js
/**
 * Parses the text representation of "sizes" into SizeList object.
 *
 * There could be any number of size options within the SizeList. They are tried
 * in the order they were defined. The final size option must not have "media"
 * condition specified. All other size options must have "media" condition
 * specified.
 *
 * See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#Attributes
 * See http://www.w3.org/html/wg/drafts/html/master/semantics.html#attr-img-sizes
 * @param {string} s
 * @param {boolean=} opt_allowPercentAsLength when parsing heights
 * @return {!SizeList}
 */
Parses the text representation of "sizes" into SizeList object.
There could be any number of size options within the SizeList. They are tried
in the order they were defined. The final size option must not have "media"
condition specified. All other size options must have "media" condition
specified.


Parses the text representation of " sizes " into SizeList object . There could be any number of size options within the SizeList . They are tried in the order they were defined . The final size option must not have " media " condition specified . All other size options must have " media " condition specified .
function parseSizeList(s, opt_allowPercentAsLength) {
  const sSizes = s.split(',');
  user().assert(sSizes.length > 0, 'sizes has to have at least one size');
  const sizes = [];
  sSizes.forEach(sSize => {
    sSize = sSize.replace(/\s+/g, ' ').trim();
    if (sSize.length == 0) {
      return;
    }
    let mediaStr;
    let sizeStr;
    const lastChar = sSize.charAt(sSize.length - 1);
    let div;
    let func = false;
    if (lastChar == ')') {
      func = true;
      let parens = 1;
      div = sSize.length - 2;
      for (; div >= 0; div--) {
        const c = sSize.charAt(div);
        if (c == '(') {
          parens--;
        } else if (c == ')') {
          parens++;
        }
        if (parens == 0) {
          break;
        }
      }
      const funcEnd = div - 1;
      if (div > 0) {
        div--;
        for (; div >= 0; div--) {
          const c = sSize.charAt(div);
          if (!(c == '%' || c == '-' || c == '_' ||
                (c >= 'a' && c <= 'z') ||
                (c >= 'A' && c <= 'Z') ||
                (c >= '0' && c <= '9'))) {
            break;
          }
        }
      }
      user().assert(div < funcEnd, 'Invalid CSS function in "%s"', sSize);
    } else {
      div = sSize.length - 2;
      for (; div >= 0; div--) {
        const c = sSize.charAt(div);
        if (!(c == '%' || c == '.' ||
              (c >= 'a' && c <= 'z') ||
              (c >= 'A' && c <= 'Z') ||
              (c >= '0' && c <= '9'))) {
          break;
        }
      }
    }
    if (div >= 0) {
      mediaStr = sSize.substring(0, div + 1).trim();
      sizeStr = sSize.substring(div + 1).trim();
    } else {
      sizeStr = sSize;
      mediaStr = undefined;
    }
    sizes.push({mediaQuery: mediaStr,
      size: func ? sizeStr :
          opt_allowPercentAsLength ?
              assertLengthOrPercent(sizeStr) :
              assertLength(sizeStr)});
  });
  return new SizeList(sizes);
}
function parseSizeList ( s , opt_allowPercentAsLength ) { const sSizes = s . split ( ',' ) ; user ( ) . assert ( sSizes . length > 0 , 'sizes has to have at least one size' ) ; const sizes = [ ] ; sSizes . forEach ( sSize => { sSize = sSize . replace ( / \s+ / g , ' ' ) . trim ( ) ; if ( sSize . length == 0 ) { return ; } let mediaStr ; let sizeStr ; const lastChar = sSize . charAt ( sSize . length - 1 ) ; let div ; let func = false ; if ( lastChar == ')' ) { func = true ; let parens = 1 ; div = sSize . length - 2 ; for ( ; div >= 0 ; div -- ) { const c = sSize . charAt ( div ) ; if ( c == '(' ) { parens -- ; } else if ( c == ')' ) { parens ++ ; } if ( parens == 0 ) { break ; } } const funcEnd = div - 1 ; if ( div > 0 ) { div -- ; for ( ; div >= 0 ; div -- ) { const c = sSize . charAt ( div ) ; if ( ! ( c == '%' || c == '-' || c == '_' || ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) || ( c >= '0' && c <= '9' ) ) ) { break ; } } } user ( ) . assert ( div < funcEnd , 'Invalid CSS function in "%s"' , sSize ) ; } else { div = sSize . length - 2 ; for ( ; div >= 0 ; div -- ) { const c = sSize . charAt ( div ) ; if ( ! ( c == '%' || c == '.' || ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) || ( c >= '0' && c <= '9' ) ) ) { break ; } } } if ( div >= 0 ) { mediaStr = sSize . substring ( 0 , div + 1 ) . trim ( ) ; sizeStr = sSize . substring ( div + 1 ) . trim ( ) ; } else { sizeStr = sSize ; mediaStr = undefined ; } sizes . push ( { mediaQuery : mediaStr , size : func ? sizeStr : opt_allowPercentAsLength ? assertLengthOrPercent ( sizeStr ) : assertLength ( sizeStr ) } ) ; } ) ; return new SizeList ( sizes ) ; }
Parses the text representation of "sizes" into SizeList object.
Parses the text representation of " sizes " into SizeList object .
// Process the expression from the end. // Value is the CSS function, e.g. `calc(50vw + 10px)`. // First, skip to the opening paren. // Then, skip to the begining to the function's name. // Value is the length or a percent: accept a wide range of values, // including invalid values - they will be later asserted to conform // to exact CSS length or percent value.