123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467 |
- var test = require("tape");
- var styleSearch = require("./index");
- function styleSearchResults(options) {
- const results = [];
- styleSearch(options, function(match) {
- results.push(match.startIndex);
- });
- return results;
- }
- test("default options", function(t) {
- t.deepEqual(styleSearchResults({
- source: "abc cba",
- target: "c",
- }), [ 2, 4 ]);
- t.deepEqual(styleSearchResults({
- source: "abc cb",
- target: "a",
- }), [0]);
- t.deepEqual(styleSearchResults({
- source: "abc cba",
- target: "b",
- }), [ 1, 5 ]);
- t.deepEqual(styleSearchResults({
- source: "abc \"var(--cba)\"",
- target: "a",
- }), [0]);
- t.end();
- });
- test("once", function(t) {
- t.deepEqual(styleSearchResults({
- source: "abc cba",
- target: "c",
- once: true,
- }), [2]);
- t.deepEqual(styleSearchResults({
- source: "abc cba",
- target: "a",
- once: true,
- }), [0]);
- t.deepEqual(styleSearchResults({
- source: "abc cba",
- target: "b",
- once: false,
- }), [ 1, 5 ]);
- t.end();
- });
- test("functionArguments: 'only'", function(t) {
- t.deepEqual(styleSearchResults({
- source: "abc var(--cba)",
- target: "c",
- functionArguments: "only",
- }), [10]);
- t.deepEqual(styleSearchResults({
- source: "abc var(--cba)",
- target: "a",
- functionArguments: "only",
- }), [12]);
- t.deepEqual(styleSearchResults({
- source: "abc \"var(--cba)\"",
- target: "a",
- functionArguments: "only",
- }), []);
- t.deepEqual(styleSearchResults({
- source: "translate(1px, calc(1px * 2))",
- target: "1",
- functionArguments: "only",
- }), [ 10, 20 ]);
- t.deepEqual(styleSearchResults({
- source: "var(--horse)",
- target: "v",
- functionArguments: "only",
- }), []);
- t.deepEqual(styleSearchResults({
- source: "abc (abc)",
- target: "b",
- functionArguments: "only",
- }), [], "parens without function is not interpreted as a function");
- t.deepEqual(styleSearchResults({
- source: "de$(abc)fg",
- target: "b",
- functionArguments: "only",
- }), [], "parens preceded by `$`, for postcss-simple-vars interpolation, not interpreted as a function");
- t.deepEqual(styleSearchResults({
- source: "de$(abc)fg",
- target: ")",
- functionArguments: "only",
- }), [], "closing paren of non-function is ignored");
- t.end();
- });
- test("functionArguments: 'skip'", function(t) {
- t.deepEqual(styleSearchResults({
- source: "abc var(--cba)",
- target: "c",
- functionArguments: "skip",
- }), [2]);
- t.deepEqual(styleSearchResults({
- source: "abc var(--cba)",
- target: "a",
- functionArguments: "skip",
- }), [0]);
- t.deepEqual(styleSearchResults({
- source: "abc \"a var(--cba)\"",
- target: "a",
- functionArguments: "skip",
- }), [0]);
- t.deepEqual(styleSearchResults({
- source: "translate(1px, calc(1px * 2))",
- target: "1",
- functionArguments: "skip",
- }), []);
- t.deepEqual(styleSearchResults({
- source: "var(--horse)",
- target: "v",
- functionArguments: "skip",
- }), []);
- t.deepEqual(styleSearchResults({
- source: "abc (def)",
- target: "e",
- functionArguments: "skip",
- }), [6], "parens without function is not interpreted as a function");
- t.end();
- });
- test("parentheticals: 'skip'", function(t) {
- t.deepEqual(styleSearchResults({
- source: "abc var(--cba)",
- target: "c",
- parentheticals: "skip",
- }), [2]);
- t.deepEqual(styleSearchResults({
- source: "abc var(--cba)",
- target: "a",
- parentheticals: "skip",
- }), [0]);
- t.deepEqual(styleSearchResults({
- source: "abc \"a var(--cba)\"",
- target: "a",
- parentheticals: "skip",
- }), [0]);
- t.deepEqual(styleSearchResults({
- source: "translate(1px, calc(1px * 2))",
- target: "1",
- parentheticals: "skip",
- }), []);
- t.deepEqual(styleSearchResults({
- source: "var(--horse)",
- target: "v",
- parentheticals: "skip",
- }), []);
- t.deepEqual(styleSearchResults({
- source: "abc (def)",
- target: "e",
- parentheticals: "skip",
- }), [], "parens without function are still ignored");
- t.end();
- });
- test("ignores matches inside single-quote strings", function(t) {
- t.deepEqual(styleSearchResults({
- source: "abc 'abc'",
- target: "c",
- }), [2]);
- t.deepEqual(styleSearchResults({
- source: "abc 'abc' cba",
- target: "c",
- }), [ 2, 10 ]);
- t.end();
- });
- test("ignores matches inside double-quote strings", function(t) {
- t.deepEqual(styleSearchResults({
- source: 'abc "abc"',
- target: "c",
- }), [2]);
- t.deepEqual(styleSearchResults({
- source: 'abc "abc" cba',
- target: "c",
- }), [ 2, 10 ]);
- t.end();
- });
- test("strings: 'check'", function(t) {
- t.deepEqual(styleSearchResults({
- source: "abc 'abc'",
- target: "b",
- strings: "check",
- }), [ 1, 6 ]);
- t.deepEqual(styleSearchResults({
- source: "abc /* 'abc' */",
- target: "b",
- strings: "check",
- }), [1], "no strings inside comments");
- t.end();
- });
- test("strings: 'only'", function(t) {
- t.deepEqual(styleSearchResults({
- source: 'abc "abc"',
- target: "b",
- strings: "only",
- }), [6]);
- t.deepEqual(styleSearchResults({
- source: "p[href^='https://']:before { content: \"\/*\"; \n top: 0;\n}",
- target: "\n",
- strings: "only",
- }), [], "comments do not start inside strings");
- t.end();
- });
- test("ignores matches inside comments", function(t) {
- t.deepEqual(styleSearchResults({
- source: "abc/*comment*/",
- target: "m",
- }), []);
- t.deepEqual(styleSearchResults({
- source: "abc/*command*/",
- target: "a",
- }), [0]);
- t.end();
- });
- test("comments: 'check'", function(t) {
- t.deepEqual(styleSearchResults({
- source: "abc/*abc*/",
- target: "b",
- comments: "check",
- }), [ 1, 6 ]);
- t.end();
- });
- test("comments: 'only'", function(t) {
- t.deepEqual(styleSearchResults({
- source: "abc/*abc*/",
- target: "b",
- comments: "only",
- }), [6]);
- t.deepEqual(styleSearchResults({
- source: "abc/*/abc*/",
- target: "b",
- comments: "only",
- }), [7]);
- t.deepEqual(styleSearchResults({
- source: "ab'c/*abc*/c'",
- target: "b",
- comments: "only",
- }), [], "no comments inside strings");
- t.end();
- });
- test("ignores matches inside single-line comment", function(t) {
- t.deepEqual(styleSearchResults({
- source: "abc // comment",
- target: "m",
- }), []);
- t.deepEqual(styleSearchResults({
- source: "abc // command",
- target: "a",
- }), [0]);
-
- t.deepEqual(styleSearchResults({
- source: "abc /// it's all ok",
- target: "a",
- }), [0]);
- t.end();
- });
- test("handles escaped double-quotes in double-quote strings", function(t) {
- t.deepEqual(styleSearchResults({
- source: 'abc "ab\\"c"',
- target: "c",
- }), [2]);
- t.deepEqual(styleSearchResults({
- source: 'abc "a\\"bc" foo cba',
- target: "c",
- }), [ 2, 16 ]);
- t.end();
- });
- test("handles escaped double-quotes in single-quote strings", function(t) {
- t.deepEqual(styleSearchResults({
- source: "abc 'ab\\'c'",
- target: "c",
- }), [2]);
- t.deepEqual(styleSearchResults({
- source: "abc 'a\\'bc' foo cba",
- target: "c",
- }), [ 2, 16 ]);
- t.end();
- });
- test("count", function(t) {
- const endCounts = []
- styleSearch({ source: "123 123 123", target: "1" }, function(index, count) {
- endCounts.push(count);
- });
- t.deepEqual(endCounts, [ 1, 2, 3 ]);
- t.end();
- });
- test("finds parentheses", function(t) {
- t.deepEqual(styleSearchResults({
- source: "a { color: rgb(0,0,0); }",
- target: "(",
- }), [14]);
- t.deepEqual(styleSearchResults({
- source: "a { color: rgb(0,0,0); }",
- target: ")",
- }), [20]);
- t.end();
- });
- test("functionNames: 'check'", function(t) {
- t.deepEqual(styleSearchResults({
- source: "a { color: rgb(0,0,0); }",
- target: "rgb",
- }), []);
- t.deepEqual(styleSearchResults({
- source: "a { color: rgb(0,0,0); }",
- target: "rgb",
- functionNames: "check"
- }), [11]);
- t.end();
- });
- test("non-single-character target", function(t) {
- t.deepEqual(styleSearchResults({
- source: "abc cba",
- target: "abc",
- }), [0]);
- t.deepEqual(styleSearchResults({
- source: "abc cba",
- target: "cb",
- }), [4]);
- t.deepEqual(styleSearchResults({
- source: "abc cba",
- target: "c c",
- }), [2]);
- t.deepEqual(styleSearchResults({
- source: "abc cba abc",
- target: "abc",
- }), [ 0, 8 ]);
- t.deepEqual(styleSearchResults({
- source: "abc cba 'abc'",
- target: "abc",
- }), [0]);
- t.deepEqual(styleSearchResults({
- source: "abc cb",
- target: "aa",
- }), []);
- t.end();
- });
- test("array target", function(t) {
- t.deepEqual(styleSearchResults({
- source: "abc cba",
- target: [ "a", "b" ],
- }), [ 0, 1, 5, 6 ]);
- t.deepEqual(styleSearchResults({
- source: "abc cba",
- target: [ "c", "b" ],
- }), [ 1, 2, 4, 5 ]);
- t.deepEqual(styleSearchResults({
- source: "abc cba",
- target: [ "bc", "a" ],
- }), [ 0, 1, 6 ]);
- t.deepEqual(styleSearchResults({
- source: "abc cba",
- target: [ "abc", "f" ],
- }), [0]);
- t.deepEqual(styleSearchResults({
- source: "abc cba",
- target: [ 0, 1, 2 ],
- }), []);
- t.end();
- });
- test("match object", function(t) {
- styleSearch({ source: "abc", target: "bc" }, function(match) {
- t.equal(match.startIndex, 1);
- t.equal(match.endIndex, 3);
- t.equal(match.target, "bc");
- t.equal(match.insideFunctionArguments, false);
- t.equal(match.insideComment, false);
- });
- const twoMatches = []
- styleSearch({ source: "abc bca", target: [ "bc ", "ca" ] }, function(match) {
- twoMatches.push(match);
- });
- const firstMatch = twoMatches[0]
- const secondMatch = twoMatches[1]
- t.equal(firstMatch.startIndex, 1);
- t.equal(firstMatch.endIndex, 4);
- t.equal(firstMatch.target, "bc ");
- t.equal(firstMatch.insideFunctionArguments, false);
- t.equal(firstMatch.insideComment, false);
- t.equal(secondMatch.startIndex, 5);
- t.equal(secondMatch.endIndex, 7);
- t.equal(secondMatch.target, "ca");
- t.equal(secondMatch.insideFunctionArguments, false);
- t.equal(secondMatch.insideComment, false);
- t.end();
- });
- test("match inside a function", function(t) {
- styleSearch({ source: "a { color: rgb(0, 0, 1); }", target: "1" }, function(match) {
- t.equal(match.insideFunctionArguments, true);
- t.equal(match.insideComment, false);
- t.end();
- });
- });
- test("match inside a comment", function(t) {
- styleSearch({
- source: "a { color: /* 1 */ pink; }",
- target: "1",
- comments: "check"
- }, function(match) {
- t.equal(match.insideFunctionArguments, false);
- t.equal(match.insideComment, true);
- t.end();
- });
- });
- test("match inside a block comment", function(t) {
- styleSearch({
- source: "a { color:\n/**\n * 0\n * 1\n */\npink; }",
- target: "1",
- comments: "check"
- }, function(match) {
- t.equal(match.insideFunctionArguments, false);
- t.equal(match.insideComment, true);
- t.end();
- });
- });
- test("match inside a comment inside function", function(t) {
- styleSearch({
- source: "a { color: rgb(0, 0, 0 /* 1 */); }",
- target: "1",
- comments: "check"
- }, function(match) {
- t.equal(match.insideFunctionArguments, true);
- t.equal(match.insideComment, true);
- t.end();
- });
- });
- test("error on multiple 'only' options", function(t) {
- t.throws(function() {
- styleSearch({
- source: "a {}",
- target: "a",
- comments: "only",
- strings: "only",
- }, function(match) {});
- }, /Only one syntax/);
- t.end();
- });
|