Skip to content

Commit

Permalink
Issue #48: improve handling of special chars
Browse files Browse the repository at this point in the history
  • Loading branch information
amadanmath authored and jeromegn committed Feb 11, 2021
1 parent 4c009cf commit c2d90a9
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
4 changes: 3 additions & 1 deletion spec/fixtures/attribute-wrappers.slang
Expand Up @@ -3,4 +3,6 @@ div(hello="world" foo="bar")
div[hello="world" foo="bar"]
div(hello=Process.pid)
div{hello="world" foo="bar"}
div{hello="#{"world"}" foo="bar"}
div{hello="world\u0021" foo="bar"}
div{hello="#{"world"}" foo="bar"}
div{hello="#{"world\u0021"}" foo="bar"}
7 changes: 6 additions & 1 deletion spec/fixtures/script.slang
Expand Up @@ -14,4 +14,9 @@ javascript:
something
</div>
`
})
})

javascript:
let twoLines = "bar\nbaz";
let hello = "#{"Hello, " + "world!"}";
let obj = #{{:a => 17, "b" => "foo"}.to_json};
3 changes: 2 additions & 1 deletion spec/fixtures/with_html.slang
@@ -1,2 +1,3 @@
table
<tr><td>#{Process.pid}</td></tr>
<tr><td>#{Process.pid}</td></tr>
<tr><td>"hello\u0021"</td></tr>
9 changes: 9 additions & 0 deletions spec/slang_spec.cr
@@ -1,4 +1,5 @@
require "./spec_helper"
require "json"

def evaluates_to_true
1 == 1
Expand Down Expand Up @@ -172,6 +173,7 @@ describe Slang do
res.should eq <<-HTML
<table>
<tr><td>#{Process.pid}</td></tr>
<tr><td>"hello\\u0021"</td></tr>
</table>
HTML
end
Expand Down Expand Up @@ -279,6 +281,11 @@ describe Slang do
`
})
</script>
<script>
let twoLines = "bar\\nbaz";
let hello = "Hello, world!";
let obj = {"a":17,"b":"foo"};
</script>
HTML
end
it "renders stylesheets" do
Expand Down Expand Up @@ -335,7 +342,9 @@ describe Slang do
<div hello="world" foo="bar"></div>
<div hello="#{Process.pid}"></div>
<div hello="world" foo="bar"></div>
<div hello="world\\u0021" foo="bar"></div>
<div hello="world" foo="bar"></div>
<div hello="world!" foo="bar"></div>
HTML
end
end
Expand Down
23 changes: 14 additions & 9 deletions src/slang/lexer.cr
Expand Up @@ -206,7 +206,7 @@ module Slang
@token.type = :CONTROL
next_char
next_char if current_char == ' '
@token.value = consume_line
@token.value = consume_line(escape_double_quotes=false)
end

private def consume_output
Expand All @@ -228,7 +228,7 @@ module Slang
end

skip_whitespace
@token.value = consume_line.strip
@token.value = consume_line(escape_double_quotes=false).strip
@token.value = " #{@token.value}" if prepend_whitespace
@token.value = "#{@token.value} " if append_whitespace
end
Expand Down Expand Up @@ -257,14 +257,14 @@ module Slang
cc = STRING_OPEN_CLOSE_CHARS_MAP[current_char]
str << current_char
next_char
str << consume_string open_char: oc, close_char: cc
str << consume_string open_char: oc, close_char: cc, crystal: true
next
end
if current_char == '"' || current_char == '\''
ch = current_char
str << current_char
next_char
str << consume_string open_char: ch, close_char: ch
str << consume_string open_char: ch, close_char: ch, crystal: true
next
end
if current_char == '}'
Expand All @@ -283,7 +283,7 @@ module Slang
end
end

private def consume_string(open_char = '"', close_char = '"', escape_double_quotes = false)
private def consume_string(open_char = '"', close_char = '"', escape_double_quotes = false, crystal = false)
level = 0
escaped = false
maybe_string_interpolation = false
Expand Down Expand Up @@ -326,10 +326,11 @@ module Slang
maybe_string_interpolation = true
end

if current_char == '\\' && !escaped
escaped = true
else
if escaped
escaped = false
str << '\\' unless crystal
elsif current_char == '\\'
escaped = true
end

if current_char == '\n' || current_char == '\0'
Expand All @@ -348,12 +349,16 @@ module Slang
@token.value = "\"#{consume_line}\""
end

private def consume_line
private def consume_line(escape_double_quotes=true)
String.build do |str|
loop do
if current_char == '\n' || current_char == '\0'
break
else
if escape_double_quotes && (current_char == '"' || current_char == '\\')
str << '\\'
end

str << current_char
next_char
end
Expand Down

0 comments on commit c2d90a9

Please sign in to comment.