src/sanitizer.js
/**
 * Rewrites the URL attribute values. URLs are rewritten as following:
 * - If URL is absolute, it is not rewritten
 * - If URL is relative, it's rewritten as absolute against the source origin
 * - If resulting URL is a `http:` URL and it's for image, the URL is rewritten
 *   again to be served with AMP Cache (cdn.ampproject.org).
 *
 * @param {string} tagName
 * @param {string} attrName
 * @param {string} attrValue
 * @param {!Location} windowLocation
 * @return {string}
 * @private Visible for testing.
 */
Rewrites the URL attribute values. URLs are rewritten as following:
- If URL is absolute, it is not rewritten
- If URL is relative, it's rewritten as absolute against the source origin
- If resulting URL is a `http:` URL and it's for image, the URL is rewritten
again to be served with AMP Cache (cdn.ampproject.org).


Rewrites the URL attribute values . URLs are rewritten as following : - If URL is absolute it is not rewritten - If URL is relative it ' s rewritten as absolute against the source origin - If resulting URL is a ` http : ` URL and it ' s for image the URL is rewritten again to be served with AMP Cache ( cdn . ampproject . org ) .
function resolveUrlAttr(tagName, attrName, attrValue, windowLocation) {
  checkCorsUrl(attrValue);
  const isProxyHost = isProxyOrigin(windowLocation);
  const baseUrl = parseUrl(getSourceUrl(windowLocation));
  if (attrName == 'href' && !startsWith(attrValue, '#')) {
    return resolveRelativeUrl(attrValue, baseUrl);
  }
  if (attrName == 'src') {
    if (tagName == 'amp-img') {
      return resolveImageUrlAttr(attrValue, baseUrl, isProxyHost);
    }
    return resolveRelativeUrl(attrValue, baseUrl);
  }
  if (attrName == 'srcset') {
    let srcset;
    try {
      srcset = parseSrcset(attrValue);
    } catch (e) {
      user().error(TAG, 'Failed to parse srcset: ', e);
      return attrValue;
    }
    const sources = srcset.getSources();
    for (let i = 0; i < sources.length; i++) {
      sources[i].url = resolveImageUrlAttr(
          sources[i].url, baseUrl, isProxyHost);
    }
    return srcset.stringify();
  }
  return attrValue;
}
function resolveUrlAttr ( tagName , attrName , attrValue , windowLocation ) { checkCorsUrl ( attrValue ) ; const isProxyHost = isProxyOrigin ( windowLocation ) ; const baseUrl = parseUrl ( getSourceUrl ( windowLocation ) ) ; if ( attrName == 'href' && ! startsWith ( attrValue , '#' ) ) { return resolveRelativeUrl ( attrValue , baseUrl ) ; } if ( attrName == 'src' ) { if ( tagName == 'amp-img' ) { return resolveImageUrlAttr ( attrValue , baseUrl , isProxyHost ) ; } return resolveRelativeUrl ( attrValue , baseUrl ) ; } if ( attrName == 'srcset' ) { let srcset ; try { srcset = parseSrcset ( attrValue ) ; } catch ( e ) { user ( ) . error ( TAG , 'Failed to parse srcset: ' , e ) ; return attrValue ; } const sources = srcset . getSources ( ) ; for ( let i = 0 ; i < sources . length ; i ++ ) { sources [ i ] . url = resolveImageUrlAttr ( sources [ i ] . url , baseUrl , isProxyHost ) ; } return srcset . stringify ( ) ; } return attrValue ; }
Rewrites the URL attribute values.
Rewrites the URL attribute values .
// Do not fail the whole template just because one srcset is broken. // An AMP element will pick it up and report properly.