Class: Phlex::SVG

Inherits:
SGML
  • Object
show all
Includes:
StandardElements
Defined in:
lib/phlex/svg.rb

Defined Under Namespace

Modules: StandardElements

Constant Summary

Constants inherited from SGML

Phlex::SGML::REF_ATTRIBUTES, Phlex::SGML::UNSAFE_ATTRIBUTES

Instance Method Summary collapse

Methods included from Phlex::SGML::Elements

#__register_void_element__, #__registered_elements__, #register_element

Methods inherited from SGML

#cache, #call, call, #capture, #comment, #context, #flush, #fragment, #internal_call, #low_level_cache, new, #plain, #raw, #render, #rendering?, #safe, #to_proc, #view_template, #whitespace

Instance Method Details

#content_typeObject

Returns the string “image/svg+xml”



9
10
11
# File 'lib/phlex/svg.rb', line 9

def content_type
	"image/svg+xml"
end

#filenameObject

Override to provide a filename for the SVG file



14
15
16
# File 'lib/phlex/svg.rb', line 14

def filename
	nil
end

#tag(name, **attributes) ⇒ Object



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
# File 'lib/phlex/svg.rb', line 18

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

	if (tag = StandardElements.__registered_elements__[name]) || (tag = name.name.tr("_", "-")).include?("-")
		if attributes.length > 0 # with attributes
			if block_given # with content block
				buffer << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes)) << ">"
				__yield_content__(&)
				buffer << "</#{tag}>"
			else # without content
				buffer << "<#{tag}" << (::Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes)) << "></#{tag}>"
			end
		else # without attributes
			if block_given # with content block
				buffer << ("<#{tag}>")
				__yield_content__(&)
				buffer << "</#{tag}>"
			else # without content
				buffer << "<#{tag}></#{tag}>"
			end
		end
	else
		raise Phlex::ArgumentError.new("Invalid SVG tag: #{name}")
	end
end