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

Fix more whitespace stripping bugs #97

Merged
merged 3 commits into from Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 22 additions & 33 deletions Rakefile
Expand Up @@ -63,41 +63,30 @@ namespace :test do

desc "Run sass-spec tests against the local code."
task :spec do
require "yaml"
sass_spec_options = YAML.load_file(scope("test/sass-spec.yml"))
enabled = sass_spec_options.delete(:enabled)
unless enabled
puts "SassSpec tests are disabled."
next
end
if ruby_version_at_least?("1.9.2")
old_load_path = $:.dup
old_load_path = $:.dup
begin
$:.unshift(File.join(File.dirname(__FILE__), "lib"))
begin
$:.unshift(File.join(File.dirname(__FILE__), "lib"))
begin
require 'sass_spec'
rescue LoadError
puts "You probably forgot to run: bundle exec rake"
raise
end
default_options = {
:spec_directory => SassSpec::SPEC_DIR,
:engine_adapter => SassEngineAdapter.new,
:generate => false,
:tap => false,
:skip => false,
:verbose => false,
:filter => "",
:limit => -1,
:unexpected_pass => false,
:nuke => false,
}
SassSpec::Runner.new(default_options.merge(sass_spec_options)).run || exit(1)
ensure
$:.replace(old_load_path)
require 'sass_spec'
rescue LoadError
puts "You probably forgot to run: bundle exec rake"
raise
end
else
"Skipping sass-spec on ruby versions less than 1.9.2"
SassSpec::Runner.new(
language_version: get_version[/^\d+\.\d+/],
spec_directory: SassSpec::SPEC_DIR,
engine_adapter: SassEngineAdapter.new,
generate: false,
tap: false,
skip: false,
verbose: false,
filter: "",
limit: -1,
unexpected_pass: false,
nuke: false,
).run || exit(1)
ensure
$:.replace(old_load_path)
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions doc-src/SASS_CHANGELOG.md
Expand Up @@ -3,6 +3,10 @@
* Table of contents
{:toc}

## 3.7.2 (Unreleased)

* Fix more escaped-whitespace edge cases.

## 3.7.1 (7 November 2018)

* Properly handle escaped whitespace and other unusual characters.
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.strip))
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
3 changes: 0 additions & 3 deletions test/sass-spec.yml

This file was deleted.