Skip to content

Fix up tag flags #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ GEM

PLATFORMS
arm64-darwin-22
arm64-darwin-23
x86_64-linux

DEPENDENCIES
Expand Down
20 changes: 13 additions & 7 deletions lib/syntax_tree/haml/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def initialize(
.lines
.each
.with_index(1) do |line, index|
@literal_lines[index] = line.strip if line.lstrip.start_with?("!")
if (literal_line = line[/\A\s*(?:%[a-z]+)?(!.*)\s*\z/, 1])
@literal_lines[index] = literal_line
end
end

super(source, *rest, options: options)
Expand Down Expand Up @@ -384,8 +386,12 @@ def visit_tag(node)
# tag.
q.breakable_empty

if node.value[:parse]
format_tag_value(q, value)
if line = q.literal_lines[node.line]
q.text(line)
elsif node.value[:parse]
prefix = node.value[:preserve_script] ? "~" : "="
prefix = "&#{prefix}" if node.value[:escape_html]
format_tag_value(q, prefix, value)
else
q.if_break { q.text("") }.if_flat { q.text(" ") }
q.text(value)
Expand All @@ -399,10 +405,10 @@ def visit_tag(node)

private

def format_tag_value(q, value)
def format_tag_value(q, prefix, value)
program = SyntaxTree.parse(value)
if !program || program.statements.body.length > 1
return q.text("= #{value}")
return q.text("#{prefix} #{value}")
end

statement = program.statements.body.first
Expand All @@ -418,10 +424,10 @@ def format_tag_value(q, value)
q.if_break { q.text("") }.if_flat { q.text(" ") }
q.text(formatted[1...-1].gsub(/\\"/, "\""))
else
q.text("= #{formatted}")
q.text("#{prefix} #{formatted}")
end
rescue Parser::ParseError
q.text("= #{value}")
q.text("#{prefix} #{value}")
end

# When printing out sequences of silent scripts, sometimes subsequent nodes
Expand Down
16 changes: 16 additions & 0 deletions test/tag_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,20 @@ def test_interpolation_in_strings
def test_interpolation_in_value
assert_format("%p <small>hello</small>\"\#{1 + 2} little pigs\"")
end

def test_preserve
assert_format("%p~ foo")
end

def test_escape_html
assert_format("%p&= foo")
end

def test_preserve_escape_html
assert_format("%p&~ foo")
end

def test_unescape
assert_format("%p!= foo")
end
end
Loading