[Pkg-javascript-commits] [less.js] 21/88: Added support for variables in certain at-rules (keyframes, namespace, charset).
Jonas Smedegaard
dr at jones.dk
Mon Oct 26 23:22:22 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 09c4311594f4be0c6ecb0be71bc5d3cb9fa83fd8
Author: seven-phases-max <seven.phases.max at gmail.com>
Date: Sun Feb 9 13:42:01 2014 +0400
Added support for variables in certain at-rules (keyframes, namespace, charset).
---
lib/less/parser.js | 58 +++++++++++++++-----------
lib/less/tree/directive.js | 62 +++++++++++++++-------------
test/css/debug/linenumbers-all.css | 4 +-
test/css/debug/linenumbers-comments.css | 2 +-
test/css/debug/linenumbers-mediaquery.css | 2 +-
test/css/variables-in-at-rules.css | 18 ++++++++
test/less/errors/at-rules-undefined-var.less | 4 ++
test/less/errors/at-rules-undefined-var.txt | 4 ++
test/less/variables-in-at-rules.less | 20 +++++++++
9 files changed, 118 insertions(+), 56 deletions(-)
diff --git a/lib/less/parser.js b/lib/less/parser.js
index 0f19d48..b88270f 100644
--- a/lib/less/parser.js
+++ b/lib/less/parser.js
@@ -1662,7 +1662,7 @@ less.Parser = function Parser(env) {
//
directive: function () {
var index = i, name, value, rules, nonVendorSpecificName,
- hasBlock, hasIdentifier, hasExpression, identifier;
+ hasIdentifier, hasExpression, hasUnknown, hasBlock = true;
if (input.charAt(i) !== '@') { return; }
@@ -1683,9 +1683,8 @@ less.Parser = function Parser(env) {
}
switch(nonVendorSpecificName) {
+ /*
case "@font-face":
- hasBlock = true;
- break;
case "@viewport":
case "@top-left":
case "@top-left-corner":
@@ -1705,42 +1704,55 @@ less.Parser = function Parser(env) {
case "@right-bottom":
hasBlock = true;
break;
- case "@host":
- case "@page":
- case "@document":
- case "@supports":
- case "@keyframes":
- hasBlock = true;
+ */
+ case "@charset":
hasIdentifier = true;
+ hasBlock = false;
break;
case "@namespace":
hasExpression = true;
+ hasBlock = false;
+ break;
+ case "@keyframes":
+ hasIdentifier = true;
+ break;
+ case "@host":
+ case "@page":
+ case "@document":
+ case "@supports":
+ hasUnknown = true;
break;
}
if (hasIdentifier) {
- identifier = ($re(/^[^{]+/) || '').trim();
- if (identifier) {
- name += " " + identifier;
+ value = this.entity();
+ if (!value) {
+ error("expected " + name + " identifier");
+ }
+ } else if (hasExpression) {
+ value = this.expression();
+ if (!value) {
+ error("expected " + name + " expression");
+ }
+ } else if (hasUnknown) {
+ value = ($re(/^[^{;]+/) || '').trim();
+ if (value) {
+ value = new(tree.Anonymous)(value);
}
}
if (hasBlock) {
rules = this.block();
- if (rules) {
- return new(tree.Directive)(name, rules, index, env.currentFileInfo);
- }
- } else {
- value = hasExpression ? this.expression() : this.entity();
- if (value && $char(';')) {
- var directive = new(tree.Directive)(name, value, index, env.currentFileInfo);
- if (env.dumpLineNumbers) {
- directive.debugInfo = getDebugInfo(i, input, env);
- }
- return directive;
+ if (rules) {
+ rules = new(tree.Ruleset)(null, rules);
}
}
+ if (rules || (!hasBlock && value && $char(';'))) {
+ return new(tree.Directive)(name, value, rules, index, env.currentFileInfo,
+ env.dumpLineNumbers ? getDebugInfo(index, input, env) : null);
+ }
+
restore();
},
diff --git a/lib/less/tree/directive.js b/lib/less/tree/directive.js
index a4404bd..b59a16b 100644
--- a/lib/less/tree/directive.js
+++ b/lib/less/tree/directive.js
@@ -1,58 +1,62 @@
(function (tree) {
-tree.Directive = function (name, value, index, currentFileInfo) {
- this.name = name;
-
- if (Array.isArray(value)) {
- this.rules = [new(tree.Ruleset)(null, value)];
- this.rules[0].allowImports = true;
- } else {
- this.value = value;
+tree.Directive = function (name, value, rules, index, currentFileInfo, debugInfo) {
+ this.name = name;
+ this.value = value;
+ if (rules) {
+ this.rules = rules;
+ this.rules.allowImports = true;
}
this.index = index;
this.currentFileInfo = currentFileInfo;
-
+ this.debugInfo = debugInfo;
};
+
tree.Directive.prototype = {
type: "Directive",
accept: function (visitor) {
- if (this.rules) {
- this.rules = visitor.visitArray(this.rules);
+ var value = this.value, rules = this.rules;
+ if (rules) {
+ rules = visitor.visit(rules);
}
- if (this.value) {
- this.value = visitor.visit(this.value);
+ if (value) {
+ value = visitor.visit(value);
}
},
genCSS: function (env, output) {
+ var value = this.value, rules = this.rules;
output.add(this.name, this.currentFileInfo, this.index);
- if (this.rules) {
- tree.outputRuleset(env, output, this.rules);
- } else {
+ if (value) {
output.add(' ');
- this.value.genCSS(env, output);
+ value.genCSS(env, output);
+ }
+ if (rules) {
+ tree.outputRuleset(env, output, [rules]);
+ } else {
output.add(';');
}
},
toCSS: tree.toCSS,
eval: function (env) {
- var evaldDirective = this;
- if (this.rules) {
- env.frames.unshift(this);
- evaldDirective = new(tree.Directive)(this.name, null, this.index, this.currentFileInfo);
- evaldDirective.rules = [this.rules[0].eval(env)];
- evaldDirective.rules[0].root = true;
- env.frames.shift();
+ var value = this.value, rules = this.rules;
+ if (value) {
+ value = value.eval(env);
+ }
+ if (rules) {
+ rules = rules.eval(env);
+ rules.root = true;
}
- return evaldDirective;
+ return new(tree.Directive)(this.name, value, rules,
+ this.index, this.currentFileInfo, this.debugInfo);
},
- variable: function (name) { return tree.Ruleset.prototype.variable.call(this.rules[0], name); },
- find: function () { return tree.Ruleset.prototype.find.apply(this.rules[0], arguments); },
- rulesets: function () { return tree.Ruleset.prototype.rulesets.apply(this.rules[0]); },
+ variable: function (name) { if (this.rules) return tree.Ruleset.prototype.variable.call(this.rules, name); },
+ find: function () { if (this.rules) return tree.Ruleset.prototype.find.apply(this.rules, arguments); },
+ rulesets: function () { if (this.rules) return tree.Ruleset.prototype.rulesets.apply(this.rules); },
markReferenced: function () {
var i, rules;
this.isReferenced = true;
if (this.rules) {
- rules = this.rules[0].rules;
+ rules = this.rules.rules;
for (i = 0; i < rules.length; i++) {
if (rules[i].markReferenced) {
rules[i].markReferenced();
diff --git a/test/css/debug/linenumbers-all.css b/test/css/debug/linenumbers-all.css
index bf15631..87022ae 100644
--- a/test/css/debug/linenumbers-all.css
+++ b/test/css/debug/linenumbers-all.css
@@ -1,6 +1,6 @@
@charset "UTF-8";
-/* line 3, {pathimport}test.less */
- at media -sass-debug-info{filename{font-family:file\:\/\/{pathimportesc}test\.less}line{font-family:\000033}}
+/* line 1, {pathimport}test.less */
+ at media -sass-debug-info{filename{font-family:file\:\/\/{pathimportesc}test\.less}line{font-family:\000031}}
/* @charset "ISO-8859-1"; */
/* line 23, {pathimport}test.less */
@media -sass-debug-info{filename{font-family:file\:\/\/{pathimportesc}test\.less}line{font-family:\0000323}}
diff --git a/test/css/debug/linenumbers-comments.css b/test/css/debug/linenumbers-comments.css
index ddeb0c4..e5d6bb3 100644
--- a/test/css/debug/linenumbers-comments.css
+++ b/test/css/debug/linenumbers-comments.css
@@ -1,5 +1,5 @@
@charset "UTF-8";
-/* line 3, {pathimport}test.less */
+/* line 1, {pathimport}test.less */
/* @charset "ISO-8859-1"; */
/* line 23, {pathimport}test.less */
.tst3 {
diff --git a/test/css/debug/linenumbers-mediaquery.css b/test/css/debug/linenumbers-mediaquery.css
index 9d272f4..e252ab3 100644
--- a/test/css/debug/linenumbers-mediaquery.css
+++ b/test/css/debug/linenumbers-mediaquery.css
@@ -1,5 +1,5 @@
@charset "UTF-8";
- at media -sass-debug-info{filename{font-family:file\:\/\/{pathimportesc}test\.less}line{font-family:\000033}}
+ at media -sass-debug-info{filename{font-family:file\:\/\/{pathimportesc}test\.less}line{font-family:\000031}}
/* @charset "ISO-8859-1"; */
@media -sass-debug-info{filename{font-family:file\:\/\/{pathimportesc}test\.less}line{font-family:\0000323}}
.tst3 {
diff --git a/test/css/variables-in-at-rules.css b/test/css/variables-in-at-rules.css
new file mode 100644
index 0000000..0327eb1
--- /dev/null
+++ b/test/css/variables-in-at-rules.css
@@ -0,0 +1,18 @@
+ at charset "UTF-8";
+ at namespace less "http://lesscss.org";
+ at keyframes enlarger {
+ from {
+ font-size: 12px;
+ }
+ to {
+ font-size: 15px;
+ }
+}
+ at -webkit-keyframes reducer {
+ from {
+ font-size: 13px;
+ }
+ to {
+ font-size: 10px;
+ }
+}
diff --git a/test/less/errors/at-rules-undefined-var.less b/test/less/errors/at-rules-undefined-var.less
new file mode 100644
index 0000000..a147380
--- /dev/null
+++ b/test/less/errors/at-rules-undefined-var.less
@@ -0,0 +1,4 @@
+
+ at keyframes @name {
+ 50% {width: 20px;}
+}
diff --git a/test/less/errors/at-rules-undefined-var.txt b/test/less/errors/at-rules-undefined-var.txt
new file mode 100644
index 0000000..48ca57c
--- /dev/null
+++ b/test/less/errors/at-rules-undefined-var.txt
@@ -0,0 +1,4 @@
+NameError: variable @name is undefined in {path}at-rules-undefined-var.less on line 2, column 12:
+1
+2 @keyframes @name {
+3 50% {width: 20px;}
diff --git a/test/less/variables-in-at-rules.less b/test/less/variables-in-at-rules.less
new file mode 100644
index 0000000..96d8c61
--- /dev/null
+++ b/test/less/variables-in-at-rules.less
@@ -0,0 +1,20 @@
+
+ at Eight: 8;
+ at charset "UTF-@{Eight}";
+
+ at ns: less;
+ at namespace @ns "http://lesscss.org";
+
+ at name: enlarger;
+ at keyframes @name {
+ from {font-size: 12px;}
+ to {font-size: 15px;}
+}
+
+.m(reducer);
+.m(@name) {
+ @-webkit-keyframes @name {
+ from {font-size: 13px;}
+ to {font-size: 10px;}
+ }
+}
--
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