Created
April 25, 2014 12:29
-
-
Save MiguelDebruyne/11287971 to your computer and use it in GitHub Desktop.
Sass: Debug function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Returns $list as a string | |
// ------------------------------------------------------------------------------- | |
// @documentation http://sassylists.com/documentation/#debug | |
// ------------------------------------------------------------------------------- | |
// @example debug(a b c d e) => [ a, b, c, d, e ] | |
// @example debug(a b (c d e)) => [ a, b, [ c, d, e ] ] | |
// ------------------------------------------------------------------------------- | |
// @param $list [List] : list | |
// @param $pre [Boolean] : enable/disable variables type and proper indentation | |
// @param $level [Number] : internal variable for recursivity | |
// ------------------------------------------------------------------------------- | |
// @return [String] | |
@function debug($list, $pre: false, $level: 1) { | |
$tab: " "; | |
$indent: ""; | |
$break: if($pre, "\A ", ""); | |
@if length($list) == 0 { | |
@return "( )"; | |
} | |
@if length($list) == 1 { | |
@return if($pre, "(" + type-of($list) + ") ", "") + $list; | |
} | |
@for $i from 1 to $level { | |
$indent: $indent + $tab; | |
} | |
$result: "[" + $break; | |
@for $i from 1 through length($list) { | |
$item: nth($list, $i); | |
$result: $result + if($pre, $indent + $tab, " "); | |
@if length($item) > 1 { | |
$result: $result | |
+ if($pre, "(list: " + length($item) + ") ", "") | |
+ debug($item, $pre, $level + 1); | |
} | |
@else { | |
@if $pre { | |
$result: $result + "(" + type-of($item) + ") "; | |
} | |
@if length($item) == 0 { | |
$result: $result + "( )"; | |
} | |
@else if type-of($item) == string { | |
$result: $result + quote($item); | |
} | |
@else if $item == null { | |
$result: $result + "null"; | |
} | |
@else { | |
$result: $result + $item; | |
} | |
} | |
@if $i != length($list) { | |
$result: $result + "," + $break; | |
} | |
} | |
$result: $result + $break + if($pre, if($level > 1, $indent, ""), " ") + "]"; | |
@return quote($result); | |
} | |
// Mixin displaying clean debug | |
// ------------------------------------------------------------------------------- | |
// @param $list [List] : list | |
@mixin debug($list) { | |
body:before { | |
content: debug($list, true) !important; | |
display: block !important; | |
margin: 1em !important; | |
padding: .5em !important; | |
background: #EFEFEF !important; | |
border: 1px solid #DDD !important; | |
border-radius: .2em !important; | |
color: #333 !important; | |
font: .75em/1.5 "Courier New", monospace !important; | |
text-shadow: 0 1px white !important; | |
white-space: pre-wrap !important; | |
} | |
} | |
/** | |
* Here is a cute list | |
*/ | |
$list: (a #BADA55 42, (false (yummy cupcake)), 14px, "gloubiboulga", (), null); | |
/** | |
* Awesome debug mixin is awesome | |
*/ | |
@include debug($list); | |
/** | |
* Simple debug function | |
*/ | |
body:after { | |
display: block; | |
content: debug($list); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment