Skip to content
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

Fix an error when parsing attribute without quotes and value #138

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion lib/better_html/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def build_attribute_value_node(tokens)
:attribute_unquoted_value,
)

build_node(:attribute_value, children)
build_node(:attribute_value, children) if children.any?
end

def build_text_node(tokens)
Expand Down
31 changes: 31 additions & 0 deletions test/better_html/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,37 @@ class ParserTest < ActiveSupport::TestCase
tree.ast
end

test "consumes tag attributes without value" do
tree = Parser.new(buffer("<div foo=>bar</div>"))
assert_equal s(
:document,
s(
:tag,
nil,
s(:tag_name, "div"),
s(
:tag_attributes,
s(
:attribute,
s(:attribute_name, "foo"),
s(:equal),
nil,
),
),
nil,
),
s(:text, "bar"),
s(
:tag,
s(:solidus),
s(:tag_name, "div"),
nil,
nil,
),
),
tree.ast
end

test "consume tag attributes nodes interpolation in name and value" do
tree = Parser.new(buffer("<div data-<%= foo %>=\"some <%= value %> foo\">"))
assert_equal s(
Expand Down
Loading