How to use srcset - 10 common examples

To help you get started, we’ve selected a few srcset examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github interactivethings / catalog / packages / core / src / specimens / Image.js View on Github external
};

    const backgroundStyle = {
      ...(options.plain ? styles.plain : null),
      ...(options.light ? styles.light : null),
      ...(options.dark ? styles.dark : null),
      ...(options.plain && options.light ? styles.plain_light : null),
      ...(options.plain && options.dark ? styles.plain_dark : null)
    };

    // Deconstruct srcset strings
    const imageSrcset = srcset
      .parse(src)
      .map(img => ({ ...img, url: getPublicPath(img.url, catalog) }));
    const overlaySrcset = overlay
      ? srcset
          .parse(overlay)
          .map(img => ({ ...img, url: getPublicPath(img.url, catalog) }))
      : [];

    const fallbackSrc = imageSrcset[0].url;
    const fallbackOverlay = overlay ? overlaySrcset[0].url : undefined;

    return (
      <div>
        <div>
          <img src="{fallbackSrc}" srcset="{srcset.stringify(imageSrcset)}">
          {overlay &amp;&amp; (</div></div>
github lowwebtech / low-web-extension / src / scripts / main.js View on Github external
function cleanSrcset( srcset, width ){
  const parsed = srcsetUtil.parse(srcset);
  
  let cleanedSrcset
  if( true ){
    cleanedSrcset = getSmallestSrcset( parsed, width )
  }else{
    cleanedSrcset = noRetinaSrcset( parsed )
  }

  if( cleanedSrcset ){
    return srcsetUtil.stringify(cleanedSrcset)
  }else{
    return false
  }
}
  // chrome.tabs.onUpdated.addListener(
github interactivethings / catalog / packages / core / src / specimens / Image.js View on Github external
plain_dark: {
        background: theme.bgDark,
        padding: "20px"
      }
    };

    const backgroundStyle = {
      ...(options.plain ? styles.plain : null),
      ...(options.light ? styles.light : null),
      ...(options.dark ? styles.dark : null),
      ...(options.plain &amp;&amp; options.light ? styles.plain_light : null),
      ...(options.plain &amp;&amp; options.dark ? styles.plain_dark : null)
    };

    // Deconstruct srcset strings
    const imageSrcset = srcset
      .parse(src)
      .map(img =&gt; ({ ...img, url: getPublicPath(img.url, catalog) }));
    const overlaySrcset = overlay
      ? srcset
          .parse(overlay)
          .map(img =&gt; ({ ...img, url: getPublicPath(img.url, catalog) }))
      : [];

    const fallbackSrc = imageSrcset[0].url;
    const fallbackOverlay = overlay ? overlaySrcset[0].url : undefined;

    return (
      <div>
        <div>
          </div></div>
github DavidAnson / check-pages / checkPages.js View on Github external
$(element).each(function() {
      var value = $(this).attr(attribute);
      if (value) {
        var links = [value];
        if (attribute === 'srcset') {
          links = srcset.parse(value).map(function(size) {
            return size.url;
          });
        }
        links.forEach(function(link) {
          var resolvedLink = url.resolve(page, link);
          var parsedLink = url.parse(resolvedLink);
          if (((parsedLink.protocol === 'http:') || (parsedLink.protocol === 'https:') || (parsedLink.protocol === 'file:')) &&
              (!options.onlySameDomain || (parsedLink.hostname === pageHostname)) &&
              !isLinkIgnored(resolvedLink)) {
            // Add to beginning of queue (in order) so links gets processed before the next page
            pendingCallbacks.splice(index, 0, testLink(page, resolvedLink));
            index++;
            linkCount++;
          }
        });
      }
github lowwebtech / low-web-extension / src / scripts / main.js View on Github external
function cleanSrcset( srcset, width ){
  const parsed = srcsetUtil.parse(srcset);
  
  let cleanedSrcset
  if( true ){
    cleanedSrcset = getSmallestSrcset( parsed, width )
  }else{
    cleanedSrcset = noRetinaSrcset( parsed )
  }

  if( cleanedSrcset ){
    return srcsetUtil.stringify(cleanedSrcset)
  }else{
    return false
  }
}
  // chrome.tabs.onUpdated.addListener(
github apostrophecms / sanitize-html / src / index.js View on Github external
if (a === 'srcset') {
              try {
                parsed = srcset.parse(value);
                each(parsed, function(value) {
                  if (naughtyHref('srcset', value.url)) {
                    value.evil = true;
                  }
                });
                parsed = filter(parsed, function(v) {
                  return !v.evil;
                });
                if (!parsed.length) {
                  delete frame.attribs[a];
                  return;
                } else {
                  value = srcset.stringify(filter(parsed, function(v) {
                    return !v.evil;
                  }));
                  frame.attribs[a] = value;
                }
              } catch (e) {
                // Unparseable srcset
                delete frame.attribs[a];
                return;
              }
            }
            if (a === 'class') {
              value = filterClasses(value, allowedClassesMap[name]);
              if (!value.length) {
                delete frame.attribs[a];
                return;
              }
github lowwebtech / low-web-extension / src / scripts / content_script / image / srcset.js View on Github external
function cleanSrcset(srcset, width) {
  const parsed = srcsetUtil.parse(srcset);
  let cleanedSrcset;
  switch (parseInt(store.getters.image_srcset)) {
    case 1:
      cleanedSrcset = noRetina(parsed);
      break;
    case 2:
      cleanedSrcset = smallest(parsed, width);
      break;
    case 3:
      cleanedSrcset = smart(parsed, width);
      break;
  }
  if (cleanedSrcset) {
    return srcsetUtil.stringify(cleanedSrcset);
  } else {
    return false;
github ehmorris / High-Resolution-Downloader-For-Instagram / content / src / getMediaUrl.js View on Github external
const pickBiggestSourceFromSrcset = srcset =&gt; {
  const sourcesWithBytesize = srcsetParser.parse(srcset).map(source =&gt; {
    return fetch(source.url)
      .then(response =&gt; response.blob())
      .then(({ size: bytesize }) =&gt; Object.assign(source, { bytesize }));
  });

  return Promise.all(sourcesWithBytesize).then(sources =&gt; {
    sources.sort((first, second) =&gt; {
      if (first.bytesize &gt; second.bytesize) return -1;
      if (first.bytesize &lt; second.bytesize) return 1;
      return 0;
    });

    return sources[0].url;
  });
};
github website-scraper / node-website-scraper / lib / resource-handler / path-containers / html-img-srcset-tag.js View on Github external
constructor (text) {
		this.text = text || '';
		this.imgSrcsetParts = srcset.parse(this.text);
		this.paths = this.imgSrcsetParts.map(imgSrcset => imgSrcset.url);
	}
github danburzo / percollate / src / enhancements.js View on Github external
function absoluteSrcset(str) {
		return srcset.stringify(
			srcset.parse(str).map(item => ({
				...item,
				url: new URL(item.url, doc.baseURI).href
			}))
		);
	}

srcset

Parse and stringify the HTML `<img>` srcset attribute

MIT
Latest version published 10 months ago

Package Health Score

71 / 100
Full package analysis

Popular srcset functions