export the transform attribute method

This commit is contained in:
Yanzhen Yu
2019-11-09 15:28:04 +08:00
parent 0b3b630c60
commit 2af6e7af7f
2 changed files with 21 additions and 12 deletions

View File

@@ -1,4 +1,4 @@
import snapshot, { serializeNodeWithId } from './snapshot'; import snapshot, { serializeNodeWithId, transformAttribute } from './snapshot';
import rebuild, { buildNodeWithSN, addHoverClass } from './rebuild'; import rebuild, { buildNodeWithSN, addHoverClass } from './rebuild';
export * from './types'; export * from './types';
@@ -8,4 +8,5 @@ export {
rebuild, rebuild,
buildNodeWithSN, buildNodeWithSN,
addHoverClass, addHoverClass,
transformAttribute,
}; };

View File

@@ -113,7 +113,7 @@ function getAbsoluteSrcsetString(doc: Document, attributeValue: string) {
return resultingSrcsetString; return resultingSrcsetString;
} }
function absoluteToDoc(doc: Document, attributeValue: string): string { export function absoluteToDoc(doc: Document, attributeValue: string): string {
if (attributeValue.trim() === '') { if (attributeValue.trim() === '') {
return attributeValue; return attributeValue;
} }
@@ -126,6 +126,23 @@ function isSVGElement(el: Element): boolean {
return el.tagName === 'svg' || el instanceof SVGElement; 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( function serializeNode(
n: Node, n: Node,
doc: Document, doc: Document,
@@ -160,16 +177,7 @@ function serializeNode(
const tagName = (n as HTMLElement).tagName.toLowerCase(); const tagName = (n as HTMLElement).tagName.toLowerCase();
let attributes: attributes = {}; let attributes: attributes = {};
for (const { name, value } of Array.from((n as HTMLElement).attributes)) { for (const { name, value } of Array.from((n as HTMLElement).attributes)) {
// relative path in attribute attributes[name] = transformAttribute(doc, name, value);
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;
}
} }
// remote css // remote css
if (tagName === 'link' && inlineStylesheet) { if (tagName === 'link' && inlineStylesheet) {