Skip to content

Commit ec3a026

Browse files
committed
Use "type" property for consistency with the spec
1 parent 14ab89c commit ec3a026

File tree

2 files changed

+413
-413
lines changed

2 files changed

+413
-413
lines changed

parse-css.js

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ function tokenize(str) {
156156
else if(code == 0x22) return consumeAStringToken();
157157
else if(code == 0x23) {
158158
if(namechar(next()) || areAValidEscape(next(1), next(2))) {
159-
const isIdent = wouldStartAnIdentifier(next(1), next(2), next(3));
160-
return new HashToken(consumeAName(), isIdent);
159+
const type = wouldStartAnIdentifier(next(1), next(2), next(3)) ? 'id' : 'unrestricted';
160+
return new HashToken(consumeAName(), type);
161161
} else {
162162
return new DelimToken(code);
163163
}
@@ -255,15 +255,15 @@ function tokenize(str) {
255255
};
256256

257257
var consumeANumericToken = function() {
258-
var {value, isInteger, sign} = consumeANumber();
258+
var {value, type, sign} = consumeANumber();
259259
if(wouldStartAnIdentifier(next(1), next(2), next(3))) {
260260
const unit = consumeAName();
261-
return new DimensionToken(value, isInteger, unit, sign);
261+
return new DimensionToken(value, type, unit, sign);
262262
} else if(next() == 0x25) {
263263
consume();
264264
return new PercentageToken(value, sign);
265265
} else {
266-
return new NumberToken(value, isInteger, sign);
266+
return new NumberToken(value, type, sign);
267267
}
268268
};
269269

@@ -428,7 +428,7 @@ function tokenize(str) {
428428
};
429429

430430
var consumeANumber = function() {
431-
let isInteger = true;
431+
let type = 'integer';
432432
let sign;
433433
let numberPart = "";
434434
let exponentPart = "";
@@ -448,7 +448,7 @@ function tokenize(str) {
448448
consume();
449449
numberPart += String.fromCodePoint(code);
450450
}
451-
isInteger = false;
451+
type = 'number';
452452
}
453453
var c1 = next(1), c2 = next(2), c3 = next(3);
454454
const eDigit = (c1 == 0x45 || c1 == 0x65) && digit(c2);
@@ -463,7 +463,7 @@ function tokenize(str) {
463463
consume();
464464
exponentPart += String.fromCodePoint(code);
465465
}
466-
isInteger = false;
466+
type = 'number';
467467
}
468468

469469
// parse with native engine to prevent a precision issue
@@ -472,7 +472,7 @@ function tokenize(str) {
472472
// let value = +numberPart;
473473
// if(exponentPart) value = value * Math.pow(10, +exponentPart);
474474

475-
return {value, isInteger, sign};
475+
return {value, type, sign};
476476
};
477477

478478
var consumeTheRemnantsOfABadURL = function() {
@@ -504,12 +504,12 @@ function tokenize(str) {
504504
}
505505

506506
class CSSParserToken {
507-
constructor(type) {
508-
this.type = type;
507+
constructor(TYPE) {
508+
this.TYPE = TYPE;
509509
}
510510

511-
toJSON() { return {type:this.type}; }
512-
toString() { return this.type; }
511+
toJSON() { return {TYPE:this.TYPE}; }
512+
toString() { return this.TYPE; }
513513
toSource() { throw new Error("Not implemented."); }
514514
}
515515
//toJSON()
@@ -639,7 +639,7 @@ class DelimToken extends CSSParserToken {
639639
this.value = val;
640640
}
641641
toString() { return `DELIM(${this.value})`; }
642-
toJSON() { return {type:this.type, value:this.value}; }
642+
toJSON() { return {TYPE:this.TYPE, value:this.value}; }
643643
toSource() {
644644
if(this.value == "\\") return "\\\n";
645645
return this.value;
@@ -652,7 +652,7 @@ class IdentToken extends CSSParserToken {
652652
this.value = val;
653653
}
654654
toString() { return `IDENT(${this.value})`; }
655-
toJSON() { return {type:this.type, value:this.value}; }
655+
toJSON() { return {TYPE:this.TYPE, value:this.value}; }
656656
toSource() { return escapeIdent(this.value); }
657657
}
658658

@@ -663,7 +663,7 @@ class FunctionToken extends CSSParserToken {
663663
this.mirror = CloseParenToken;
664664
}
665665
toString() { return `FUNCTION(${this.value})`; }
666-
toJSON() { return {type:this.type, value:this.value}; }
666+
toJSON() { return {TYPE:this.TYPE, value:this.value}; }
667667
toSource() { return escapeIdent(this.value) + "("; }
668668
}
669669

@@ -673,20 +673,20 @@ class AtKeywordToken extends CSSParserToken {
673673
this.value = val;
674674
}
675675
toString() { return `AT(${this.value})`; }
676-
toJSON() { return {type:this.type, value:this.value }; }
676+
toJSON() { return {TYPE:this.TYPE, value:this.value }; }
677677
toSource() { return "@" + escapeIdent(this.value); }
678678
}
679679

680680
class HashToken extends CSSParserToken {
681-
constructor(val, isIdent) {
681+
constructor(val, type) {
682682
super("HASH");
683683
this.value = val;
684-
this.isIdent = isIdent;
684+
this.type = type;
685685
}
686686
toString() { return `HASH(${this.value})`; }
687-
toJSON() { return {type:this.type, value:this.value, isIdent:this.isIdent}; }
687+
toJSON() { return {TYPE:this.TYPE, value:this.value, type:this.type}; }
688688
toSource() {
689-
if(this.isIdent) {
689+
if(this.type === 'id') {
690690
return "#" + escapeIdent(this.value);
691691
}
692692
return "#" + escapeHash(this.value);
@@ -699,7 +699,7 @@ class StringToken extends CSSParserToken {
699699
this.value = val;
700700
}
701701
toString() { return `STRING(${this.value})`; }
702-
toJSON() { return {type:this.type, value:this.value}; }
702+
toJSON() { return {TYPE:this.TYPE, value:this.value}; }
703703
toSource() { return `"${escapeString(this.value)}"`; }
704704
}
705705

@@ -709,23 +709,23 @@ class URLToken extends CSSParserToken {
709709
this.value = val;
710710
}
711711
toString() { return `URL(${this.value})`; }
712-
toJSON() { return {type:this.type, value:this.value}; }
712+
toJSON() { return {TYPE:this.TYPE, value:this.value}; }
713713
toSource() { return `url("${escapeString(this.value)}")`; }
714714
}
715715

716716
class NumberToken extends CSSParserToken {
717-
constructor(val, isInteger, sign=undefined) {
717+
constructor(val, type, sign=undefined) {
718718
super("NUMBER");
719719
this.value = val;
720-
this.isInteger = isInteger;
720+
this.type = type;
721721
this.sign = sign;
722722
}
723723
toString() {
724-
const name = this.isInteger ? "INT" : "NUMBER";
724+
const name = this.type === 'integer' ? "INT" : "NUMBER";
725725
const sign = this.sign == "+" ? "+" : "";
726726
return `${name}(${sign}${this.value})`;
727727
}
728-
toJSON() { return {type:this.type, value:this.value, isInteger:this.isInteger, sign:this.sign}; }
728+
toJSON() { return {TYPE:this.TYPE, value:this.value, type:this.type, sign:this.sign}; }
729729
toSource() { return formatNumber(this.value, this.sign); }
730730
}
731731

@@ -739,23 +739,23 @@ class PercentageToken extends CSSParserToken {
739739
const sign = this.sign == "+" ? "+" : "";
740740
return `PERCENTAGE(${sign}${this.value})`;
741741
}
742-
toJSON() { return {type:this.type, value:this.value, sign:this.sign}; }
742+
toJSON() { return {TYPE:this.TYPE, value:this.value, sign:this.sign}; }
743743
toSource() { return `${formatNumber(this.value, this.sign)}%`; }
744744
}
745745

746746
class DimensionToken extends CSSParserToken {
747-
constructor(val, isInteger, unit, sign=undefined) {
747+
constructor(val, type, unit, sign=undefined) {
748748
super("DIMENSION");
749749
this.value = val;
750-
this.isInteger = isInteger;
750+
this.type = type;
751751
this.unit = unit;
752752
this.sign = sign;
753753
}
754754
toString() {
755755
const sign = this.sign == "+" ? "+" : "";
756756
return `DIM(${sign}${this.value}, ${this.unit})`;
757757
}
758-
toJSON() { return {type:this.type, value:this.value, isInteger:this.isInteger, unit:this.unit, sign:this.sign}; }
758+
toJSON() { return {TYPE:this.TYPE, value:this.value, type:this.type, unit:this.unit, sign:this.sign}; }
759759
toSource() {
760760
let unit = escapeIdent(this.unit);
761761
if(unit[0].toLowerCase() == "e" && (unit[1] == "-" || digit(unit[1].charCodeAt(0)))) {
@@ -1142,24 +1142,24 @@ function isValidInContext(construct, context) {
11421142
// Trivial validator, without any special CSS knowledge.
11431143

11441144
// All at-rules are valid, who cares.
1145-
if(construct.type == "AT-RULE") return true;
1145+
if(construct.TYPE == "AT-RULE") return true;
11461146

11471147
// Exclude qualified rules that ended up with a semicolon
11481148
// in their prelude.
11491149
// (Can only happen at the top level of a stylesheet.)
1150-
if(construct.type == "QUALIFIED-RULE") {
1150+
if(construct.TYPE == "QUALIFIED-RULE") {
11511151
for(const val of construct.prelude) {
1152-
if(val.type == "SEMICOLON") return false;
1152+
if(val.TYPE == "SEMICOLON") return false;
11531153
}
11541154
return true;
11551155
}
11561156

11571157
// Exclude properties that ended up with a {}-block
11581158
// in their value, unless they're custom.
1159-
if(construct.type == "DECLARATION") {
1159+
if(construct.TYPE == "DECLARATION") {
11601160
if(construct.name.slice(0, 2) == "--") return true;
11611161
for(const val of construct.value) {
1162-
if(val.type == "BLOCK" && val.name == "{") return false;
1162+
if(val.TYPE == "BLOCK" && val.name == "{") return false;
11631163
}
11641164
return true;
11651165
}
@@ -1248,7 +1248,7 @@ function parseACommaSeparatedListOfComponentValues(s) {
12481248

12491249

12501250
class CSSParserRule {
1251-
constructor(type) { this.type = type; }
1251+
constructor(TYPE) { this.TYPE = TYPE; }
12521252
toString(indent) {
12531253
return JSON.stringify(this,null,indent);
12541254
}
@@ -1262,7 +1262,7 @@ class Stylesheet extends CSSParserRule {
12621262
}
12631263
toJSON() {
12641264
return {
1265-
type: this.type,
1265+
TYPE: this.TYPE,
12661266
rules: this.rules,
12671267
}
12681268
}
@@ -1282,7 +1282,7 @@ class AtRule extends CSSParserRule {
12821282
}
12831283
toJSON() {
12841284
return {
1285-
type: this.type,
1285+
TYPE: this.TYPE,
12861286
name: this.name,
12871287
prelude: this.prelude,
12881288
declarations: this.declarations,
@@ -1318,7 +1318,7 @@ class QualifiedRule extends CSSParserRule {
13181318
}
13191319
toJSON() {
13201320
return {
1321-
type: this.type,
1321+
TYPE: this.TYPE,
13221322
prelude: this.prelude,
13231323
declarations: this.declarations,
13241324
rules: this.rules,
@@ -1349,7 +1349,7 @@ class Declaration extends CSSParserRule {
13491349
}
13501350
toJSON() {
13511351
return {
1352-
type: this.type,
1352+
TYPE: this.TYPE,
13531353
name: this.name,
13541354
value: this.value,
13551355
important: this.important,
@@ -1367,15 +1367,15 @@ class Declaration extends CSSParserRule {
13671367
}
13681368

13691369
class SimpleBlock extends CSSParserRule {
1370-
constructor(type) {
1370+
constructor(TYPE) {
13711371
super("BLOCK");
1372-
this.name = type;
1372+
this.name = TYPE;
13731373
this.value = [];
13741374
return this;
13751375
}
13761376
toJSON() {
13771377
return {
1378-
type: this.type,
1378+
TYPE: this.TYPE,
13791379
name: this.name,
13801380
value: this.value,
13811381
}
@@ -1395,7 +1395,7 @@ class Func extends CSSParserRule {
13951395
}
13961396
toJSON() {
13971397
return {
1398-
type: this.type,
1398+
TYPE: this.TYPE,
13991399
name: this.name,
14001400
value: this.value,
14011401
}

0 commit comments

Comments
 (0)