From c7f4c36702d7e2ef020f5d87e79d3923bd1cd6be Mon Sep 17 00:00:00 2001 From: Yanzhen Yu Date: Thu, 4 Feb 2021 14:48:28 +0800 Subject: [PATCH] fix href value --- src/snapshot.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/snapshot.ts b/src/snapshot.ts index 3d04acc3..fcfb9f56 100644 --- a/src/snapshot.ts +++ b/src/snapshot.ts @@ -149,6 +149,13 @@ function isSVGElement(el: Element): boolean { return el.tagName === 'svg' || el instanceof SVGElement; } +function getHref() { + // return a href without hash + const a = document.createElement('a'); + a.href = ''; + return a.href; +} + export function transformAttribute( doc: Document, name: string, @@ -160,7 +167,7 @@ export function transformAttribute( } else if (name === 'srcset' && value) { return getAbsoluteSrcsetString(doc, value); } else if (name === 'style' && value) { - return absoluteToStylesheet(value, location.href); + return absoluteToStylesheet(value, getHref()); } else { return value; } @@ -262,7 +269,7 @@ function serializeNode( (n as HTMLStyleElement).sheet as CSSStyleSheet, ); if (cssText) { - attributes._cssText = absoluteToStylesheet(cssText, location.href); + attributes._cssText = absoluteToStylesheet(cssText, getHref()); } } // form fields @@ -335,7 +342,7 @@ function serializeNode( let textContent = (n as Text).textContent; const isStyle = parentTagName === 'STYLE' ? true : undefined; if (isStyle && textContent) { - textContent = absoluteToStylesheet(textContent, location.href); + textContent = absoluteToStylesheet(textContent, getHref()); } if (parentTagName === 'SCRIPT') { textContent = 'SCRIPT_PLACEHOLDER';