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

Commit f00c123

Browse files
committed
Avoid stripping escapes in more places
1 parent 19f065c commit f00c123

File tree

8 files changed

+37
-12
lines changed

8 files changed

+37
-12
lines changed

doc-src/SASS_CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
## 3.7.2 (Unreleased)
77

8-
* Fix another escaped-whitespace edge case.
8+
* Fix more escaped-whitespace edge cases.
99

1010
## 3.7.1 (7 November 2018)
1111

lib/sass/scss/css_parser.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ def ruleset
4747
def keyframes_ruleset
4848
start_pos = source_position
4949
return unless (selector = keyframes_selector)
50-
block(node(Sass::Tree::KeyframeRuleNode.new(selector.strip), start_pos), :ruleset)
50+
block(
51+
node(
52+
Sass::Tree::KeyframeRuleNode.new(
53+
Sass::Util.strip_except_escapes(selector)),
54+
start_pos),
55+
:ruleset)
5156
end
5257

5358
@sass_script_parser = Sass::Script::CssParser

lib/sass/scss/parser.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -992,8 +992,9 @@ def value!
992992
# This results in a dramatic speed increase.
993993
if (val = tok(STATIC_VALUE))
994994
# If val ends with escaped whitespace, leave it be.
995-
val = val.lstrip.sub(/(?<!\\)\s*$/, '\1')
996-
str = Sass::Script::Tree::Literal.new(Sass::Script::Value::String.new(val))
995+
str = Sass::Script::Tree::Literal.new(
996+
Sass::Script::Value::String.new(
997+
Sass::Util.strip_except_escapes(val)))
997998
str.line = start_pos.line
998999
str.source_range = range(start_pos)
9991000
return str

lib/sass/selector/pseudo.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def to_s(opts = {})
134134
res = (syntactic_type == :class ? ":" : "::") + @name
135135
if @arg || @selector
136136
res << "("
137-
res << @arg.strip if @arg
137+
res << Sass::Util.strip_except_escapes(@arg) if @arg
138138
res << " " if @arg && @selector
139139
res << @selector.to_s(opts) if @selector
140140
res << ")"

lib/sass/tree/rule_node.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ def try_to_parse_non_interpolated_rules
140140
# When we get it, we'll set it on the parsed rules if possible.
141141
parser = nil
142142
warnings = Sass.logger.capture do
143-
parser = Sass::SCSS::StaticParser.new(@rule.join.strip, nil, nil, 1)
143+
parser = Sass::SCSS::StaticParser.new(
144+
Sass::Util.strip_except_escapes(@rule.join), nil, nil, 1)
144145
@parsed_rules = parser.parse_selector rescue nil
145146
end
146147

lib/sass/tree/visitors/perform.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ def run_interp_no_strip(text)
553553
end
554554

555555
def run_interp(text)
556-
run_interp_no_strip(text).strip
556+
Sass::Util.strip_except_escapes(run_interp_no_strip(text))
557557
end
558558

559559
def handle_import_loop!(node)

lib/sass/tree/visitors/to_css.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def visit_rule(node)
298298
rule_part.gsub!(/nth([^( ]*)\(([^)]*)\)/m) do |match|
299299
match.tr(" \t\n", "")
300300
end
301-
rule_part.strip!
301+
rule_part = Sass::Util.strip_except_escapes(rule_part)
302302
end
303303
rule_part
304304
end.compact.join(rule_separator)

lib/sass/util.rb

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,15 +246,15 @@ def substitute(ary, from, to)
246246
res
247247
end
248248

249-
# Destructively strips whitespace from the beginning and end
250-
# of the first and last elements, respectively,
251-
# in the array (if those elements are strings).
249+
# Destructively strips whitespace from the beginning and end of the first
250+
# and last elements, respectively, in the array (if those elements are
251+
# strings). Preserves CSS escapes at the end of the array.
252252
#
253253
# @param arr [Array]
254254
# @return [Array] `arr`
255255
def strip_string_array(arr)
256256
arr.first.lstrip! if arr.first.is_a?(String)
257-
arr.last.rstrip! if arr.last.is_a?(String)
257+
arr[-1] = Sass::Util.rstrip_except_escapes(arr[-1]) if arr.last.is_a?(String)
258258
arr
259259
end
260260

@@ -289,6 +289,24 @@ def escaped_char(escape)
289289
end
290290
end
291291

292+
# Like [String#strip], but preserves escaped whitespace at the end of the
293+
# string.
294+
#
295+
# @param string [String]
296+
# @return [String]
297+
def strip_except_escapes(string)
298+
rstrip_except_escapes(string.lstrip)
299+
end
300+
301+
# Like [String#rstrip], but preserves escaped whitespace at the end of the
302+
# string.
303+
#
304+
# @param string [String]
305+
# @return [String]
306+
def rstrip_except_escapes(string)
307+
string.sub(/(?<!\\)\s+$/, '')
308+
end
309+
292310
# Return an array of all possible paths through the given arrays.
293311
#
294312
# @param arrs [Array<Array>]

0 commit comments

Comments
 (0)