lib/phlex/html.rb

Lines

94%

64 of 68 covered

Branches

92%

35 of 38 covered

Methods

80%

4 of 5 covered

Not run
29-3050-51
Never called
1# frozen_string_literal: true
2
31class Phlex::HTML < Phlex::SGML
41 extend Phlex::SGML::Elements
51 include VoidElements, StandardElements
6
7 # Output an HTML doctype.
81 def doctype
97 state = @_state
107 return unless state.should_render?
11
121 state.buffer << "<!doctype html>"
131 nil
14 end
15
16 # Outputs an `<svg>` tag.
17 #
18 # [MDN Docs](https://developer.mozilla.org/docs/Web/SVG/Element/svg)
19 # [Spec](https://html.spec.whatwg.org/#the-svg-element)
201 def svg(*, **, &)
2155 if block_given?
2291 super { render Phlex::SVG.new(&) }
23 else
249 super
25 end
26 end
27
28 # Override to provide a filename for the HTML file
290 def filename
300 nil
31 end
32
33 # Returns the string "text/html"
341 def content_type
351 "text/html"
36 end
37
38 # Output an HTML tag dynamically, e.g:
39 #
40 # ```ruby
41 # @tag_name = :h1
42 # tag(@tag_name, class: "title")
43 # ```
441 def tag(name, **attributes, &)
45584 state = @_state
46584 block_given = block_given?
47584 buffer = state.buffer
48
49584 unless state.should_render?
500 yield(self) if block_giventhen never taken
510 return nil
52 end
53
54584 unless Symbol === name
551 raise Phlex::ArgumentError.new("Expected the tag name to be a Symbol.")
56 end
57
58583 if (tag = StandardElements.__registered_elements__[name])
59453 void = false
60130 elsif (tag = VoidElements.__registered_elements__[name])
6153 void = true
6277 elsif (registrar = __element_registrar__(name))
6339 registry = registrar.__element_registry__
6439 tag = registry.elements[name]
6539 void = registry.void_elements.key?(name)
6638 elsif (tag = name.name.tr("_", "-")).include?("-") && tag.match?(/\A[a-z][a-z0-9-]*\z/)
6726 void = false
68 else
6912 raise Phlex::ArgumentError.new("Invalid HTML tag: #{name}")
70 end
71
72571 if void
7368 if block_given
7415 raise Phlex::ArgumentError.new("Void elements cannot have content blocks.")
75 end
76
7753 if attributes.length > 0 # with attributes
7837 buffer << "<#{tag}"
79 begin
8037 if (normalizer = Phlex::SGML::Elements::ATTRIBUTE_NORMALIZERS[name])
8120 Phlex::SGML::Elements.public_send(normalizer, attributes)
82 end
83
8449 buffer << state.attribute_cache.fetch(attributes) { Phlex::SGML::Attributes.generate_attributes(attributes) }
85 ensure
8637 buffer << ">"
87 end
88 else # without attributes
8916 buffer << "<#{tag}>"
90 end
91
9252 nil
93 else
94503 if attributes.length > 0 # with attributes
95242 if block_given # with content block
96133 buffer << "<#{tag}"
97 begin
98138 buffer << state.attribute_cache.fetch(attributes) { Phlex::SGML::Attributes.generate_attributes(attributes) }
99 ensure
100133 buffer << ">"
101 end
102
103 begin
104131 if :svg == name
1058 render Phlex::SVG.new(&)
106 else
107123 __yield_content__(&)
108 end
109 ensure
110131 buffer << "</#{tag}>"
111 end
112 else # without content
113109 buffer << "<#{tag}"
114 begin
115112 buffer << state.attribute_cache.fetch(attributes) { Phlex::SGML::Attributes.generate_attributes(attributes) }
116 ensure
117109 buffer << "></#{tag}>"
118 end
119 end
120 else # without attributes
121261 if block_given # with content block
122146 buffer << ("<#{tag}>")
123 begin
124146 if :svg == name
1259 render Phlex::SVG.new(&)
126 else
127137 __yield_content__(&)
128 end
129 ensure
130146 buffer << "</#{tag}>"
131 end
132 else # without content
133115 buffer << "<#{tag}></#{tag}>"
134 end
135 end
136
137457 flush if "head" == tag
138
139457 nil
140 end
141 end
142end