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';
export * from './types';
@@ -8,4 +8,5 @@ export {
rebuild,
buildNodeWithSN,
addHoverClass,
transformAttribute,
};

View File

@@ -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) {