lib/phlex/svg.rb

Lines

88%

54 of 61 covered

Branches

76%

26 of 34 covered

Methods

75%

3 of 4 covered

Not run
12-132638-394367
Never called
1# frozen_string_literal: true
2
31class Phlex::SVG < Phlex::SGML
41 include StandardElements
5
6 # Returns the string "image/svg+xml"
71 def content_type
81 "image/svg+xml"
9 end
10
11 # Override to provide a filename for the SVG file
120 def filename
130 nil
14 end
15
161 def cdata(content = nil, &block)
1710 state = @_state
1810 return unless state.should_render?then never taken
19
2010 if !block && String === content
215 state.buffer << "<![CDATA[" << content.gsub("]]>", "]]]]><![CDATA[>") << "]]>"
225 elsif block && nil == content
235 state.buffer << "<![CDATA[" << capture(&block).gsub("]]>", "]]]]><![CDATA[>") << "]]>"
24 else
25
260 raise Phlex::ArgumentError.new("Expected a String or block.")else never taken
27 end
28
2910 nil
30 end
31
321 def tag(name, **attributes, &)
33327 state = @_state
34327 block_given = block_given?
35327 buffer = state.buffer
36
37327 unless state.should_render?
380 yield(self) if block_giventhen never taken
390 return nil
40 end
41
42327 unless Symbol === name
430 raise Phlex::ArgumentError.new("Expected the tag name to be a Symbol.")then never taken
44 end
45
46327 if (tag = StandardElements.__registered_elements__[name])
47276 void = false
4851 elsif (registrar = __element_registrar__(name))
4918 registry = registrar.__element_registry__
5018 tag = registry.elements[name]
5118 void = registry.void_elements.key?(name)
5233 elsif (tag = name.name.tr("_", "-")).include?("-") && tag.match?(/\A[a-z][a-z0-9-]*\z/)
5323 void = false
54 else
5510 raise Phlex::ArgumentError.new("Invalid SVG tag: #{name}")
56 end
57
58317 if void
599 if block_given
601 raise Phlex::ArgumentError.new("Void elements cannot have content blocks.")
61 end
62
638 if attributes.length > 0 # with attributes
641 buffer << "<#{tag}"
65 begin
661 if (normalizer = Phlex::SGML::Elements::ATTRIBUTE_NORMALIZERS[name])
670 Phlex::SGML::Elements.public_send(normalizer, attributes)then never taken
68 end
69
701 buffer << state.attribute_cache.fetch(attributes) { Phlex::SGML::Attributes.generate_attributes(attributes) }
71 ensure
721 buffer << ">"
73 end
74 else # without attributes
757 buffer << "<#{tag}>"
76 end
77
788 nil
79 else
80308 if attributes.length > 0 # with attributes
81148 if block_given # with content block
8281 buffer << "<#{tag}"
83 begin
8484 buffer << state.attribute_cache.fetch(attributes) { Phlex::SGML::Attributes.generate_attributes(attributes) }
85 ensure
8681 buffer << ">"
87 end
88
89 begin
9079 __yield_content__(&)
91 ensure
9279 buffer << "</#{tag}>"
93 end
94 else # without content
9567 buffer << "<#{tag}"
96 begin
9769 buffer << state.attribute_cache.fetch(attributes) { Phlex::SGML::Attributes.generate_attributes(attributes) }
98 ensure
9967 buffer << "></#{tag}>"
100 end
101 end
102 else # without attributes
103160 if block_given # with content block
10490 buffer << ("<#{tag}>")
105 begin
10690 __yield_content__(&)
107 ensure
10890 buffer << "</#{tag}>"
109 end
110 else # without content
11170 buffer << "<#{tag}></#{tag}>"
112 end
113 end
114
115276 flush if "head" == tagthen never taken
116
117276 nil
118 end
119 end
120end