Skip to content

Commit d63d44d

Browse files
committed
Add support for <if()>
1 parent ba11c57 commit d63d44d

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

__tests__/style.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ describe('arbitrary substitution', () => {
412412
['--custom(--custom(!))'],
413413
['attr(name, attr())'],
414414
['env(name, env())'],
415+
['if(if())', 'if(if())'],
415416
['inherit(--custom, inherit())'],
416417
['random-item(--key, random-item())'],
417418
['var(--custom, var())'],
@@ -421,6 +422,7 @@ describe('arbitrary substitution', () => {
421422
[' /**/ @1/**/1e0 --CUSTOM( /**/ 1e0 /**/ ', '@1 1 --CUSTOM(1)'],
422423
[' /**/ @1/**/1e0 ATTR( name, /**/ 1e0 /**/ ', '@1 1 attr(name, 1)'],
423424
[' /**/ @1/**/1e0 ENV( name, /**/ 1e0 /**/ ', '@1 1 env(name, 1)'],
425+
[' /**/ @1/**/1e0 IF( if(): /**/ 1e0 /**/ ', '@1 1 if(if(): 1)'],
424426
[' /**/ @1/**/1e0 RANDOM-ITEM( --key, /**/ 1e0 /**/ ', '@1 1 random-item(--key, 1)'],
425427
[' /**/ @1/**/1e0 VAR( --custom, /**/ 1e0 /**/ ', '@1 1 var(--custom, 1)'],
426428
]
@@ -4712,6 +4714,10 @@ describe('CSSFontFaceDescriptors', () => {
47124714
style.sizeAdjust = 'env(name, attr(name))'
47134715
expect(style.fontWeight).toBe('env(name, attr(name))')
47144716
expect(style.sizeAdjust).toBe('env(name, attr(name))')
4717+
style.fontWeight = 'if(media(width): 1)'
4718+
style.sizeAdjust = 'if(media(width): 1%)'
4719+
expect(style.fontWeight).toBe('if(media(width): 1)')
4720+
expect(style.sizeAdjust).toBe('if(media(width): 1%)')
47154721
style.fontWeight = 'first-valid(1)'
47164722
style.sizeAdjust = 'first-valid(1%)'
47174723
expect(style.fontWeight).toBe('1')
@@ -4786,6 +4792,8 @@ describe('CSSKeyframeProperties', () => {
47864792
// Dependency-free substitution
47874793
style.fontWeight = 'env(name)'
47884794
expect(style.fontWeight).toBe('env(name)')
4795+
style.fontWeight = 'if(media(width): 1)'
4796+
expect(style.fontWeight).toBe('if(media(width): 1)')
47894797
style.fontWeight = 'first-valid(1)'
47904798
expect(style.fontWeight).toBe('1')
47914799
style.fontWeight = 'calc(progress(1, 0, 1))'
@@ -4846,6 +4854,8 @@ describe('CSSMarginDescriptors', () => {
48464854
// Dependency-free substitution
48474855
style.fontWeight = 'env(name)'
48484856
expect(style.fontWeight).toBe('env(name)')
4857+
style.fontWeight = 'if(media(width): 1)'
4858+
expect(style.fontWeight).toBe('if(media(width): 1)')
48494859
style.fontWeight = 'first-valid(1)'
48504860
expect(style.fontWeight).toBe('1')
48514861
style.fontWeight = 'calc(progress(1, 0, 1))'
@@ -4908,6 +4918,10 @@ describe('CSSPageDescriptors', () => {
49084918
style.size = 'env(name, attr(name))'
49094919
expect(style.fontWeight).toBe('env(name, attr(name))')
49104920
expect(style.size).toBe('env(name, attr(name))')
4921+
style.fontWeight = 'if(media(width): 1)'
4922+
style.size = 'if(media(width): 1px)'
4923+
expect(style.fontWeight).toBe('if(media(width): 1)')
4924+
expect(style.size).toBe('if(media(width): 1px)')
49114925
style.fontWeight = 'first-valid(1)'
49124926
style.size = 'first-valid(1px)'
49134927
expect(style.fontWeight).toBe('1')
@@ -4997,6 +5011,8 @@ describe('CSSPositionTryDescriptors', () => {
49975011
// Dependency-free substitution
49985012
style.top = 'env(name)'
49995013
expect(style.top).toBe('env(name)')
5014+
style.top = 'if(media(width): 1px)'
5015+
expect(style.top).toBe('if(media(width): 1px)')
50005016
style.top = 'first-valid(1px)'
50015017
expect(style.top).toBe('1px')
50025018
style.top = 'calc(1px * progress(1, 0, 1))'

lib/values/definitions.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,12 @@ module.exports = {
222222
'<ident()>': 'ident(<ident-arg>+)',
223223
'<ident-arg>': '<string> | <integer> | <ident>',
224224
'<ident>': '<ident-token>',
225+
'<if()>': 'if([<if-branch> ;]* <if-branch> ;?)',
226+
'<if-args-branch>': '<declaration-value> [: <declararion-value>?]?',
227+
'<if-args>': 'if([<if-args-branch> ;]* <if-args-branch> ;?)',
228+
'<if-branch>': '<if-condition> : <declaration-value>?',
229+
'<if-condition>': '<boolean-expr[<if-test> ]> | else',
230+
'<if-test>': 'supports(<ident> : <declaration-value> | <supports-condition>) | media(<media-feature> | <media-condition>) | style(<style-query>)',
225231
'<image()>': 'image(<image-tags>? [<image-src>? , <color>?]!)',
226232
'<image-1D>': '<stripes()>',
227233
'<image-set()>': 'image-set(<image-set-option>#)',

lib/values/substitutions.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const properties = require('../properties/definitions.js')
44
/**
55
* @see {@link https://drafts.csswg.org/css-env-1/#funcdef-env}
66
* @see {@link https://drafts.csswg.org/css-values-5/#funcdef-attr}
7+
* @see {@link https://drafts.csswg.org/css-values-5/#funcdef-if}
78
* @see {@link https://drafts.csswg.org/css-values-5/#funcdef-inherit}
89
* @see {@link https://drafts.csswg.org/css-values-5/#funcdef-random-item}
910
* @see {@link https://drafts.csswg.org/css-variables-2/#funcdef-var}
@@ -12,6 +13,7 @@ const arbitrary = [
1213
{ definition: '<dashed-function>', element: true, name: '<dashed-ident>' },
1314
{ definition: '<attr-args>', element: true, name: 'attr' },
1415
{ definition: '<env-args>', name: 'env' },
16+
{ definition: '<if-args>', name: 'if' },
1517
{ definition: '<inherit-args>', cascade: true, name: 'inherit' },
1618
{ definition: '<random-item-args>', element: true, name: 'random-item' },
1719
{ definition: '<var-args>', cascade: true, name: 'var' },

scripts/extract.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ const replaced = {
186186
'<radial-size>': 'closest-corner | farthest-corner | <radial-radius>{1,2}',
187187
// https://github.com/w3c/csswg-drafts/issues/11842
188188
'<control-value()>': 'control-value(<syntax-type-name>?)',
189+
// https://github.com/w3c/csswg-drafts/issues/12487
190+
'<if-args-branch>': '<declaration-value> [: <declararion-value>?]?',
189191
// https://github.com/w3c/fxtf-drafts/issues/532
190192
'<mask-reference>': 'none | <image>',
191193
// https://github.com/w3c/csswg-drafts/pull/10131
@@ -587,13 +589,6 @@ const excluded = {
587589
'<mask-source>',
588590
],
589591
'css-values-5': [
590-
// TODO: add support for `<boolean-expr[<test>]>`
591-
'<if()>',
592-
'<if-args>',
593-
'<if-args-branch>',
594-
'<if-branch>',
595-
'<if-condition>',
596-
'<if-test>',
597592
// https://github.com/w3c/csswg-drafts/pull/12349
598593
'<input-position>{1,2} : <output-value>',
599594
],

0 commit comments

Comments
 (0)