From 07942aef43733a54c9a2b86131ee9fa841159b52 Mon Sep 17 00:00:00 2001 From: Roman Dvornov Date: Sun, 15 Jan 2017 00:20:13 +0300 Subject: [PATCH] add support for :matches (fixes #28) --- lib/parser/index.js | 5 +- .../parse/simpleSelector/pseudo-matches.json | 161 ++++++++++++++++++ .../{Negation.json => pseudo-negation.json} | 0 3 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 test/fixture/parse/simpleSelector/pseudo-matches.json rename test/fixture/parse/simpleSelector/{Negation.json => pseudo-negation.json} (100%) diff --git a/lib/parser/index.js b/lib/parser/index.js index cbbe4642..af4b987a 100644 --- a/lib/parser/index.js +++ b/lib/parser/index.js @@ -52,7 +52,8 @@ var SCOPE_ATRULE_EXPRESSION = { }; var SCOPE_SELECTOR = { url: getUri, - not: getNotFunction + not: getSelectorFunction, + matches: getSelectorFunction }; var SCOPE_VALUE = { url: getUri, @@ -1065,7 +1066,7 @@ function getFunctionArguments(scope) { return children; } -function getNotFunction(scope, info, name) { +function getSelectorFunction(scope, info, name) { return getFunctionInternal(function() { return new List().appendData(getSelector(true)); }, scope, info, name); diff --git a/test/fixture/parse/simpleSelector/pseudo-matches.json b/test/fixture/parse/simpleSelector/pseudo-matches.json new file mode 100644 index 00000000..45fb5478 --- /dev/null +++ b/test/fixture/parse/simpleSelector/pseudo-matches.json @@ -0,0 +1,161 @@ +{ + "basic negation": { + "source": ":matches(el.class-postfix)", + "ast": { + "type": "PseudoClass", + "name": "matches", + "children": [ + { + "type": "Selector", + "children": [ + { + "type": "SimpleSelector", + "children": [ + { + "type": "Type", + "name": "el" + }, + { + "type": "Class", + "name": "class-postfix" + } + ] + } + ] + } + ] + } + }, + "negation with selector group": { + "source": ":matches(.a,.b)", + "ast": { + "type": "PseudoClass", + "name": "matches", + "children": [ + { + "type": "Selector", + "children": [ + { + "type": "SimpleSelector", + "children": [ + { + "type": "Class", + "name": "a" + } + ] + }, + { + "type": "SimpleSelector", + "children": [ + { + "type": "Class", + "name": "b" + } + ] + } + ] + } + ] + } + }, + "negation with selector group and spaces": { + "source": ":matches( .a , .b )", + "translate": ":matches(.a,.b)", + "ast": { + "type": "PseudoClass", + "name": "matches", + "children": [ + { + "type": "Selector", + "children": [ + { + "type": "SimpleSelector", + "children": [ + { + "type": "Class", + "name": "a" + } + ] + }, + { + "type": "SimpleSelector", + "children": [ + { + "type": "Class", + "name": "b" + } + ] + } + ] + } + ] + } + }, + "should be case insensitive": { + "source": ":MatcheS(.a,.b)", + "ast": { + "type": "PseudoClass", + "name": "MatcheS", + "children": [ + { + "type": "Selector", + "children": [ + { + "type": "SimpleSelector", + "children": [ + { + "type": "Class", + "name": "a" + } + ] + }, + { + "type": "SimpleSelector", + "children": [ + { + "type": "Class", + "name": "b" + } + ] + } + ] + } + ] + } + }, + "error #1": { + "source": ":matches(.a{)", + "offset": " ^", + "error": "Unexpected input" + }, + "error #2": { + "source": ":matches(,.b)", + "offset": " ^", + "error": "Unexpected comma" + }, + "error #3": { + "source": ":matches(.a,)", + "offset": " ^", + "error": "Unexpected trailing comma" + }, + "error #4": { + "source": ":matches(.a,,)", + "offset": " ^", + "error": "Unexpected comma" + }, + "error #5": { + "source": ":matches(.a,.b{)", + "offset": " ^", + "error": "Unexpected input" + }, + "error #6": { + "source": ":matches(--test) {}", + "offset": " ^", + "error": "Identifier is expected" + }, + "error #7": { + "source": ":matches(var(--test)) {}", + "offset": " ^", + "error": "Unexpected input" + } +} diff --git a/test/fixture/parse/simpleSelector/Negation.json b/test/fixture/parse/simpleSelector/pseudo-negation.json similarity index 100% rename from test/fixture/parse/simpleSelector/Negation.json rename to test/fixture/parse/simpleSelector/pseudo-negation.json