[Pkg-javascript-commits] [node-css-what] 02/04: Imported Upstream version 2.1.0

Thorsten Alteholz alteholz at moszumanska.debian.org
Sat Feb 6 16:36:31 UTC 2016


This is an automated email from the git hooks/post-receive script.

alteholz pushed a commit to branch master
in repository node-css-what.

commit 718e37073c1a9a1ae330c9e14d54bef3422e13be
Author: Thorsten Alteholz <debian at alteholz.de>
Date:   Sat Feb 6 17:36:23 2016 +0100

    Imported Upstream version 2.1.0
---
 index.js      | 207 ++++++++++++++++++++++++++++++----------------------------
 package.json  |   2 +-
 tests/test.js |  26 ++++++++
 3 files changed, 133 insertions(+), 102 deletions(-)

diff --git a/index.js b/index.js
index d5d813e..859324c 100644
--- a/index.js
+++ b/index.js
@@ -35,21 +35,22 @@ var attribSelectors = {
 
 //pseudos, whose data-property is parsed as well
 var unpackPseudos = {
-    __proto__: null,
-    "has": true,
-    "not": true,
-    "matches": true
+	__proto__: null,
+	"has": true,
+	"not": true,
+	"matches": true
 };
 
 var stripQuotesFromPseudos = {
-    __proto__: null,
-    "contains": true
+	__proto__: null,
+	"contains": true,
+	"icontains": true
 };
 
 var quotes = {
-    __proto__: null,
-    "\"": true,
-    "'": true
+	__proto__: null,
+	"\"": true,
+	"'": true
 };
 
 //unescape function taken from https://github.com/jquery/sizzle/blob/master/src/sizzle.js#L139
@@ -72,25 +73,25 @@ function unescapeCSS(str){
 }
 
 function isWhitespace(c){
-    return c === " " || c === "\n" || c === "\t" || c === "\f" || c === "\r";
+	return c === " " || c === "\n" || c === "\t" || c === "\f" || c === "\r";
 }
 
 function parse(selector, options){
 	var subselects = [];
 
-    selector = parseSelector(subselects, selector + "", options);
+	selector = parseSelector(subselects, selector + "", options);
 
-    if(selector !== ""){
-        throw new SyntaxError("Unmatched selector: " + selector);
-    }
+	if(selector !== ""){
+		throw new SyntaxError("Unmatched selector: " + selector);
+	}
 
-    return subselects;
+	return subselects;
 }
 
 function parseSelector(subselects, selector, options){
-    var tokens = [],
-        sawWS = false,
-        data, firstChar, name, quot;
+	var tokens = [],
+		sawWS = false,
+		data, firstChar, name, quot;
 
 	function getName(){
 		var sub = selector.match(re_name)[0];
@@ -98,45 +99,45 @@ function parseSelector(subselects, selector, options){
 		return unescapeCSS(sub);
 	}
 
-    function stripWhitespace(start){
-        while(isWhitespace(selector.charAt(start))) start++;
-        selector = selector.substr(start);
-    }
+	function stripWhitespace(start){
+		while(isWhitespace(selector.charAt(start))) start++;
+		selector = selector.substr(start);
+	}
 
-    stripWhitespace(0);
+	stripWhitespace(0);
 
 	while(selector !== ""){
-        firstChar = selector.charAt(0);
+		firstChar = selector.charAt(0);
 
-        if(isWhitespace(firstChar)){
+		if(isWhitespace(firstChar)){
 			sawWS = true;
-            stripWhitespace(1);
+			stripWhitespace(1);
 		} else if(firstChar in simpleSelectors){
-            tokens.push({type: simpleSelectors[firstChar]});
-            sawWS = false;
-
-            stripWhitespace(1);
-        } else if(firstChar === ","){
-            if(tokens.length === 0){
-                throw new SyntaxError("empty sub-selector");
-            }
-            subselects.push(tokens);
-            tokens = [];
-            sawWS = false;
-            stripWhitespace(1);
-        } else {
+			tokens.push({type: simpleSelectors[firstChar]});
+			sawWS = false;
+
+			stripWhitespace(1);
+		} else if(firstChar === ","){
+			if(tokens.length === 0){
+				throw new SyntaxError("empty sub-selector");
+			}
+			subselects.push(tokens);
+			tokens = [];
+			sawWS = false;
+			stripWhitespace(1);
+		} else {
 			if(sawWS){
-                if(tokens.length > 0){
-                    tokens.push({type: "descendant"});
-                }
+				if(tokens.length > 0){
+					tokens.push({type: "descendant"});
+				}
 				sawWS = false;
 			}
 
-            if(firstChar === "*"){
-                selector = selector.substr(1);
+			if(firstChar === "*"){
+				selector = selector.substr(1);
 				tokens.push({type: "universal"});
 			} else if(firstChar in attribSelectors){
-                selector = selector.substr(1);
+				selector = selector.substr(1);
 				tokens.push({
 					type: "attribute",
 					name: attribSelectors[firstChar][0],
@@ -145,7 +146,7 @@ function parseSelector(subselects, selector, options){
 					ignoreCase: false
 				});
 			} else if(firstChar === "["){
-                selector = selector.substr(1);
+				selector = selector.substr(1);
 				data = selector.match(re_attr);
 				if(!data){
 					throw new SyntaxError("Malformed attribute selector: " + selector);
@@ -172,91 +173,95 @@ function parseSelector(subselects, selector, options){
 				});
 
 			} else if(firstChar === ":"){
-				//if(selector.charAt(1) === ":"){} //TODO pseudo-element
+				if(selector.charAt(1) === ":"){
+					selector = selector.substr(2);
+					tokens.push({type: "pseudo-element", name: getName().toLowerCase()});
+					continue;
+				}
 
-                selector = selector.substr(1);
+				selector = selector.substr(1);
 
 				name = getName().toLowerCase();
 				data = null;
 
 				if(selector.charAt(0) === "("){
-                    if(name in unpackPseudos){
-                        quot = selector.charAt(1);
-                        var quoted = quot in quotes;
+					if(name in unpackPseudos){
+						quot = selector.charAt(1);
+						var quoted = quot in quotes;
 
-                        selector = selector.substr(quoted + 1);
+						selector = selector.substr(quoted + 1);
 
-                        data = [];
-                        selector = parseSelector(data, selector, options);
+						data = [];
+						selector = parseSelector(data, selector, options);
 
-                        if(quoted){
-                            if(selector.charAt(0) !== quot){
-                                throw new SyntaxError("unmatched quotes in :" + name);
-                            } else {
-                                selector = selector.substr(1);
-                            }
-                        }
+						if(quoted){
+							if(selector.charAt(0) !== quot){
+								throw new SyntaxError("unmatched quotes in :" + name);
+							} else {
+								selector = selector.substr(1);
+							}
+						}
 
-                        if(selector.charAt(0) !== ")"){
-                            throw new SyntaxError("missing closing parenthesis in :" + name + " " + selector);
-                        }
+						if(selector.charAt(0) !== ")"){
+							throw new SyntaxError("missing closing parenthesis in :" + name + " " + selector);
+						}
 
-                        selector = selector.substr(1);
-                    } else {
-                        var pos = 1, counter = 1;
+						selector = selector.substr(1);
+					} else {
+						var pos = 1, counter = 1;
 
-                        for(; counter > 0 && pos < selector.length; pos++){
-                            if(selector.charAt(pos) === "(") counter++;
-                            else if(selector.charAt(pos) === ")") counter--;
-                        }
+						for(; counter > 0 && pos < selector.length; pos++){
+							if(selector.charAt(pos) === "(") counter++;
+							else if(selector.charAt(pos) === ")") counter--;
+						}
 
-                        if(counter){
-                            throw new SyntaxError("parenthesis not matched");
-                        }
+						if(counter){
+							throw new SyntaxError("parenthesis not matched");
+						}
 
-    					data = selector.substr(1, pos - 2);
-    					selector = selector.substr(pos);
+						data = selector.substr(1, pos - 2);
+						selector = selector.substr(pos);
 
-                        if(name in stripQuotesFromPseudos){
-                            quot = data.charAt(0);
+						if(name in stripQuotesFromPseudos){
+							quot = data.charAt(0);
 
-                        	if(quot === data.slice(-1) && quot in quotes){
-                        		data = data.slice(1, -1);
-                        	}
+							if(quot === data.slice(-1) && quot in quotes){
+								data = data.slice(1, -1);
+							}
 
-                            data = unescapeCSS(data);
-                        }
-                    }
+							data = unescapeCSS(data);
+						}
+					}
 				}
 
 				tokens.push({type: "pseudo", name: name, data: data});
 			} else if(re_name.test(selector)){
-    			name = getName();
-
-    			if(!options || ("lowerCaseTags" in options ? options.lowerCaseTags : !options.xmlMode)){
-    				name = name.toLowerCase();
-    			}
-
-    			tokens.push({type: "tag", name: name});
-    		} else {
-                if(tokens.length && tokens[tokens.length - 1].type === "descendant"){
-                    tokens.pop();
-                }
-                addToken(subselects, tokens);
-                return selector;
+				name = getName();
+
+				if(!options || ("lowerCaseTags" in options ? options.lowerCaseTags : !options.xmlMode)){
+					name = name.toLowerCase();
+				}
+
+				tokens.push({type: "tag", name: name});
+			} else {
+				if(tokens.length && tokens[tokens.length - 1].type === "descendant"){
+					tokens.pop();
+				}
+				addToken(subselects, tokens);
+				return selector;
 			}
 		}
 	}
 
-    addToken(subselects, tokens);
+	addToken(subselects, tokens);
 
 	return selector;
 }
 
 function addToken(subselects, tokens){
-    if(subselects.length > 0 && tokens.length === 0){
-    	throw new SyntaxError("empty sub-selector");
-    }
+	if(subselects.length > 0 && tokens.length === 0){
+		throw new SyntaxError("empty sub-selector");
+	}
 
 	subselects.push(tokens);
 }
diff --git a/package.json b/package.json
index 85b30e6..b55c98c 100644
--- a/package.json
+++ b/package.json
@@ -2,7 +2,7 @@
   "author": "Felix Böhm <me at feedic.com> (http://feedic.com)",
   "name": "css-what",
   "description": "a CSS selector parser",
-  "version": "2.0.2",
+  "version": "2.1.0",
   "repository": {
     "url": "https://github.com/fb55/css-what"
   },
diff --git a/tests/test.js b/tests/test.js
index aa34b70..30a9f65 100644
--- a/tests/test.js
+++ b/tests/test.js
@@ -353,6 +353,32 @@ var tests = [
 		],
 		"pseudo selector with data"
 	],
+	[
+		":icontains('')",
+		[
+			[
+				{
+					"type": "pseudo",
+					"name": "icontains",
+					"data": ""
+				}
+			]
+		],
+		"pseudo selector with quote-stripped data"
+	],
+	[
+		":contains(\"(foo)\")",
+		[
+			[
+				{
+					"type": "pseudo",
+					"name": "contains",
+					"data": "(foo)"
+				}
+			]
+		],
+		"pseudo selector with data"
+	],
 
 	//multiple selectors
 	[

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-css-what.git



More information about the Pkg-javascript-commits mailing list