Skip to content

Commit 84779f7

Browse files
committed
Refactoring
- merged parseCSSValue() and parseCSSGrammar() - all entry points now consistently returns error, null, or a valid value
1 parent 286828a commit 84779f7

18 files changed

+276
-271
lines changed

__tests__/value.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const { cssom, install } = require('../lib/index.js')
3030
const { createContext, parseCSSGrammar } = require('../lib/parse/parser.js')
3131
const { toDegrees, toRadians } = require('../lib/utils/math.js')
3232
const { keywords: cssWideKeywords } = require('../lib/values/substitutions.js')
33+
const { isFailure } = require('../lib/utils/value.js')
3334
const { serializeCSSComponentValue } = require('../lib/serialize.js')
3435

3536
/**
@@ -41,11 +42,14 @@ const { serializeCSSComponentValue } = require('../lib/serialize.js')
4142
*/
4243
function parse(definition, value, serialize = true, context) {
4344
value = parseCSSGrammar(value, definition, context)
44-
if (serialize) {
45-
if (value) {
46-
return serializeCSSComponentValue(value)
45+
if (isFailure(value)) {
46+
if (serialize) {
47+
return ''
4748
}
48-
return ''
49+
return null
50+
}
51+
if (serialize) {
52+
return serializeCSSComponentValue(value)
4953
}
5054
return value
5155
}

lib/cssom/CSS-impl.js

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

2-
const { isComputationallyIndependent, isList } = require('../utils/value.js')
2+
const { isComputationallyIndependent, isFailure, isList } = require('../utils/value.js')
33
const { parseCSSDeclaration, parseCSSGrammar } = require('../parse/parser.js')
44
const { serializeIdentifier, serializeCSSComponentValue } = require('../serialize.js')
55
const { keywords: cssWideKeywords } = require('../values/substitutions.js')
@@ -40,7 +40,7 @@ const MISSING_INITIAL_CUSTOM_PROPERTY_VALUE = {
4040
* definition in multiple places.
4141
*/
4242
function supportsValue(property, value) {
43-
return parseCSSDeclaration(property, value, false, '@style') !== null
43+
return !isFailure(parseCSSDeclaration(property, value, false, '@style'))
4444
}
4545

4646
/**
@@ -50,7 +50,7 @@ function supportsValue(property, value) {
5050
*/
5151
function supportsCondition(conditionText) {
5252
const condition = parseCSSGrammar(`(${conditionText})`, '<supports-condition>')
53-
return condition ? matchSupport(condition) : false
53+
return !isFailure(condition) && matchSupport(condition)
5454
}
5555

5656
/**
@@ -81,15 +81,15 @@ class CSSImpl {
8181
* @see {@link https://drafts.css-houdini.org/css-properties-values-api-1/#dom-css-registerproperty}
8282
*/
8383
registerProperty({ name, inherits, initialValue, syntax }) {
84-
if (!parseCSSGrammar(name, '<custom-property-name>', '@property')) {
84+
if (isFailure(parseCSSGrammar(name, '<custom-property-name>', '@property'))) {
8585
throw INVALID_CUSTOM_PROPERTY_NAME
8686
}
8787
const register = globalThis.document._registeredPropertySet
8888
if (register.some(definition => definition.name === name)) {
8989
throw INVALID_CUSTOM_PROPERTY_OVERRIDE
9090
}
9191
syntax = parseCSSGrammar(syntax, '<syntax>', '@property')
92-
if (syntax === null) {
92+
if (isFailure(syntax)) {
9393
throw INVALID_CUSTOM_PROPERTY_SYNTAX
9494
}
9595
syntax = serializeCSSComponentValue(syntax)
@@ -99,15 +99,15 @@ class CSSImpl {
9999
return
100100
}
101101
initialValue = parseCSSGrammar(initialValue, '<declaration-value>?', '@property')
102-
if (initialValue && isValidUniversalInitialValue(initialValue)) {
102+
if (!isFailure(initialValue) && isValidUniversalInitialValue(initialValue)) {
103103
register.push({ inherits, initialValue, name, syntax })
104104
return
105105
}
106106
throw INVALID_INITIAL_CUSTOM_PROPERTY_VALUE_UNIVERSAL
107107
}
108108
if (initialValue) {
109109
initialValue = parseCSSGrammar(initialValue, syntax, '@property')
110-
if (initialValue && isComputationallyIndependent(initialValue)) {
110+
if (!isFailure(initialValue) && isComputationallyIndependent(initialValue)) {
111111
register.push({ inherits, initialValue, name, syntax })
112112
return
113113
}

lib/cssom/CSSCounterStyleRule-impl.js

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

2+
const { isFailure, isList } = require('../utils/value.js')
23
const { parseBlockContents, parseCSSDeclaration, parseCSSGrammar } = require('../parse/parser.js')
34
const { serializeCSSComponentValue, serializeCSSValue } = require('../serialize.js')
45
const { implementation: CSSRuleImpl } = require('./CSSRule-impl.js')
56
const counterStyles = require('../values/counter-styles.js')
67
const { cssPropertyToIDLAttribute } = require('../utils/string.js')
78
const { '@counter-style': descriptors } = require('../descriptors/definitions.js')
8-
const { isList } = require('../utils/value.js')
99
const { keyword } = require('../values/value.js')
1010

1111
const descriptorNames = Object.keys(descriptors)
@@ -56,9 +56,9 @@ class CSSCounterStyleRuleImpl extends CSSRuleImpl {
5656
if (this.system !== 'additive') {
5757
return
5858
}
59-
value = parseCSSDeclaration('additive-symbols', value, false, this)?.value
60-
if (value) {
61-
this._additiveSymbols = value
59+
value = parseCSSDeclaration('additive-symbols', value, false, this)
60+
if (!isFailure(value)) {
61+
this._additiveSymbols = value.value
6262
}
6363
}
6464

@@ -70,9 +70,9 @@ class CSSCounterStyleRuleImpl extends CSSRuleImpl {
7070
return _fallback ? serializeCSSValue({ name: 'fallback', value: _fallback }) : ''
7171
}
7272
set fallback(value) {
73-
value = parseCSSDeclaration('fallback', value, false, this)?.value
74-
if (value) {
75-
this._fallback = value
73+
value = parseCSSDeclaration('fallback', value, false, this)
74+
if (!isFailure(value)) {
75+
this._fallback = value.value
7676
}
7777
}
7878

@@ -84,7 +84,7 @@ class CSSCounterStyleRuleImpl extends CSSRuleImpl {
8484
}
8585
set name(name) {
8686
name = parseCSSGrammar(CSS.escape(name), '<counter-style-name>', this)
87-
if (name) {
87+
if (!isFailure(name)) {
8888
const lowercase = name.value.toLowerCase()
8989
if (counterStyles.predefined.includes(lowercase)) {
9090
name = keyword(lowercase, ['counter-style-name'])
@@ -101,9 +101,9 @@ class CSSCounterStyleRuleImpl extends CSSRuleImpl {
101101
return _negative ? serializeCSSValue({ name: 'negative', value: _negative }) : ''
102102
}
103103
set negative(value) {
104-
value = parseCSSDeclaration('negative', value, false, this)?.value
105-
if (value) {
106-
this._negative = value
104+
value = parseCSSDeclaration('negative', value, false, this)
105+
if (!isFailure(value)) {
106+
this._negative = value.value
107107
}
108108
}
109109

@@ -115,9 +115,9 @@ class CSSCounterStyleRuleImpl extends CSSRuleImpl {
115115
return _pad ? serializeCSSValue({ name: 'pad', value: _pad }) : ''
116116
}
117117
set pad(value) {
118-
value = parseCSSDeclaration('pad', value, false, this)?.value
119-
if (value) {
120-
this._pad = value
118+
value = parseCSSDeclaration('pad', value, false, this)
119+
if (!isFailure(value)) {
120+
this._pad = value.value
121121
}
122122
}
123123

@@ -129,9 +129,9 @@ class CSSCounterStyleRuleImpl extends CSSRuleImpl {
129129
return _prefix ? serializeCSSValue({ name: 'prefix', value: _prefix }) : ''
130130
}
131131
set prefix(value) {
132-
value = parseCSSDeclaration('prefix', value, false, this)?.value
133-
if (value) {
134-
this._prefix = value
132+
value = parseCSSDeclaration('prefix', value, false, this)
133+
if (!isFailure(value)) {
134+
this._prefix = value.value
135135
}
136136
}
137137

@@ -143,9 +143,9 @@ class CSSCounterStyleRuleImpl extends CSSRuleImpl {
143143
return _range ? serializeCSSValue({ name: 'range', value: _range }) : ''
144144
}
145145
set range(value) {
146-
value = parseCSSDeclaration('range', value, false, this)?.value
147-
if (value) {
148-
this._range = value
146+
value = parseCSSDeclaration('range', value, false, this)
147+
if (!isFailure(value)) {
148+
this._range = value.value
149149
}
150150
}
151151

@@ -157,9 +157,9 @@ class CSSCounterStyleRuleImpl extends CSSRuleImpl {
157157
return _speakAs ? serializeCSSValue({ name: 'speak-as', value: _speakAs }) : ''
158158
}
159159
set speakAs(value) {
160-
value = parseCSSDeclaration('speak-as', value, false, this)?.value
161-
if (value) {
162-
this._speakAs = value
160+
value = parseCSSDeclaration('speak-as', value, false, this)
161+
if (!isFailure(value)) {
162+
this._speakAs = value.value
163163
}
164164
}
165165

@@ -171,9 +171,9 @@ class CSSCounterStyleRuleImpl extends CSSRuleImpl {
171171
return _suffix ? serializeCSSValue({ name: 'suffix', value: _suffix }) : ''
172172
}
173173
set suffix(value) {
174-
value = parseCSSDeclaration('suffix', value, false, this)?.value
175-
if (value) {
176-
this._suffix = value
174+
value = parseCSSDeclaration('suffix', value, false, this)
175+
if (!isFailure(value)) {
176+
this._suffix = value.value
177177
}
178178
}
179179

@@ -189,9 +189,9 @@ class CSSCounterStyleRuleImpl extends CSSRuleImpl {
189189
if (system === 'additive' || system.startsWith('extends')) {
190190
return
191191
}
192-
value = parseCSSDeclaration('symbols', value, false, this)?.value
193-
if (value && (1 < value.length || (system !== 'alphabetic' && system !== 'numeric'))) {
194-
this._symbols = value
192+
value = parseCSSDeclaration('symbols', value, false, this)
193+
if (!isFailure(value) && (1 < value.value.length || (system !== 'alphabetic' && system !== 'numeric'))) {
194+
this._symbols = value.value
195195
}
196196
}
197197

@@ -206,15 +206,15 @@ class CSSCounterStyleRuleImpl extends CSSRuleImpl {
206206
const { _system } = this
207207
if (!_system) {
208208
// Explicit defaut system
209-
value = parseCSSDeclaration('system', value, false, this)?.value
210-
if (value?.value === 'symbolic') {
211-
this._system = value
209+
value = parseCSSDeclaration('system', value, false, this)
210+
if (!isFailure(value) && value.value.value === 'symbolic') {
211+
this._system = value.value
212212
}
213213
} else if (isList(_system)) {
214214
// Fixed <integer>? or extends <counter-style-name>
215-
value = parseCSSDeclaration('system', value, false, this)?.value
216-
if (value && value[0]?.value === _system[0]?.value) {
217-
this._system = value
215+
value = parseCSSDeclaration('system', value, false, this)
216+
if (!isFailure(value) && value.value[0]?.value === _system[0]?.value) {
217+
this._system = value.value
218218
}
219219
}
220220
}

lib/cssom/CSSFontFeatureValuesMap-impl.js

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

2+
const { isFailure, isList } = require('../utils/value.js')
23
const { parseBlockContents, parseCSSDeclaration } = require('../parse/parser.js')
34
const error = require('../error.js')
4-
const { isList } = require('../utils/value.js')
55
const root = require('../rules/definitions.js')
66

77
const INVALID_FONT_FEATURE_VALUE_ERROR = {
@@ -87,7 +87,7 @@ class CSSFontFeatureValuesMapImpl {
8787
if (!Array.isArray(values)) {
8888
values = [values]
8989
}
90-
if (!parseCSSDeclaration(key, values.join(' '), false, this)) {
90+
if (isFailure(parseCSSDeclaration(key, values.join(' '), false, this))) {
9191
throw error(INVALID_FONT_FEATURE_VALUE_ERROR)
9292
}
9393
this._map.set(key, values)

lib/cssom/CSSKeyframeRule-impl.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { parseBlockContents, parseCSSGrammar } = require('../parse/parser.js')
33
const CSSKeyframeProperties = require('./CSSKeyframeProperties.js')
44
const { implementation: CSSRuleImpl } = require('./CSSRule-impl')
55
const error = require('../error.js')
6+
const { isFailure } = require('../utils/value.js')
67
const { serializeCSSComponentValueList } = require('../serialize.js')
78

89
const SET_INVALID_KEY_TEXT_ERROR = {
@@ -53,11 +54,10 @@ class CSSKeyframeRuleImpl extends CSSRuleImpl {
5354
*/
5455
set keyText(selector) {
5556
selector = parseCSSGrammar(selector, '<keyframe-selector>#', this)
56-
if (selector) {
57-
this._keyText = selector
58-
} else {
57+
if (isFailure(selector)) {
5958
throw error(SET_INVALID_KEY_TEXT_ERROR)
6059
}
60+
this._keyText = selector
6161
}
6262
}
6363

lib/cssom/CSSKeyframesRule-impl.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { implementation: CSSRuleImpl } = require('./CSSRule-impl.js')
55
const CSSRuleList = require('./CSSRuleList.js')
66
const error = require('../error.js')
77
const idlUtils = require('./utils.js')
8+
const { isFailure } = require('../utils/value.js')
89

910
const SET_INVALID_NAME_ERROR = {
1011
message: "Cannot set 'name': invalid value",
@@ -63,11 +64,10 @@ class CSSKeyframesRuleImpl extends CSSRuleImpl {
6364
*/
6465
set name(name) {
6566
name = parseCSSGrammar(CSS.escape(name), '<keyframes-name>', this)
66-
if (name) {
67-
this._name = name
68-
} else {
67+
if (isFailure(name)) {
6968
throw error(SET_INVALID_NAME_ERROR)
7069
}
70+
this._name = name
7171
}
7272

7373
/**
@@ -108,7 +108,7 @@ class CSSKeyframesRuleImpl extends CSSRuleImpl {
108108
*/
109109
deleteRule(selector) {
110110
selector = parseCSSGrammar(selector, '<keyframe-selector>#', this)
111-
if (selector) {
111+
if (!isFailure(selector)) {
112112
selector = serializeCSSComponentValueList(selector)
113113
const rules = this.cssRules._rules
114114
const index = rules.findLastIndex(rule => rule.keyText === selector)
@@ -125,7 +125,7 @@ class CSSKeyframesRuleImpl extends CSSRuleImpl {
125125
*/
126126
findRule(selector) {
127127
selector = parseCSSGrammar(selector, '<keyframe-selector>#', this)
128-
if (selector) {
128+
if (!isFailure(selector)) {
129129
selector = serializeCSSComponentValueList(selector)
130130
const rules = this.cssRules._rules
131131
const index = rules.findLastIndex(rule => rule.keyText === selector)

lib/cssom/CSSPageRule-impl.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { implementation: CSSGroupingRuleImpl } = require('./CSSGroupingRule-impl'
44
const CSSPageDeclarations = require('./CSSPageDeclarations.js')
55
const CSSPageDescriptors = require('./CSSPageDescriptors.js')
66
const CSSRuleList = require('./CSSRuleList.js')
7+
const { isFailure } = require('../utils/value.js')
78
const { serializeCSSComponentValue } = require('../serialize.js')
89

910
/**
@@ -74,7 +75,7 @@ class CSSPageRuleImpl extends CSSGroupingRuleImpl {
7475
*/
7576
set selectorText(selector) {
7677
selector = parseCSSGrammar(selector, '<page-selector-list>', this)
77-
if (selector) {
78+
if (!isFailure(selector)) {
7879
this._selectors = selector
7980
}
8081
}

lib/cssom/CSSStyleDeclaration-impl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ class CSSStyleDeclarationImpl {
210210
}
211211
const context = this.parentRule ?? this._ownerNode
212212
const declaration = parseCSSDeclaration(name, value, !!priority, context)
213-
if (declaration === null) {
213+
if (isFailure(declaration)) {
214214
return
215215
}
216216
let updated = true

lib/cssom/CSSStyleRule-impl.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const CSSRuleList = require('./CSSRuleList.js')
77
const CSSScopeRule = require('./CSSScopeRule.js')
88
const CSSStyleProperties = require('./CSSStyleProperties.js')
99
const CSSStyleRule = require('./CSSStyleRule.js')
10+
const { isFailure } = require('../utils/value.js')
1011
const { serializeCSSComponentValueList } = require('../serialize.js')
1112

1213
const nestingSelector = { types: ['<delimiter-token>'], value: '&' }
@@ -103,7 +104,7 @@ class CSSStyleRuleImpl extends CSSGroupingRuleImpl {
103104
set selectorText(selector) {
104105
const definition = this.parentRule ? '<relative-selector-list>' : '<selector-list>'
105106
selector = parseCSSGrammar(selector, definition, this)
106-
if (selector) {
107+
if (!isFailure(selector)) {
107108
this._selectors = selector
108109
}
109110
}

lib/parse/arbitrary.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function matchCommaContainingValue(node, match) {
103103
* @returns {SyntaxError|object|null}
104104
*/
105105
function matchDeclaration(node, parser) {
106-
return parser.parseDeclaration(node.input, '@style') ?? null
106+
return parser.parseDeclaration(node.input, '@style')
107107
}
108108

109109
/**

0 commit comments

Comments
 (0)