Skip to content

Commit 56fa29b

Browse files
committed
Wrap declarations after/in nested group rules in CSSNestedDeclarations
Declarations are no longer hoisted or wrapped in a style rule. Some problem remains... 1. @font-feature-values, @function, @page, can also have declarations interleaved with rules, but CSSNestedDeclarations is currently associated to style properties. (w3c/csswg-drafts#11272) 2. "}" should be ignored in a "style" attribute value (a declaration block) but the algorithm has been replaced with the same algorithm than for a block of rules and declarations. (w3c/csswg-drafts#11113) 3. The current text wants declarations interleaved by an invalid at-rule or an invalid (qualified) rule error to be separated, but the reason is not stated and no browser does this. (w3c/csswg-drafts#11271) 4. The current text clearly has some other minor typos and is not clear about whether rules and declaration, or only rules, should be returned. (w3c/csswg-drafts#11017)
1 parent 70911e3 commit 56fa29b

25 files changed

+385
-260
lines changed

__tests__/stylesheet.js

Lines changed: 192 additions & 159 deletions
Large diffs are not rendered by default.

lib/cssom/CSSColorProfileRule-impl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class CSSColorProfileRuleImpl extends CSSRuleImpl {
2020
constructor(globalObject, args, privateData) {
2121
super(globalObject, args, privateData)
2222
const { prelude, value } = privateData
23-
const { declarations } = parseBlockContents(value, this)
23+
const [declarations = []] = parseBlockContents(value, this)
2424
this.name = serializeCSSComponentValue(prelude)
2525
descriptorNames.forEach(name => {
2626
const declaration = declarations.find(declaration => declaration.name === name)

lib/cssom/CSSConditionRule-impl.js

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

22
const { implementation: CSSGroupingRuleImpl } = require('./CSSGroupingRule-impl.js')
3+
const CSSNestedDeclarations = require('./CSSNestedDeclarations.js')
34
const CSSRuleList = require('./CSSRuleList.js')
4-
const CSSStyleRule = require('./CSSStyleRule.js')
55
const { parseBlockContents } = require('../parse/parser.js')
66
const { serializeCSSComponentValue } = require('../serialize.js')
77

@@ -17,16 +17,19 @@ class CSSConditionRuleImpl extends CSSGroupingRuleImpl {
1717
*/
1818
constructor(globalObject, args, privateData) {
1919
super(globalObject, args, privateData)
20+
const { parentStyleSheet } = this
2021
const { prelude, value } = privateData
21-
const { declarations, rules } = parseBlockContents(value, this)
22+
const rules = parseBlockContents(value, this).map(rule => {
23+
if (Array.isArray(rule) ) {
24+
return CSSNestedDeclarations.createImpl(globalObject, undefined, {
25+
declarations: rule,
26+
parentRule: this,
27+
parentStyleSheet,
28+
})
29+
}
30+
return rule
31+
})
2232
this._condition = prelude
23-
// Wrap declarations in nested style rule
24-
if (0 < declarations.length) {
25-
const data = { parentRule: this, parentStyleSheet: this.parentStyleSheet }
26-
const styleRule = CSSStyleRule.createImpl(globalObject, undefined, data)
27-
styleRule.style._declarations.push(...declarations)
28-
rules.unshift(styleRule)
29-
}
3033
this.cssRules = CSSRuleList.createImpl(globalObject, undefined, { rules })
3134
}
3235

lib/cssom/CSSCounterStyleRule-impl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class CSSCounterStyleRuleImpl extends CSSRuleImpl {
2222
constructor(globalObject, args, privateData) {
2323
super(globalObject, args, privateData)
2424
const { prelude, value } = privateData
25-
const { declarations } = parseBlockContents(value, this)
25+
const [declarations = []] = parseBlockContents(value, this)
2626
this._name = serializeCSSComponentValue(prelude)
2727
declarations.forEach(({ name, value }) => this[`_${cssPropertyToIDLAttribute(name)}`] = value)
2828
}

lib/cssom/CSSFontFaceRule-impl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class CSSFontFaceRuleImpl extends CSSRuleImpl {
1515
*/
1616
constructor(globalObject, args, privateData) {
1717
super(globalObject, args, privateData)
18-
const { declarations } = parseBlockContents(privateData.value, this)
18+
const [declarations = []] = parseBlockContents(privateData.value, this)
1919
this.style = CSSFontFaceDescriptors.createImpl(globalObject, undefined, { declarations, parentRule: this })
2020
}
2121

lib/cssom/CSSFontFeatureValuesMap-impl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class CSSFontFeatureValuesMapImpl {
2626
this.parentRule = parentRule
2727
this.parentStyleSheet = parentStyleSheet
2828
if (value) {
29-
const { declarations } = parseBlockContents(value, this)
29+
const [declarations = []] = parseBlockContents(value, this)
3030
const entries = declarations.map(({ name, value }) =>
3131
[name, Array.isArray(value) ? [...value.map(component => component.value)] : [value.value]])
3232
this._map = new Map(entries)

lib/cssom/CSSFontFeatureValuesRule-impl.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
const { serializeCSSComponentValueList, serializeCSSValue } = require('../serialize.js')
33
const CSSFontFeatureValuesMap = require('./CSSFontFeatureValuesMap.js')
4+
const CSSNestedDeclarations = require('./CSSNestedDeclarations.js')
45
const { implementation: CSSRuleImpl } = require('./CSSRule-impl.js')
56
const { cssPropertyToIDLAttribute } = require('../utils/string.js')
67
const { parseBlockContents } = require('../parse/parser.js')
@@ -22,7 +23,18 @@ class CSSFontFeatureValuesRuleImpl extends CSSRuleImpl {
2223
super(globalObject, args, privateData)
2324
const { parentStyleSheet } = this
2425
const { prelude, value } = privateData
25-
const { declarations, rules } = parseBlockContents(value, this)
26+
const contents = parseBlockContents(value, this)
27+
const declarations = Array.isArray(contents[0]) ? contents.shift() : []
28+
const rules = contents.map(rule => {
29+
if (Array.isArray(rule) ) {
30+
return CSSNestedDeclarations.createImpl(globalObject, undefined, {
31+
declarations,
32+
parentRule: this,
33+
parentStyleSheet,
34+
})
35+
}
36+
return rule
37+
})
2638
this.fontFamily = serializeCSSComponentValueList(prelude)
2739
declarations.forEach(declaration =>
2840
this[cssPropertyToIDLAttribute(declaration.name)] = serializeCSSValue(declaration))

lib/cssom/CSSFontPaletteValuesRule-impl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class CSSFontPaletteValuesRuleImpl extends CSSRuleImpl {
2020
constructor(globalObject, args, privateData) {
2121
super(globalObject, args, privateData)
2222
const { prelude, value } = privateData
23-
const { declarations } = parseBlockContents(value, this)
23+
const [declarations = []] = parseBlockContents(value, this)
2424
this.name = serializeCSSComponentValue(prelude)
2525
descriptorNames.forEach(name => {
2626
const declaration = declarations.find(declaration => declaration.name === name)

lib/cssom/CSSFunctionRule-impl.js

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

22
const { implementation: CSSGroupingRuleImpl } = require('./CSSGroupingRule-impl.js')
3+
const CSSNestedDeclarations = require('./CSSNestedDeclarations.js')
34
const CSSRuleList = require('./CSSRuleList.js')
45
const { parseBlockContents } = require('../parse/parser.js')
56
const { serializeCSSComponentValueList } = require('../serialize.js')
@@ -16,8 +17,18 @@ class CSSFunctionRuleImpl extends CSSGroupingRuleImpl {
1617
*/
1718
constructor(globalObject, args, privateData) {
1819
super(globalObject, args, privateData)
20+
const { parentStyleSheet } = this
1921
const { prelude, value } = privateData
20-
const { rules } = parseBlockContents(value, this)
22+
const rules = parseBlockContents(value, this).map(rule => {
23+
if (Array.isArray(rule) ) {
24+
return CSSNestedDeclarations.createImpl(globalObject, undefined, {
25+
declarations: rule,
26+
parentRule: this,
27+
parentStyleSheet,
28+
})
29+
}
30+
return rule
31+
})
2132
this._prelude = serializeCSSComponentValueList(prelude)
2233
this.cssRules = CSSRuleList.createImpl(globalObject, undefined, { rules })
2334
}

lib/cssom/CSSKeyframeRule-impl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class CSSKeyframeRuleImpl extends CSSRuleImpl {
2323
constructor(globalObject, args, privateData) {
2424
super(globalObject, args, privateData)
2525
const { prelude, value } = privateData
26-
const { declarations } = parseBlockContents(value, this)
26+
const [declarations = []] = parseBlockContents(value, this)
2727
this._keyText = prelude
2828
this.style = CSSKeyframeProperties.createImpl(globalObject, undefined, { declarations, parentRule: this })
2929
}

0 commit comments

Comments
 (0)