diff --git a/src/style/css.js b/src/style/css.js index 89f7540..cf91245 100644 --- a/src/style/css.js +++ b/src/style/css.js @@ -6,17 +6,53 @@ import camelcase from 'camelcase' import genScopeID from '../gen-scope-id' import debug from '../debug' +/** + * filter invalid tag, e.g. percentage, keyword(from, to)... + * @param tag + * @returns {boolean} + */ +function isInvalidTag (tag) { + if ( + tag === 'from' || + tag === 'to' || + /^\d/.test(tag) + ) { + return true + } +} + const addScopeID = postcss.plugin('add-scope-id', options => { const selectorTransformer = selectorParser(selectors => { selectors.each(selector => { let target = null selector.each(n => { + if (n.type === 'combinator' && n.value === '>>>') { + n.value = ' ' + n.spaces.before = n.spaces.after = '' + return false + } + + if (n.type === 'tag') { + if (n.value === '/deep/') { + const next = n.next() + + if (next.type === 'combinator' && next.value === ' ') { + next.remove() + } + + n.remove() + return false + } else if (isInvalidTag(n.value)) { + return + } + } + if (n.type !== 'pseudo' && n.type !== 'combinator') { target = n } }) - selector.insertAfter(target, selectorParser.attribute({ + target && selector.insertAfter(target, selectorParser.attribute({ attribute: options.scopeID })) }) diff --git a/test/expects/scoped-css-with-deep-tag.css b/test/expects/scoped-css-with-deep-tag.css new file mode 100644 index 0000000..bffe2ce --- /dev/null +++ b/test/expects/scoped-css-with-deep-tag.css @@ -0,0 +1,22 @@ +.test[data-v-00b08a60] a { + color: red; +} + +.test[data-v-00b08a60] .text { + background-color: red; +} + +@keyframes test { + 0% { + color: red; + } + + 50% { + color: green; + } + + 100% { + color: yellow; + } +} + diff --git a/test/expects/scoped-css-with-deep-tag.js b/test/expects/scoped-css-with-deep-tag.js new file mode 100644 index 0000000..e635aa5 --- /dev/null +++ b/test/expects/scoped-css-with-deep-tag.js @@ -0,0 +1,3 @@ +var scopedCssWithDeepTag = { template: "