[Pkg-javascript-commits] [less.js] 24/88: detached rulesets

Jonas Smedegaard dr at jones.dk
Mon Oct 26 23:22:23 UTC 2015


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

js pushed a commit to annotated tag v1.7.0
in repository less.js.

commit f7414a1072094240c0b464ddf8f20d67e7cd88de
Author: Luke Page <luke.a.page at gmail.com>
Date:   Sun Feb 9 22:20:08 2014 +0000

    detached rulesets
---
 build/build.yml                  |  1 +
 lib/less/index.js                |  1 +
 lib/less/parser.js               | 59 ++++++++++++++++++++++++++++++----------
 lib/less/tree/ruleset-call.js    | 15 ++++++++++
 lib/less/tree/ruleset.js         | 13 +++++++++
 test/css/detached-rulesets.css   |  7 +++++
 test/less/detached-rulesets.less | 16 +++++++++++
 7 files changed, 97 insertions(+), 15 deletions(-)

diff --git a/build/build.yml b/build/build.yml
index dd4236b..68fa260 100644
--- a/build/build.yml
+++ b/build/build.yml
@@ -143,6 +143,7 @@ tree:
   - <%= build.lib %>/tree/quoted.js
   - <%= build.lib %>/tree/rule.js
   - <%= build.lib %>/tree/ruleset.js
+  - <%= build.lib %>/tree/ruleset-call.js
   - <%= build.lib %>/tree/selector.js
   - <%= build.lib %>/tree/unicode-descriptor.js
   - <%= build.lib %>/tree/url.js
diff --git a/lib/less/index.js b/lib/less/index.js
index 7aee17c..b85a84f 100644
--- a/lib/less/index.js
+++ b/lib/less/index.js
@@ -120,6 +120,7 @@ require('./tree/media');
 require('./tree/unicode-descriptor');
 require('./tree/negative');
 require('./tree/extend');
+require('./tree/ruleset-call');
 
 
 var isUrlRe = /^(?:https?:)?\/\//i;
