Skip to content

Commit a5e0a7e

Browse files
committed
Fix up tag flags
1 parent e8defa4 commit a5e0a7e

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

Gemfile.lock

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ GEM
3131

3232
PLATFORMS
3333
arm64-darwin-22
34+
arm64-darwin-23
3435
x86_64-linux
3536

3637
DEPENDENCIES

lib/syntax_tree/haml/format.rb

+13-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ def initialize(
1616
.lines
1717
.each
1818
.with_index(1) do |line, index|
19-
@literal_lines[index] = line.strip if line.lstrip.start_with?("!")
19+
if (literal_line = line[/\A\s*(?:%[a-z]+)?(!.*)\s*\z/, 1])
20+
@literal_lines[index] = literal_line
21+
end
2022
end
2123

2224
super(source, *rest, options: options)
@@ -384,8 +386,12 @@ def visit_tag(node)
384386
# tag.
385387
q.breakable_empty
386388

387-
if node.value[:parse]
388-
format_tag_value(q, value)
389+
if line = q.literal_lines[node.line]
390+
q.text(line)
391+
elsif node.value[:parse]
392+
prefix = node.value[:preserve_script] ? "~" : "="
393+
prefix = "&#{prefix}" if node.value[:escape_html]
394+
format_tag_value(q, prefix, value)
389395
else
390396
q.if_break { q.text("") }.if_flat { q.text(" ") }
391397
q.text(value)
@@ -399,10 +405,10 @@ def visit_tag(node)
399405

400406
private
401407

402-
def format_tag_value(q, value)
408+
def format_tag_value(q, prefix, value)
403409
program = SyntaxTree.parse(value)
404410
if !program || program.statements.body.length > 1
405-
return q.text("= #{value}")
411+
return q.text("#{prefix} #{value}")
406412
end
407413

408414
statement = program.statements.body.first
@@ -418,10 +424,10 @@ def format_tag_value(q, value)
418424
q.if_break { q.text("") }.if_flat { q.text(" ") }
419425
q.text(formatted[1...-1].gsub(/\\"/, "\""))
420426
else
421-
q.text("= #{formatted}")
427+
q.text("#{prefix} #{formatted}")
422428
end
423429
rescue Parser::ParseError
424-
q.text("= #{value}")
430+
q.text("#{prefix} #{value}")
425431
end
426432

427433
# When printing out sequences of silent scripts, sometimes subsequent nodes

test/tag_test.rb

+16
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,20 @@ def test_interpolation_in_strings
135135
def test_interpolation_in_value
136136
assert_format("%p <small>hello</small>\"\#{1 + 2} little pigs\"")
137137
end
138+
139+
def test_preserve
140+
assert_format("%p~ foo")
141+
end
142+
143+
def test_escape_html
144+
assert_format("%p&= foo")
145+
end
146+
147+
def test_preserve_escape_html
148+
assert_format("%p&~ foo")
149+
end
150+
151+
def test_unescape
152+
assert_format("%p!= foo")
153+
end
138154
end

0 commit comments

Comments
 (0)