Skip to content

Commit d1f28b2

Browse files
committed
Minor refactoring
1 parent 7e972f9 commit d1f28b2

File tree

9 files changed

+88
-59
lines changed

9 files changed

+88
-59
lines changed

lib/cssom/CSSStyleDeclaration-impl.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class CSSStyleDeclarationImpl {
158158
* value as a whitespace.
159159
*/
160160
getPropertyValue(name) {
161-
name = getDeclarationName(name, this.parentRule ?? this._ownerNode)
161+
name = getDeclarationName(this.parentRule ?? this._ownerNode, name)
162162
if (shorthands.has(name)) {
163163
const value = []
164164
let important = null
@@ -234,7 +234,7 @@ class CSSStyleDeclarationImpl {
234234
if (this._readOnly) {
235235
throw error(UPDATE_COMPUTED_STYLE_DECLARATION_ERROR)
236236
}
237-
name = getDeclarationName(name, this.parentRule ?? this._ownerNode)
237+
name = getDeclarationName(this.parentRule ?? this._ownerNode, name)
238238
if (!name) {
239239
return ''
240240
}
@@ -270,7 +270,7 @@ class CSSStyleDeclarationImpl {
270270
if (this._computed) {
271271
return ''
272272
}
273-
name = getDeclarationName(name, this.parentRule ?? this._ownerNode)
273+
name = getDeclarationName(this.parentRule ?? this._ownerNode, name)
274274
if (shorthands.has(name)) {
275275
if (shorthands.get(name).every(longhand => this.getPropertyPriority(longhand) === 'important')) {
276276
return 'important'

lib/parse/grammar.js

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

22
const { createPermutationIterator, getPermutationIndex } = require('./permutation.js')
33
const { isBlock, isDelimiter, isList, isOmitted } = require('../utils/value.js')
4-
const { isBranch, isCSSType, isSequence } = require('../utils/definition.js')
4+
const { isBranch, isCSSType, isSequence } = require('../utils/node.js')
55
const { list, omitted } = require('../values/value.js')
66
const Stream = require('./stream.js')
77
const actions = require('./actions.js')
@@ -140,7 +140,7 @@ function isInputAtEnd(value, node) {
140140
* @returns {boolean}
141141
*/
142142
function isNonTerminal(value, node) {
143-
return isBranch(node.definition)
143+
return isBranch(node)
144144
}
145145

146146
/**
@@ -204,10 +204,10 @@ function intercept(node, parser, error) {
204204
* @returns {SyntaxError|object|object[]|null|undefined}
205205
*/
206206
function match(node, parser) {
207-
const { context, definition, input } = node
208-
if (isBranch(definition)) {
207+
if (isBranch(node)) {
209208
return traverse(node, parser)
210209
}
210+
const { context, definition, input } = node
211211
const { name, type } = definition
212212
switch (type) {
213213
case 'arbitrary':
@@ -340,9 +340,10 @@ function replace(node, parser) {
340340
* @param {object|object[]} value
341341
* @returns {object|object[]}
342342
*/
343-
function tag({ definition }, parser, value) {
344-
const { name } = definition
345-
if (value !== omitted && isCSSType(definition) && name !== '<declaration>') {
343+
function tag(node, parser, value) {
344+
const { definition: { name } } = node
345+
if (value !== omitted && isCSSType(node) && name !== '<declaration>') {
346+
// TODO: review `consumeDeclaration()`
346347
if (!isList(value)) {
347348
value = { ...value, types: [...value.types] }
348349
}
@@ -481,7 +482,7 @@ function traverse(node, parser) {
481482
continue
482483
}
483484

484-
if (isSequence(definition)) {
485+
if (isSequence(node)) {
485486
let values
486487
if (type === '&&' || type === '||') {
487488
values = value.map(definition =>

lib/parse/parser.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,11 @@ function createCSSRule(name, prelude, value, definition, { root, rule }) {
225225
}
226226

227227
/**
228+
* @param {CSSRuleImpl|Element|object|string} context
228229
* @param {string} name
229-
* @param {CSSRuleImpl|Element|string} context
230230
* @returns {string}
231231
*/
232-
function getDeclarationName(name, context) {
232+
function getDeclarationName(context, name) {
233233
context = createContext(context)
234234
const { rule: { definition: { name: ruleName, value } } } = context
235235
if (name.startsWith('--')) {
@@ -243,13 +243,13 @@ function getDeclarationName(name, context) {
243243
}
244244

245245
/**
246-
* @param {string} name
247246
* @param {object} context
247+
* @param {string} name
248248
* @returns {object|null}
249249
*/
250-
function getDeclarationDefinition(name, context) {
251-
name = getDeclarationName(name, context)
250+
function getDeclarationDefinition(context, name) {
252251
const { rule: { definition: { name: ruleName, value: { descriptors, properties } } } } = context
252+
name = getDeclarationName(context, name)
253253
if (properties) {
254254
if (name.startsWith('--')) {
255255
const definition = properties?.['--*']
@@ -541,7 +541,7 @@ function consumeDeclaration(tokens, context, nested) {
541541
if (!name) {
542542
return null
543543
}
544-
const definition = getDeclarationDefinition(name.value, context)
544+
const definition = getDeclarationDefinition(context, name.value)
545545
if (!definition) {
546546
return null
547547
}
@@ -1130,7 +1130,7 @@ function parseCSSDeclaration(name, value, important, context) {
11301130
return null
11311131
}
11321132

1133-
const definition = getDeclarationDefinition(name, context)
1133+
const definition = getDeclarationDefinition(context, name)
11341134
if (!definition) {
11351135
return null
11361136
}

lib/parse/postprocess.js

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

22
const { angle, filter, keyword, list, map, number, omitted, string } = require('../values/value.js')
3-
const { findContext, findParent, findSibling } = require('../utils/context.js')
3+
const { findContext, findSibling, isDeclaredBy, isProducedBy } = require('../utils/context.js')
44
const { addTypes, dimensionTypes, getCalculationType, matchNumericType, types: numericTypes } = require('./types.js')
55
const { isCalculation, isColon, isList, isOmitted, isWhitespace } = require('../utils/value.js')
66
const { isHexadecimal, isIdentifierCharacter, isWhitespace: isWhitespaceCharacter } = require('./tokenize.js')
@@ -167,7 +167,7 @@ function postParseAnimateableFeature(name, node) {
167167
* It aborts parsing when the timeline is a keyword used as <progress-source>.
168168
*/
169169
function postParseAnimationTimeline(timeline, node) {
170-
if (timeline.types.includes('<keyword>') && findParent(node, node => node.definition.name === '<progress-source>')) {
170+
if (timeline.types.includes('<keyword>') && isProducedBy(node, '<progress-source>')) {
171171
return error(node)
172172
}
173173
return timeline
@@ -260,10 +260,7 @@ function postParseCalcValue(value) {
260260
* in @font-palette-values.
261261
*/
262262
function postParseColor(color, node) {
263-
if (
264-
node.context.declaration?.definition.name === 'override-colors'
265-
&& !color.types.includes('<color-base>')
266-
) {
263+
if (isDeclaredBy(node, 'override-colors') && !color.types.includes('<color-base>')) {
267264
return error(node)
268265
}
269266
return color
@@ -500,7 +497,7 @@ function postParseDimension(dimension, { definition: { max, min, name } }) {
500497
* It aborts parsing the name when it is invalid in the context.
501498
*/
502499
function postParseFamilyName(name, node) {
503-
const invalid = node.context.declaration?.definition.name === 'voice-family'
500+
const invalid = isDeclaredBy(node, 'voice-family')
504501
? reserved.voiceFamilyName
505502
: reserved.fontFamilyName
506503
if (name.types.includes('<string>') || 1 < name.length || !invalid.includes(name[0].value.toLowerCase())) {
@@ -1015,7 +1012,7 @@ function postParseMediaFeatureName(name, node) {
10151012
if (descriptors[contextName][lowercase]) {
10161013
return { ...name, value: lowercase }
10171014
}
1018-
} else if (parent?.parent?.definition.name === '<mf-plain>'/* only for <mf-name> tests: */ || !parent) {
1015+
} else if (isProducedBy(node, '<mf-plain>') /* only for <mf-name> tests: */ || !node.parent) {
10191016
if (target) {
10201017
return { ...name, value: `${lowercase.includes('min-') ? 'min' : 'max'}-${target}` }
10211018
}
@@ -1260,7 +1257,7 @@ function postParsePaintOrder(paint) {
12601257
* is the offset-path property.
12611258
*/
12621259
function postParsePath(path, node) {
1263-
if (node.context.declaration?.definition.name === 'offset-path') {
1260+
if (isDeclaredBy(node, 'offset-path')) {
12641261
const { value: [,, ...tail] } = path
12651262
return { ...path, value: list([omitted, omitted, ...tail]) }
12661263
}
@@ -1573,7 +1570,7 @@ function postParseStyleFeature(feature, node, { createContext, getDeclarationDef
15731570
const { types, value } = feature
15741571
if (
15751572
types[0] === '<ident-token>'
1576-
? getDeclarationDefinition(value, createContext('@style'))
1573+
? getDeclarationDefinition(createContext('@style'), value)
15771574
: !value.value?.startsWith?.('revert')
15781575
) {
15791576
return feature

lib/parse/preprocess.js

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

2-
const { findContext, findParent, findSibling } = require('../utils/context.js')
2+
const { findContext, findParent, findSibling, isProducedBy } = require('../utils/context.js')
33
const { isColon, isComma, isDelimiter, isOmitted, isWhitespace } = require('../utils/value.js')
44
const { length, omitted } = require('../values/value.js')
55
const createError = require('../error.js')
@@ -32,7 +32,7 @@ function preParseCalcOperator(node) {
3232
if (input.atEnd()) {
3333
return null
3434
}
35-
if (!isWhitespace(input.current) && findParent(node, node => node.definition.name === '<calc-sum>')) {
35+
if (!isWhitespace(input.current) && isProducedBy(node, '<calc-sum>')) {
3636
if (isDelimiter(['-', '+'], input.next())) {
3737
return error(node)
3838
}
@@ -234,7 +234,7 @@ function preParseImageSetOption(node) {
234234
if (next) {
235235
const name = next.name?.toLowerCase()
236236
if (
237-
context.function.parent.parent?.definition.name === '<url-set>'
237+
isProducedBy(context.function.parent, '<url-set>')
238238
&& name !== 'src'
239239
&& name !== 'url'
240240
&& next.types[0] !== '<string-token>'

lib/parse/replace.js

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

22
const { colorFunctionKeywords, colorSpaces, colorSpaceKeywords } = require('../values/colors.js')
3+
const { findSibling, isProducedBy } = require('../utils/context.js')
34
const { getCalculationType, matchNumericType } = require('./types.js')
45
const createError = require('../error.js')
5-
const { findSibling } = require('../utils/context.js')
66
const { isDelimiter } = require('../utils/value.js')
77
const { simplifyCalculation } = require('./simplify.js')
88
const substitutions = require('../values/substitutions.js')
@@ -47,7 +47,7 @@ function replaceNumeric(node, parser) {
4747

4848
const { context, definition, input, parent } = node
4949
const name = input.next().name?.toLowerCase()
50-
const topLevel = !context.replaced
50+
const topLevel = !isProducedBy(node, '<calc-value>')
5151

5252
const fn = name && substitutions.numeric.find(definition => definition.name === name)
5353

@@ -71,7 +71,7 @@ function replaceNumeric(node, parser) {
7171
}
7272

7373
// Validate type and simplify calculations once from top to bottom
74-
if (topLevel && parent?.parent?.definition.name !== '<mf-value>') {
74+
if (topLevel && !isProducedBy(node, '<mf-value>')) {
7575

7676
const { name, range } = definition
7777
const replacedType = name === '<integer>' ? '<number>' : name

lib/serialize.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const { MAX_INTEGER, MIN_INTEGER } = require('./values/integers.js')
33
const { canonicalize, definitions: dimensions, getCanonicalUnitFromType } = require('./values/dimensions.js')
44
const { clamp, isDegenerate, safeFloat, toEightBit } = require('./utils/math.js')
5-
const { combinators, isCombination, isCompound, isMultiplied } = require('./utils/definition.js')
5+
const { combinators, isCombination, isCompound, isMultiplied } = require('./utils/node.js')
66
const { hslToRgb, hwbToRgb } = require('./utils/color.js')
77
const { isBlock, isCalculationOperator, isComma, isList, isNumeric, isOmitted } = require('./utils/value.js')
88
const { isDigit, isIdentifierCharacter, isNonASCIIIdentifierCharacter } = require('./parse/tokenize.js')
@@ -671,10 +671,10 @@ function serializeDeclaration(declaration) {
671671
*/
672672
function serializeDefinition(definition, parentPrecedence = 0) {
673673
const { associatedToken, max, min, name, range, separator, type, value } = definition
674-
if (isMultiplied(definition)) {
674+
if (isMultiplied({ definition })) {
675675
let string = ''
676676
string = serializeDefinition(value)
677-
if (isCombination(value) || isCompound(value) || value.type === 'optional' || string.endsWith('}')) {
677+
if (isCombination({ definition: value }) || isCompound({ definition: value }) || value.type === 'optional' || string.endsWith('}')) {
678678
string = `[${string}]`
679679
}
680680
if (type === 'optional') {
@@ -686,7 +686,7 @@ function serializeDefinition(definition, parentPrecedence = 0) {
686686
}
687687
return string
688688
}
689-
if (isCombination(definition)) {
689+
if (isCombination({ definition })) {
690690
const [seed, ...types] = value
691691
const precedence = combinators.indexOf(type)
692692
const combinator = type === ' ' ? type : ` ${type} `

lib/utils/context.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
const { isCSSType } = require('./node.js')
3+
24
/**
35
* @param {object} node
46
* @param {function} accept
@@ -85,8 +87,37 @@ function findSibling({ context, parent }, accept, abort, climb) {
8587
return null
8688
}
8789

90+
/**
91+
* @param {object} node
92+
* @returns {object}
93+
*/
94+
function getProduction(node) {
95+
return findParent(node, isCSSType, isCSSType)
96+
}
97+
98+
/**
99+
* @param {object} node
100+
* @param {string} name
101+
* @returns {boolean}
102+
*/
103+
function isDeclaredBy(node, name) {
104+
return node.context.declaration?.definition.name === name
105+
}
106+
107+
/**
108+
* @param {object} node
109+
* @param {string} name
110+
* @returns {boolean}
111+
*/
112+
function isProducedBy(node, name) {
113+
return getProduction(node)?.definition.name === name
114+
}
115+
88116
module.exports = {
89117
findContext,
90118
findParent,
91119
findSibling,
120+
getProduction,
121+
isDeclaredBy,
122+
isProducedBy,
92123
}

0 commit comments

Comments
 (0)