Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

support /deep/ and >>> #120

Merged
merged 3 commits into from
Aug 18, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion src/style/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,56 @@ 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) {
let isValid = false

if (tag === 'from' ||
tag === 'to' ||
tag.match(/^\d/)
) {
isValid = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can directly return the expression in if.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regex .test would be better than tag.match

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check again?

}

return isValid
}

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
}))
})
Expand Down
22 changes: 22 additions & 0 deletions test/expects/scoped-css-with-deep-tag.css
Original file line number Diff line number Diff line change
@@ -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;
}
}

3 changes: 3 additions & 0 deletions test/expects/scoped-css-with-deep-tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var scopedCssWithDeepTag = { template: "<div class=\"test\">Foo</div>",_scopeId: 'data-v-00b08a60',};

export default scopedCssWithDeepTag;
31 changes: 31 additions & 0 deletions test/fixtures/scoped-css-with-deep-tag.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<template>
<div class="test">Foo</div>
</template>

<script lang="babel">
export default {}
</script>

<style lang="css" scoped>
.test /deep/ a {
color: red;
}

.test >>> .text {
background-color: red;
}

@keyframes test {
0% {
color: red;
}

50% {
color: green;
}

100% {
color: yellow;
}
}
</style>
1 change: 1 addition & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ function test(name) {
'pug',
'scoped-css',
'scoped-css-with-no-auto-style',
'scoped-css-with-deep-tag',
'scss',
'sass',
'pug',
Expand Down