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 | ||
| 3 | 1 | class Phlex::SVG < Phlex::SGML |
| 4 | 1 | include StandardElements |
| 5 | ||
| 6 | # Returns the string "image/svg+xml" | |
| 7 | 1 | def content_type |
| 8 | 1 | "image/svg+xml" |
| 9 | end | |
| 10 | ||
| 11 | # Override to provide a filename for the SVG file | |
| 12 | 0 | def filename |
| 13 | 0 | nil |
| 14 | end | |
| 15 | ||
| 16 | 1 | def cdata(content = nil, &block) |
| 17 | 10 | state = @_state |
| 18 | 10 | return unless state.should_render?then never taken |
| 19 | ||
| 20 | 10 | if !block && String === content |
| 21 | 5 | state.buffer << "<![CDATA[" << content.gsub("]]>", "]]]]><![CDATA[>") << "]]>" |
| 22 | 5 | elsif block && nil == content |
| 23 | 5 | state.buffer << "<![CDATA[" << capture(&block).gsub("]]>", "]]]]><![CDATA[>") << "]]>" |
| 24 | else | |
| 25 | ||
| 26 | 0 | raise Phlex::ArgumentError.new("Expected a String or block.")else never taken |
| 27 | end | |
| 28 | ||
| 29 | 10 | nil |
| 30 | end | |
| 31 | ||
| 32 | 1 | def tag(name, **attributes, &) |
| 33 | 327 | state = @_state |
| 34 | 327 | block_given = block_given? |
| 35 | 327 | buffer = state.buffer |
| 36 | ||
| 37 | 327 | unless state.should_render? |
| 38 | 0 | yield(self) if block_giventhen never taken |
| 39 | 0 | return nil |
| 40 | end | |
| 41 | ||
| 42 | 327 | unless Symbol === name |
| 43 | 0 | raise Phlex::ArgumentError.new("Expected the tag name to be a Symbol.")then never taken |
| 44 | end | |
| 45 | ||
| 46 | 327 | if (tag = StandardElements.__registered_elements__[name]) |
| 47 | 276 | void = false |
| 48 | 51 | elsif (registrar = __element_registrar__(name)) |
| 49 | 18 | registry = registrar.__element_registry__ |
| 50 | 18 | tag = registry.elements[name] |
| 51 | 18 | void = registry.void_elements.key?(name) |
| 52 | 33 | elsif (tag = name.name.tr("_", "-")).include?("-") && tag.match?(/\A[a-z][a-z0-9-]*\z/) |
| 53 | 23 | void = false |
| 54 | else | |
| 55 | 10 | raise Phlex::ArgumentError.new("Invalid SVG tag: #{name}") |
| 56 | end | |
| 57 | ||
| 58 | 317 | if void |
| 59 | 9 | if block_given |
| 60 | 1 | raise Phlex::ArgumentError.new("Void elements cannot have content blocks.") |
| 61 | end | |
| 62 | ||
| 63 | 8 | if attributes.length > 0 # with attributes |
| 64 | 1 | buffer << "<#{tag}" |
| 65 | begin | |
| 66 | 1 | if (normalizer = Phlex::SGML::Elements::ATTRIBUTE_NORMALIZERS[name]) |
| 67 | 0 | Phlex::SGML::Elements.public_send(normalizer, attributes)then never taken |
| 68 | end | |
| 69 | ||
| 70 | 1 | buffer << state.attribute_cache.fetch(attributes) { Phlex::SGML::Attributes.generate_attributes(attributes) } |
| 71 | ensure | |
| 72 | 1 | buffer << ">" |
| 73 | end | |
| 74 | else # without attributes | |
| 75 | 7 | buffer << "<#{tag}>" |
| 76 | end | |
| 77 | ||
| 78 | 8 | nil |
| 79 | else | |
| 80 | 308 | if attributes.length > 0 # with attributes |
| 81 | 148 | if block_given # with content block |
| 82 | 81 | buffer << "<#{tag}" |
| 83 | begin | |
| 84 | 84 | buffer << state.attribute_cache.fetch(attributes) { Phlex::SGML::Attributes.generate_attributes(attributes) } |
| 85 | ensure | |
| 86 | 81 | buffer << ">" |
| 87 | end | |
| 88 | ||
| 89 | begin | |
| 90 | 79 | __yield_content__(&) |
| 91 | ensure | |
| 92 | 79 | buffer << "</#{tag}>" |
| 93 | end | |
| 94 | else # without content | |
| 95 | 67 | buffer << "<#{tag}" |
| 96 | begin | |
| 97 | 69 | buffer << state.attribute_cache.fetch(attributes) { Phlex::SGML::Attributes.generate_attributes(attributes) } |
| 98 | ensure | |
| 99 | 67 | buffer << "></#{tag}>" |
| 100 | end | |
| 101 | end | |
| 102 | else # without attributes | |
| 103 | 160 | if block_given # with content block |
| 104 | 90 | buffer << ("<#{tag}>") |
| 105 | begin | |
| 106 | 90 | __yield_content__(&) |
| 107 | ensure | |
| 108 | 90 | buffer << "</#{tag}>" |
| 109 | end | |
| 110 | else # without content | |
| 111 | 70 | buffer << "<#{tag}></#{tag}>" |
| 112 | end | |
| 113 | end | |
| 114 | ||
| 115 | 276 | flush if "head" == tagthen never taken |
| 116 | ||
| 117 | 276 | nil |
| 118 | end | |
| 119 | end | |
| 120 | end |