From da182f49276361275ae91b6e045d20e0e42afa9f Mon Sep 17 00:00:00 2001 From: Yanzhen Yu Date: Sat, 21 Nov 2020 11:03:32 +0800 Subject: [PATCH] fix lint --- src/css.ts | 6 +- src/snapshot.ts | 191 ++++++++++++++++++++++++++---------------------- 2 files changed, 108 insertions(+), 89 deletions(-) diff --git a/src/css.ts b/src/css.ts index 1bef07fb..950ec0b4 100644 --- a/src/css.ts +++ b/src/css.ts @@ -440,11 +440,11 @@ export function parse(css: string, options: ParserOptions = {}) { * http://ostermiller.org/findcomment.html */ return trim(m[0]) .replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/g, '') - .replace(/"(?:\\"|[^"])*"|'(?:\\'|[^'])*'/g, m => { + .replace(/"(?:\\"|[^"])*"|'(?:\\'|[^'])*'/g, (m) => { return m.replace(/,/g, '\u200C'); }) .split(/\s*(?![^(]*\)),\s*/) - .map(s => { + .map((s) => { return s.replace(/\u200C/g, ','); }); } @@ -888,7 +888,7 @@ function addParent(obj: Stylesheet, parent?: Stylesheet) { for (const k of Object.keys(obj)) { const value = obj[k as keyof Stylesheet]; if (Array.isArray(value)) { - value.forEach(v => { + value.forEach((v) => { addParent(v, childParent); }); } else if (value && typeof value === 'object') { diff --git a/src/snapshot.ts b/src/snapshot.ts index 9db891ba..d13c48d0 100644 --- a/src/snapshot.ts +++ b/src/snapshot.ts @@ -34,9 +34,7 @@ function getValidTagName(tagName: string): string { function getCssRulesString(s: CSSStyleSheet): string | null { try { const rules = s.rules || s.cssRules; - return rules - ? Array.from(rules).map(getCssRuleString).join('') - : null; + return rules ? Array.from(rules).map(getCssRuleString).join('') : null; } catch (error) { return null; } @@ -74,18 +72,20 @@ export function absoluteToStylesheet( URL_IN_CSS_REF, (origin, quote1, path1, quote2, path2, path3) => { const filePath = path1 || path2 || path3; - const maybe_quote = quote1 || quote2 || ''; + const maybeQuote = quote1 || quote2 || ''; if (!filePath) { return origin; } if (!RELATIVE_PATH.test(filePath)) { - return `url(${maybe_quote}${filePath}${maybe_quote})`; + return `url(${maybeQuote}${filePath}${maybeQuote})`; } if (DATA_URI.test(filePath)) { - return `url(${maybe_quote}${filePath}${maybe_quote})`; + return `url(${maybeQuote}${filePath}${maybeQuote})`; } if (filePath[0] === '/') { - return `url(${maybe_quote}${extractOrigin(href) + filePath}${maybe_quote})`; + return `url(${maybeQuote}${ + extractOrigin(href) + filePath + }${maybeQuote})`; } const stack = href.split('/'); const parts = filePath.split('/'); @@ -99,7 +99,7 @@ export function absoluteToStylesheet( stack.push(part); } } - return `url(${maybe_quote}${stack.join('/')}${maybe_quote})`; + return `url(${maybeQuote}${stack.join('/')}${maybeQuote})`; }, ); } @@ -179,7 +179,7 @@ export function _isBlockedElement( }); } if (blockSelector) { - return element.matches(blockSelector) + return element.matches(blockSelector); } return false; @@ -208,7 +208,11 @@ function serializeNode( systemId: (n as DocumentType).systemId, }; case n.ELEMENT_NODE: - const needBlock = _isBlockedElement(n as HTMLElement, blockClass, blockSelector); + const needBlock = _isBlockedElement( + n as HTMLElement, + blockClass, + blockSelector, + ); const tagName = getValidTagName((n as HTMLElement).tagName); let attributes: attributes = {}; for (const { name, value } of Array.from((n as HTMLElement).attributes)) { @@ -339,7 +343,7 @@ function serializeNode( } } -function lowerIfExists(maybeAttr : string | number | boolean) : string { +function lowerIfExists(maybeAttr: string | number | boolean): string { if (maybeAttr === undefined) { return ''; } else { @@ -347,68 +351,83 @@ function lowerIfExists(maybeAttr : string | number | boolean) : string { } } -function slimDOMExcluded(sn: serializedNode, slimDOMOptions: SlimDOMOptions): boolean { +function slimDOMExcluded( + sn: serializedNode, + slimDOMOptions: SlimDOMOptions, +): boolean { if (slimDOMOptions.comment && sn.type === NodeType.Comment) { // TODO: convert IE conditional comments to real nodes return true; } else if (sn.type === NodeType.Element) { - if (slimDOMOptions.script && - (sn.tagName === 'script' || - (sn.tagName === 'link' && sn.attributes.rel === 'preload' && sn.attributes['as'] === 'script') - )) { + if ( + slimDOMOptions.script && + (sn.tagName === 'script' || + (sn.tagName === 'link' && + sn.attributes.rel === 'preload' && + sn.attributes.as === 'script')) + ) { return true; - } else if (slimDOMOptions.headFavicon && ( - (sn.tagName === 'link' && sn.attributes.rel === 'shortcut icon') - || (sn.tagName === 'meta' && ( - lowerIfExists(sn.attributes['name']).match(/^msapplication-tile(image|color)$/) - || lowerIfExists(sn.attributes['name']) === 'application-name' - || lowerIfExists(sn.attributes['rel']) === 'icon' - || lowerIfExists(sn.attributes['rel']) === 'apple-touch-icon' - || lowerIfExists(sn.attributes['rel']) === 'shortcut icon' - )))) { + } else if ( + slimDOMOptions.headFavicon && + ((sn.tagName === 'link' && sn.attributes.rel === 'shortcut icon') || + (sn.tagName === 'meta' && + (lowerIfExists(sn.attributes.name).match( + /^msapplication-tile(image|color)$/, + ) || + lowerIfExists(sn.attributes.name) === 'application-name' || + lowerIfExists(sn.attributes.rel) === 'icon' || + lowerIfExists(sn.attributes.rel) === 'apple-touch-icon' || + lowerIfExists(sn.attributes.rel) === 'shortcut icon'))) + ) { return true; } else if (sn.tagName === 'meta') { - if (slimDOMOptions.headMetaDescKeywords && ( - lowerIfExists(sn.attributes['name']).match(/^description|keywords$/) - )) { + if ( + slimDOMOptions.headMetaDescKeywords && + lowerIfExists(sn.attributes.name).match(/^description|keywords$/) + ) { return true; - } else if (slimDOMOptions.headMetaSocial && ( - lowerIfExists(sn.attributes['property']).match(/^(og|twitter|fb):/) // og = opengraph (facebook) - || lowerIfExists(sn.attributes['name']).match(/^(og|twitter):/) - || lowerIfExists(sn.attributes['name']) === 'pinterest' - )) { + } else if ( + slimDOMOptions.headMetaSocial && + (lowerIfExists(sn.attributes.property).match(/^(og|twitter|fb):/) || // og = opengraph (facebook) + lowerIfExists(sn.attributes.name).match(/^(og|twitter):/) || + lowerIfExists(sn.attributes.name) === 'pinterest') + ) { return true; - } else if (slimDOMOptions.headMetaRobots && ( - lowerIfExists(sn.attributes['name']) === 'robots' - || lowerIfExists(sn.attributes['name']) === 'googlebot' - || lowerIfExists(sn.attributes['name']) === 'bingbot' - )) { + } else if ( + slimDOMOptions.headMetaRobots && + (lowerIfExists(sn.attributes.name) === 'robots' || + lowerIfExists(sn.attributes.name) === 'googlebot' || + lowerIfExists(sn.attributes.name) === 'bingbot') + ) { return true; - } else if (slimDOMOptions.headMetaHttpEquiv && ( + } else if ( + slimDOMOptions.headMetaHttpEquiv && sn.attributes['http-equiv'] !== undefined - )) { + ) { // e.g. X-UA-Compatible, Content-Type, Content-Language, // cache-control, X-Translated-By return true; - } else if (slimDOMOptions.headMetaAuthorship && ( - lowerIfExists(sn.attributes['name']) === 'author' - || lowerIfExists(sn.attributes['name']) === 'generator' - || lowerIfExists(sn.attributes['name']) === 'framework' - || lowerIfExists(sn.attributes['name']) === 'publisher' - || lowerIfExists(sn.attributes['name']) === 'progid' - || lowerIfExists(sn.attributes['property']).match(/^article:/) - || lowerIfExists(sn.attributes['property']).match(/^product:/) - )) { + } else if ( + slimDOMOptions.headMetaAuthorship && + (lowerIfExists(sn.attributes.name) === 'author' || + lowerIfExists(sn.attributes.name) === 'generator' || + lowerIfExists(sn.attributes.name) === 'framework' || + lowerIfExists(sn.attributes.name) === 'publisher' || + lowerIfExists(sn.attributes.name) === 'progid' || + lowerIfExists(sn.attributes.property).match(/^article:/) || + lowerIfExists(sn.attributes.property).match(/^product:/)) + ) { return true; - } else if (slimDOMOptions.headMetaVerification && ( - lowerIfExists(sn.attributes['name']) === 'google-site-verification' - || lowerIfExists(sn.attributes['name']) === 'yandex-verification' - || lowerIfExists(sn.attributes['name']) === 'csrf-token' - || lowerIfExists(sn.attributes['name']) === 'p:domain_verify' - || lowerIfExists(sn.attributes['name']) === 'verify-v1' - || lowerIfExists(sn.attributes['name']) === 'verification' - || lowerIfExists(sn.attributes['name']) === 'shopify-checkout-api-token' - )) { + } else if ( + slimDOMOptions.headMetaVerification && + (lowerIfExists(sn.attributes.name) === 'google-site-verification' || + lowerIfExists(sn.attributes.name) === 'yandex-verification' || + lowerIfExists(sn.attributes.name) === 'csrf-token' || + lowerIfExists(sn.attributes.name) === 'p:domain_verify' || + lowerIfExists(sn.attributes.name) === 'verify-v1' || + lowerIfExists(sn.attributes.name) === 'verification' || + lowerIfExists(sn.attributes.name) === 'shopify-checkout-api-token') + ) { return true; } } @@ -448,12 +467,13 @@ export function serializeNodeWithId( // Try to reuse the previous id if ('__sn' in n) { id = n.__sn.id; - } else if (slimDOMExcluded(_serializedNode, slimDOMOptions) || - (!preserveWhiteSpace && - _serializedNode.type === NodeType.Text && - !_serializedNode.isStyle && - !_serializedNode.textContent.replace(/^\s+|\s+$/gm,'').length - )) { + } else if ( + slimDOMExcluded(_serializedNode, slimDOMOptions) || + (!preserveWhiteSpace && + _serializedNode.type === NodeType.Text && + !_serializedNode.isStyle && + !_serializedNode.textContent.replace(/^\s+|\s+$/gm, '').length) + ) { id = IGNORED_NODE; } else { id = genId(); @@ -461,7 +481,7 @@ export function serializeNodeWithId( const serializedNode = Object.assign(_serializedNode, { id }); (n as INode).__sn = serializedNode; if (id === IGNORED_NODE) { - return null; // slimDOM + return null; // slimDOM } map[id] = n as INode; let recordChild = !skipChild; @@ -476,9 +496,9 @@ export function serializeNodeWithId( recordChild ) { if ( - (slimDOMOptions.headWhitespace && - _serializedNode.type === NodeType.Element && - _serializedNode.tagName == 'head') + slimDOMOptions.headWhitespace && + _serializedNode.type === NodeType.Element && + _serializedNode.tagName === 'head' // would impede performance: || getComputedStyle(n)['white-space'] === 'normal' ) { preserveWhiteSpace = false; @@ -538,24 +558,23 @@ function snapshot( ? {} : maskAllInputsOrOptions; const slimDOMOptions: SlimDOMOptions = - (slimDOMSensibleOrOptions === true || - slimDOMSensibleOrOptions === 'all') - // if true: set of sensible options that should not throw away any information - ? { - script: true, - comment: true, - headFavicon: true, - headWhitespace: true, - headMetaDescKeywords: slimDOMSensibleOrOptions === 'all', // destructive - headMetaSocial: true, - headMetaRobots: true, - headMetaHttpEquiv: true, - headMetaAuthorship: true, - headMetaVerification: true, - } - : slimDOMSensibleOrOptions === false - ? {} - : slimDOMSensibleOrOptions; + slimDOMSensibleOrOptions === true || slimDOMSensibleOrOptions === 'all' + ? // if true: set of sensible options that should not throw away any information + { + script: true, + comment: true, + headFavicon: true, + headWhitespace: true, + headMetaDescKeywords: slimDOMSensibleOrOptions === 'all', // destructive + headMetaSocial: true, + headMetaRobots: true, + headMetaHttpEquiv: true, + headMetaAuthorship: true, + headMetaVerification: true, + } + : slimDOMSensibleOrOptions === false + ? {} + : slimDOMSensibleOrOptions; return [ serializeNodeWithId( n,