Skip to content

Commit

Permalink
allow any pseudo-element to be a functional-pseudo (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Feb 2, 2017
1 parent 9c894f2 commit 49f6d4a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 14 deletions.
3 changes: 2 additions & 1 deletion lib/parser/index.js
Expand Up @@ -109,7 +109,8 @@ Parser.prototype = {
'nth-child': sequence.nthWithOfClause,
'nth-last-child': sequence.nthWithOfClause,
'nth-of-type': sequence.nth,
'nth-last-of-type': sequence.nth
'nth-last-of-type': sequence.nth,
'slotted': sequence.compoundSelectorList
},
context: {
stylesheet: getStyleSheet,
Expand Down
8 changes: 8 additions & 0 deletions lib/parser/sequence.js
Expand Up @@ -23,6 +23,7 @@ var RELATIVE = true;
var ALLOW_OF_CLAUSE = true;
var DISALLOW_OF_CLAUSE = false;
var DISALLOW_VAR = false;
var DISALLOW_COMBINATORS = true;

function singleIdentifier() {
return new List().appendData(
Expand All @@ -42,6 +43,12 @@ function relativeSelectorList() {
);
}

function compoundSelectorList() {
return new List().appendData(
this.Selector(ABSOLUTE, DISALLOW_COMBINATORS)
);
}

function nth() {
return new List().appendData(
this.Nth(DISALLOW_OF_CLAUSE)
Expand Down Expand Up @@ -171,6 +178,7 @@ module.exports = {
singleIdentifier: singleIdentifier,
selectorList: selectorList,
relativeSelectorList: relativeSelectorList,
compoundSelectorList: compoundSelectorList,
nth: nth,
nthWithOfClause: nthWithOfClause,
default: defaultSequence
Expand Down
7 changes: 4 additions & 3 deletions lib/parser/type/PseudoClass.js
Expand Up @@ -3,17 +3,18 @@ var TYPE = require('../../scanner').TYPE;

var COLON = TYPE.Colon;
var LEFTPARENTHESIS = TYPE.LeftParenthesis;
var RIGHTPARENTESIS = TYPE.RightParenthesis;
var RIGHTPARENTHESIS = TYPE.RightParenthesis;
var DISALLOW_VAR = false;
var BALANCED = true;

// : ( ident | function )
// : ident [ '(' .. ')' ]?
module.exports = function PseudoClass() {
var start = this.scanner.tokenStart;
var name;
var children = null;

this.scanner.eat(COLON);

name = this.readIdent(DISALLOW_VAR);

if (this.scanner.tokenType === LEFTPARENTHESIS) {
Expand All @@ -29,7 +30,7 @@ module.exports = function PseudoClass() {
children = new List().appendData(this.Raw(BALANCED, 0, 0));
}

this.scanner.eat(RIGHTPARENTESIS);
this.scanner.eat(RIGHTPARENTHESIS);
}

return {
Expand Down
28 changes: 18 additions & 10 deletions lib/parser/type/PseudoElement.js
Expand Up @@ -4,10 +4,10 @@ var TYPE = require('../../scanner').TYPE;
var COLON = TYPE.Colon;
var LEFTPARENTHESIS = TYPE.LeftParenthesis;
var RIGHTPARENTHESIS = TYPE.RightParenthesis;
var ABSOLUTE = false;
var DISALLOW_COMBINATORS = true;
var DISALLOW_VAR = false;
var BALANCED = true;

// :: ident
// :: ident [ '(' .. ')' ]?
module.exports = function PseudoElement() {
var start = this.scanner.tokenStart;
var name;
Expand All @@ -16,14 +16,22 @@ module.exports = function PseudoElement() {
this.scanner.eat(COLON);
this.scanner.eat(COLON);

// https://drafts.csswg.org/css-scoping/#slotted-pseudo
if (this.scanner.lookupValue(0, 'slotted')) {
name = this.readIdent(false);
this.scanner.eat(LEFTPARENTHESIS);
children = new List().appendData(this.Selector(ABSOLUTE, DISALLOW_COMBINATORS));
name = this.readIdent(DISALLOW_VAR);

if (this.scanner.tokenType === LEFTPARENTHESIS) {
var nameLowerCase = name.toLowerCase();

this.scanner.next();

if (this.pseudo.hasOwnProperty(nameLowerCase)) {
this.readSC();
children = this.pseudo[nameLowerCase].call(this);
this.readSC();
} else {
children = new List().appendData(this.Raw(BALANCED, 0, 0));
}

this.scanner.eat(RIGHTPARENTHESIS);
} else {
name = this.readIdent(false);
}

return {
Expand Down
13 changes: 13 additions & 0 deletions test/fixture/parse/selector/PseudoElement.json
Expand Up @@ -22,5 +22,18 @@
"name": "before",
"children": null
}
},
"functional pseudo element": {
"source": "::test(1 + 2)",
"ast": {
"type": "PseudoElement",
"name": "test",
"children": [
{
"type": "Raw",
"value": "1 + 2"
}
]
}
}
}

0 comments on commit 49f6d4a

Please sign in to comment.