Example #1
0
			forEach( images, function( image ) {
				let imgSource = image.getAttribute( 'src' ),
					parsedImgSrc = url.parse( imgSource, false, true ),
					hostName = parsedImgSrc.hostname;

				let safeSource;
				// if imgSource is relative, prepend post domain so it isn't relative to calypso
				if ( ! hostName ) {
					imgSource = url.resolve( post.URL, imgSource );
					parsedImgSrc = url.parse( imgSource, false, true );
				}

				safeSource = safeImageURL( imgSource );
				if ( ! safeSource && parsedImgSrc.search ) {
					// we can't make externals with a querystring safe.
					// try stripping it and retry
					parsedImgSrc.search = null;
					parsedImgSrc.query = null;
					safeSource = safeImageURL( url.format( parsedImgSrc ) );
				}

				removeUnsafeAttributes( image );

				if ( ! safeSource || imageShouldBeRemovedFromContent( imgSource ) ) {
					image.parentNode.removeChild( image );
					// fun fact: removing the node from the DOM will not prevent it from loading. You actually have to
					// change out the src to change what loads. The following is a 1x1 transparent gif as a data URL
					image.setAttribute( 'src', TRANSPARENT_GIF );
					image.removeAttribute( 'srcset' );
					return;
				}

				if ( maxWidth ) {
					safeSource = maxWidthPhotonishURL( safeSource, maxWidth );
				}

				image.setAttribute( 'src', safeSource );

				if ( image.hasAttribute( 'srcset' ) ) {
					const imgSrcSet = srcset.parse( image.getAttribute( 'srcset' ) ).map( imgSrc => {
						if ( ! url.parse( imgSrc.url, false, true ).hostname ) {
							imgSrc.url = url.resolve( post.URL, imgSrc.url );
						}
						imgSrc.url = safeImageURL( imgSrc.url );
						return imgSrc;
					} );
					image.setAttribute( 'srcset', srcset.stringify( imgSrcSet ) );
				}

				if ( isCandidateForContentImage( imgSource ) ) {
					content_images.push( {
						src: safeSource,
						original_src: imgSource,
						width: image.width,
						height: image.height
					} );
				}
			} );
Example #2
0
					el.getAttribute(name, (value) => {
						try {
							if (name === 'srcset') {
								value = srcset.stringify(srcset.parse(value).map(src => (src.url = rewrite(src.url, req.baseUrl), src)));
							} else if (name === 'style') {
								value = value.replace(cssUrlPattern, ($0, $1, $2, $3) => {
									return `url("${rewrite($2 || $3, req.baseUrl)}")`;
								});
							} else {
								value = rewrite(value, req.baseUrl);
							}

							el.setAttribute(name, value);
						} catch (e) {}
					});
	}).then(function updateSrcset () {
		return Promise.resolve(srcset.stringify(imgScrsetParts));
	});