lib/phlex/html.rb
Lines
94%64 of 68 covered
Branches
92%35 of 38 covered
Methods
80%4 of 5 covered
- Not run
- 29-3050-51
- Never called
| 1 | # frozen_string_literal: true | |
| 2 | ||
| 3 | 1 | class Phlex::HTML < Phlex::SGML |
| 4 | 1 | extend Phlex::SGML::Elements |
| 5 | 1 | include VoidElements, StandardElements |
| 6 | ||
| 7 | # Output an HTML doctype. | |
| 8 | 1 | def doctype |
| 9 | 7 | state = @_state |
| 10 | 7 | return unless state.should_render? |
| 11 | ||
| 12 | 1 | state.buffer << "<!doctype html>" |
| 13 | 1 | nil |
| 14 | end | |
| 15 | ||
| 16 | # Outputs an `<svg>` tag. | |
| 17 | # | |
| 18 | # [MDN Docs](https://developer.mozilla.org/docs/Web/SVG/Element/svg) | |
| 19 | # [Spec](https://html.spec.whatwg.org/#the-svg-element) | |
| 20 | 1 | def svg(*, **, &) |
| 21 | 55 | if block_given? |
| 22 | 91 | super { render Phlex::SVG.new(&) } |
| 23 | else | |
| 24 | 9 | super |
| 25 | end | |
| 26 | end | |
| 27 | ||
| 28 | # Override to provide a filename for the HTML file | |
| 29 | 0 | def filename |
| 30 | 0 | nil |
| 31 | end | |
| 32 | ||
| 33 | # Returns the string "text/html" | |
| 34 | 1 | def content_type |
| 35 | 1 | "text/html" |
| 36 | end | |
| 37 | ||
| 38 | # Output an HTML tag dynamically, e.g: | |
| 39 | # | |
| 40 | # ```ruby | |
| 41 | # @tag_name = :h1 | |
| 42 | # tag(@tag_name, class: "title") | |
| 43 | # ``` | |
| 44 | 1 | def tag(name, **attributes, &) |
| 45 | 584 | state = @_state |
| 46 | 584 | block_given = block_given? |
| 47 | 584 | buffer = state.buffer |
| 48 | ||
| 49 | 584 | unless state.should_render? |
| 50 | 0 | yield(self) if block_giventhen never taken |
| 51 | 0 | return nil |
| 52 | end | |
| 53 | ||
| 54 | 584 | unless Symbol === name |
| 55 | 1 | raise Phlex::ArgumentError.new("Expected the tag name to be a Symbol.") |
| 56 | end | |
| 57 | ||
| 58 | 583 | if (tag = StandardElements.__registered_elements__[name]) |
| 59 | 453 | void = false |
| 60 | 130 | elsif (tag = VoidElements.__registered_elements__[name]) |
| 61 | 53 | void = true |
| 62 | 77 | elsif (registrar = __element_registrar__(name)) |
| 63 | 39 | registry = registrar.__element_registry__ |
| 64 | 39 | tag = registry.elements[name] |
| 65 | 39 | void = registry.void_elements.key?(name) |
| 66 | 38 | elsif (tag = name.name.tr("_", "-")).include?("-") && tag.match?(/\A[a-z][a-z0-9-]*\z/) |
| 67 | 26 | void = false |
| 68 | else | |
| 69 | 12 | raise Phlex::ArgumentError.new("Invalid HTML tag: #{name}") |
| 70 | end | |
| 71 | ||
| 72 | 571 | if void |
| 73 | 68 | if block_given |
| 74 | 15 | raise Phlex::ArgumentError.new("Void elements cannot have content blocks.") |
| 75 | end | |
| 76 | ||
| 77 | 53 | if attributes.length > 0 # with attributes |
| 78 | 37 | buffer << "<#{tag}" |
| 79 | begin | |
| 80 | 37 | if (normalizer = Phlex::SGML::Elements::ATTRIBUTE_NORMALIZERS[name]) |
| 81 | 20 | Phlex::SGML::Elements.public_send(normalizer, attributes) |
| 82 | end | |
| 83 | ||
| 84 | 49 | buffer << state.attribute_cache.fetch(attributes) { Phlex::SGML::Attributes.generate_attributes(attributes) } |
| 85 | ensure | |
| 86 | 37 | buffer << ">" |
| 87 | end | |
| 88 | else # without attributes | |
| 89 | 16 | buffer << "<#{tag}>" |
| 90 | end | |
| 91 | ||
| 92 | 52 | nil |
| 93 | else | |
| 94 | 503 | if attributes.length > 0 # with attributes |
| 95 | 242 | if block_given # with content block |
| 96 | 133 | buffer << "<#{tag}" |
| 97 | begin | |
| 98 | 138 | buffer << state.attribute_cache.fetch(attributes) { Phlex::SGML::Attributes.generate_attributes(attributes) } |
| 99 | ensure | |
| 100 | 133 | buffer << ">" |
| 101 | end | |
| 102 | ||
| 103 | begin | |
| 104 | 131 | if :svg == name |
| 105 | 8 | render Phlex::SVG.new(&) |
| 106 | else | |
| 107 | 123 | __yield_content__(&) |
| 108 | end | |
| 109 | ensure | |
| 110 | 131 | buffer << "</#{tag}>" |
| 111 | end | |
| 112 | else # without content | |
| 113 | 109 | buffer << "<#{tag}" |
| 114 | begin | |
| 115 | 112 | buffer << state.attribute_cache.fetch(attributes) { Phlex::SGML::Attributes.generate_attributes(attributes) } |
| 116 | ensure | |
| 117 | 109 | buffer << "></#{tag}>" |
| 118 | end | |
| 119 | end | |
| 120 | else # without attributes | |
| 121 | 261 | if block_given # with content block |
| 122 | 146 | buffer << ("<#{tag}>") |
| 123 | begin | |
| 124 | 146 | if :svg == name |
| 125 | 9 | render Phlex::SVG.new(&) |
| 126 | else | |
| 127 | 137 | __yield_content__(&) |
| 128 | end | |
| 129 | ensure | |
| 130 | 146 | buffer << "</#{tag}>" |
| 131 | end | |
| 132 | else # without content | |
| 133 | 115 | buffer << "<#{tag}></#{tag}>" |
| 134 | end | |
| 135 | end | |
| 136 | ||
| 137 | 457 | flush if "head" == tag |
| 138 | ||
| 139 | 457 | nil |
| 140 | end | |
| 141 | end | |
| 142 | end |