From 431d4777a84c49cb72bb9d54170dc97f60ad00fb Mon Sep 17 00:00:00 2001 From: xzyfer Date: Mon, 28 Dec 2015 15:41:04 +1100 Subject: [PATCH] Support multiline url declarations This is a continuation of the previous work done to bring our `url()` parsing up to scratch with Ruby Sass. With this update we can remove the trasnitional legacy `url()` parsing logic. Fixes #1096 --- src/parser.cpp | 45 +++++++++++++++++++++++++++++++++++---------- src/parser.hpp | 1 + src/prelexer.cpp | 17 ----------------- src/prelexer.hpp | 1 - 4 files changed, 36 insertions(+), 28 deletions(-) diff --git a/src/parser.cpp b/src/parser.cpp index f6f624a1e5..933eac6b43 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -298,12 +298,13 @@ namespace Sass { else if (lex< uri_prefix >()) { Arguments* args = SASS_MEMORY_NEW(ctx.mem, Arguments, pstate); Function_Call* result = SASS_MEMORY_NEW(ctx.mem, Function_Call, pstate, "url", args); + if (lex< quoted_string >()) { Expression* the_url = parse_string(); *args << SASS_MEMORY_NEW(ctx.mem, Argument, the_url->pstate(), the_url); } - else if (lex < uri_value >(false)) { // don't skip comments - String* the_url = parse_interpolated_chunk(lexed); + else if (String* the_url = parse_url_function_argument()) { + *args << SASS_MEMORY_NEW(ctx.mem, Argument, the_url->pstate(), the_url); } else if (peek < skip_over_scopes < exactly < '(' >, exactly < ')' > > >(position)) { @@ -1687,14 +1688,40 @@ namespace Sass { } String* Parser::parse_url_function_string() + { + std::string prefix(""); + if (lex< uri_prefix >()) { + prefix = std::string(lexed); + } + + String* url_string = parse_url_function_argument(); + + std::string suffix(""); + if (lex< real_uri_suffix >()) { + suffix = std::string(lexed); + } + + if (String_Schema* schema = dynamic_cast(url_string)) { + String_Schema* res = SASS_MEMORY_NEW(ctx.mem, String_Schema, pstate); + (*res) << SASS_MEMORY_NEW(ctx.mem, String_Constant, pstate, prefix); + (*res) += schema; + (*res) << SASS_MEMORY_NEW(ctx.mem, String_Constant, pstate, suffix); + return res; + } else { + std::string res = prefix + url_string->to_string() + suffix; + return SASS_MEMORY_NEW(ctx.mem, String_Constant, pstate, res); + } + } + + String* Parser::parse_url_function_argument() { const char* p = position; - lex< uri_prefix >(); - std::string prefix = lexed; + std::string uri(""); + if (lex< real_uri_value >(false)) { + uri = lexed.to_string(); + } - lex< real_uri_value >(false); - std::string uri = lexed; if (peek< exactly< hash_lbrace > >()) { const char* pp = position; @@ -1702,14 +1729,12 @@ namespace Sass { while (peek< exactly< hash_lbrace > >(pp)) { pp = sequence< interpolant, real_uri_value >(pp); } - position = peek< real_uri_suffix >(pp); + position = pp; return parse_interpolated_chunk(Token(p, position)); } else { - lex< real_uri_suffix >(); - std::string res = prefix + Util::rtrim(uri) + lexed.to_string(); + std::string res = Util::rtrim(uri); return SASS_MEMORY_NEW(ctx.mem, String_Constant, pstate, res); } - } Function_Call* Parser::parse_function_call() diff --git a/src/parser.hpp b/src/parser.hpp index 2a9cde5433..e0167703d1 100644 --- a/src/parser.hpp +++ b/src/parser.hpp @@ -253,6 +253,7 @@ namespace Sass { Function_Call* parse_function_call(); Function_Call_Schema* parse_function_call_schema(); String* parse_url_function_string(); + String* parse_url_function_argument(); String* parse_interpolated_chunk(Token, bool constant = false); String* parse_string(); String_Constant* parse_static_expression(); diff --git a/src/prelexer.cpp b/src/prelexer.cpp index eaf91eef28..bef6ba6ffb 100644 --- a/src/prelexer.cpp +++ b/src/prelexer.cpp @@ -605,23 +605,6 @@ namespace Sass { const char* uri_prefix(const char* src) { return exactly(src); } - const char* uri_value(const char* src) - { - return - sequence < - negate < - exactly < '$' > - >, - zero_plus < - alternatives < - alnum, - interpolant, - exactly <'/'>, - class_char < uri_chars > - > - > - >(src); - } // TODO: rename the following two functions /* no longer used - remove? diff --git a/src/prelexer.hpp b/src/prelexer.hpp index bcf5d7899f..f0fc525902 100644 --- a/src/prelexer.hpp +++ b/src/prelexer.hpp @@ -296,7 +296,6 @@ namespace Sass { // const char* rgb_prefix(const char* src); // Match CSS uri specifiers. const char* uri_prefix(const char* src); - const char* uri_value(const char* src); // Match CSS "!important" keyword. const char* kwd_important(const char* src); // Match CSS "!optional" keyword.