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

Constants included from SGML::Elements

SGML::Elements::COMMA_SEPARATED_TOKENS

Constants inherited from SGML

SGML::ERBCompiler, SGML::REF_ATTRIBUTES, SGML::UNSAFE_ATTRIBUTES

Instance Method Summary collapse

Methods included from SGML::Elements

__register_void_element__, __registered_elements__, register_element

Methods inherited from SGML

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

Instance Method Details

#content_typeObject

Returns the string “text/html”



34
35
36
# File 'lib/phlex/html.rb', line 34

def content_type
  "text/html"
end

#doctypeObject

Output an HTML doctype.



8
9
10
11
12
13
14
# File 'lib/phlex/html.rb', line 8

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

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

#filenameObject

Override to provide a filename for the HTML file



29
30
31
# File 'lib/phlex/html.rb', line 29

def filename
  nil
end

#svgObject



20
21
22
23
24
25
26
# File 'lib/phlex/html.rb', line 20

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:

“‘ruby tag(@tag_name, class: “title”) “`



44
45
46
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
# File 'lib/phlex/html.rb', line 44

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)) << ">"
        if tag == "svg"
          render Phlex::SVG.new(&)
        else
          __yield_content__(&)
        end
        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}>")
        if tag == "svg"
          render Phlex::SVG.new(&)
        else
          __yield_content__(&)
        end
        buffer << "</#{tag}>"
      else # without content
        buffer << "<#{tag}></#{tag}>"
      end
    end
  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}" << (::Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes)) << ">"
    else # without attributes
      buffer << "<#{tag}>"
    end

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