From: Cédric Exbrayat Date: Mon, 19 Jul 2021 21:24:40 +0000 (+0200) Subject: perf(compiler-sfc): ignore empty blocks (#3520) X-Git-Tag: v3.2.0-beta.2~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b771fdbef9a8dadd4c9cc939cc104f7764e40373;p=thirdparty%2Fvuejs%2Fcore.git perf(compiler-sfc): ignore empty blocks (#3520) --- diff --git a/packages/compiler-sfc/__tests__/parse.spec.ts b/packages/compiler-sfc/__tests__/parse.spec.ts index fff6cf3c29..470bfb9d0b 100644 --- a/packages/compiler-sfc/__tests__/parse.spec.ts +++ b/packages/compiler-sfc/__tests__/parse.spec.ts @@ -135,8 +135,13 @@ h1 { color: red } test('should ignore other nodes with no content', () => { expect(parse(``).descriptor.script).toBe(null) expect(parse(``).descriptor.styles.length).toBe(0) expect(parse(``).descriptor.customBlocks.length).toBe(0) + expect( + parse(` \n\t `).descriptor.customBlocks.length + ).toBe(0) }) test('handle empty nodes with src attribute', () => { diff --git a/packages/compiler-sfc/src/parse.ts b/packages/compiler-sfc/src/parse.ts index 7e9c2c256d..2650d48064 100644 --- a/packages/compiler-sfc/src/parse.ts +++ b/packages/compiler-sfc/src/parse.ts @@ -162,7 +162,8 @@ export function parse( if (node.type !== NodeTypes.ELEMENT) { return } - if (!node.children.length && !hasSrc(node) && node.tag !== 'template') { + // we only want to keep the nodes that are not empty (when the tag is not a template) + if (node.tag !== 'template' && isEmpty(node) && !hasSrc(node)) { return } switch (node.tag) { @@ -415,3 +416,15 @@ function hasSrc(node: ElementNode) { return p.name === 'src' }) } + +/** + * Returns true if the node has no children + * once the empty text nodes (trimmed content) have been filtered out. + */ +function isEmpty(node: ElementNode) { + return ( + node.children.filter( + child => child.type !== NodeTypes.TEXT || child.content.trim() !== '' + ).length === 0 + ) +}