Skip to content

Commit aecba50

Browse files
committed
Merge pull request #33 from mattiacci/bg-position
Fix validation of background-position
2 parents 832c9cb + 8d9b1c5 commit aecba50

File tree

2 files changed

+57
-38
lines changed

2 files changed

+57
-38
lines changed

src/css/ValidationTypes.js

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var ValidationTypes = {
4040
},
4141

4242
/**
43-
* Determines if the next part(s) of the given expresion
43+
* Determines if the next part(s) of the given expression
4444
* are one of a group.
4545
*/
4646
isAnyOfGroup: function(expression, types) {
@@ -187,10 +187,16 @@ var ValidationTypes = {
187187
var types = this,
188188
result = false,
189189
numeric = "<percentage> | <length>",
190-
xDir = "left | center | right",
191-
yDir = "top | center | bottom",
192-
part,
193-
i, len;
190+
xDir = "left | right",
191+
yDir = "top | bottom",
192+
count = 0,
193+
hasNext = function() {
194+
return expression.hasNext() && expression.peek() != ",";
195+
};
196+
197+
while (expression.peek(count) && expression.peek(count) != ",") {
198+
count++;
199+
}
194200

195201
/*
196202
<position> = [
@@ -202,40 +208,48 @@ var ValidationTypes = {
202208
[ center | [ left | right ] [ <percentage> | <length> ]? ] &&
203209
[ center | [ top | bottom ] [ <percentage> | <length> ]? ]
204210
]
211+
*/
205212

206-
*/
207-
208-
if (ValidationTypes.isAny(expression, "top | bottom")) {
209-
result = true;
213+
if (count < 3) {
214+
if (ValidationTypes.isAny(expression, xDir + " | center | " + numeric)) {
215+
result = true;
216+
ValidationTypes.isAny(expression, yDir + " | center | " + numeric);
217+
} else if (ValidationTypes.isAny(expression, yDir)) {
218+
result = true;
219+
ValidationTypes.isAny(expression, xDir + " | center");
220+
}
210221
} else {
211-
212-
//must be two-part
213-
if (ValidationTypes.isAny(expression, numeric)){
214-
if (expression.hasNext()){
215-
result = ValidationTypes.isAny(expression, numeric + " | " + yDir);
216-
}
217-
} else if (ValidationTypes.isAny(expression, xDir)){
218-
if (expression.hasNext()){
219-
220-
//two- or three-part
221-
if (ValidationTypes.isAny(expression, yDir)){
222+
if (ValidationTypes.isAny(expression, xDir)) {
223+
if (ValidationTypes.isAny(expression, yDir)) {
224+
result = true;
225+
ValidationTypes.isAny(expression, numeric);
226+
} else if (ValidationTypes.isAny(expression, numeric)) {
227+
if (ValidationTypes.isAny(expression, yDir)) {
222228
result = true;
223-
224229
ValidationTypes.isAny(expression, numeric);
225-
226-
} else if (ValidationTypes.isAny(expression, numeric)){
227-
228-
//could also be two-part, so check the next part
229-
if (ValidationTypes.isAny(expression, yDir)){
230-
ValidationTypes.isAny(expression, numeric);
231-
}
232-
230+
} else if (ValidationTypes.isAny(expression, "center")) {
233231
result = true;
234232
}
235233
}
236-
}
237-
}
238-
234+
} else if (ValidationTypes.isAny(expression, yDir)) {
235+
if (ValidationTypes.isAny(expression, xDir)) {
236+
result = true;
237+
ValidationTypes.isAny(expression, numeric);
238+
} else if (ValidationTypes.isAny(expression, numeric)) {
239+
if (ValidationTypes.isAny(expression, xDir)) {
240+
result = true;
241+
ValidationTypes.isAny(expression, numeric);
242+
} else if (ValidationTypes.isAny(expression, "center")) {
243+
result = true;
244+
}
245+
}
246+
} else if (ValidationTypes.isAny(expression, "center")) {
247+
if (ValidationTypes.isAny(expression, xDir + " | " + yDir)) {
248+
result = true;
249+
ValidationTypes.isAny(expression, numeric);
250+
}
251+
}
252+
}
239253

240254
return result;
241255
},
@@ -339,4 +353,4 @@ var ValidationTypes = {
339353
return result;
340354
}
341355
}
342-
};
356+
};

tests/css/Validation.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,23 +153,28 @@
153153
"top",
154154
"bottom",
155155
"center",
156+
"100%",
156157
"left center",
158+
"bottom left",
157159
"left 10px",
158160
"center bottom",
159161
"10% top",
160162
"left 10px bottom",
161163
"right top 5%",
162-
"center 3em center 10%",
164+
"top 3em center",
165+
"center top 3em",
166+
"top 3em right 10%",
163167
"top, bottom",
164168
"left 10px, left 10px",
165169
"right top 5%, left 10px bottom"
166170
],
167171

168172
invalid: {
169-
"foo" : "Expected (<bg-position>) but found 'foo'.",
170-
"left center right" : "Expected end of value but found 'right'."
171-
172-
}
173+
"foo" : "Expected (<bg-position>) but found 'foo'.",
174+
"10% left" : "Expected end of value but found 'left'.",
175+
"left center right" : "Expected end of value but found 'center'.",
176+
"center 3em right 10%": "Expected end of value but found '3em'.",
177+
}
173178
}));
174179

175180
suite.add(new ValidationTestCase({

0 commit comments

Comments
 (0)