postcss-selector-parser
Advanced tools
Comparing version
40
API.md
@@ -14,5 +14,10 @@ # API Documentation | ||
### `parser(transform)` | ||
### `parser([transform])` | ||
Creates a new `processor` instance | ||
```js | ||
var processor = parser(); | ||
// or, with optional transform function | ||
var transform = function (selectors) { | ||
@@ -24,6 +29,11 @@ selectors.eachUniversal(function (selector) { | ||
var result = parser(transform).process('*.class').result; | ||
var processor = parser(transform) | ||
// Example | ||
var result = processor.process('*.class').result; | ||
// => .class | ||
``` | ||
[See processor documentation](#processor) | ||
Arguments: | ||
@@ -591,1 +601,27 @@ | ||
``` | ||
## `processor` | ||
### `process(cssText, [options])` | ||
Processes the `cssText`, returning the parsed output | ||
```js | ||
var processor = parser(); | ||
var result = processor.process(' .class').result; | ||
// => .class | ||
// To have the parser normalize whitespace values, utilize the options | ||
var result = processor.process(' .class ', {lossless: false}).result; | ||
// => .class | ||
``` | ||
Arguments: | ||
* `cssText (string)`: The css to be parsed. | ||
* `[options] (object)`: Process options | ||
Options: | ||
* `loseless (boolean)`: false to normalize the selector whitespace, defaults to true |
@@ -0,1 +1,6 @@ | ||
# 2.2.0 | ||
* Added a new option to normalize whitespace when parsing the selector string | ||
(thanks to @adam-26). | ||
# 2.1.1 | ||
@@ -2,0 +7,0 @@ |
@@ -90,2 +90,3 @@ 'use strict'; | ||
this.input = input; | ||
this.lossy = input.options.lossless === false; | ||
this.position = 0; | ||
@@ -98,3 +99,7 @@ this.root = new _root2.default(); | ||
this.current = selectors; | ||
this.tokens = (0, _tokenize2.default)(input); | ||
if (this.lossy) { | ||
this.tokens = (0, _tokenize2.default)({ safe: input.safe, css: input.css.trim() }); | ||
} else { | ||
this.tokens = (0, _tokenize2.default)(input); | ||
} | ||
@@ -137,6 +142,6 @@ return this.loop(); | ||
} | ||
attributeProps.attribute = namespace[2]; | ||
attributeProps.namespace = namespace[0]; | ||
attributeProps.attribute = this.parseValue(namespace[2]); | ||
attributeProps.namespace = this.parseNamespace(namespace[0]); | ||
} else { | ||
attributeProps.attribute = parts[0]; | ||
attributeProps.attribute = this.parseValue(parts[0]); | ||
} | ||
@@ -147,8 +152,10 @@ attr = new _attribute2.default(attributeProps); | ||
var insensitive = parts[2].split(/(\s+i\s*?)$/); | ||
attr.value = insensitive[0]; | ||
var trimmedValue = insensitive[0].trim(); | ||
attr.value = this.lossy ? trimmedValue : insensitive[0]; | ||
if (insensitive[1]) { | ||
attr.insensitive = true; | ||
attr.raws.insensitive = insensitive[1]; | ||
if (!this.lossy) { | ||
attr.raws.insensitive = insensitive[1]; | ||
} | ||
} | ||
var trimmedValue = attr.value.trim(); | ||
attr.quoted = trimmedValue[0] === '\'' || trimmedValue[0] === '"'; | ||
@@ -181,3 +188,3 @@ attr.raws.unquoted = attr.quoted ? trimmedValue.slice(1, -1) : trimmedValue; | ||
if (this.nextToken && this.nextToken[0] === 'combinator') { | ||
node.spaces.before = this.currToken[1]; | ||
node.spaces.before = this.parseSpace(this.currToken[1]); | ||
node.source.start.line = this.nextToken[2]; | ||
@@ -189,5 +196,7 @@ node.source.start.column = this.nextToken[3]; | ||
} else if (this.prevToken && this.prevToken[0] === 'combinator') { | ||
node.spaces.after = this.currToken[1]; | ||
} else if (this.currToken[0] === 'space' || this.currToken[0] === 'combinator') { | ||
node.spaces.after = this.parseSpace(this.currToken[1]); | ||
} else if (this.currToken[0] === 'combinator') { | ||
node.value = this.currToken[1]; | ||
} else if (this.currToken[0] === 'space' && !(this.lossy && this.prevToken[0] === '&')) { | ||
node.value = this.parseSpace(this.currToken[1], ' '); | ||
} | ||
@@ -310,3 +319,3 @@ this.position++; | ||
} | ||
last.value += this.currToken[1]; | ||
last.value += this.parseParenthesisToken(this.currToken); | ||
this.position++; | ||
@@ -366,6 +375,6 @@ } | ||
if (this.position === 0 || this.prevToken[0] === ',' || this.prevToken[0] === '(') { | ||
this.spaces = token[1]; | ||
this.spaces = this.parseSpace(token[1]); | ||
this.position++; | ||
} else if (this.position === this.tokens.length - 1 || this.nextToken[0] === ',' || this.nextToken[0] === ')') { | ||
this.current.last.spaces.after = token[1]; | ||
this.current.last.spaces.after = this.parseSpace(token[1]); | ||
this.position++; | ||
@@ -431,3 +440,3 @@ } else { | ||
if (next && next[0] === 'space') { | ||
word += next[1]; | ||
word += this.parseSpace(next[1], ' '); | ||
this.position++; | ||
@@ -573,5 +582,38 @@ } | ||
Parser.prototype.parseNamespace = function parseNamespace(namespace) { | ||
if (this.lossy && typeof namespace === 'string') { | ||
var trimmed = namespace.trim(); | ||
if (!trimmed.length) { | ||
return true; | ||
} | ||
return trimmed; | ||
} | ||
return namespace; | ||
}; | ||
Parser.prototype.parseSpace = function parseSpace(space, replacement) { | ||
return this.lossy ? replacement || '' : space; | ||
}; | ||
Parser.prototype.parseValue = function parseValue(value) { | ||
return this.lossy && value && typeof value === 'string' ? value.trim() : value; | ||
}; | ||
Parser.prototype.parseParenthesisToken = function parseParenthesisToken(token) { | ||
if (!this.lossy) { | ||
return token[1]; | ||
} | ||
if (token[0] === 'space') { | ||
return this.parseSpace(token[1], ' '); | ||
} | ||
return this.parseValue(token[1]); | ||
}; | ||
Parser.prototype.newNode = function newNode(node, namespace) { | ||
if (namespace) { | ||
node.namespace = namespace; | ||
node.namespace = this.parseNamespace(namespace); | ||
} | ||
@@ -578,0 +620,0 @@ if (this.spaces) { |
@@ -24,2 +24,4 @@ 'use strict'; | ||
Processor.prototype.process = function process(selectors) { | ||
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var input = new _parser2.default({ | ||
@@ -29,3 +31,4 @@ css: selectors, | ||
throw new Error(e); | ||
} | ||
}, | ||
options: options | ||
}); | ||
@@ -32,0 +35,0 @@ this.res = input; |
{ | ||
"name": "postcss-selector-parser", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"devDependencies": { | ||
@@ -5,0 +5,0 @@ "ava": "^0.15.0", |
@@ -27,2 +27,10 @@ # postcss-selector-parser [](https://travis-ci.org/postcss/postcss-selector-parser) | ||
To normalize selector whitespace: | ||
```js | ||
var parser = require('postcss-selector-parser'); | ||
var normalized = parser().process('h1, h2, h3', {lossless:false}).result; | ||
// -> h1,h2,h3 | ||
``` | ||
## API | ||
@@ -29,0 +37,0 @@ |
90384
3.1%1550
2.38%47
20.51%