Skip to content

Commit 66cd34b

Browse files
committed
Absolutize nested style rule selector with non-omitted combinator
The previous implementation did not absolutized `> foo &`, ie. a relative selector starting with a non-omitted combinator and including `&`.
1 parent e68c4f5 commit 66cd34b

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

lib/cssom/CSSStyleRule-impl.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11

22
import { delimiter, list, omitted } from '../values/value.js'
3+
import { isFailure, isOmitted } from '../utils/value.js'
34
import CSSGroupingRuleImpl from './CSSGroupingRule-impl.js'
45
import CSSNestedDeclarations from './CSSNestedDeclarations.js'
56
import CSSRuleList from './CSSRuleList.js'
67
import CSSScopeRule from './CSSScopeRule.js'
78
import CSSStyleProperties from './CSSStyleProperties.js'
89
import CSSStyleRule from './CSSStyleRule.js'
9-
import { isFailure } from '../utils/value.js'
1010
import { parseGrammar } from '../parse/parser.js'
1111
import { serializeComponentValueList } from '../serialize.js'
1212

@@ -87,11 +87,11 @@ export default class CSSStyleRuleImpl extends CSSGroupingRuleImpl {
8787
if (isNestedStyleRule(parentRule)) {
8888
return _selectors
8989
.map(selector => {
90-
selector = serializeComponentValueList(selector)
91-
if (!selector.includes('&')) {
92-
selector = `& ${selector}`
90+
const string = serializeComponentValueList(selector)
91+
if (!isOmitted(selector[0]) || !string.includes('&')) {
92+
return `& ${string}`
9393
}
94-
return selector
94+
return string
9595
})
9696
.join(', ')
9797
}

test/stylesheet.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,48 +1103,53 @@ describe('CSSStyleRule', () => {
11031103

11041104
const styleSheet = createStyleSheet(`
11051105
style {
1106+
@media {
1107+
nested {}
1108+
nested & {}
1109+
> nested & {}
1110+
}
11061111
@scope {
11071112
scoped {
11081113
nested {}
11091114
}
11101115
}
1111-
nested {}
11121116
color: green;
11131117
}
11141118
`)
11151119
const styleRule = styleSheet.cssRules[0]
1116-
const { cssRules: [scopeRule, nestedStyleRule] } = styleRule
1120+
const { cssRules: [mediaRule, scopeRule] } = styleRule
1121+
const nestedStyleRule = mediaRule.cssRules[0]
11171122
const scopedStyleRule = scopeRule.cssRules[0]
11181123

11191124
// CSSRule
1120-
assert.equal(styleRule.cssText, 'style { @scope { scoped { & nested {} } } & nested {} color: green; }')
1121-
assert.equal(scopedStyleRule.cssText, 'scoped { & nested {} }')
1125+
assert.equal(styleRule.cssText, 'style { @media { & nested {} nested & {} & > nested & {} } @scope { scoped { & nested {} } } color: green; }')
11221126
assert.equal(nestedStyleRule.cssText, '& nested {}')
1127+
assert.equal(scopedStyleRule.cssText, 'scoped { & nested {} }')
11231128
assert.equal(styleRule.parentRule, null)
1129+
assert.equal(nestedStyleRule.parentRule, mediaRule)
11241130
assert.equal(scopedStyleRule.parentRule, scopeRule)
1125-
assert.equal(nestedStyleRule.parentRule, styleRule)
11261131
assert.equal(styleRule.parentStyleSheet, styleSheet)
1127-
assert.equal(scopedStyleRule.parentStyleSheet, styleSheet)
11281132
assert.equal(nestedStyleRule.parentStyleSheet, styleSheet)
1133+
assert.equal(scopedStyleRule.parentStyleSheet, styleSheet)
11291134

11301135
// CSSGroupingRule
11311136
assert.equal(CSSRuleList.is(styleRule.cssRules), true)
1132-
assert.equal(CSSRuleList.is(scopedStyleRule.cssRules), true)
11331137
assert.equal(CSSRuleList.is(nestedStyleRule.cssRules), true)
1138+
assert.equal(CSSRuleList.is(scopedStyleRule.cssRules), true)
11341139

11351140
// CSSStyleRule
11361141
assert.equal(styleRule.selectorText, 'style')
1137-
assert.equal(scopedStyleRule.selectorText, 'scoped')
11381142
assert.equal(nestedStyleRule.selectorText, '& nested')
1143+
assert.equal(scopedStyleRule.selectorText, 'scoped')
11391144
styleRule.selectorText = 'parent'
1140-
scopedStyleRule.selectorText = 'scoped-parent'
11411145
nestedStyleRule.selectorText = 'child'
1146+
scopedStyleRule.selectorText = 'scoped-parent'
11421147
assert.equal(styleRule.selectorText, 'parent')
1143-
assert.equal(scopedStyleRule.selectorText, 'scoped-parent')
11441148
assert.equal(nestedStyleRule.selectorText, '& child')
1149+
assert.equal(scopedStyleRule.selectorText, 'scoped-parent')
11451150
assert.equal(CSSStyleProperties.is(styleRule.style), true)
1146-
assert.equal(CSSStyleProperties.is(scopedStyleRule.style), true)
11471151
assert.equal(CSSStyleProperties.is(nestedStyleRule.style), true)
1152+
assert.equal(CSSStyleProperties.is(scopedStyleRule.style), true)
11481153
})
11491154
test('methods', () => {
11501155

0 commit comments

Comments
 (0)