Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Unified Diff: node_modules/vulcanize/node_modules/whacko/node_modules/CSSselect/lib/compile.js

Issue 800513006: Added vulcanize under third_party/npm_modules (Closed) Base URL: https://chromium.googlesource.com/infra/third_party/npm_modules.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: node_modules/vulcanize/node_modules/whacko/node_modules/CSSselect/lib/compile.js
diff --git a/node_modules/vulcanize/node_modules/whacko/node_modules/CSSselect/lib/compile.js b/node_modules/vulcanize/node_modules/whacko/node_modules/CSSselect/lib/compile.js
new file mode 100644
index 0000000000000000000000000000000000000000..5c617d353f2782023346c03678cf784e3acf64f4
--- /dev/null
+++ b/node_modules/vulcanize/node_modules/whacko/node_modules/CSSselect/lib/compile.js
@@ -0,0 +1,84 @@
+/*
+ compiles a selector to an executable function
+*/
+
+module.exports = compile;
+module.exports.compileUnsafe = compileUnsafe;
+
+var parse = require("CSSwhat"),
+ DomUtils = require("domutils"),
+ isTag = DomUtils.isTag,
+ Rules = require("./general.js"),
+ sortRules = require("./sort.js"),
+ BaseFuncs = require("./basefunctions.js"),
+ trueFunc = BaseFuncs.trueFunc,
+ falseFunc = BaseFuncs.falseFunc;
+
+function compile(selector, options){
+ var next = compileUnsafe(selector, options);
+
+ return function base(elem){
+ return isTag(elem) && next(elem);
+ };
+}
+
+function compileUnsafe(selector, options){
+ return parse(selector, options)
+ .map(compileRules)
+ .reduce(reduceRules, falseFunc);
+}
+
+function compileRules(arr){
+ if(arr.length === 0) return falseFunc;
+ return sortRules(arr).reduce(function(func, rule){
+ if(func === falseFunc) return func;
+ return Rules[rule.type](func, rule);
+ }, trueFunc);
+}
+
+function reduceRules(a, b){
+ if(b === falseFunc || a === trueFunc){
+ return a;
+ }
+ if(a === falseFunc || b === trueFunc){
+ return b;
+ }
+
+ return function combine(elem){
+ return a(elem) || b(elem);
+ };
+}
+
+//:not and :has have to compile selectors
+//doing this in lib/pseudos.js would lead to circular dependencies,
+//so we add them here
+
+var Pseudos = require("./pseudos.js"),
+ filters = Pseudos.filters,
+ isParent = Pseudos.pseudos.parent,
+ existsOne = DomUtils.existsOne,
+ getChildren = DomUtils.getChildren;
+
+filters.not = function(next, select){
+ var func = compileUnsafe(select);
+
+ if(func === falseFunc) return next;
+ if(func === trueFunc) return falseFunc;
+
+ return function(elem){
+ return !func(elem) && next(elem);
+ };
+};
+
+filters.has = function(next, selector){
+ var func = compile(selector);
+
+ if(func === falseFunc) return falseFunc;
+ if(func === trueFunc) return function(elem){
+ return isParent(elem) && next(elem);
+ };
+
+ return function has(elem){
+ return next(elem) && existsOne(func, getChildren(elem));
+ };
+};

Powered by Google App Engine
This is Rietveld 408576698