1 var test = require("tape");
2 var styleSearch = require("./index");
4 function styleSearchResults(options) {
6 styleSearch(options, function(match) {
7 results.push(match.startIndex);
12 test("default options", function(t) {
13 t.deepEqual(styleSearchResults({
17 t.deepEqual(styleSearchResults({
21 t.deepEqual(styleSearchResults({
25 t.deepEqual(styleSearchResults({
26 source: "abc \"var(--cba)\"",
32 test("once", function(t) {
33 t.deepEqual(styleSearchResults({
38 t.deepEqual(styleSearchResults({
43 t.deepEqual(styleSearchResults({
51 test("functionArguments: 'only'", function(t) {
52 t.deepEqual(styleSearchResults({
53 source: "abc var(--cba)",
55 functionArguments: "only",
57 t.deepEqual(styleSearchResults({
58 source: "abc var(--cba)",
60 functionArguments: "only",
62 t.deepEqual(styleSearchResults({
63 source: "abc \"var(--cba)\"",
65 functionArguments: "only",
67 t.deepEqual(styleSearchResults({
68 source: "translate(1px, calc(1px * 2))",
70 functionArguments: "only",
72 t.deepEqual(styleSearchResults({
73 source: "var(--horse)",
75 functionArguments: "only",
77 t.deepEqual(styleSearchResults({
80 functionArguments: "only",
81 }), [], "parens without function is not interpreted as a function");
82 t.deepEqual(styleSearchResults({
85 functionArguments: "only",
86 }), [], "parens preceded by `$`, for postcss-simple-vars interpolation, not interpreted as a function");
87 t.deepEqual(styleSearchResults({
90 functionArguments: "only",
91 }), [], "closing paren of non-function is ignored");
95 test("functionArguments: 'skip'", function(t) {
96 t.deepEqual(styleSearchResults({
97 source: "abc var(--cba)",
99 functionArguments: "skip",
101 t.deepEqual(styleSearchResults({
102 source: "abc var(--cba)",
104 functionArguments: "skip",
106 t.deepEqual(styleSearchResults({
107 source: "abc \"a var(--cba)\"",
109 functionArguments: "skip",
111 t.deepEqual(styleSearchResults({
112 source: "translate(1px, calc(1px * 2))",
114 functionArguments: "skip",
116 t.deepEqual(styleSearchResults({
117 source: "var(--horse)",
119 functionArguments: "skip",
121 t.deepEqual(styleSearchResults({
124 functionArguments: "skip",
125 }), [6], "parens without function is not interpreted as a function");
129 test("parentheticals: 'skip'", function(t) {
130 t.deepEqual(styleSearchResults({
131 source: "abc var(--cba)",
133 parentheticals: "skip",
135 t.deepEqual(styleSearchResults({
136 source: "abc var(--cba)",
138 parentheticals: "skip",
140 t.deepEqual(styleSearchResults({
141 source: "abc \"a var(--cba)\"",
143 parentheticals: "skip",
145 t.deepEqual(styleSearchResults({
146 source: "translate(1px, calc(1px * 2))",
148 parentheticals: "skip",
150 t.deepEqual(styleSearchResults({
151 source: "var(--horse)",
153 parentheticals: "skip",
155 t.deepEqual(styleSearchResults({
158 parentheticals: "skip",
159 }), [], "parens without function are still ignored");
163 test("ignores matches inside single-quote strings", function(t) {
164 t.deepEqual(styleSearchResults({
168 t.deepEqual(styleSearchResults({
169 source: "abc 'abc' cba",
175 test("ignores matches inside double-quote strings", function(t) {
176 t.deepEqual(styleSearchResults({
180 t.deepEqual(styleSearchResults({
181 source: 'abc "abc" cba',
187 test("strings: 'check'", function(t) {
188 t.deepEqual(styleSearchResults({
194 t.deepEqual(styleSearchResults({
195 source: "abc /* 'abc' */",
198 }), [1], "no strings inside comments");
202 test("strings: 'only'", function(t) {
203 t.deepEqual(styleSearchResults({
209 t.deepEqual(styleSearchResults({
210 source: "p[href^='https://']:before { content: \"\/*\"; \n top: 0;\n}",
213 }), [], "comments do not start inside strings");
218 test("ignores matches inside comments", function(t) {
219 t.deepEqual(styleSearchResults({
220 source: "abc/*comment*/",
223 t.deepEqual(styleSearchResults({
224 source: "abc/*command*/",
230 test("comments: 'check'", function(t) {
231 t.deepEqual(styleSearchResults({
232 source: "abc/*abc*/",
239 test("comments: 'only'", function(t) {
240 t.deepEqual(styleSearchResults({
241 source: "abc/*abc*/",
245 t.deepEqual(styleSearchResults({
246 source: "abc/*/abc*/",
250 t.deepEqual(styleSearchResults({
251 source: "ab'c/*abc*/c'",
254 }), [], "no comments inside strings");
258 test("ignores matches inside single-line comment", function(t) {
259 t.deepEqual(styleSearchResults({
260 source: "abc // comment",
263 t.deepEqual(styleSearchResults({
264 source: "abc // command",
267 // Triple-slash comments are used for sassdoc
268 t.deepEqual(styleSearchResults({
269 source: "abc /// it's all ok",
275 test("handles escaped double-quotes in double-quote strings", function(t) {
276 t.deepEqual(styleSearchResults({
277 source: 'abc "ab\\"c"',
280 t.deepEqual(styleSearchResults({
281 source: 'abc "a\\"bc" foo cba',
287 test("handles escaped double-quotes in single-quote strings", function(t) {
288 t.deepEqual(styleSearchResults({
289 source: "abc 'ab\\'c'",
292 t.deepEqual(styleSearchResults({
293 source: "abc 'a\\'bc' foo cba",
299 test("count", function(t) {
301 styleSearch({ source: "123 123 123", target: "1" }, function(index, count) {
302 endCounts.push(count);
304 t.deepEqual(endCounts, [ 1, 2, 3 ]);
308 test("finds parentheses", function(t) {
309 t.deepEqual(styleSearchResults({
310 source: "a { color: rgb(0,0,0); }",
313 t.deepEqual(styleSearchResults({
314 source: "a { color: rgb(0,0,0); }",
320 test("functionNames: 'check'", function(t) {
321 t.deepEqual(styleSearchResults({
322 source: "a { color: rgb(0,0,0); }",
325 t.deepEqual(styleSearchResults({
326 source: "a { color: rgb(0,0,0); }",
328 functionNames: "check"
333 test("non-single-character target", function(t) {
334 t.deepEqual(styleSearchResults({
338 t.deepEqual(styleSearchResults({
342 t.deepEqual(styleSearchResults({
346 t.deepEqual(styleSearchResults({
347 source: "abc cba abc",
350 t.deepEqual(styleSearchResults({
351 source: "abc cba 'abc'",
354 t.deepEqual(styleSearchResults({
361 test("array target", function(t) {
362 t.deepEqual(styleSearchResults({
364 target: [ "a", "b" ],
366 t.deepEqual(styleSearchResults({
368 target: [ "c", "b" ],
370 t.deepEqual(styleSearchResults({
372 target: [ "bc", "a" ],
374 t.deepEqual(styleSearchResults({
376 target: [ "abc", "f" ],
378 t.deepEqual(styleSearchResults({
385 test("match object", function(t) {
386 styleSearch({ source: "abc", target: "bc" }, function(match) {
387 t.equal(match.startIndex, 1);
388 t.equal(match.endIndex, 3);
389 t.equal(match.target, "bc");
390 t.equal(match.insideFunctionArguments, false);
391 t.equal(match.insideComment, false);
394 const twoMatches = []
395 styleSearch({ source: "abc bca", target: [ "bc ", "ca" ] }, function(match) {
396 twoMatches.push(match);
398 const firstMatch = twoMatches[0]
399 const secondMatch = twoMatches[1]
400 t.equal(firstMatch.startIndex, 1);
401 t.equal(firstMatch.endIndex, 4);
402 t.equal(firstMatch.target, "bc ");
403 t.equal(firstMatch.insideFunctionArguments, false);
404 t.equal(firstMatch.insideComment, false);
405 t.equal(secondMatch.startIndex, 5);
406 t.equal(secondMatch.endIndex, 7);
407 t.equal(secondMatch.target, "ca");
408 t.equal(secondMatch.insideFunctionArguments, false);
409 t.equal(secondMatch.insideComment, false);
413 test("match inside a function", function(t) {
414 styleSearch({ source: "a { color: rgb(0, 0, 1); }", target: "1" }, function(match) {
415 t.equal(match.insideFunctionArguments, true);
416 t.equal(match.insideComment, false);
421 test("match inside a comment", function(t) {
423 source: "a { color: /* 1 */ pink; }",
427 t.equal(match.insideFunctionArguments, false);
428 t.equal(match.insideComment, true);
433 test("match inside a block comment", function(t) {
435 source: "a { color:\n/**\n * 0\n * 1\n */\npink; }",
439 t.equal(match.insideFunctionArguments, false);
440 t.equal(match.insideComment, true);
445 test("match inside a comment inside function", function(t) {
447 source: "a { color: rgb(0, 0, 0 /* 1 */); }",
451 t.equal(match.insideFunctionArguments, true);
452 t.equal(match.insideComment, true);
457 test("error on multiple 'only' options", function(t) {
458 t.throws(function() {
464 }, function(match) {});
465 }, /Only one syntax/);