Skip to content

Add minimal support of CSS custom properties #250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2019
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
1 change: 1 addition & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"bitwise": true,
"curly": true,
"eqeqeq": true,
"eqnull": true,
"evil": true,
"forin": true,
"freeze": true,
Expand Down
29 changes: 21 additions & 8 deletions src/css/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1077,10 +1077,10 @@ Parser.prototype = function() {
* ;
*/

var tokenStream = this._tokenStream,
value = null,
hack = null,
tokenValue,
var tokenStream = this._tokenStream,
value = null,
hack = null,
propertyName = "",
token,
line,
col;
Expand All @@ -1094,18 +1094,31 @@ Parser.prototype = function() {
col = token.startCol;
}

// consume a single hyphen before finding the identifier, to support custom properties
if (tokenStream.peek() === Tokens.MINUS) {
tokenStream.get();
token = tokenStream.token();
propertyName = token.value;
line = token.startLine;
col = token.startCol;
}

if (tokenStream.match(Tokens.IDENT)) {
token = tokenStream.token();
tokenValue = token.value;
propertyName += token.value;

// check for underscore hack - no error if not allowed because it's valid CSS syntax
if (tokenValue.charAt(0) === "_" && this.options.underscoreHack) {
if (propertyName.charAt(0) === "_" && this.options.underscoreHack) {
hack = "_";
tokenValue = tokenValue.substring(1);
propertyName = propertyName.substring(1);
}

value = new PropertyName(tokenValue, hack, line || token.startLine, col || token.startCol);
value = new PropertyName(propertyName, hack, line || token.startLine, col || token.startCol);
this._readWhitespace();
} else if (tokenStream.peek() === Tokens.RBRACE) {
// Encountered when there are no more properties.
} else {
this._unexpectedToken(tokenStream.LT(1));
}

return value;
Expand Down
145 changes: 93 additions & 52 deletions src/css/TokenStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,31 @@ var h = /^[0-9a-fA-F]$/,


function isHexDigit(c) {
return c !== null && h.test(c);
return c != null && h.test(c);
}

function isDigit(c) {
return c !== null && /\d/.test(c);
return c != null && /\d/.test(c);
}

function isWhitespace(c) {
return c !== null && whitespace.test(c);
return c != null && whitespace.test(c);
}

function isNewLine(c) {
return c !== null && nl.test(c);
return c != null && nl.test(c);
}

function isNameStart(c) {
return c !== null && /[a-z_\u00A0-\uFFFF\\]/i.test(c);
return c != null && /[a-z_\u00A0-\uFFFF\\]/i.test(c);
}

function isNameChar(c) {
return c !== null && (isNameStart(c) || /[0-9\-\\]/.test(c));
return c != null && (isNameStart(c) || /[0-9\-\\]/.test(c));
}

function isIdentStart(c) {
return c !== null && (isNameStart(c) || /-\\/.test(c));
return c != null && (isNameStart(c) || /-\\/.test(c));
}

function mix(receiver, supplier) {
Expand All @@ -54,6 +54,16 @@ function mix(receiver, supplier) {
return receiver;
}

function wouldStartIdent(twoCodePoints) {
return typeof twoCodePoints === "string" &&
(twoCodePoints[0] === "-" && isNameStart(twoCodePoints[1]) || isNameStart(twoCodePoints[0]));
}

function wouldStartUnsignedNumber(twoCodePoints) {
return typeof twoCodePoints === "string" &&
(isDigit(twoCodePoints[0]) || (twoCodePoints[0] === "." && isDigit(twoCodePoints[1])));
}

//-----------------------------------------------------------------------------
// CSS Token Stream
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -89,7 +99,6 @@ TokenStream.prototype = mix(new TokenStreamBase(), {

c = reader.read();


while (c) {
switch (c) {

Expand Down Expand Up @@ -170,16 +179,31 @@ TokenStream.prototype = mix(new TokenStreamBase(), {
/*
* Potential tokens:
* - CDC
* - MINUS
* - NUMBER
* - DIMENSION
* - PERCENTAGE
* - IDENT
* - MINUS
*/
case "-":
if (reader.peek() === "-") { // could be closing HTML-style comment
if (wouldStartUnsignedNumber(reader.peekCount(2))) {
token = this.numberToken(c, startLine, startCol);
break;
} else if (reader.peekCount(2) === "->") {
token = this.htmlCommentEndToken(c, startLine, startCol);
} else if (isNameStart(reader.peek())) {
token = this.identOrFunctionToken(c, startLine, startCol);
} else {
token = this._getDefaultToken(c, startLine, startCol);
}
break;

/*
* Potential tokens:
* - NUMBER
* - PLUS
*/
case "+":
if (wouldStartUnsignedNumber(reader.peekCount(2))) {
token = this.numberToken(c, startLine, startCol);
} else {
token = this.charToken(c, startLine, startCol);
}
Expand Down Expand Up @@ -242,48 +266,13 @@ TokenStream.prototype = mix(new TokenStreamBase(), {
case "u":
if (reader.peek() === "+") {
token = this.unicodeRangeToken(c, startLine, startCol);
break;
}
/* falls through */
default:

/*
* Potential tokens:
* - NUMBER
* - DIMENSION
* - LENGTH
* - FREQ
* - TIME
* - EMS
* - EXS
* - ANGLE
*/
if (isDigit(c)) {
token = this.numberToken(c, startLine, startCol);
} else

/*
* Potential tokens:
* - S
*/
if (isWhitespace(c)) {
token = this.whitespaceToken(c, startLine, startCol);
} else

/*
* Potential tokens:
* - IDENT
*/
if (isIdentStart(c)) {
token = this.identOrFunctionToken(c, startLine, startCol);
} else {
/*
* Potential tokens:
* - CHAR
* - PLUS
*/
token = this.charToken(c, startLine, startCol);
token = this._getDefaultToken(c, startLine, startCol);
}
break;

default:
token = this._getDefaultToken(c, startLine, startCol);

}

Expand All @@ -299,6 +288,58 @@ TokenStream.prototype = mix(new TokenStreamBase(), {
return token;
},

/**
* Produces a token based on the given character and location in the
* stream, when no other case applies.
* Potential tokens:
* - NUMBER
* - DIMENSION
* - LENGTH
* - FREQ
* - TIME
* - EMS
* - EXS
* - ANGLE
* @param {String} c The character for the token.
* @param {int} startLine The beginning line for the character.
* @param {int} startCol The beginning column for the character.
* @return {Object} A token object.
* @method _getDefaultToken
*/
_getDefaultToken: function(c, startLine, startCol) {
var reader = this._reader,
token = null;

if (isDigit(c)) {
token = this.numberToken(c, startLine, startCol);
} else

/*
* Potential tokens:
* - S
*/
if (isWhitespace(c)) {
token = this.whitespaceToken(c, startLine, startCol);
} else

/*
* Potential tokens:
* - IDENT
*/
if (wouldStartIdent(c + reader.peekCount(1))) {
token = this.identOrFunctionToken(c, startLine, startCol);
} else {
/*
* Potential tokens:
* - CHAR
* - PLUS
*/
token = this.charToken(c, startLine, startCol);
}

return token;
},

//-------------------------------------------------------------------------
// Methods to create tokens
//-------------------------------------------------------------------------
Expand Down
12 changes: 12 additions & 0 deletions src/util/StringReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@ StringReader.prototype = {
// Advanced reading
//-------------------------------------------------------------------------

/**
* Reads a given number of characters without advancing the cursor.
* @param {int} count How many characters to look ahead (default is 1).
* @return {String} The characters as a string, or an empty string if
* there is no next character.
* @method peek
*/
peekCount: function(count) {
count = typeof count === "undefined" ? 1 : Math.max(count, 0);
return this._input.substring(this._cursor, this._cursor + count);
},

/**
* Reads up to and including the given string. Throws an error if that
* string is not found.
Expand Down
43 changes: 42 additions & 1 deletion tests/css/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2138,6 +2138,21 @@ var YUITest = require("yuitest"),
parser.parse(".foo {\n color: #fff;\n}");
},

"Test rule with custom property": function() {
var parser = new Parser({ strict: true });
parser.addListener("property", function(event) {
Assert.areEqual("--color-Foo_BAR", event.property.toString());
Assert.areEqual("#fff", event.value.toString());
Assert.areEqual(3, event.property.col, "Property column should be 3.");
Assert.areEqual(2, event.property.line, "Property line should be 2.");
Assert.areEqual(3, event.col, "Event column should be 3.");
Assert.areEqual(2, event.line, "Event line should be 2.");
Assert.areEqual(20, event.value.parts[0].col, "First part column should be 20.");
Assert.areEqual(2, event.value.parts[0].line, "First part line should be 2.");
});
parser.parse(".foo {\n --color-Foo_BAR: #fff;\n}");
},

"Test rule with star hack property": function() {
var parser = new Parser({
strict: true,
Expand Down Expand Up @@ -2289,7 +2304,33 @@ var YUITest = require("yuitest"),

name: "Invalid CSS Parsing Tests",

"Test parsing invalid celector": function() {
"Test parsing custom property typo": function() {
var error;
var parser = new Parser();
parser.addListener("error", function(e) {
error = e;
});
parser.parse("a:hover{\ncolor:red;\n==myFont:Helvetica;/*dropped*/;\nborder:0\n}");

Assert.areEqual("error", error.type);
Assert.areEqual(3, error.line);
Assert.areEqual(1, error.col);
},

"Test parsing invalid property": function() {
var error;
var parser = new Parser();
parser.addListener("error", function(e) {
error = e;
});
parser.parse("a:hover{\ncolor:red;\nfont::Helvetica;/*dropped*/;\nborder:0\n}");

Assert.areEqual("error", error.type);
Assert.areEqual(3, error.line);
Assert.areEqual(6, error.col);
},

"Test parsing invalid selector": function() {
var error;
var parser = new Parser();
parser.addListener("error", function(e) {
Expand Down
Loading