@percy/dom
Advanced tools
Comparing version 1.0.0-beta.58 to 1.0.0-beta.59
@@ -62,2 +62,41 @@ (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); | ||
} | ||
} | ||
} // Recursively serializes iframe documents into srcdoc attributes. | ||
function serializeFrames(dom, clone, { | ||
@@ -81,2 +120,3 @@ enableJavaScript | ||
let serialized = serializeDOM({ | ||
domTransformation: transformRelativeUrls, | ||
dom: frame.contentDocument, | ||
@@ -83,0 +123,0 @@ enableJavaScript |
{ | ||
"name": "@percy/dom", | ||
"version": "1.0.0-beta.58", | ||
"version": "1.0.0-beta.59", | ||
"license": "MIT", | ||
@@ -32,3 +32,3 @@ "main": "dist/bundle.js", | ||
}, | ||
"gitHead": "8da6a7bc9cad64b3a2f905a38b1488f5816b5e21" | ||
"gitHead": "bcc5b6da44aef8927cc3acb9406f5ee54ba4dde0" | ||
} |
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
14125
201