Skip to content

Commit ff49fcd

Browse files
committed
Serialize feature values as CSS component values
w3c/csswg-drafts#11041
1 parent d40fbb4 commit ff49fcd

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

__tests__/value.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3794,7 +3794,13 @@ describe('<mf-plain>', () => {
37943794
.toMatchObject(list([name, delimiter(':'), value], ' ', ['<mf-plain>']))
37953795
})
37963796
test('valid', () => {
3797-
expect(parse('<mf-plain>', 'min-width: 1px', true, mediaQueryContext)).toBe('min-width: 1px')
3797+
const valid = [
3798+
['orientation: PORTRAIT', 'orientation: portrait'],
3799+
['color: 1.0', 'color: 1'],
3800+
['min-width: 0', 'min-width: 0px'],
3801+
['aspect-ratio: 1', 'aspect-ratio: 1 / 1'],
3802+
]
3803+
valid.forEach(([input, expected]) => expect(parse('<mf-plain>', input, true, mediaQueryContext)).toBe(expected))
37983804
})
37993805
})
38003806
describe('<mf-range>', () => {
@@ -3821,10 +3827,8 @@ describe('<mf-range>', () => {
38213827
.toMatchObject(list([name, comparator, value], ' ', ['<mf-range>']))
38223828
})
38233829
test('valid', () => {
3824-
expect(parse('<mf-range>', 'width < 1px', true, mediaQueryContext))
3825-
.toBe('width < 1px')
3826-
expect(parse('<mf-range>', '0 / 0 < aspect-ratio < 1 / 1', true, mediaQueryContext))
3827-
.toBe('0 / 0 < aspect-ratio < 1 / 1')
3830+
expect(parse('<mf-range>', 'width < 0', true, mediaQueryContext)).toBe('width < 0px')
3831+
expect(parse('<mf-range>', '0 < aspect-ratio < 1', true, mediaQueryContext)).toBe('0 / 1 < aspect-ratio < 1 / 1')
38283832
})
38293833
})
38303834
describe('<opentype-tag>', () => {

lib/parse/postprocess.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,13 @@ function postParseMediaFeaturePlain(declaration, { context, input }, parser) {
909909
const unprefixed = name.value.replace(/(min|max)-/, '')
910910
const definition = descriptors[getMediaFeatureContextName(context)][unprefixed].value
911911
input = getInputComponentValuesFromMatch(value, input.data)
912-
return parser.parseCSSValue(input, definition, context) ? declaration : null
912+
const validated = parser.parseCSSValue(input, definition, context)
913+
if (validated) {
914+
validated.types.push('<mf-value>')
915+
declaration.splice(2, 1, validated)
916+
return declaration
917+
}
918+
return null
913919
}
914920

915921
/**
@@ -924,14 +930,23 @@ function postParseMediaFeaturePlain(declaration, { context, input }, parser) {
924930
*/
925931
function postParseMediaFeatureRange(range, { context, input }, parser) {
926932
const name = range.find(component => component.types.at(-1) === '<mf-name>')
927-
const values = range
928-
.filter(component => component.types.at(-1) === '<mf-value>')
929-
.map(value => getInputComponentValuesFromMatch(value, input.data))
930933
const { type, value: definition } = descriptors[getMediaFeatureContextName(context)][name.value]
931-
if (type === 'range' && values.every(value => parser.parseCSSValue(value, definition, context))) {
932-
return range
934+
if (type !== 'range') {
935+
return null
933936
}
934-
return null
937+
for (let component of range.splice(0, range.length)) {
938+
if (component.types.at(-1) === '<mf-value>') {
939+
if (name.value === 'aspect-ratio') debugger;
940+
const value = getInputComponentValuesFromMatch(component, input.data)
941+
component = parser.parseCSSValue(value, definition, context)
942+
if (!component) {
943+
return null
944+
}
945+
component.types.push('<mf-value>')
946+
}
947+
range.push(component)
948+
}
949+
return range
935950
}
936951

937952
/**

0 commit comments

Comments
 (0)