format code

This commit is contained in:
Yanzhen Yu
2026-04-01 12:00:00 +08:00
parent 94d64e3424
commit 927f754a9d
2 changed files with 31 additions and 29 deletions

View File

@@ -86,34 +86,36 @@ export function absoluteToStylesheet(cssText: string, href: string): string {
}
function getAbsoluteSrcsetString(doc: Document, attributeValue: string) {
if(attributeValue.trim() === "") {
return attributeValue
if (attributeValue.trim() === '') {
return attributeValue;
}
const srcsetValues = attributeValue.split(",")
const srcsetValues = attributeValue.split(',');
// srcset attributes is defined as such:
// srcset = "url size,url1 size1"
const resultingSrcsetString = srcsetValues.map((srcItem) => {
// removing all but middle spaces
const trimmedSrcItem = srcItem.trimLeft().trimRight()
const urlAndSize = trimmedSrcItem.split(" ")
// this means we have both 0:url and 1:size
if (urlAndSize.length === 2) {
const absUrl = absoluteToDoc(doc, urlAndSize[0])
return `${absUrl} ${urlAndSize[1]}`
} else if(urlAndSize.length === 1){
const absUrl = absoluteToDoc(doc, urlAndSize[0])
return `${absUrl}`
}
return ""
}).join(',')
const resultingSrcsetString = srcsetValues
.map(srcItem => {
// removing all but middle spaces
const trimmedSrcItem = srcItem.trimLeft().trimRight();
const urlAndSize = trimmedSrcItem.split(' ');
// this means we have both 0:url and 1:size
if (urlAndSize.length === 2) {
const absUrl = absoluteToDoc(doc, urlAndSize[0]);
return `${absUrl} ${urlAndSize[1]}`;
} else if (urlAndSize.length === 1) {
const absUrl = absoluteToDoc(doc, urlAndSize[0]);
return `${absUrl}`;
}
return '';
})
.join(',');
return resultingSrcsetString
return resultingSrcsetString;
}
function absoluteToDoc(doc: Document, attributeValue: string): string {
if (attributeValue.trim() === ""){
return attributeValue
if (attributeValue.trim() === '') {
return attributeValue;
}
const a: HTMLAnchorElement = doc.createElement('a');
a.href = attributeValue;
@@ -161,8 +163,8 @@ function serializeNode(
// 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 === 'srcset') {
attributes[name] = getAbsoluteSrcsetString(doc, value);
} else if (name === 'style') {
attributes[name] = absoluteToStylesheet(value, location.href);
} else {
@@ -226,7 +228,7 @@ function serializeNode(
}
// canvas image data
if (tagName === 'canvas') {
attributes.rr_dataURL = (n as HTMLCanvasElement).toDataURL()
attributes.rr_dataURL = (n as HTMLCanvasElement).toDataURL();
}
if (needBlock) {
const { width, height } = (n as HTMLElement).getBoundingClientRect();