Skip to content

Commit 7fcf5aa

Browse files
committed
Allow matching functions as literals.
I've updated several simple ValidationType functions to show how this can be used to simplify common matchers. It will also be used in a follow-up patch to validate the `font-variant-alternates` property which involves a large number of CSS function options.
1 parent 739bf0a commit 7fcf5aa

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

src/css/ValidationTypes.js

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ var ValidationTypes = {
88
i, len, found = false;
99

1010
for (i=0,len=args.length; i < len && !found; i++){
11-
if (text === args[i].toLowerCase()){
11+
if (args[i].slice(-2) === "()"){
12+
found = (part.type === "function" &&
13+
part.name === args[i].slice(0, -2));
14+
} else if (text === args[i].toLowerCase()){
1215
found = true;
1316
}
1417
}
@@ -92,7 +95,7 @@ var ValidationTypes = {
9295
},
9396

9497
"<attr>": function(part){
95-
return part.type === "function" && part.name === "attr";
98+
return ValidationTypes.isLiteral(part, "attr()");
9699
},
97100

98101
"<bg-image>": function(part){
@@ -108,7 +111,7 @@ var ValidationTypes = {
108111
},
109112

110113
"<content>": function(part){
111-
return part.type === "function" && part.name === "content";
114+
return ValidationTypes.isLiteral(part, "content()");
112115
},
113116

114117
"<relative-size>": function(part){
@@ -186,17 +189,15 @@ var ValidationTypes = {
186189
},
187190

188191
"<shape>": function(part){
189-
return part.type === "function" && (part.name === "rect" || part.name === "inset-rect");
192+
return ValidationTypes.isLiteral(part, "rect() | inset-rect()");
190193
},
191194

192195
"<basic-shape>": function(part){
193196
// inset() = inset( <shape-arg>{1,4} [round <border-radius>]? )
194197
// circle() = circle( [<shape-radius>]? [at <position>]? )
195198
// ellipse() = ellipse( [<shape-radius>{2}]? [at <position>]? )
196199
// polygon() = polygon( [<fill-rule>,]? [<shape-arg> <shape-arg>]# )
197-
return part.type === "function" && (
198-
part.name === "inset" || part.name === "circle" || part.name === "ellipse" || part.name === "polygon"
199-
);
200+
return ValidationTypes.isLiteral(part, "inset() | circle() | ellipse() | polygon()");
200201
},
201202

202203
"<shape-box>": function(part) {
@@ -240,18 +241,11 @@ var ValidationTypes = {
240241
},
241242

242243
"<filter-function>": function(part){
243-
return part.type === "function" && (
244-
part.name === 'blur' ||
245-
part.name === 'brightness' ||
246-
part.name === 'contrast' ||
247-
part.name === 'custom' || // Not actually in formal spec.
248-
part.name === 'drop-shadow' ||
249-
part.name === 'grayscale' ||
250-
part.name === 'hue-rotate' ||
251-
part.name === 'invert' ||
252-
part.name === 'opacity' ||
253-
part.name === 'saturate' ||
254-
part.name === 'sepia');
244+
// custom() isn't actually in the spec
245+
return ValidationTypes.isLiteral(
246+
part, "blur() | brightness() | contrast() | custom() | " +
247+
"drop-shadow() | grayscale() | hue-rotate() | invert() | " +
248+
"opacity() | saturate() | sepia()");
255249
}
256250
},
257251

0 commit comments

Comments
 (0)