Bug 1275546 - Use CSS lexer to parse angle values in css-angle.js . r=nchevobbe

MozReview-Commit-ID: DVqlbrruf2b
This commit is contained in:
Sebastin Santy 2016-07-08 06:29:00 +02:00
parent dbf7715579
commit ed4558f67f
3 changed files with 57 additions and 2 deletions

View File

@ -10,6 +10,8 @@ const SPECIALVALUES = new Set([
"unset"
]);
const {getCSSLexer} = require("devtools/shared/css-lexer");
/**
* This module is used to convert between various angle units.
*
@ -66,7 +68,12 @@ CssAngle.prototype = {
},
get valid() {
return /^-?\d+\.?\d*(deg|rad|grad|turn)$/gi.test(this.authored);
let token = getCSSLexer(this.authored).nextToken();
if (!token) {
return false;
}
return (token.tokenType === "dimension"
&& token.text.toLowerCase() in CssAngle.ANGLEUNIT);
},
get specialValue() {

View File

@ -156,7 +156,7 @@ OutputParser.prototype = {
};
let angleOK = function (angle) {
return /^-?\d+\.?\d*(deg|rad|grad|turn)$/gi.test(angle);
return (new angleUtils.CssAngle(angle)).valid;
};
while (true) {

View File

@ -13,6 +13,7 @@ add_task(function* () {
info("Starting the test");
testAngleUtils();
testAngleValidity();
host.destroy();
gBrowser.removeCurrentTab();
@ -35,6 +36,16 @@ function testAngleUtils() {
}
}
function testAngleValidity() {
let data = getAngleValidityData();
for (let {angle, result} of data) {
let testAngle = new angleUtils.CssAngle(angle);
is(testAngle.valid, result, `Testing that "${angle}" is ${testAngle.valid ? " a valid" : "an invalid" } angle`);
}
}
function testToString(angle, deg, rad, grad, turn) {
angle.angleUnit = angleUtils.CssAngle.ANGLEUNIT.deg;
is(angle.toString(), deg, "toString() with deg type");
@ -49,6 +60,43 @@ function testToString(angle, deg, rad, grad, turn) {
is(angle.toString(), turn, "toString() with turn type");
}
function getAngleValidityData() {
return [{
angle: "0.2turn",
result: true
}, {
angle: "-0.2turn",
result: true
}, {
angle: "-.2turn",
result: true
}, {
angle: "1e02turn",
result: true
}, {
angle: "-2e2turn",
result: true
}, {
angle: ".2turn",
result: true
}, {
angle: "0.2aaturn",
result: false
}, {
angle: "2dega",
result: false
}, {
angle: "0.deg",
result: false
}, {
angle: ".deg",
result: false
}, {
angle: "..2turn",
result: false
}];
}
function getTestData() {
return [{
authored: "0deg",