From 0b3b630c60b112d774ab990d6df48c60e6fbd1f6 Mon Sep 17 00:00:00 2001 From: Yanzhen Yu Date: Sat, 9 Nov 2019 15:20:58 +0800 Subject: [PATCH] format code --- src/rebuild.ts | 12 ++++++------ src/snapshot.ts | 48 +++++++++++++++++++++++++----------------------- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/rebuild.ts b/src/rebuild.ts index ba614144..83510588 100644 --- a/src/rebuild.ts +++ b/src/rebuild.ts @@ -99,7 +99,7 @@ function buildNode( } for (const name in n.attributes) { if (!n.attributes.hasOwnProperty(name)) { - continue + continue; } let value = n.attributes[name]; value = typeof value === 'boolean' ? '' : value; @@ -137,14 +137,14 @@ function buildNode( } else { // handle internal attributes if (tagName === 'canvas' && name === 'rr_dataURL') { - const image = document.createElement('img') - image.src = value + const image = document.createElement('img'); + image.src = value; image.onload = () => { - const ctx = (node as HTMLCanvasElement).getContext('2d') + const ctx = (node as HTMLCanvasElement).getContext('2d'); if (ctx) { - ctx.drawImage(image, 0, 0, image.width, image.height) + ctx.drawImage(image, 0, 0, image.width, image.height); } - } + }; } if (name === 'rr_width') { (node as HTMLElement).style.width = value; diff --git a/src/snapshot.ts b/src/snapshot.ts index 74126f54..520eed5e 100644 --- a/src/snapshot.ts +++ b/src/snapshot.ts @@ -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();