]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
perf(compiler-sfc): ignore empty blocks (#3520)
authorCédric Exbrayat <cexbrayat@users.noreply.github.com>
Mon, 19 Jul 2021 21:24:40 +0000 (23:24 +0200)
committerGitHub <noreply@github.com>
Mon, 19 Jul 2021 21:24:40 +0000 (17:24 -0400)
packages/compiler-sfc/__tests__/parse.spec.ts
packages/compiler-sfc/src/parse.ts

index fff6cf3c29f90b15e45275dc23bdd1e284d24fcc..470bfb9d0bad9e0394ab375cac44d4146925938a 100644 (file)
@@ -135,8 +135,13 @@ h1 { color: red }
 
   test('should ignore other nodes with no content', () => {
     expect(parse(`<script/>`).descriptor.script).toBe(null)
+    expect(parse(`<script> \n\t  </script>`).descriptor.script).toBe(null)
     expect(parse(`<style/>`).descriptor.styles.length).toBe(0)
+    expect(parse(`<style> \n\t </style>`).descriptor.styles.length).toBe(0)
     expect(parse(`<custom/>`).descriptor.customBlocks.length).toBe(0)
+    expect(
+      parse(`<custom> \n\t </custom>`).descriptor.customBlocks.length
+    ).toBe(0)
   })
 
   test('handle empty nodes with src attribute', () => {
index 7e9c2c256d8dd00264ed59a30ce8f8d46d7ed02d..2650d48064bcd97ee906c50bce9d5772519f83a3 100644 (file)
@@ -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
+  )
+}