diff --git a/lib/less/parser.js b/lib/less/parser.js
index 0f19d48..04330f7 100644
--- a/lib/less/parser.js
+++ b/lib/less/parser.js
@@ -729,7 +729,7 @@ less.Parser = function Parser(env) {
                 while (current)
                 {
                     node = this.extendRule() || mixin.definition() || this.rule() || this.ruleset() ||
-                        mixin.call() || this.comment() || this.directive();
+                        mixin.call() || this.comment() || this.rulesetCall() || this.directive();
                     if (node) {
                         root.push(node);
                     } else {
@@ -1028,6 +1028,19 @@ less.Parser = function Parser(env) {
             },
 
             //
+            // The variable part of a variable definition. Used in the `rule` parser
+            //
+            //     @fink();
+            //
+            rulesetCall: function () {
+                var name;
+
+                if (input.charAt(i) === '@' && (name = $re(/^(@[\w-]+)\s*\(\s*\)\s*;/))) { 
+                    return new tree.RulesetCall(name[1]); 
+                }
+            },
+
+            //
             // extend syntax - used to extend selectors
             //
             extend: function(isRule) {
@@ -1126,7 +1139,7 @@ less.Parser = function Parser(env) {
 
                     while (true) {
                         if (isCall) {
-                            arg = parsers.expression();
+                            arg = parsers.blockRuleset() || parsers.expression();
                         } else {
                             parsers.comments();
                             if (input.charAt(i) === '.' && $re(/^\.{3}/)) {
@@ -1154,7 +1167,7 @@ less.Parser = function Parser(env) {
 
                         if (isCall) {
                             // Variable
-                            if (arg.value.length == 1) {
+                            if (arg.value && arg.value.length == 1) {
                                 val = arg.value[0];
                             }
                         } else {
@@ -1442,6 +1455,14 @@ less.Parser = function Parser(env) {
                 }
             },
 
+            blockRuleset: function() {
+                var block = this.block();
+                if (block) {
+                    block = new tree.Ruleset(null, block);
+                }
+                return block;
+            },
+
             //
             // div, .class, body > p {...}
             //
@@ -1484,25 +1505,33 @@ less.Parser = function Parser(env) {
                 }
             },
             rule: function (tryAnonymous) {
-                var name, value, c = input.charAt(i), important, merge;
+                var name, value, c = input.charAt(i), important, merge, isVariable;
                 save();
 
                 if (c === '.' || c === '#' || c === '&') { return; }
 
                 name = this.variable() || this.ruleProperty();
                 if (name) {
-                    // prefer to try to parse first if its a variable or we are compressing
-                    // but always fallback on the other one
-                    value = !tryAnonymous && (env.compress || (name.charAt && (name.charAt(0) === '@'))) ?
-                        (this.value() || this.anonymousValue()) :
-                        (this.anonymousValue() || this.value());
-
-                    important = this.important();
+                    isVariable = typeof name === "string";
                     
-                    // a name returned by this.ruleProperty() is always an array of the form:
-                    // [string-1, ..., string-n, ""] or [string-1, ..., string-n, "+"]
-                    // where each item is a tree.Keyword or tree.Variable
-                    merge = name.pop && name.pop().value;
+                    if (isVariable) {
+                        value = this.blockRuleset();
+                    }
+                    
+                    if (!value) {
+                        // prefer to try to parse first if its a variable or we are compressing
+                        // but always fallback on the other one
+                        value = !tryAnonymous && (env.compress || isVariable) ?
+                            (this.value() || this.anonymousValue()) :
+                            (this.anonymousValue() || this.value());
+    
+                        important = this.important();
+                        
+                        // a name returned by this.ruleProperty() is always an array of the form:
+                        // [string-1, ..., string-n, ""] or [string-1, ..., string-n, "+"]
+                        // where each item is a tree.Keyword or tree.Variable
+                        merge = !isVariable && name.pop().value;
+                    }
 
                     if (value && this.end()) {
                         return new (tree.Rule)(name, value, important, merge, memo, env.currentFileInfo);
diff --git a/lib/less/tree/ruleset-call.js b/lib/less/tree/ruleset-call.js
new file mode 100644
index 0000000..ec9e98d
--- /dev/null
+++ b/lib/less/tree/ruleset-call.js
@@ -0,0 +1,15 @@
+(function (tree) {
+
+tree.RulesetCall = function (variable) {
+    this.variable = variable;
+};
+tree.RulesetCall.prototype = {
+    type: "RulesetCall",
+    accept: function (visitor) {
+    },
+    eval: function (env) {
+        return new(tree.Variable)(this.variable).eval(env);
+    }
+};
+
+})(require('../tree'));
diff --git a/lib/less/tree/ruleset.js b/lib/less/tree/ruleset.js
index e789576..15cc80d 100644
--- a/lib/less/tree/ruleset.js
+++ b/lib/less/tree/ruleset.js
@@ -90,6 +90,19 @@ tree.Ruleset.prototype = {
                 rsRuleCnt += rules.length - 1;
                 i += rules.length-1;
                 ruleset.resetCache();
+            } else if (rsRules[i] instanceof tree.RulesetCall) {
+                /*jshint loopfunc:true */
+                rules = rsRules[i].eval(env).rules.filter(function(r) {
+                    if ((r instanceof tree.Rule) && r.variable) {
+                        // do not pollute the scope at all
+                        return false;
+                    }
+                    return true;
+                });
+                rsRules.splice.apply(rsRules, [i, 1].concat(rules));
+                rsRuleCnt += rules.length - 1;
+                i += rules.length-1;
+                ruleset.resetCache();
             }
         }
 
diff --git a/test/css/detached-rulesets.css b/test/css/detached-rulesets.css
new file mode 100644
index 0000000..08e82bc
--- /dev/null
+++ b/test/css/detached-rulesets.css
@@ -0,0 +1,7 @@
+.wrap-selector {
+  color: black;
+}
+.wrap-selector {
+  color: black;
+  background: white;
+}
diff --git a/test/less/detached-rulesets.less b/test/less/detached-rulesets.less
new file mode 100644
index 0000000..4936076
--- /dev/null
+++ b/test/less/detached-rulesets.less
@@ -0,0 +1,16 @@
+ at ruleset: {
+     color: black;
+     background: white;
+  };
+
+.wrap-mixin(@ruleset) {
+  .wrap-selector {
+      @ruleset();
+    }
+};
+
+.wrap-mixin({
+  color: black;
+});
+
+.wrap-mixin(@ruleset);
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/less.js.git



More information about the Pkg-javascript-commits mailing list