@percy/dom
Advanced tools
Comparing version 1.0.0-beta.65 to 1.0.0-beta.66
@@ -62,38 +62,9 @@ (function() { | ||
const URI_ATTRS = ['href', 'src', 'srcset', 'poster', 'background']; | ||
const URI_SELECTOR = URI_ATTRS.map(a => `[${a}]`).join(',') + ',[style*="url("]'; // include elements with url style attributes | ||
// A loose srcset image candidate regex split into capture groups | ||
// https://html.spec.whatwg.org/multipage/images.html#srcset-attribute | ||
const SRCSET_REGEX = /(\s*)([^,]\S*[^,])((?:\s+[^,]+)*\s*(?:,|$))/g; // A loose CSS url() regex split into capture groups | ||
const CSS_URL_REGEX = /(url\((["']?))((?:\\.|(?!\2).|[^)])+)(\2\))/g; // Transforms URL attributes within a document to be fully qualified URLs. This is necessary when | ||
// embedded documents are serialized and their contents become root-relative. | ||
function transformRelativeUrls(dom) { | ||
// transform style elements that might contain URLs | ||
for (let style of dom.querySelectorAll('style')) { | ||
style.innerHTML && (style.innerHTML = style.innerHTML.replace(CSS_URL_REGEX, (_, $1, $2, uri, $4) => `${$1}${new URL(uri, style.baseURI).href}${$4}`)); | ||
} // transform element attributes that might contain URLs | ||
for (let el of dom.querySelectorAll(URI_SELECTOR)) { | ||
for (let attr of URI_ATTRS.concat('style')) { | ||
if (!(attr in el) || !el[attr] || !el.hasAttribute(attr)) continue; | ||
let value = el[attr]; | ||
if (attr === 'style') { | ||
// transform inline style url() usage | ||
value = el.getAttribute('style').replace(CSS_URL_REGEX, (_, $1, $2, uri, $4) => `${$1}${new URL(uri, el.baseURI).href}${$4}`); | ||
} else if (attr === 'srcset') { | ||
// transform each srcset URL | ||
value = value.replace(SRCSET_REGEX, (_, $1, uri, $3) => `${$1}${new URL(uri, el.baseURI).href}${$3}`); | ||
} else { | ||
// resolve the URL with the node's base URI | ||
value = new URL(value, el.baseURI).href; | ||
} | ||
el.setAttribute(attr, value); | ||
} | ||
} | ||
function setBaseURI(dom) { | ||
if (!new URL(dom.baseURI).hostname) return; | ||
let $base = document.createElement('base'); | ||
$base.href = dom.baseURI; | ||
dom.querySelector('head').prepend($base); | ||
} // Recursively serializes iframe documents into srcdoc attributes. | ||
@@ -120,3 +91,3 @@ | ||
let serialized = serializeDOM({ | ||
domTransformation: transformRelativeUrls, | ||
domTransformation: setBaseURI, | ||
dom: frame.contentDocument, | ||
@@ -123,0 +94,0 @@ enableJavaScript |
{ | ||
"name": "@percy/dom", | ||
"version": "1.0.0-beta.65", | ||
"version": "1.0.0-beta.66", | ||
"license": "MIT", | ||
@@ -37,3 +37,3 @@ "main": "dist/bundle.js", | ||
}, | ||
"gitHead": "19a727cb92b9b6c06dec9ccec7cc198b0d8f1e12" | ||
"gitHead": "740a335bab4966f7dda4e4693505e17a5fdcfd4c" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12568
178