Skip to content

Commit 9f746e2

Browse files
committed
Begin adding toSource methods. WIP.
1 parent 6882036 commit 9f746e2

File tree

1 file changed

+65
-1
lines changed

1 file changed

+65
-1
lines changed

parse-css.js

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,7 @@ CSSParserToken.prototype.toJSON = function() {
539539
return {token: this.tokenType};
540540
}
541541
CSSParserToken.prototype.toString = function() { return this.tokenType; }
542+
CSSParserToken.prototype.toSource = function() { return ''+this; }
542543

543544
function BadStringToken() { return this; }
544545
BadStringToken.prototype = Object.create(CSSParserToken.prototype);
@@ -552,14 +553,17 @@ function WhitespaceToken() { return this; }
552553
WhitespaceToken.prototype = Object.create(CSSParserToken.prototype);
553554
WhitespaceToken.prototype.tokenType = "WHITESPACE";
554555
WhitespaceToken.prototype.toString = function() { return "WS"; }
556+
WhitespaceToken.prototype.toSource = function() { return " "; }
555557

556558
function CDOToken() { return this; }
557559
CDOToken.prototype = Object.create(CSSParserToken.prototype);
558560
CDOToken.prototype.tokenType = "CDO";
561+
CDOToken.prototype.toSource = function() { return "<!--"; }
559562

560563
function CDCToken() { return this; }
561564
CDCToken.prototype = Object.create(CSSParserToken.prototype);
562565
CDCToken.prototype.tokenType = "CDC";
566+
CDCToken.prototype.toSource = function() { return "-->"; }
563567

564568
function ColonToken() { return this; }
565569
ColonToken.prototype = Object.create(CSSParserToken.prototype);
@@ -627,6 +631,7 @@ ColumnToken.prototype.tokenType = "||";
627631
function EOFToken() { return this; }
628632
EOFToken.prototype = Object.create(CSSParserToken.prototype);
629633
EOFToken.prototype.tokenType = "EOF";
634+
EOFToken.prototype.toSource = function() { return ""; }
630635

631636
function DelimToken(code) {
632637
this.value = stringFromCode(code);
@@ -640,6 +645,12 @@ DelimToken.prototype.toJSON = function() {
640645
json.value = this.value;
641646
return json;
642647
}
648+
DelimToken.prototype.toSource = function() {
649+
if(this.value == "\\")
650+
return "\\\n";
651+
else
652+
return this.value;
653+
}
643654

644655
function StringValuedToken() { throw "Abstract Base Class"; }
645656
StringValuedToken.prototype = Object.create(CSSParserToken.prototype);
@@ -658,6 +669,9 @@ function IdentToken(val) {
658669
IdentToken.prototype = Object.create(StringValuedToken.prototype);
659670
IdentToken.prototype.tokenType = "IDENT";
660671
IdentToken.prototype.toString = function() { return "IDENT("+this.value+")"; }
672+
IdentToken.prototype.toSource = function() {
673+
return escapeIdent(this.value);
674+
}
661675

662676
function FunctionToken(val) {
663677
this.value = val;
@@ -666,13 +680,19 @@ function FunctionToken(val) {
666680
FunctionToken.prototype = Object.create(StringValuedToken.prototype);
667681
FunctionToken.prototype.tokenType = "FUNCTION";
668682
FunctionToken.prototype.toString = function() { return "FUNCTION("+this.value+")"; }
683+
FunctionToken.prototype.toSource = function() {
684+
return escapeIdent(this.value) + "(";
685+
}
669686

670687
function AtKeywordToken(val) {
671688
this.value = val;
672689
}
673690
AtKeywordToken.prototype = Object.create(StringValuedToken.prototype);
674691
AtKeywordToken.prototype.tokenType = "AT-KEYWORD";
675692
AtKeywordToken.prototype.toString = function() { return "AT("+this.value+")"; }
693+
AtKeywordToken.prototype.toSource = function() {
694+
return "@" + escapeIdent(this.value);
695+
}
676696

677697
function HashToken(val) {
678698
this.value = val;
@@ -687,20 +707,32 @@ HashToken.prototype.toJSON = function() {
687707
json.type = this.type;
688708
return json;
689709
}
710+
HashToken.prototype.toSource = function() {
711+
if(this.type == "id") {
712+
return "#" + escapeIdent(this.value);
713+
} else {
714+
return "#" + escapeHash(this.value);
715+
}
716+
}
690717

691718
function StringToken(val) {
692719
this.value = val;
693720
}
694721
StringToken.prototype = Object.create(StringValuedToken.prototype);
695722
StringToken.prototype.tokenType = "STRING";
696-
StringToken.prototype.toString = function() { return "\""+this.value+"\""; }
723+
StringToken.prototype.toString = function() {
724+
return '"' + escapeString(this.value) + '"';
725+
}
697726

698727
function URLToken(val) {
699728
this.value = val;
700729
}
701730
URLToken.prototype = Object.create(StringValuedToken.prototype);
702731
URLToken.prototype.tokenType = "URL";
703732
URLToken.prototype.toString = function() { return "URL("+this.value+")"; }
733+
URLToken.prototype.toSource = function() {
734+
return "url(" + escapeURL(this.value) + ")";
735+
}
704736

705737
function NumberToken() {
706738
this.value = null;
@@ -721,6 +753,7 @@ NumberToken.prototype.toJSON = function() {
721753
json.repr = this.repr;
722754
return json;
723755
}
756+
NumberToken.prototype.toSource = function() { return this.repr; };
724757

725758
function PercentageToken() {
726759
this.value = null;
@@ -735,6 +768,7 @@ PercentageToken.prototype.toJSON = function() {
735768
json.repr = this.repr;
736769
return json;
737770
}
771+
PercentageToken.prototype.toSource = function() { return this.repr + "%"; }
738772

739773
function DimensionToken() {
740774
this.value = null;
@@ -753,6 +787,36 @@ DimensionToken.prototype.toJSON = function() {
753787
json.unit = this.unit;
754788
return json;
755789
}
790+
DimensionToken.prototype.toSource = function() {
791+
var source = this.repr;
792+
var unit = escapeIdent(this.unit);
793+
if(unit[0].toLowerCase() == "e" && (unit[1] == "-" || between(unit.charCodeAt(1), 0x30, 0x39))) {
794+
// Unit is ambiguous with scinot
795+
// Remove the leading "e", replace with escape.
796+
unit = "\\65 " + unit.slice(1, unit.length);
797+
}
798+
return source+unit;
799+
}
800+
801+
function escapeIdent(string) {
802+
// TODO
803+
return string;
804+
}
805+
806+
function escapeHash(string) {
807+
// TODO
808+
return string;
809+
}
810+
811+
function escapeString(string) {
812+
// TODO
813+
return string;
814+
}
815+
816+
function escapeURL(string) {
817+
// TODO
818+
return string;
819+
}
756820

757821
// Exportation.
758822
exports.tokenize = tokenize;

0 commit comments

Comments
 (0)