From 15d95fbb29785e6eeb5efcf02586542680b22ca6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Kr=CC=8Ci=CC=81z=CC=8C?= Date: Fri, 24 Apr 2015 11:02:10 +0200 Subject: [PATCH] Add support for defines with hash values --- lib/stylus/runtime/compiler.js | 13 +++++++----- spec/stylesheets/definition_hash.styl | 8 +++++++ spec/stylus_spec.rb | 30 +++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 spec/stylesheets/definition_hash.styl diff --git a/lib/stylus/runtime/compiler.js b/lib/stylus/runtime/compiler.js index bec09c9..81a5c42 100644 --- a/lib/stylus/runtime/compiler.js +++ b/lib/stylus/runtime/compiler.js @@ -17,11 +17,14 @@ function compile(str, options, plugins, imports, definitions) { obj = definitions[definition]; value = obj.value - if(obj.literal) { - value = new stylus.nodes.Literal(value); + if (typeof value == 'object') { + style.define(definition, value, true); + } else { + if(obj.literal == true) { + value = new stylus.nodes.Literal(value); + } + style.define(definition, value); } - - style.define(definition, value); } style.render(function(error, css) { @@ -37,4 +40,4 @@ function convert(str) { function version() { return stylus.version; -} \ No newline at end of file +} diff --git a/spec/stylesheets/definition_hash.styl b/spec/stylesheets/definition_hash.styl new file mode 100644 index 0000000..c1dcb52 --- /dev/null +++ b/spec/stylesheets/definition_hash.styl @@ -0,0 +1,8 @@ + +p + text-align my_obj['text_align'] + +if my_obj.if_cond + indent = my_obj['text_indent'] + div + text-indent unquote(indent) diff --git a/spec/stylus_spec.rb b/spec/stylus_spec.rb index c034bd6..5bb5abd 100644 --- a/spec/stylus_spec.rb +++ b/spec/stylus_spec.rb @@ -79,6 +79,36 @@ expect(Stylus.compile(input)).to match(/content: red/) end + it 'defines a hash variable' do + obj = { + :text_indent => '10px', + :text_align => 10, + :if_cond => true, + } + Stylus.define :my_obj, obj + + input, _output = fixture(:definition_hash) + + compiled = Stylus.compile(input) + expect(compiled).to match(/text-indent: 10px/) + expect(compiled).to match(/text-align: 10\b/) + end + + it 'defines a hash variable and supports if' do + obj = { + :text_indent => '20px', + :text_align => 20, + :if_cond => false, + } + Stylus.define :my_obj, obj + + input, _output = fixture(:definition_hash) + + compiled = Stylus.compile(input) + expect(compiled).to_not match(/text-indent: 20px\b/) + expect(compiled).to match(/text-align: 20\b/) + end + it 'includes and imports "nib" automatically' do Stylus.nib = true input, output = fixture(:nib)