Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Fix minor TimingFunction validation issue #8910

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ define(function (require, exports, module) {
* If string is a number, then convert it.
*
* @param {string} str value parsed from page.
* @return { isNumber: boolean, value: number }
* @return { isNumber: boolean, value: ?number }
*/
function _convertToNumber(str) {
if (typeof (str) !== "string") {
return { isNumber: false };
if (typeof str !== "string") {
return { isNumber: false, value: null };
}

var val = parseFloat(str, 10),
isNum = (typeof (val) === "number") && !isNaN(val) &&
var val = parseFloat(+str, 10),
isNum = (typeof val === "number") && !isNaN(val) &&
(val !== Infinity) && (val !== -Infinity);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcelgerber This is an interesting case. Due to the regex used to parse the cubic-bezier string, the last value is "everything past the 3rd comma and past whitespace". So there could be a lot of weird stuff in here -- too much I think to try to parse with our own regex.

So, I looked at the code that currently traps the exception and it's using the unary operator to convert from string to number. I think we still need parseFloat to make sure it's a base 10 number, but this code seems to be simpler, solve the problem, and all unit tests still work:

        if (typeof str !== "string") {
            return { isNumber: false, value: null };
        }

        var val = parseFloat(+str, 10),
            isNum = (typeof val === "number") && !isNaN(val) &&
                    (val !== Infinity) && (val !== -Infinity);

What do you think of this solution?


return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ define(function (require, exports, module) {
it("should correct cubic-bezier function with 5 parameters", function () {
testInvalidBezier("cubic-bezier(0, 0, 1, 1, 1)", ["cubic-bezier(0, 0, 1, 1)", "0", "0", "1", "1"]);
});
it("should correct cubic-bezier function with trailing comma", function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay! Unit test!

testInvalidBezier("cubic-bezier(.42, 0, .58, .5,)", ["cubic-bezier(.42, 0, .58, .5)", ".42", "0", ".58", ".5"]);
});

// Real invalid cubic-beziers - they should NOT be corrected automatically
it("should not match cubic-bezier function with invalid whitespace", function () {
Expand Down