Skip to content

Commit cf23af0

Browse files
authored
Handles doctype more freely (#56)
- Support ERB-tags before DOCTYPE. - Parse error on multiple DOCTYPE elements.
1 parent 375d720 commit cf23af0

File tree

5 files changed

+36
-8
lines changed

5 files changed

+36
-8
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
66

77
## [Unreleased]
88

9+
## [0.10.1] - 2023-08-20
10+
11+
### Added
12+
13+
- Allow `DOCTYPE` to be after other tags, to work with e.g. ERB-tags on first line.
14+
915
## [0.10.0] - 2023-08-20
1016

1117
- Changes how whitespace and newlines are handled.
@@ -70,7 +76,8 @@ Output:
7076
- Can format a lot of .html.erb-syntax and works as a plugin to syntax_tree.
7177
- This is still early and there are a lot of different weird syntaxes out there.
7278

73-
[unreleased]: https://github.com/davidwessman/syntax_tree-erb/compare/v0.10.0...HEAD
79+
[unreleased]: https://github.com/davidwessman/syntax_tree-erb/compare/v0.10.1...HEAD
80+
[0.10.1]: https://github.com/davidwessman/syntax_tree-erb/compare/v0.10.0...v0.10.1
7481
[0.10.0]: https://github.com/davidwessman/syntax_tree-erb/compare/v0.9.5...v0.10.0
7582
[0.9.5]: https://github.com/davidwessman/syntax_tree-erb/compare/v0.9.4...v0.9.5
7683
[0.9.4]: https://github.com/davidwessman/syntax_tree-erb/compare/v0.9.3...v0.9.4

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
w_syntax_tree-erb (0.10.0)
4+
w_syntax_tree-erb (0.10.1)
55
prettier_print (~> 1.2, >= 1.2.0)
66
syntax_tree (~> 6.1, >= 6.1.1)
77

lib/syntax_tree/erb/parser.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ class MissingTokenError < ParseError
2020
def initialize(source)
2121
@source = source
2222
@tokens = make_tokens
23+
@found_doctype = false
2324
end
2425

2526
def parse
26-
doctype = maybe { parse_doctype }
2727
elements = many { parse_any_tag }
2828

2929
location =
3030
elements.first.location.to(elements.last.location) if elements.any?
3131

32-
Document.new(elements: [doctype].compact + elements, location: location)
32+
Document.new(elements: elements, location: location)
3333
end
3434

3535
def debug_tokens
@@ -44,11 +44,19 @@ def parse_any_tag
4444
loop do
4545
tag =
4646
atleast do
47-
maybe { parse_html_comment } || maybe { parse_erb_tag } ||
48-
maybe { parse_erb_comment } || maybe { parse_html_element } ||
49-
maybe { parse_new_line } || maybe { parse_chardata }
47+
maybe { parse_doctype } || maybe { parse_html_comment } ||
48+
maybe { parse_erb_tag } || maybe { parse_erb_comment } ||
49+
maybe { parse_html_element } || maybe { parse_new_line } ||
50+
maybe { parse_chardata }
5051
end
5152

53+
if tag.is_a?(Doctype)
54+
if @found_doctype
55+
raise(ParseError, "Only one doctype element is allowed")
56+
else
57+
@found_doctype = true
58+
end
59+
end
5260
# Allow skipping empty CharData
5361
return tag unless tag.skip?
5462
end

lib/syntax_tree/erb/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module SyntaxTree
44
module ERB
5-
VERSION = "0.10.0"
5+
VERSION = "0.10.1"
66
end
77
end

test/html_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ def test_html_doctype
3434

3535
parsed = ERB.parse("<!doctype html>")
3636
assert_instance_of(SyntaxTree::ERB::Doctype, parsed.elements.first)
37+
38+
# Allow doctype to not be the first element
39+
parsed = ERB.parse("<% theme = \"general\" %> <!DOCTYPE html>")
40+
assert_equal(2, parsed.elements.size)
41+
assert_equal(
42+
[SyntaxTree::ERB::ErbNode, SyntaxTree::ERB::Doctype],
43+
parsed.elements.map(&:class)
44+
)
45+
46+
# Do not allow multiple doctype elements
47+
assert_raises(SyntaxTree::ERB::Parser::ParseError) do
48+
ERB.parse("<!DOCTYPE html>\n<!DOCTYPE html>\n")
49+
end
3750
end
3851

3952
def test_html_comment

0 commit comments

Comments
 (0)