Class: Phlex::HTML

Inherits:
SGML
  • Object
show all
Extended by:
SGML::Elements
Includes:
StandardElements, VoidElements
Defined in:
lib/phlex/html.rb

Defined Under Namespace

Modules: StandardElements, VoidElements

Constant Summary collapse

UNSAFE_TAGS =

Elements tag won't render, since its name may come from a user.

Set.new(%w[script style base]).freeze

Constants included from SGML::Elements

SGML::Elements::COMMA_SEPARATED_TOKENS

Instance Method Summary collapse

Methods included from SGML::Elements

__register_void_element__, __registered_elements__, register_element

Instance Method Details

#content_type ⇒ Object

Returns the string "text/html"



37
38
39
# File 'lib/phlex/html.rb', line 37

def content_type
	"text/html"
end

#doctype ⇒ Object

Output an HTML doctype.



11
12
13
14
15
16
17
# File 'lib/phlex/html.rb', line 11

def doctype
	state = @_state
	return unless state.should_render?

	state.buffer << "<!doctype html>"
	nil
end

#filename ⇒ Object

Override to provide a filename for the HTML file



32
33
34
# File 'lib/phlex/html.rb', line 32

def filename
	nil
end

#svg ⇒ Object

Outputs an <svg> tag.

MDN Docs Spec



23
24
25
26
27
28
29
# File 'lib/phlex/html.rb', line 23

def svg(*, **, &)
	if block_given?
		super { render Phlex::SVG.new(&) }
	else
		super
	end
end

#tag(name, **attributes) ⇒ Object

Output an HTML tag dynamically, e.g:

tag(@tag_name, class: "title")


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
102
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
133
# File 'lib/phlex/html.rb', line 47

def tag(name, **attributes, &)
	state = @_state
	block_given = block_given?
	buffer = state.buffer

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

	unless Symbol === name
		raise Phlex::ArgumentError.new("Expected the tag name to be a Symbol.")
	end

	# The name passed to `tag` may come from a user, so it can't render an
	# element whose content or attributes run as code or change where code loads from.
	if (unsafe_tag = StandardElements.__registered_elements__[name] || VoidElements.__registered_elements__[name]) && UNSAFE_TAGS.include?(unsafe_tag)
		raise Phlex::ArgumentError.new("`tag` can't render <#{unsafe_tag}>. Call `#{unsafe_tag}` directly instead.")
	end

	if (tag = StandardElements.__registered_elements__[name]) || ((tag = name.name.tr("_", "-")).include?("-") && tag.match?(/\A[a-z0-9-]+\z/))
		if attributes.length > 0 # with attributes
			if block_given # with content block
				buffer << "<#{tag}"
				begin
					buffer << (Phlex::ATTRIBUTE_CACHE[attributes] ||= Phlex::SGML::Attributes.generate_attributes(attributes))
				ensure
					buffer << ">"
				end
				begin
					if tag == "svg"
						render Phlex::SVG.new(&)
					else
						__yield_content__(&)
					end
				ensure
					buffer << "</#{tag}>"
				end
			else # without content
				buffer << "<#{tag}"
				begin
					buffer << (::Phlex::ATTRIBUTE_CACHE[attributes] ||= Phlex::SGML::Attributes.generate_attributes(attributes))
				ensure
					buffer << "></#{tag}>"
				end
			end
		else # without attributes
			if block_given # with content block
				buffer << ("<#{tag}>")
				begin
					if tag == "svg"
						render Phlex::SVG.new(&)
					else
						__yield_content__(&)
					end
				ensure
					buffer << "</#{tag}>"
				end
			else # without content
				buffer << "<#{tag}></#{tag}>"
			end
		end

		flush if "head" == tag

		nil
	elsif (tag = VoidElements.__registered_elements__[name])
		if block_given
			raise Phlex::ArgumentError.new("Void elements cannot have content blocks.")
		end

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

		nil
	else
		raise Phlex::ArgumentError.new("Invalid HTML tag: #{name}")
	end
end