postcss-selector-parser
Advanced tools
Comparing version
@@ -22,2 +22,8 @@ 'use strict'; | ||
t.equal(tree.selectors[0].rules[1].type, 'class'); | ||
}); | ||
(0, _utilHelpers.test)('escaped numbers in class name', '.\\31\\ 0', function (t, tree, d) { | ||
t.plan(2); | ||
t.equal(tree.selectors[0].rules[0].type, 'class'); | ||
t.equal(tree.selectors[0].rules[0].value, '\\31\\ 0'); | ||
}); |
@@ -12,2 +12,9 @@ 'use strict'; | ||
(0, _utilHelpers.test)('column combinator (2)', '.selected || td', function (t, tree) { | ||
t.plan(3); | ||
t.equal(tree.selectors[0].rules[0].value, 'selected'); | ||
t.equal(tree.selectors[0].rules[0].combinator, ' || '); | ||
t.equal(tree.selectors[0].rules[0].rules[0].value, 'td'); | ||
}); | ||
(0, _utilHelpers.test)('descendant combinator', 'h1 h2', function (t, tree) { | ||
@@ -20,9 +27,2 @@ t.plan(3); | ||
(0, _utilHelpers.test)('column combinator', '.selected||td', function (t, tree) { | ||
t.plan(3); | ||
t.equal(tree.selectors[0].rules[0].value, 'selected'); | ||
t.equal(tree.selectors[0].rules[0].combinator, '||'); | ||
t.equal(tree.selectors[0].rules[0].rules[0].value, 'td'); | ||
}); | ||
(0, _utilHelpers.test)('multiple descendant combinators', 'h1 h2 h3 h4', function (t, tree) { | ||
@@ -29,0 +29,0 @@ t.plan(4); |
@@ -21,8 +21,13 @@ 'use strict'; | ||
var parse = function parse(input, transform) { | ||
return (0, _index2['default'])(transform).process(input).result; | ||
}; | ||
exports.parse = parse; | ||
var test = function test(spec, input, callback) { | ||
var tree; | ||
var result = (0, _index2['default'])(function (selectors) { | ||
var result = parse(input, function (selectors) { | ||
tree = selectors; | ||
}).process(input).result; | ||
}); | ||
@@ -29,0 +34,0 @@ if (callback) { |
@@ -90,4 +90,5 @@ 'use strict'; | ||
var attribute = ''; | ||
var attr = undefined; | ||
this.position++; | ||
while (this.position < this.tokens.length && this.tokens[this.position][0] !== ']') { | ||
while (this.position < this.tokens.length && this.currToken[0] !== ']') { | ||
attribute += this.tokens[this.position][1]; | ||
@@ -100,7 +101,21 @@ this.position++; | ||
var parts = attribute.split(/((?:[*~^$|]?)=)/); | ||
var attr = new _selectorsAttribute2['default']({ | ||
attribute: parts[0], | ||
operator: parts[1], | ||
value: parts[2] | ||
}); | ||
var namespace = parts[0].split(/(\|)/g); | ||
if (namespace.length > 1) { | ||
if (namespace[0] === '') { | ||
namespace[0] = true; | ||
} | ||
attr = new _selectorsAttribute2['default']({ | ||
attribute: namespace[2], | ||
namespace: namespace[0], | ||
operator: parts[1], | ||
value: parts[2] | ||
}); | ||
} else { | ||
attr = new _selectorsAttribute2['default']({ | ||
attribute: parts[0], | ||
operator: parts[1], | ||
value: parts[2] | ||
}); | ||
} | ||
if (parts[2]) { | ||
@@ -120,8 +135,10 @@ var insensitive = parts[2].split(/(\s+i\s*?)$/); | ||
value: function combinator() { | ||
if (this.currToken[1] === '|') { | ||
return this.namespace(); | ||
} | ||
var combinator = ''; | ||
var tokens = this.tokens; | ||
while (this.position < tokens.length && tokens[this.position][0] === 'space' || tokens[this.position][0] === 'combinator') { | ||
combinator += this.tokens[this.position][1]; | ||
while (this.position < this.tokens.length && this.currToken[0] === 'space' || this.currToken[0] === 'combinator') { | ||
combinator += this.currToken[1]; | ||
this.position++; | ||
if (this.position === tokens.length) { | ||
if (this.position === this.tokens.length) { | ||
this.error('Unexpected right hand side combinator.'); | ||
@@ -156,3 +173,3 @@ } | ||
value: function comment() { | ||
var comment = new _selectorsComment2['default']({ value: this.tokens[this.position][1] }); | ||
var comment = new _selectorsComment2['default']({ value: this.currToken[1] }); | ||
this.current.append(comment); | ||
@@ -167,14 +184,26 @@ this.position++; | ||
}, { | ||
key: 'namespace', | ||
value: function namespace() { | ||
var before = this.prevToken && this.prevToken[1] || true; | ||
if (this.nextToken[0] === 'word') { | ||
this.position++; | ||
return this.word(before); | ||
} else if (this.nextToken[0] === '*') { | ||
this.position++; | ||
return this.universal(before); | ||
} | ||
} | ||
}, { | ||
key: 'pseudo', | ||
value: function pseudo() { | ||
var pseudoStr = ''; | ||
while (this.tokens[this.position][0] === ':') { | ||
pseudoStr += this.tokens[this.position][1]; | ||
while (this.currToken[0] === ':') { | ||
pseudoStr += this.currToken[1]; | ||
this.position++; | ||
} | ||
if (this.tokens[this.position][0] === 'word') { | ||
pseudoStr += this.tokens[this.position][1]; | ||
if (this.currToken[0] === 'word') { | ||
pseudoStr += this.currToken[1]; | ||
this.position++; | ||
var pseudo = new _selectorsPseudo2['default']({ value: pseudoStr }); | ||
if (this.tokens[this.position] && this.tokens[this.position][0] === '(') { | ||
if (this.currToken && this.currToken[0] === '(') { | ||
var balanced = 1; | ||
@@ -184,6 +213,6 @@ var inside = []; | ||
while (this.position < this.tokens.length && balanced) { | ||
if (this.tokens[this.position][0] === '(') balanced++; | ||
if (this.tokens[this.position][0] === ')') balanced--; | ||
if (this.currToken[0] === '(') balanced++; | ||
if (this.currToken[0] === ')') balanced--; | ||
if (balanced) { | ||
inside.push(this.tokens[this.position]); | ||
inside.push(this.currToken); | ||
} | ||
@@ -204,3 +233,3 @@ this.position++; | ||
while (this.position < this.tokens.length) { | ||
switch (this.tokens[this.position][0]) { | ||
switch (this.currToken[0]) { | ||
case 'space': | ||
@@ -236,3 +265,3 @@ this.space(); | ||
} else { | ||
this.error('Unexpected "' + this.tokens[this.position][0] + '" found.'); | ||
this.error('Unexpected "' + this.currToken[0] + '" found.'); | ||
} | ||
@@ -243,5 +272,5 @@ } | ||
value: function space() { | ||
var token = this.tokens[this.position]; | ||
var token = this.currToken; | ||
// Handle space before and after the selector | ||
if (this.position === 0 || this.tokens[this.position - 1][0] === ',') { | ||
if (this.position === 0 || this.prevToken[0] === ',') { | ||
this.current.spaces.before = token[1]; | ||
@@ -258,4 +287,13 @@ this.position++; | ||
key: 'universal', | ||
value: function universal() { | ||
this.current.append(new _selectorsUniversal2['default']({ value: this.tokens[this.position][1] })); | ||
value: function universal(namespace) { | ||
var nextToken = this.nextToken; | ||
if (nextToken && nextToken[1] === '|') { | ||
this.position++; | ||
return this.namespace(); | ||
} | ||
var universal = new _selectorsUniversal2['default']({ value: this.currToken[1] }); | ||
if (namespace) { | ||
universal.namespace = namespace; | ||
} | ||
this.current.append(universal); | ||
this.position++; | ||
@@ -265,6 +303,24 @@ } | ||
key: 'word', | ||
value: function word() { | ||
value: function word(namespace) { | ||
var _this = this; | ||
var word = this.tokens[this.position][1]; | ||
var nextToken = this.nextToken; | ||
if (nextToken && nextToken[1] === '|') { | ||
this.position++; | ||
return this.namespace(); | ||
} | ||
var word = this.currToken[1]; | ||
while (nextToken && nextToken[0] === 'word') { | ||
this.position++; | ||
var current = this.currToken[1]; | ||
word += current; | ||
if (current.lastIndexOf('\\') === current.length - 1) { | ||
var next = this.nextToken; | ||
if (next[0] === 'space') { | ||
word += next[1]; | ||
this.position++; | ||
} | ||
} | ||
nextToken = this.nextToken; | ||
} | ||
var hasClass = (0, _indexesOf2['default'])(word, '.'); | ||
@@ -279,9 +335,15 @@ var hasId = (0, _indexesOf2['default'])(word, '#'); | ||
var value = word.slice(ind, index); | ||
var node = undefined; | ||
if (~hasClass.indexOf(ind)) { | ||
_this.current.append(new _selectorsClassName2['default']({ value: value.slice(1) })); | ||
node = new _selectorsClassName2['default']({ value: value.slice(1) }); | ||
} else if (~hasId.indexOf(ind)) { | ||
_this.current.append(new _selectorsId2['default']({ value: value.slice(1) })); | ||
node = new _selectorsId2['default']({ value: value.slice(1) }); | ||
} else { | ||
_this.current.append(new _selectorsTag2['default']({ value: value })); | ||
node = new _selectorsTag2['default']({ value: value }); | ||
} | ||
if (namespace) { | ||
node.namespace = namespace; | ||
} | ||
_this.current.append(node); | ||
}); | ||
@@ -294,3 +356,3 @@ this.position++; | ||
while (this.position < this.tokens.length) { | ||
switch (this.tokens[this.position][0]) { | ||
switch (this.currToken[0]) { | ||
case 'space': | ||
@@ -324,2 +386,22 @@ this.space(); | ||
} | ||
}, { | ||
key: 'currToken', | ||
/** | ||
* Helpers | ||
*/ | ||
get: function () { | ||
return this.tokens[this.position]; | ||
} | ||
}, { | ||
key: 'nextToken', | ||
get: function () { | ||
return this.tokens[this.position + 1]; | ||
} | ||
}, { | ||
key: 'prevToken', | ||
get: function () { | ||
return this.tokens[this.position - 1]; | ||
} | ||
}]); | ||
@@ -326,0 +408,0 @@ |
@@ -29,2 +29,3 @@ 'use strict'; | ||
this.operator = opts.operator; | ||
this.namespace = opts.namespace; | ||
this.type = 'attribute'; | ||
@@ -39,3 +40,4 @@ this.raw = {}; | ||
value: function toString() { | ||
var selector = [this.spaces.before, '[' + this.attribute]; | ||
var namespace = this.namespace ? (typeof this.namespace === 'string' ? this.namespace : '') + '|' : ''; | ||
var selector = [this.spaces.before, '[', namespace, this.attribute]; | ||
@@ -42,0 +44,0 @@ if (this.operator) { |
@@ -34,3 +34,4 @@ 'use strict'; | ||
value: function toString() { | ||
return [this.spaces.before, String('.' + this.value), this.combinator, this.rules.map(String).join(''), this.spaces.after].join(''); | ||
var namespace = this.namespace ? (typeof this.namespace === 'string' ? this.namespace : '') + '|' : ''; | ||
return [this.spaces.before, namespace, String('.' + this.value), this.combinator, this.rules.map(String).join(''), this.spaces.after].join(''); | ||
} | ||
@@ -37,0 +38,0 @@ }]); |
@@ -34,3 +34,4 @@ 'use strict'; | ||
value: function toString() { | ||
return [this.spaces.before, String('#' + this.value), this.combinator, this.rules.map(String).join(''), this.spaces.after].join(''); | ||
var namespace = this.namespace ? (typeof this.namespace === 'string' ? this.namespace : '') + '|' : ''; | ||
return [this.spaces.before, namespace, String('#' + this.value), this.combinator, this.rules.map(String).join(''), this.spaces.after].join(''); | ||
} | ||
@@ -37,0 +38,0 @@ }]); |
@@ -36,3 +36,4 @@ 'use strict'; | ||
value: function toString() { | ||
return [this.spaces.before, String(this.value), this.combinator, this.rules.map(String).join(''), this.spaces.after].join(''); | ||
var namespace = this.namespace ? (typeof this.namespace === 'string' ? this.namespace : '') + '|' : ''; | ||
return [this.spaces.before, namespace, String(this.value), this.combinator, this.rules.map(String).join(''), this.spaces.after].join(''); | ||
} | ||
@@ -39,0 +40,0 @@ }]); |
{ | ||
"name": "postcss-selector-parser", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"devDependencies": { | ||
@@ -17,3 +17,4 @@ "babel": "^5.4.3", | ||
"prepublish": "babel src --out-dir dist", | ||
"test": "babel-tape-runner \"src/**/__tests__/*.js\" | faucet" | ||
"test-unformatted": "babel-tape-runner \"src/**/__tests__/*.js\"", | ||
"test": "npm run test-unformatted | faucet" | ||
}, | ||
@@ -20,0 +21,0 @@ "dependencies": { |
@@ -1,2 +0,2 @@ | ||
# postcss-selector-parser | ||
# postcss-selector-parser [](https://travis-ci.org/postcss/postcss-selector-parser) | ||
@@ -3,0 +3,0 @@ Work in progress parser for PostCSS. |
71033
11.24%31
6.9%1340
12.42%