src/srcset.js
/**
 * Parses the text representation of srcset into Srcset object.
 * 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-srcset.
 * @param {string} s
 * @param {!Element=} opt_element
 * @return {!Srcset}
 */
Parses the text representation of srcset into Srcset object.
Parses the text representation of srcset into Srcset object .
function parseSrcset(s, opt_element) {
  const sSources = s.match(
      /\s*(?:[\S]*)(?:\s+(?:-?(?:\d+(?:\.(?:\d+)?)?|\.\d+)[a-zA-Z]))?(?:\s*,)?/g
  );
  user().assert(sSources.length > 0,
      'srcset has to have at least one source: %s',
      opt_element);
  const sources = [];
  for (let i = 0; i < sSources.length; i++) {
    let sSource = sSources[i].trim();
    if (sSource.substr(-1) == ',') {
      sSource = sSource.substr(0, sSource.length - 1).trim();
    }
    const parts = sSource.split(/\s+/, 2);
    if (parts.length == 0 ||
          parts.length == 1 && !parts[0] ||
          parts.length == 2 && !parts[0] && !parts[1]) {
      continue;
    }
    const url = parts[0];
    if (parts.length == 1 || parts.length == 2 && !parts[1]) {
      sources.push({url, width: undefined, dpr: 1});
    } else {
      const spec = parts[1].toLowerCase();
      const lastChar = spec.substring(spec.length - 1);
      if (lastChar == 'w') {
        sources.push({url, width: parseFloat(spec), dpr: undefined});
      } else if (lastChar == 'x') {
        sources.push({url, width: undefined, dpr: parseFloat(spec)});
      }
    }
  }
  return new Srcset(sources);
}
function parseSrcset ( s , opt_element ) { const sSources = s . match ( / \s*(?:[\S]*)(?:\s+(?:-?(?:\d+(?:\.(?:\d+)?)?|\.\d+)[a-zA-Z]))?(?:\s*,)? / g ) ; user ( ) . assert ( sSources . length > 0 , 'srcset has to have at least one source: %s' , opt_element ) ; const sources = [ ] ; for ( let i = 0 ; i < sSources . length ; i ++ ) { let sSource = sSources [ i ] . trim ( ) ; if ( sSource . substr ( - 1 ) == ',' ) { sSource = sSource . substr ( 0 , sSource . length - 1 ) . trim ( ) ; } const parts = sSource . split ( / \s+ / , 2 ) ; if ( parts . length == 0 || parts . length == 1 && ! parts [ 0 ] || parts . length == 2 && ! parts [ 0 ] && ! parts [ 1 ] ) { continue ; } const url = parts [ 0 ] ; if ( parts . length == 1 || parts . length == 2 && ! parts [ 1 ] ) { sources . push ( { url , width : undefined , dpr : 1 } ) ; } else { const spec = parts [ 1 ] . toLowerCase ( ) ; const lastChar = spec . substring ( spec . length - 1 ) ; if ( lastChar == 'w' ) { sources . push ( { url , width : parseFloat ( spec ) , dpr : undefined } ) ; } else if ( lastChar == 'x' ) { sources . push ( { url , width : undefined , dpr : parseFloat ( spec ) } ) ; } } } return new Srcset ( sources ) ; }
Parses the text representation of srcset into Srcset object.
Parses the text representation of srcset into Srcset object .
// General grammar: (URL [NUM[w|x]],)* // Example 1: "image1.png 100w, image2.png 50w" // Example 2: "image1.png 2x, image2.png" // Example 3: "image1,100w.png 100w, image2.png 50w" // If no "w" or "x" specified, we assume it's "1x".