Преглед изворни кода

fix(compiler-sfc): don't inject scope id before child combinator >

daiwei пре 11 месеци
родитељ
комит
522077b1bc

+ 7 - 0
packages/compiler-sfc/__tests__/compileStyle.spec.ts

@@ -520,5 +520,12 @@ describe('SFC style preprocessors', () => {
       "[data-v-test]:last-child [data-v-test]:active { color: red;
       "[data-v-test]:last-child [data-v-test]:active { color: red;
       }"
       }"
     `)
     `)
+    expect(compileScoped(`main { > * { background-color: yellow; } }`))
+      .toMatchInlineSnapshot(`
+        "main {
+        > [data-v-test] { background-color: yellow;
+        }
+        }"
+      `)
   })
   })
 })
 })

+ 23 - 15
packages/compiler-sfc/src/style/pluginScoped.ts

@@ -255,6 +255,13 @@ function rewriteSelector(
       )
       )
       shouldInject = false
       shouldInject = false
     }
     }
+  } else {
+    // #13387 don't inject [id] at the selector start if node is null
+    // and the selector starts with `>`
+    const { type, value } = selector.first
+    if (type === 'combinator' && value === '>') {
+      shouldInject = false
+    }
   }
   }
 
 
   if (node) {
   if (node) {
@@ -266,8 +273,8 @@ function rewriteSelector(
     selector.first.spaces.before = ''
     selector.first.spaces.before = ''
   }
   }
 
 
+  const idToAdd = slotted ? id + '-s' : id
   if (shouldInject) {
   if (shouldInject) {
-    const idToAdd = slotted ? id + '-s' : id
     selector.insertAfter(
     selector.insertAfter(
       // If node is null it means we need to inject [id] at the start
       // If node is null it means we need to inject [id] at the start
       // insertAfter can handle `null` here
       // insertAfter can handle `null` here
@@ -279,20 +286,21 @@ function rewriteSelector(
         quoteMark: `"`,
         quoteMark: `"`,
       }),
       }),
     )
     )
-    // Used for trailing universal selectors (#12906)
-    // `.foo * {}` -> `.foo[xxxxxxx] [xxxxxxx] {}`
-    if (starNode) {
-      selector.insertBefore(
-        starNode,
-        selectorParser.attribute({
-          attribute: idToAdd,
-          value: idToAdd,
-          raws: {},
-          quoteMark: `"`,
-        }),
-      )
-      selector.removeChild(starNode)
-    }
+  }
+
+  // Used for trailing universal selectors (#12906)
+  // `.foo * {}` -> `.foo[xxxxxxx] [xxxxxxx] {}`
+  if (starNode) {
+    selector.insertBefore(
+      starNode,
+      selectorParser.attribute({
+        attribute: idToAdd,
+        value: idToAdd,
+        raws: {},
+        quoteMark: `"`,
+      }),
+    )
+    selector.removeChild(starNode)
   }
   }
 }
 }