diff --git a/stylesheets/_SassyLists.scss b/stylesheets/_SassyLists.scss index 96569bf..f134d87 100644 --- a/stylesheets/_SassyLists.scss +++ b/stylesheets/_SassyLists.scss @@ -4,6 +4,7 @@ // Repository: https://github.com/at-import/functions/ @import 'helpers/missing-dependencies'; +@import 'helpers/safe-call-function'; @import 'helpers/str-compare'; @import 'helpers/true'; @import 'helpers/is-number'; diff --git a/stylesheets/functions/_every.scss b/stylesheets/functions/_every.scss index 799cdc8..852d2b6 100644 --- a/stylesheets/functions/_every.scss +++ b/stylesheets/functions/_every.scss @@ -3,9 +3,9 @@ /// /// @ignore Documentation: http://at-import.github.io/SassyLists/documentation/#function-sl-every /// -/// @param {List} $list - list to run test against -/// @param {String} $function - function to run against every item from list -/// @param {ArgList} $args - extra arguments to pass to the function +/// @param {List} $list - list to run test against +/// @param {Function|String} $function - function or function name string to run against every item from list +/// @param {ArgList} $args - extra arguments to pass to the function /// /// @example /// sl-every(1 2 3, unitless) @@ -20,10 +20,10 @@ @function sl-every($list, $function, $args...) { @each $item in $list { - @if not call($function, $item, $args...) { + @if not sl-safe-call-function($function, $item, $args...) { @return false; - } + } } - + @return true; } diff --git a/stylesheets/functions/_some.scss b/stylesheets/functions/_some.scss index d6945b8..43eed23 100644 --- a/stylesheets/functions/_some.scss +++ b/stylesheets/functions/_some.scss @@ -3,9 +3,9 @@ /// /// @ignore Documentation: http://at-import.github.io/SassyLists/documentation/#function-sl-some /// -/// @param {List} $list - list to run test against -/// @param {String} $function - function to run against every item from list -/// @param {ArgList} $args - extra arguments to pass to the function +/// @param {List} $list - list to run test against +/// @param {Function|String} $function - function or function name string to run against every item from list +/// @param {ArgList} $args - extra arguments to pass to the function /// /// @example /// sl-some(1 2 3, unitless) @@ -21,13 +21,13 @@ /// /// @return {Bool} /// - + @function sl-some($list, $function, $args...) { @each $item in $list { - @if call($function, $item, $args...) { + @if sl-safe-call-function($function, $item, $args...) { @return true; - } + } } - + @return false; } diff --git a/stylesheets/functions/_walk.scss b/stylesheets/functions/_walk.scss index 23f9f17..3b78602 100644 --- a/stylesheets/functions/_walk.scss +++ b/stylesheets/functions/_walk.scss @@ -3,9 +3,9 @@ /// /// @ignore Documentation: http://at-import.github.io/SassyLists/documentation/#function-sl-walk /// -/// @param {List} $list - list to update -/// @param {String} $function - function to call on each value -/// @param {ArgList} $args - optional function arguments +/// @param {List} $list - list to update +/// @param {Function|String} $function - function or function name string to call on each value +/// @param {ArgList} $args - optional function arguments /// /// @throws There is no `$function` function for `sl-walk`. /// @@ -18,18 +18,18 @@ /// /// @return {List | Null} /// - + @function sl-walk($list, $function, $args...) { $_: sl-missing-dependencies('sl-to-map', 'sl-to-list'); - + @if not function-exists($function) { @error 'There is no `#{$function}` function for `sl-walk`.'; } @each $index, $value in sl-to-map($list) { $arguments: join($value, $args); - $list: set-nth($list, $index, call($function, $arguments...)); + $list: set-nth($list, $index, sl-safe-call-function($function, $arguments...)); } - + @return sl-to-list($list); } diff --git a/stylesheets/helpers/_safe-call-function.scss b/stylesheets/helpers/_safe-call-function.scss new file mode 100644 index 0000000..e3256d8 --- /dev/null +++ b/stylesheets/helpers/_safe-call-function.scss @@ -0,0 +1,28 @@ +/// +/// Polyfill for the `call` function supporting both functions and strings. +/// +/// In order to improve modular namespacing, LibSass 4 only accepts first-class +/// functions as argument so functions are called in their own context. +/// In most case, `get-function()` must only be used by the user in its own +/// context. It is used in this library to keep a maximum compatibility. +/// End developer must be encouraged to use first-class functions. +/// +/// @link http://oddbird.net/2017/03/30/safe-get +/// @link http://sass.logdown.com/posts/809572-sass-35-release-candidate +/// +/// @access private +/// +/// @param {Function|String} $func - Function or name of the function to call +/// @param {*} $args... - Arguments to call the function with +/// +/// @return {*} +/// +@function sl-safe-call-function($func, $args...) { + @if (function-exists('get-function') and type-of($func) == 'string') { + @warn '#{$func} function name was passed for call(). It is recommended to use `get-function()` and first-class functions instead. See http://oddbird.net/2017/03/30/safe-get'; + @return call(get-function($func), $args...); + } + @else { + @return call($func, $args...); + } +}