From ca9a7446bd93d0354f0c42d21399b48ba55a8c9a Mon Sep 17 00:00:00 2001 From: Marcel Greter Date: Mon, 16 Mar 2015 17:40:51 +0100 Subject: [PATCH] Implement css string reading function Handle trailing backslash + newline token https://github.com/sass/libsass/issues/942 --- ast.hpp | 8 ++++---- sass_context.cpp | 4 ++-- util.cpp | 23 +++++++++++++++++++++++ util.hpp | 1 + 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/ast.hpp b/ast.hpp index 80232b62cb..6bbf167031 100644 --- a/ast.hpp +++ b/ast.hpp @@ -1392,16 +1392,16 @@ namespace Sass { size_t hash_; public: String_Constant(ParserState pstate, string val) - : String(pstate), quote_mark_(0), value_(val), hash_(0) + : String(pstate), quote_mark_(0), value_(read_css_string(val)), hash_(0) { } String_Constant(ParserState pstate, const char* beg) - : String(pstate), quote_mark_(0), value_(string(beg)), hash_(0) + : String(pstate), quote_mark_(0), value_(read_css_string(string(beg))), hash_(0) { } String_Constant(ParserState pstate, const char* beg, const char* end) - : String(pstate), quote_mark_(0), value_(string(beg, end-beg)), hash_(0) + : String(pstate), quote_mark_(0), value_(read_css_string(string(beg, end-beg))), hash_(0) { } String_Constant(ParserState pstate, const Token& tok) - : String(pstate), quote_mark_(0), value_(string(tok.begin, tok.end)), hash_(0) + : String(pstate), quote_mark_(0), value_(read_css_string(string(tok.begin, tok.end))), hash_(0) { } string type() { return "string"; } static string type_name() { return "string"; } diff --git a/sass_context.cpp b/sass_context.cpp index b59012e63f..3f9b4eb6cd 100644 --- a/sass_context.cpp +++ b/sass_context.cpp @@ -320,7 +320,7 @@ extern "C" { } // generic compilation function (not exported, use file/data compile instead) - static Context* sass_prepare_context (Sass_Context* c_ctx, Context::Data cpp_opt) + static Context* sass_prepare_context (Sass_Context* c_ctx, Context::Data cpp_opt) throw() { try { @@ -422,7 +422,7 @@ extern "C" { } - static Block* sass_parse_block (Sass_Context* c_ctx, Context* cpp_ctx) + static Block* sass_parse_block (Sass_Context* c_ctx, Context* cpp_ctx) throw() { try { diff --git a/util.cpp b/util.cpp index eedf11f310..d59c17c5f1 100644 --- a/util.cpp +++ b/util.cpp @@ -79,6 +79,29 @@ namespace Sass { return out; } + // read css string (handle multiline DELIM) + string read_css_string(const string& str) + { + string out(""); + bool esc = false; + for (auto i : str) { + if (i == '\\') { + esc = ! esc; + } else if (esc && i == '\r') { + out.pop_back(); + continue; + } else if (esc && i == '\n') { + out.pop_back(); + esc = false; + continue; + } else { + esc = false; + } + out.push_back(i); + } + return out; + } + // evacuate unescaped quoted // leave everything else untouched string evacuate_quotes(const string& str) diff --git a/util.hpp b/util.hpp index 5d01045cc6..b0ffd11f48 100644 --- a/util.hpp +++ b/util.hpp @@ -12,6 +12,7 @@ namespace Sass { double sass_atof(const char* str); string string_escape(const string& str); string string_unescape(const string& str); + string read_css_string(const string& str); string evacuate_quotes(const string& str); string evacuate_escapes(const string& str); string string_to_output(const string& str);