diff --git a/lib/less/browser.js b/lib/less/browser.js index ae2667d97..cba4c3b61 100644 --- a/lib/less/browser.js +++ b/lib/less/browser.js @@ -101,8 +101,14 @@ function loadStyles() { for (var i = 0; i < styles.length; i++) { if (styles[i].type.match(typePattern)) { new(less.Parser)().parse(styles[i].innerHTML || '', function (e, tree) { - styles[i].type = 'text/css'; - styles[i].innerHTML = tree.toCSS(); + var css = tree.toCSS(); + var style = styles[i]; + try { + style.innerHTML = css; + } catch (_) { + style.styleSheets.cssText = css; + } + style.type = 'text/css'; }); } } diff --git a/lib/less/functions.js b/lib/less/functions.js index 804b9e6f9..2889f112f 100644 --- a/lib/less/functions.js +++ b/lib/less/functions.js @@ -147,6 +147,10 @@ tree.functions = { message: "math functions take numbers as parameters" }; } + }, + argb: function (color) { + return new(tree.Anonymous)(color.toARGB()); + } }; diff --git a/lib/less/tree/color.js b/lib/less/tree/color.js index 38d34f85a..bb7646a5c 100644 --- a/lib/less/tree/color.js +++ b/lib/less/tree/color.js @@ -15,11 +15,6 @@ tree.Color = function (rgb, a) { this.rgb = rgb.match(/.{2}/g).map(function (c) { return parseInt(c, 16); }); - } else if (rgb.length == 8) { - this.alpha = parseInt(rgb.substring(0,2), 16) / 255.0; - this.rgb = rgb.substr(2).match(/.{2}/g).map(function (c) { - return parseInt(c, 16); - }); } else { this.rgb = rgb.split('').map(function (c) { return parseInt(c + c, 16); @@ -91,6 +86,14 @@ tree.Color.prototype = { h /= 6; } return { h: h * 360, s: s, l: l, a: a }; + }, + toARGB: function () { + var argb = [Math.round(this.alpha * 255)].concat(this.rgb); + return '#' + argb.map(function (i) { + i = Math.round(i); + i = (i > 255 ? 255 : (i < 0 ? 0 : i)).toString(16); + return i.length === 1 ? '0' + i : i; + }).join(''); } }; diff --git a/test/css/colors.css b/test/css/colors.css index 373781df3..540f9b065 100644 --- a/test/css/colors.css +++ b/test/css/colors.css @@ -7,6 +7,9 @@ #yelow #rgba { color: rgba(255, 238, 170, 0.1); } +#yelow #argb { + color: #1affeeaa; +} #blue #short { color: #00f; } @@ -16,6 +19,9 @@ #blue #rgba { color: rgba(0, 0, 255, 0.1); } +#blue #argb { + color: #1a0000ff; +} #alpha #hsla { color: rgba(61, 45, 41, 0.6); } diff --git a/test/less/colors.less b/test/less/colors.less index 5c90bcbb5..5744e1677 100644 --- a/test/less/colors.less +++ b/test/less/colors.less @@ -8,6 +8,9 @@ #rgba { color: rgba(255, 238, 170, 0.1); } + #argb { + color: argb(rgba(255, 238, 170, 0.1)); + } } #blue { @@ -20,6 +23,9 @@ #rgba { color: rgba(0, 0, 255, 0.1); } + #argb { + color: argb(rgba(0, 0, 255, 0.1)); + } } #alpha #hsla {