From f1d49c299b92e6ef55d8fbc7c267f0fa08dbd94a Mon Sep 17 00:00:00 2001 From: ScottRippey Date: Tue, 8 Jan 2013 15:59:39 -0800 Subject: [PATCH 1/6] Added 4 new functions (top right bottom left) for extracting padding values from a variable --- lib/less/functions.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/less/functions.js b/lib/less/functions.js index b077123e6..0581d371e 100644 --- a/lib/less/functions.js +++ b/lib/less/functions.js @@ -342,7 +342,34 @@ tree.functions = { }, shade: function(color, amount) { return this.mix(this.rgb(0, 0, 0), color, amount); - } + }, + + /* Shorthand Extraction */ + + top: function(n) { + return this._extractPadding('top', n); + }, + right: function(n) { + return this._extractPadding('right', n); + }, + bottom: function(n) { + return this._extractPadding('bottom', n); + }, + left: function(n) { + return this._extractPadding('left', n); + }, + _extractPadding: function(property, n) { + if (n instanceof tree.Dimension) { + return n; + } else if (n && typeof(n.length) === 'number') { + // Extract the padding property from the shorthand value: + var index = ['top', 'right', 'bottom', 'left'].indexOf(property); + return n[index % n.length]; + } else { + throw { type: "Argument", message: "argument must be a Dimension" }; + } + } + }; function hsla(color) { From 3a7285416ad338ee50173bda8ed5f6baf4d04412 Mon Sep 17 00:00:00 2001 From: ScottRippey Date: Tue, 8 Jan 2013 16:04:34 -0800 Subject: [PATCH 2/6] Normalized whitespace --- lib/less/functions.js | 50 +++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/lib/less/functions.js b/lib/less/functions.js index 0581d371e..b70e92459 100644 --- a/lib/less/functions.js +++ b/lib/less/functions.js @@ -344,31 +344,31 @@ tree.functions = { return this.mix(this.rgb(0, 0, 0), color, amount); }, - /* Shorthand Extraction */ - - top: function(n) { - return this._extractPadding('top', n); - }, - right: function(n) { - return this._extractPadding('right', n); - }, - bottom: function(n) { - return this._extractPadding('bottom', n); - }, - left: function(n) { - return this._extractPadding('left', n); - }, - _extractPadding: function(property, n) { - if (n instanceof tree.Dimension) { - return n; - } else if (n && typeof(n.length) === 'number') { - // Extract the padding property from the shorthand value: - var index = ['top', 'right', 'bottom', 'left'].indexOf(property); - return n[index % n.length]; - } else { - throw { type: "Argument", message: "argument must be a Dimension" }; - } - } + /* Shorthand Extraction */ + + top: function(n) { + return this._extractPadding('top', n); + }, + right: function(n) { + return this._extractPadding('right', n); + }, + bottom: function(n) { + return this._extractPadding('bottom', n); + }, + left: function(n) { + return this._extractPadding('left', n); + }, + _extractPadding: function(property, n) { + if (n instanceof tree.Dimension) { + return n; + } else if (n && typeof(n.length) === 'number') { + // Extract the padding property from the shorthand value: + var index = ['top', 'right', 'bottom', 'left'].indexOf(property); + return n[index % n.length]; + } else { + throw { type: "Argument", message: "argument must be a Dimension" }; + } + } }; From df2d72bcd399a0971fae3dd45799cf9cb715540b Mon Sep 17 00:00:00 2001 From: ScottRippey Date: Tue, 8 Jan 2013 16:15:44 -0800 Subject: [PATCH 3/6] Added unit tests for padding extraction --- test/css/functions.css | 18 ++++++++++++++++++ test/less/functions.less | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/test/css/functions.css b/test/css/functions.css index bf612d4e5..a291af86a 100644 --- a/test/css/functions.css +++ b/test/css/functions.css @@ -84,6 +84,24 @@ percent: true; em: true; } +#built-in .padding-extract { + top: 1px; + right: 2px; + bottom: 3px; + left: 4px; + top: 1px; + right: 2px; + bottom: 3px; + left: 2px; + top: 1px; + right: 2px; + bottom: 1px; + left: 2px; + top: 1px; + right: 1px; + bottom: 1px; + left: 1px; +} #alpha { alpha: rgba(153, 94, 51, 0.6); } diff --git a/test/less/functions.less b/test/less/functions.less index 8b80cccc1..9464eccc0 100644 --- a/test/less/functions.less +++ b/test/less/functions.less @@ -92,6 +92,24 @@ percent: ispercentage(32%); em: isem(32em); } + + .padding-extract { + // Four: + @pad: 1px 2px 3px 4px; + top: top(@pad); right: right(@pad); bottom: bottom(@pad); left: left(@pad); + + // Three: + @pad: 1px 2px 3px; + top: top(@pad); right: right(@pad); bottom: bottom(@pad); left: left(@pad); + + // Two: + @pad: 1px 2px; + top: top(@pad); right: right(@pad); bottom: bottom(@pad); left: left(@pad); + + // One: + @pad: 1px; + top: top(@pad); right: right(@pad); bottom: bottom(@pad); left: left(@pad); + } } #alpha { From f831017d06816c6f49b22405e482255aee6ab715 Mon Sep 17 00:00:00 2001 From: ScottRippey Date: Tue, 8 Jan 2013 16:28:06 -0800 Subject: [PATCH 4/6] Separated #padding-extraction unit tests --- test/css/functions.css | 30 ++++++++++++------------- test/less/functions.less | 48 +++++++++++++++++++++++++--------------- 2 files changed, 45 insertions(+), 33 deletions(-) diff --git a/test/css/functions.css b/test/css/functions.css index a291af86a..0878c5d13 100644 --- a/test/css/functions.css +++ b/test/css/functions.css @@ -84,7 +84,21 @@ percent: true; em: true; } -#built-in .padding-extract { +#alpha { + alpha: rgba(153, 94, 51, 0.6); +} +#blendmodes { + multiply: #ed0000; + screen: #f600f6; + overlay: #ed0000; + softlight: #ff0000; + hardlight: #0000ed; + difference: #f600f6; + exclusion: #f600f6; + average: #7b007b; + negation: #d73131; +} +#padding-extraction { top: 1px; right: 2px; bottom: 3px; @@ -102,17 +116,3 @@ bottom: 1px; left: 1px; } -#alpha { - alpha: rgba(153, 94, 51, 0.6); -} -#blendmodes { - multiply: #ed0000; - screen: #f600f6; - overlay: #ed0000; - softlight: #ff0000; - hardlight: #0000ed; - difference: #f600f6; - exclusion: #f600f6; - average: #7b007b; - negation: #d73131; -} diff --git a/test/less/functions.less b/test/less/functions.less index 9464eccc0..fe11fdf45 100644 --- a/test/less/functions.less +++ b/test/less/functions.less @@ -92,24 +92,6 @@ percent: ispercentage(32%); em: isem(32em); } - - .padding-extract { - // Four: - @pad: 1px 2px 3px 4px; - top: top(@pad); right: right(@pad); bottom: bottom(@pad); left: left(@pad); - - // Three: - @pad: 1px 2px 3px; - top: top(@pad); right: right(@pad); bottom: bottom(@pad); left: left(@pad); - - // Two: - @pad: 1px 2px; - top: top(@pad); right: right(@pad); bottom: bottom(@pad); left: left(@pad); - - // One: - @pad: 1px; - top: top(@pad); right: right(@pad); bottom: bottom(@pad); left: left(@pad); - } } #alpha { @@ -127,3 +109,33 @@ average: average(#f60000, #0000f6); negation: negation(#f60000, #313131); } + +#padding-extraction { + // Four: + @pad: 1px 2px 3px 4px; + top: top(@pad); + right: right(@pad); + bottom: bottom(@pad); + left: left(@pad); + + // Three: + @pad: 1px 2px 3px; + top: top(@pad); + right: right(@pad); + bottom: bottom(@pad); + left: left(@pad); + + // Two: + @pad: 1px 2px; + top: top(@pad); + right: right(@pad); + bottom: bottom(@pad); + left: left(@pad); + + // One: + @pad: 1px; + top: top(@pad); + right: right(@pad); + bottom: bottom(@pad); + left: left(@pad); +} From dc488a7928511991b56b17d82e2d33b0438ab9f7 Mon Sep 17 00:00:00 2001 From: ScottRippey Date: Tue, 8 Jan 2013 16:33:09 -0800 Subject: [PATCH 5/6] Fixed the special case of "left(1px 2px 1px)" which should return "2px" --- lib/less/functions.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/less/functions.js b/lib/less/functions.js index b70e92459..342601b59 100644 --- a/lib/less/functions.js +++ b/lib/less/functions.js @@ -364,7 +364,12 @@ tree.functions = { } else if (n && typeof(n.length) === 'number') { // Extract the padding property from the shorthand value: var index = ['top', 'right', 'bottom', 'left'].indexOf(property); - return n[index % n.length]; + if (n.length === 3 && index === 3) { + // Special case + return n[1]; + } else { + return n[index % n.length]; + } } else { throw { type: "Argument", message: "argument must be a Dimension" }; } From 29c5528c10e935b099e2e8e161ada5458535b271 Mon Sep 17 00:00:00 2001 From: ScottRippey Date: Tue, 8 Jan 2013 16:34:10 -0800 Subject: [PATCH 6/6] Fixed the special case of "left(1px 2px 1px)" which should return "2px" - (improved comment) --- lib/less/functions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/less/functions.js b/lib/less/functions.js index 342601b59..f847b5e12 100644 --- a/lib/less/functions.js +++ b/lib/less/functions.js @@ -365,7 +365,7 @@ tree.functions = { // Extract the padding property from the shorthand value: var index = ['top', 'right', 'bottom', 'left'].indexOf(property); if (n.length === 3 && index === 3) { - // Special case + // left(1px 2px 3px) should return "2px"; this is the only case where the math would be complicated return n[1]; } else { return n[index % n.length];