diff --git a/src/index.ts b/src/index.ts index 2240f589..7c059f37 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import snapshot, { serializeNodeWithId } from './snapshot'; +import snapshot, { serializeNodeWithId, transformAttribute } from './snapshot'; import rebuild, { buildNodeWithSN, addHoverClass } from './rebuild'; export * from './types'; @@ -8,4 +8,5 @@ export { rebuild, buildNodeWithSN, addHoverClass, + transformAttribute, }; diff --git a/src/snapshot.ts b/src/snapshot.ts index 520eed5e..58891587 100644 --- a/src/snapshot.ts +++ b/src/snapshot.ts @@ -113,7 +113,7 @@ function getAbsoluteSrcsetString(doc: Document, attributeValue: string) { return resultingSrcsetString; } -function absoluteToDoc(doc: Document, attributeValue: string): string { +export function absoluteToDoc(doc: Document, attributeValue: string): string { if (attributeValue.trim() === '') { return attributeValue; } @@ -126,6 +126,23 @@ function isSVGElement(el: Element): boolean { return el.tagName === 'svg' || el instanceof SVGElement; } +export function transformAttribute( + doc: Document, + name: string, + value: string, +): string { + // relative path in attribute + if (name === 'src' || name === 'href') { + return absoluteToDoc(doc, value); + } else if (name === 'srcset') { + return getAbsoluteSrcsetString(doc, value); + } else if (name === 'style') { + return absoluteToStylesheet(value, location.href); + } else { + return value; + } +} + function serializeNode( n: Node, doc: Document, @@ -160,16 +177,7 @@ function serializeNode( const tagName = (n as HTMLElement).tagName.toLowerCase(); let attributes: attributes = {}; for (const { name, value } of Array.from((n as HTMLElement).attributes)) { - // relative path in attribute - if (name === 'src' || name === 'href') { - attributes[name] = absoluteToDoc(doc, value); - } else if (name === 'srcset') { - attributes[name] = getAbsoluteSrcsetString(doc, value); - } else if (name === 'style') { - attributes[name] = absoluteToStylesheet(value, location.href); - } else { - attributes[name] = value; - } + attributes[name] = transformAttribute(doc, name, value); } // remote css if (tagName === 'link' && inlineStylesheet) {