From 39f669e83cc928c2d6af2c8ee86c5cd3f9c95d07 Mon Sep 17 00:00:00 2001 From: Luke Page Date: Thu, 21 Feb 2013 18:16:04 +0000 Subject: [PATCH] Add import visitor --- Makefile | 2 ++ lib/less/import-visitor.js | 17 +++++++++++++++++ lib/less/index.js | 1 + lib/less/parser.js | 2 ++ lib/less/tree/extend.js | 2 +- lib/less/visitor.js | 6 +++--- 6 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 lib/less/import-visitor.js diff --git a/Makefile b/Makefile index 3e7f891c..219882d0 100644 --- a/Makefile +++ b/Makefile @@ -40,6 +40,7 @@ less: ${SRC}/tree.js\ ${SRC}/env.js\ ${SRC}/visitor.js\ + ${SRC}/import-visitor.js\ ${SRC}/browser.js\ build/amd.js >> ${DIST} @@echo "})(window);" >> ${DIST} @@ -62,6 +63,7 @@ rhino: ${SRC}/parser.js\ ${SRC}/env.js\ ${SRC}/visitor.js\ + ${SRC}/import-visitor.js\ ${SRC}/functions.js\ ${SRC}/colors.js\ ${SRC}/tree/*.js\ diff --git a/lib/less/import-visitor.js b/lib/less/import-visitor.js new file mode 100644 index 00000000..3f58d980 --- /dev/null +++ b/lib/less/import-visitor.js @@ -0,0 +1,17 @@ +(function (tree) { + tree.importVisitor = function(root) { + this._visitor = new tree.visitor(this); + this._visitor.visit(root); + }; + + tree.importVisitor.prototype = { + visitImport: function (importNode, visitArgs) { + + return importNode; + }, + visitRule: function (ruleNode, visitArgs) { + visitArgs.visitDeeper = false; + return ruleNode; + } + }; +})(require('./tree')); \ No newline at end of file diff --git a/lib/less/index.js b/lib/less/index.js index 3199ba50..7acd2aa6 100644 --- a/lib/less/index.js +++ b/lib/less/index.js @@ -214,5 +214,6 @@ require('./env'); require('./functions'); require('./colors'); require('./visitor.js'); +require('./import-visitor.js'); for (var k in less) { exports[k] = less[k]; } diff --git a/lib/less/parser.js b/lib/less/parser.js index 4abf0e6d..14f063fc 100644 --- a/lib/less/parser.js +++ b/lib/less/parser.js @@ -467,6 +467,8 @@ less.Parser = function Parser(env) { ] }; } + + new tree.importVisitor(root); finish = function (e) { e = error || e || parser.imports.error; diff --git a/lib/less/tree/extend.js b/lib/less/tree/extend.js index 2ea5cc04..25e8a2aa 100644 --- a/lib/less/tree/extend.js +++ b/lib/less/tree/extend.js @@ -9,7 +9,7 @@ tree.Extend = function Extend(elements, option, index) { tree.Extend.prototype = { type: "Extend", accept: function (visitor) { - this.selector = visitor.visit(this.ruleset); + this.selector = visitor.visit(this.selector); }, eval: function (env, selectors) { var selfSelectors = findSelfSelectors(selectors || env.selectors), diff --git a/lib/less/visitor.js b/lib/less/visitor.js index b7b2afed..634badf6 100644 --- a/lib/less/visitor.js +++ b/lib/less/visitor.js @@ -20,15 +20,15 @@ visitArgs; if (func) { visitArgs = {visitDeeper: true}; - node = func(node); + node = func(node, visitArgs); } - if ((!visitArgs || visitArgs.visitDeeper) && node.accept) { + if ((!visitArgs || visitArgs.visitDeeper) && node && node.accept) { node.accept(this); } return node; }, visitArray: function(nodes) { - var i, newNodes; + var i, newNodes = []; for(i = 0; i < nodes.length; i++) { var evald = this.visit(nodes[i]); if (evald instanceof Array) {