1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

[Sass] Make sass-convert work for @extend.

This commit is contained in:
Nathan Weizenbaum 2010-04-26 12:05:11 -07:00
parent bff6707247
commit 89c9c8a44f
5 changed files with 59 additions and 11 deletions

View file

@ -19,6 +19,8 @@ This is the first time it's worked in SCSS, since the comment was silently swall
* Properly report line numbers for invalid selectors.
* `@extend` is converted via `sass-convert`.
## 3.0.0.rc.1
[Tagged on GitHub](http://github.com/nex3/haml/commit/3.0.0.rc.1).

View file

@ -46,6 +46,11 @@ module Sass::Tree
protected
# @see Node#to_src
def to_src(tabs, opts, fmt)
"#{' ' * tabs}@extend #{selector_to_src(@selector, tabs, opts, fmt).lstrip}#{semi fmt}\n"
end
# Runs SassScript interpolation in the selector,
# and then parses the result into a {Sass::Selector::CommaSequence}.
#

View file

@ -400,13 +400,50 @@ module Sass
# @param tabs [Fixnum] The amount of tabulation to use for the Sass code
# @param opts [{Symbol => Object}] An options hash (see {Sass::CSS#initialize})
# @param fmt [Symbol] `:sass` or `:scss`
# @return [String] The Sass or CSS code corresponding to the children
# @return [String] The Sass or SCSS code corresponding to the children
def children_to_src(tabs, opts, fmt)
(fmt == :sass ? "\n" : " {\n") +
children.map {|c| c.send("to_#{fmt}", tabs + 1, opts)}.join.rstrip +
(fmt == :sass ? "\n" : " }\n")
end
# Converts a selector to a Sass or SCSS string.
#
# @param sel [Array<String, Sass::Script::Node>] The selector to convert
# @param tabs [Fixnum] The indentation of the selector
# @param opts [{Symbol => Object}] An options hash (see {Sass::CSS#initialize})
# @param fmt [Symbol] `:sass` or `:scss`
# @return [String] The Sass or SCSS code corresponding to the selector
def selector_to_src(sel, tabs, opts, fmt)
fmt == :sass ? selector_to_sass(sel, opts) : selector_to_scss(sel, tabs, opts)
end
# Converts a selector to a Sass string.
#
# @param sel [Array<String, Sass::Script::Node>] The selector to convert
# @param opts [{Symbol => Object}] An options hash (see {Sass::CSS#initialize})
# @return [String] The Sass code corresponding to the selector
def selector_to_sass(sel, opts)
sel.map do |r|
if r.is_a?(String)
r.gsub(/(,[ \t]*)?\n\s*/) {$1 ? $1 + "\n" : " "}
else
"\#{#{r.to_sass(opts)}}"
end
end.join
end
# Converts a selector to a SCSS string.
#
# @param sel [Array<String, Sass::Script::Node>] The selector to convert
# @param tabs [Fixnum] The indentation of the selector
# @param opts [{Symbol => Object}] An options hash (see {Sass::CSS#initialize})
# @return [String] The SCSS code corresponding to the selector
def selector_to_scss(sel, tabs, opts)
sel.map {|r| r.is_a?(String) ? r : "\#{#{r.to_sass(opts)}}"}.
join.gsub(/^[ \t]*/, ' ' * tabs)
end
# Convert any underscores in a string into hyphens,
# but only if the `:dasherize` option is set.
#

View file

@ -84,22 +84,14 @@ module Sass::Tree
# @see Node#to_sass
def to_sass(tabs, opts = {})
name = rule.map do |r|
if r.is_a?(String)
r.gsub(/(,[ \t]*)?\n\s*/) {$1 ? $1 + "\n" : " "}
else
"\#{#{r.to_sass(opts)}}"
end
end.join
name = selector_to_sass(rule, opts)
name = "\\" + name if name[0] == ?:
name.gsub(/^/, ' ' * tabs) + children_to_src(tabs, opts, :sass)
end
# @see Node#to_scss
def to_scss(tabs, opts = {})
name = rule.map {|r| r.is_a?(String) ? r : "\#{#{r.to_sass(opts)}}"}.
join.gsub(/^[ \t]*/, ' ' * tabs)
name = selector_to_scss(rule, tabs, opts)
res = name + children_to_src(tabs, opts, :scss)
if children.last.is_a?(CommentNode) && children.last.silent

View file

@ -727,6 +727,18 @@ SASS
SCSS
end
def test_extend
assert_renders <<SASS, <<SCSS
.foo
@extend .bar
@extend .baz:bang
SASS
.foo {
@extend .bar;
@extend .baz:bang; }
SCSS
end
def test_argless_mixin_definition
assert_renders <<SASS, <<SCSS
=foo-bar