Module: Phlex::SGML::Elements

Included in:
HTML, HTML::StandardElements, HTML::VoidElements, Phlex::SVG::StandardElements
Defined in:
lib/phlex/sgml/elements.rb

Instance Method Summary collapse

Instance Method Details

#__register_void_element__(method_name, tag: method_name.name.tr("_", "-")) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/phlex/sgml/elements.rb', line 103

def __register_void_element__(method_name, tag: method_name.name.tr("_", "-"))
	class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
		# frozen_string_literal: true

		def #{method_name}(**attributes)
			state = @_state

			return unless state.should_render?

			buffer = state.buffer

			if attributes.length > 0 # with attributes
				buffer << "<#{tag}"
				begin
					buffer << (::Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes))
				ensure
					buffer << ">"
				end
			else # without attributes
				buffer << "<#{tag}>"
			end

			nil
		end
	RUBY

	__registered_elements__[method_name] = tag

	method_name
end

#__registered_elements__Object



4
5
6
# File 'lib/phlex/sgml/elements.rb', line 4

def __registered_elements__
	@__registered_elements__ ||= {}
end

#register_element(method_name, tag: method_name.name.tr("_", "-")) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/phlex/sgml/elements.rb', line 8

def register_element(method_name, tag: method_name.name.tr("_", "-"))
	class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
		# frozen_string_literal: true

		def #{method_name}(**attributes)
			state = @_state
			buffer = state.buffer
			block_given = block_given?

			unless state.should_render?
				yield(self) if block_given
				return nil
			end

			if attributes.length > 0 # with attributes
				if block_given # with content block
					buffer << "<#{tag}"
					begin
						buffer << (Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes))
					ensure
						buffer << ">"
					end

					begin
						original_length = buffer.bytesize
						content = yield(self)
						if original_length == buffer.bytesize
							case content
							when ::Phlex::SGML::SafeObject
								buffer << content.to_s
							when String
								buffer << ::Phlex::Escape.html_escape(content)
							when Symbol
								buffer << ::Phlex::Escape.html_escape(content.name)
							when nil
								nil
							else
								if (formatted_object = format_object(content))
									buffer << ::Phlex::Escape.html_escape(formatted_object)
								end
							end
						end
					ensure
						buffer << "</#{tag}>"
					end
				else # without content
					buffer << "<#{tag}"
					begin
						buffer << (::Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes))
					ensure
						buffer << "></#{tag}>"
					end
				end
			else # without attributes
				if block_given # with content block
					buffer << "<#{tag}>"

					begin
						original_length = buffer.bytesize
						content = yield(self)
						if original_length == buffer.bytesize
							case content
							when ::Phlex::SGML::SafeObject
								buffer << content.to_s
							when String
								buffer << ::Phlex::Escape.html_escape(content)
							when Symbol
								buffer << ::Phlex::Escape.html_escape(content.name)
							when nil
								nil
							else
								if (formatted_object = format_object(content))
									buffer << ::Phlex::Escape.html_escape(formatted_object)
								end
							end
						end
					ensure
						buffer << "</#{tag}>"
					end
				else # without content
					buffer << "<#{tag}></#{tag}>"
				end
			end

			#{'flush' if tag == 'head'}

			nil
		end
	RUBY

	__registered_elements__[method_name] = tag

	method_name
end