Skip to content

Commit 1a9adc8

Browse files
committed
Update to the new spec for parsing numbers and number tokens.
1 parent d128984 commit 1a9adc8

File tree

1 file changed

+29
-38
lines changed

1 file changed

+29
-38
lines changed

parse-css.js

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -466,58 +466,47 @@ function tokenize(str) {
466466
};
467467

468468
var consumeANumber = function() {
469-
var repr = [];
470-
var type = "integer";
469+
let isInteger = true;
470+
let sign;
471+
let numberPart = "";
472+
let exponentPart = "";
471473
if(next() == 0x2b || next() == 0x2d) {
472474
consume();
473-
repr += String.fromCodePoint(code);
475+
sign = String.fromCodePoint(code);
476+
numberPart += sign;
474477
}
475478
while(digit(next())) {
476479
consume();
477-
repr += String.fromCodePoint(code);
480+
numberPart += String.fromCodePoint(code);
478481
}
479482
if(next(1) == 0x2e && digit(next(2))) {
480483
consume();
481-
repr += String.fromCodePoint(code);
482-
consume();
483-
repr += String.fromCodePoint(code);
484-
type = "number";
484+
numberPart += ".";
485485
while(digit(next())) {
486486
consume();
487-
repr += String.fromCodePoint(code);
487+
numberPart += String.fromCodePoint(code);
488488
}
489+
isInteger = false;
489490
}
490491
var c1 = next(1), c2 = next(2), c3 = next(3);
491-
if((c1 == 0x45 || c1 == 0x65) && digit(c2)) {
492-
consume();
493-
repr += String.fromCodePoint(code);
492+
const eDigit = (c1 == 0x45 || c1 == 0x65) && digit(c2);
493+
const eSignDigit = (c1 == 0x45 || c1 == 0x65) && (c2 == 0x2b || c2 == 0x2d) && digit(c3);
494+
if(eDigit || eSignDigit) {
494495
consume();
495-
repr += String.fromCodePoint(code);
496-
type = "number";
497-
while(digit(next())) {
496+
if(eSignDigit) {
498497
consume();
499-
repr += String.fromCodePoint(code);
498+
exponentPart += String.fromCodePoint(code);
500499
}
501-
} else if((c1 == 0x45 || c1 == 0x65) && (c2 == 0x2b || c2 == 0x2d) && digit(c3)) {
502-
consume();
503-
repr += String.fromCodePoint(code);
504-
consume();
505-
repr += String.fromCodePoint(code);
506-
consume();
507-
repr += String.fromCodePoint(code);
508-
type = "number";
509500
while(digit(next())) {
510501
consume();
511-
repr += String.fromCodePoint(code);
502+
exponentPart += String.fromCodePoint(code);
512503
}
504+
isInteger = false;
513505
}
514-
var value = convertAStringToANumber(repr);
515-
return {value:value, isInteger: type=="integer"};
516-
};
506+
let value = +numberPart;
507+
if(exponentPart) value = value * Math.pow(10, +exponentPart);
517508

518-
var convertAStringToANumber = function(string) {
519-
// CSS's number rules are identical to JS, afaik.
520-
return +string;
509+
return {value, isInteger, sign};
521510
};
522511

523512
var consumeTheRemnantsOfABadURL = function() {
@@ -756,17 +745,19 @@ class URLToken extends CSSParserToken {
756745
}
757746

758747
class NumberToken extends CSSParserToken {
759-
constructor(val, isInteger) {
748+
constructor(val, isInteger, sign=undefined) {
760749
super("NUMBER");
761750
this.value = val;
762751
this.isInteger = isInteger;
752+
this.sign = sign;
763753
}
764754
toString() {
765-
if(this.isInteger) return `INT(${this.value})`;
766-
return `NUMBER(${this.value})`;
755+
const name = this.isInteger ? "INT" : "NUMBER";
756+
const sign = this.sign == "+" : "+" : ""
757+
return `${name}(${this.sign}${this.value})`;
767758
}
768-
toJSON() { return {type:this.type, value:this.value, isInteger:this.isInteger}; }
769-
toSource() { return formatNumber(this.value); }
759+
toJSON() { return {type:this.type, value:this.value, isInteger:this.isInteger, sign:this.sign}; }
760+
toSource() { return formatNumber(this.value, this.sign); }
770761
}
771762

772763
class PercentageToken extends CSSParserToken {
@@ -841,9 +832,9 @@ function escapeString(string) {
841832
}).join("");
842833
}
843834

844-
function formatNumber(num) {
835+
function formatNumber(num, sign=undefined) {
845836
// TODO: Fix this to match CSS stringification behavior.
846-
return String(num);
837+
return (sign == "+" ? "+" : "") + String(num);
847838
}
848839

849840
// Exportation.

0 commit comments

Comments
 (0)