Skip to content
This repository was archived by the owner on Feb 10, 2026. It is now read-only.
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- LiveViewNative.Template.Engine
- LiveViewNative.Component.Declarative
- Template parser supports namespaced attribute keys
- Templare parser ignores doctype tags at top of document

### Changed

Expand Down
30 changes: 30 additions & 0 deletions lib/live_view_native/template/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,26 @@ defmodule LiveViewNative.Template.Parser do
end
end

defp parse(<<"<!doctype ", document::binary>>, cursor, [], args) do
cursor = move_cursor(cursor, ~c"<!doctype ")

ignore_doctype(document, cursor, args)
|> case do
{:ok, {document, cursor}} -> parse(document, cursor, [], args)
error -> error
end
end

defp parse(<<"<!DOCTYPE ", document::binary>>, cursor, [], args) do
cursor = move_cursor(cursor, ~c"<!DOCTYPE ")

ignore_doctype(document, cursor, args)
|> case do
{:ok, {document, cursor}} -> parse(document, cursor, [], args)
error -> error
end
end

defp parse(<<"<", document::binary>>, cursor, nodes, args) do
cursor = move_cursor(cursor, ?<)

Expand All @@ -152,6 +172,16 @@ defmodule LiveViewNative.Template.Parser do
end
end

defp ignore_doctype(<<">", document::binary>>, cursor, _args) do
cursor = move_cursor(cursor, ?>)
{:ok, {document, cursor}}
end

defp ignore_doctype(<<char::utf8, document::binary>>, cursor, args) when char in @chars do
cursor = move_cursor(cursor, char)
ignore_doctype(document, cursor, args)
end

defp parse_text_node(<<"<", _document::binary>> = document, cursor, buffer, args) do
return_text_node(document, cursor, buffer, args)
end
Expand Down
25 changes: 25 additions & 0 deletions test/live_view_native/template/parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,31 @@ defmodule LiveViewNative.Template.ParserTest do
]
end

test "ignores document declrations" do
{:ok, nodes} = """
<!doctype gameboy>
<Text>Hello!</Text>
"""
|> parse_document()

assert nodes == [
{"Text", [], ["Hello!"]}
]
end

test "leading and trailing whitespace is ignored" do
{:ok, nodes} = """

<Text>Hello!</Text>

"""
|> parse_document()

assert nodes == [
{"Text", [], ["Hello!"]}
]
end

test "invalid attribute key name" do
doc = """
<FooBar
Expand Down