From d2188bc2f34bef4bb35c566e0b496dd23813d70a Mon Sep 17 00:00:00 2001 From: Ian Mitchell Date: Wed, 27 Jul 2016 13:32:44 -0700 Subject: [PATCH] Adds support for Spaces --- dist/_include-media.scss | 37 ++++++++++++++++++++++++++++++++++- src/helpers/_first-index.scss | 19 ++++++++++++++++++ src/helpers/_parser.scss | 2 +- src/helpers/_trim.scss | 14 +++++++++++++ tests/parse-expression.scss | 7 ++++++- 5 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 src/helpers/_first-index.scss create mode 100644 src/helpers/_trim.scss diff --git a/dist/_include-media.scss b/dist/_include-media.scss index 7a65e01..648915f 100644 --- a/dist/_include-media.scss +++ b/dist/_include-media.scss @@ -171,6 +171,26 @@ $im-no-media-breakpoint: 'desktop' !default; /// $im-no-media-expressions: ('screen', 'portrait', 'landscape') !default; +/// +/// Find first char which is not a space +/// +/// @param {String} $string - String to search +/// @param {String} $direction ['left'] - Either 'left' or 'right' to indicate search direction +/// +/// @return {Number} - Index of first non-space character +/// +@function first-index($string, $direction: 'left') { + @for $i from 1 through str-length($string) { + $index: if($direction == 'left', $i, -$i); + + @if str-slice($string, $index, $index) != ' ' { + @return $index; + } + } + + @return 0; +} + //// /// Cross-engine logging engine /// @author Hugo Giraudel @@ -322,7 +342,7 @@ $im-no-media-expressions: ('screen', 'portrait', 'landscape') !default; /// @function get-expression-value($expression, $operator) { $operator-index: str-index($expression, $operator); - $value: str-slice($expression, $operator-index + str-length($operator)); + $value: trim(str-slice($expression, $operator-index + str-length($operator))); @if map-has-key($breakpoints, $value) { $value: map-get($breakpoints, $value); @@ -469,6 +489,21 @@ $im-no-media-expressions: ('screen', 'portrait', 'landscape') !default; @return $value * map-get($units, $unit); } +/// +/// Trim's a string by removing leading and trailing spaces +/// +/// @param {String} $string - String to trim +/// +/// @return {String} - String without spaces +/// +@function trim($string) { + @return str-slice( + $string, + first-index($string, 'left'), + first-index($string, 'right') + ); +} + /// /// This mixin aims at redefining the configuration just for the scope of /// the call. It is helpful when having a component needing an extended diff --git a/src/helpers/_first-index.scss b/src/helpers/_first-index.scss new file mode 100644 index 0000000..c920bb8 --- /dev/null +++ b/src/helpers/_first-index.scss @@ -0,0 +1,19 @@ +/// +/// Find first char which is not a space +/// +/// @param {String} $string - String to search +/// @param {String} $direction ['left'] - Either 'left' or 'right' to indicate search direction +/// +/// @return {Number} - Index of first non-space character +/// +@function first-index($string, $direction: 'left') { + @for $i from 1 through str-length($string) { + $index: if($direction == 'left', $i, -$i); + + @if str-slice($string, $index, $index) != ' ' { + @return $index; + } + } + + @return 0; +} diff --git a/src/helpers/_parser.scss b/src/helpers/_parser.scss index 28c906a..f205f9e 100644 --- a/src/helpers/_parser.scss +++ b/src/helpers/_parser.scss @@ -71,7 +71,7 @@ /// @function get-expression-value($expression, $operator) { $operator-index: str-index($expression, $operator); - $value: str-slice($expression, $operator-index + str-length($operator)); + $value: trim(str-slice($expression, $operator-index + str-length($operator))); @if map-has-key($breakpoints, $value) { $value: map-get($breakpoints, $value); diff --git a/src/helpers/_trim.scss b/src/helpers/_trim.scss new file mode 100644 index 0000000..03530af --- /dev/null +++ b/src/helpers/_trim.scss @@ -0,0 +1,14 @@ +/// +/// Trim's a string by removing leading and trailing spaces +/// +/// @param {String} $string - String to trim +/// +/// @return {String} - String without spaces +/// +@function trim($string) { + @return str-slice( + $string, + first-index($string, 'left'), + first-index($string, 'right') + ); +} diff --git a/tests/parse-expression.scss b/tests/parse-expression.scss index d59c436..19c0a5b 100644 --- a/tests/parse-expression.scss +++ b/tests/parse-expression.scss @@ -21,7 +21,12 @@ $tests-parse-expression: map-merge($media-expressions, ( '>=desktop': '(min-width: 1024px)', '≥desktop': '(min-width: 1024px)', '<=desktop': '(max-width: 1024px)', - '≤desktop': '(max-width: 1024px)' + '≤desktop': '(max-width: 1024px)', + + + '> phone': '(min-width: 321px)', + '>= tablet': '(min-width: 768px)', + '≥ desktop': '(min-width: 1024px)' )); @include run(test('parse-expression', $tests-parse-expression));