root 3bf41bf355 update | 1 ano atrás | |
---|---|---|
.. | ||
.npmignore | 1 ano atrás | |
CHANGELOG.md | 1 ano atrás | |
LICENSE | 1 ano atrás | |
README.md | 1 ano atrás | |
circle.yml | 1 ano atrás | |
index.js | 1 ano atrás | |
package.json | 1 ano atrás | |
test.js | 1 ano atrás |
Search CSS (and CSS-like) strings, with sensitivity to whether matches occur inside strings, comments, and functions.
var styleSearch = require('style-search');
styleSearch(options, callback);
By default, the search ignores strings, comments, and function names. You can use the options to change this behavior or introduce other restrictions. That is what makes this module more useful for many searches than indexOf()
or a RegExp
.
However, if you need more detailed parsing, you should consider using the real parsers PostCSS, postcss-selector-parser
, and postcss-value-parser
.
/* Here is some pink */
a { color: pink; }
a::before { content: "pink" }
b { color: shadesOfPink(7); }
styleSearch({
source: theCssStringAbove,
target: "pink",
}, function(match, count) {
/* Only the "pink" in `color: pink` will be
reported as a match */
});
For every match found your callback
is invoked. It is passed two arguments:
match
object with the following properties:
startIndex
: where the match beginsendIndex
: where the match endstarget
: what got matched (useful if your target
option is an array instead of a single string)insideFunctionArguments
: whether the match is inside a function — this includes the parentheses around the argumentsinsideComment
: whether the match is inside a commentinsideString
: whether the match is inside a stringBelow you'll see that syntax feature options all accept three keywords: "skip"
, "check"
, "only"
. An error will be thrown if you use "only"
more than once.
String. Required.
The source string to search through.
String or array of strings. Required.
The target of the search. Can be
match
object passed to the callback
will differentiate which string in the array got matched via its target
property)Boolean. Default: false
.
If true
, the search will stop after one match is found.
"skip"
| "check"
| "only"
. Default: "skip"
.
This includes both standard /* CSS comments */
and non-standard but widely used // single line comments
.
"skip"
| "check"
| "only"
. Default: "skip"
.
"skip"
| "check"
| "only"
. Default: "skip"
.
"skip"
| "check"
| "only"
. Default: "check"
.
"skip"
| "check"
| "only"
. Default: "check"
.
This designates anything inside parentheses, which includes standard functions, but also Sass maps and other non-standard constructs. parentheticals
is a broader category than functionArguments
.