From 5ec708c3f4674e6e3cfec28bb4f733a8a0e69b9a Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Tue, 3 Jan 2012 18:45:31 +0100 Subject: [PATCH] type-checking stylesheet functions is-string, is-keyword, is-color, is-number --- lib/less/functions.js | 15 +++++++++++++++ lib/less/tree/keyword.js | 3 +++ test/css/functions.css | 8 ++++++++ test/less/functions.less | 9 +++++++++ 4 files changed, 35 insertions(+) diff --git a/lib/less/functions.js b/lib/less/functions.js index 4a2a6fb9..f93a5c4b 100644 --- a/lib/less/functions.js +++ b/lib/less/functions.js @@ -174,6 +174,21 @@ tree.functions = { } else { throw { type: "Argument", message: "argument must be a string" }; } + }, + 'is-color': function (n) { + return this._isa(n, tree.Color); + }, + 'is-number': function (n) { + return this._isa(n, tree.Dimension); + }, + 'is-string': function (n) { + return this._isa(n, tree.Quoted); + }, + 'is-keyword': function (n) { + return this._isa(n, tree.Keyword); + }, + _isa: function (n, Type) { + return (n instanceof Type) ? tree.True : tree.False; } }; diff --git a/lib/less/tree/keyword.js b/lib/less/tree/keyword.js index 2a703fc6..701b79e5 100644 --- a/lib/less/tree/keyword.js +++ b/lib/less/tree/keyword.js @@ -13,4 +13,7 @@ tree.Keyword.prototype = { } }; +tree.True = new(tree.Keyword)('true'); +tree.False = new(tree.Keyword)('false'); + })(require('../tree')); diff --git a/test/css/functions.css b/test/css/functions.css index 4eee191f..f823a992 100644 --- a/test/css/functions.css +++ b/test/css/functions.css @@ -27,6 +27,14 @@ percentage: 20%; color: #ff0011; } +#built-in .is-a { + color: true; + color: true; + color: true; + keyword: true; + number: true; + string: true; +} #alpha { alpha: rgba(153, 94, 51, 0.6); } diff --git a/test/less/functions.less b/test/less/functions.less index 2045a85d..44f4242c 100644 --- a/test/less/functions.less +++ b/test/less/functions.less @@ -30,6 +30,15 @@ roundedpx: round(10px / 3); percentage: percentage(10px / 50); color: color("#ff0011"); + + .is-a { + color: is-color(#ddd); + color: is-color(red); + color: is-color(rgb(0, 0, 0)); + keyword: is-keyword(hello); + number: is-number(32); + string: is-string("hello"); + } } #alpha {