Skip to content
This repository has been archived by the owner on Apr 4, 2019. It is now read-only.

Commit

Permalink
Avoid stripping escapes in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
nex3 committed Nov 7, 2018
1 parent 19f065c commit f00c123
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion doc-src/SASS_CHANGELOG.md
Expand Up @@ -5,7 +5,7 @@

## 3.7.2 (Unreleased)

* Fix another escaped-whitespace edge case.
* Fix more escaped-whitespace edge cases.

## 3.7.1 (7 November 2018)

Expand Down
7 changes: 6 additions & 1 deletion lib/sass/scss/css_parser.rb
Expand Up @@ -47,7 +47,12 @@ def ruleset
def keyframes_ruleset
start_pos = source_position
return unless (selector = keyframes_selector)
block(node(Sass::Tree::KeyframeRuleNode.new(selector.strip), start_pos), :ruleset)
block(
node(
Sass::Tree::KeyframeRuleNode.new(
Sass::Util.strip_except_escapes(selector)),
start_pos),
:ruleset)
end

@sass_script_parser = Sass::Script::CssParser
Expand Down
5 changes: 3 additions & 2 deletions lib/sass/scss/parser.rb
Expand Up @@ -992,8 +992,9 @@ def value!
# This results in a dramatic speed increase.
if (val = tok(STATIC_VALUE))
# If val ends with escaped whitespace, leave it be.
val = val.lstrip.sub(/(?<!\\)\s*$/, '\1')
str = Sass::Script::Tree::Literal.new(Sass::Script::Value::String.new(val))
str = Sass::Script::Tree::Literal.new(
Sass::Script::Value::String.new(
Sass::Util.strip_except_escapes(val)))
str.line = start_pos.line
str.source_range = range(start_pos)
return str
Expand Down
2 changes: 1 addition & 1 deletion lib/sass/selector/pseudo.rb
Expand Up @@ -134,7 +134,7 @@ def to_s(opts = {})
res = (syntactic_type == :class ? ":" : "::") + @name
if @arg || @selector
res << "("
res << @arg.strip if @arg
res << Sass::Util.strip_except_escapes(@arg) if @arg
res << " " if @arg && @selector
res << @selector.to_s(opts) if @selector
res << ")"
Expand Down
3 changes: 2 additions & 1 deletion lib/sass/tree/rule_node.rb
Expand Up @@ -140,7 +140,8 @@ def try_to_parse_non_interpolated_rules
# When we get it, we'll set it on the parsed rules if possible.
parser = nil
warnings = Sass.logger.capture do
parser = Sass::SCSS::StaticParser.new(@rule.join.strip, nil, nil, 1)
parser = Sass::SCSS::StaticParser.new(
Sass::Util.strip_except_escapes(@rule.join), nil, nil, 1)
@parsed_rules = parser.parse_selector rescue nil
end

Expand Down
2 changes: 1 addition & 1 deletion lib/sass/tree/visitors/perform.rb
Expand Up @@ -553,7 +553,7 @@ def run_interp_no_strip(text)
end

def run_interp(text)
run_interp_no_strip(text).strip
Sass::Util.strip_except_escapes(run_interp_no_strip(text))
end

def handle_import_loop!(node)
Expand Down
2 changes: 1 addition & 1 deletion lib/sass/tree/visitors/to_css.rb
Expand Up @@ -298,7 +298,7 @@ def visit_rule(node)
rule_part.gsub!(/nth([^( ]*)\(([^)]*)\)/m) do |match|
match.tr(" \t\n", "")
end
rule_part.strip!
rule_part = Sass::Util.strip_except_escapes(rule_part)
end
rule_part
end.compact.join(rule_separator)
Expand Down
26 changes: 22 additions & 4 deletions lib/sass/util.rb
Expand Up @@ -246,15 +246,15 @@ def substitute(ary, from, to)
res
end

# Destructively strips whitespace from the beginning and end
# of the first and last elements, respectively,
# in the array (if those elements are strings).
# Destructively strips whitespace from the beginning and end of the first
# and last elements, respectively, in the array (if those elements are
# strings). Preserves CSS escapes at the end of the array.
#
# @param arr [Array]
# @return [Array] `arr`
def strip_string_array(arr)
arr.first.lstrip! if arr.first.is_a?(String)
arr.last.rstrip! if arr.last.is_a?(String)
arr[-1] = Sass::Util.rstrip_except_escapes(arr[-1]) if arr.last.is_a?(String)
arr
end

Expand Down Expand Up @@ -289,6 +289,24 @@ def escaped_char(escape)
end
end

# Like [String#strip], but preserves escaped whitespace at the end of the
# string.
#
# @param string [String]
# @return [String]
def strip_except_escapes(string)
rstrip_except_escapes(string.lstrip)
end

# Like [String#rstrip], but preserves escaped whitespace at the end of the
# string.
#
# @param string [String]
# @return [String]
def rstrip_except_escapes(string)
string.sub(/(?<!\\)\s+$/, '')
end

# Return an array of all possible paths through the given arrays.
#
# @param arrs [Array<Array>]
Expand Down

0 comments on commit f00c123

Please sign in to comment.