Module: HTML

Defined in:
lib/html.rb,
lib/html/fragment.rb

Overview

Library namespace

Defined Under Namespace

Classes: Fragment

Constant Summary collapse

CONTENT_TAGS =
IceNine.deep_freeze(%w(
  a abbr address article aside audio b bdi bdo blockquote
  body button canvas caption cite code col colgroup data
  dd del details dfn div dl dt em embed eventsource fieldset
  fieldsource figcaption figure footer form h1 h2 h3 h4 h5 h6
  head header hgroup html i iframe ins kbd label legend li link
  mark menu nav noscript object ol optgroup option output p pre
  q ruby rp rt s samp script section select small span strong
  style sub summary sup table tbody textarea tfoot th thead time
  title td tr ul var video
))
NOCONTENT_TAGS =
IceNine.deep_freeze(%w(
  area br command hr img input keygen map meta meter progress
  param source track wbr
))
ESCAPE_TABLE =
IceNine.deep_freeze(
  '>' => '<',
  '<' => '&gt;',
  '"' => '&quot;',
  '&' => '&amp;'
)

Class Method Summary collapse

Class Method Details

.attributes(attributes) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create html attributes

Parameters:

  • attributes (Hash)

Returns:

  • (String)


115
116
117
118
119
# File 'lib/html.rb', line 115

def self.attributes(attributes)
  attributes.map do |key, value|
    %Q{ #{key}="#{escape(value.to_s)}"}
  end.join
end

.content_tag(type, content, attributes = {}) ⇒ Fragment

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create content tag

Parameters:

  • type (#to_str)
  • content (String)

Returns:



103
104
105
# File 'lib/html.rb', line 103

def self.(type, content, attributes={})
  Fragment.new("<#{type}#{attributes(attributes)}>#{Fragment.build(content)}</#{type}>")
end

.escape(text) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Escape html

Parameters:

  • text (String)

Returns:

  • (String)


59
60
61
62
63
# File 'lib/html.rb', line 59

def self.escape(text)
  text.gsub(
    /[><"&]/, ESCAPE_TABLE
  )
end

.join(components) ⇒ HTML::Fragment

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Join html compoinents

Parameters:

  • components (Enumerable<#to_s>)

Returns:



37
38
39
40
41
42
# File 'lib/html.rb', line 37

def self.join(components)
  contents = components.map do |component|
    Fragment.build(component)
  end
  Fragment.new(contents.join)
end

.tag(type, attributes = {}) ⇒ Fragment

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create contentless html tag

Parameters:

  • type (#to_str)
  • attributes (Hash) (defaults to: {})

Returns:



90
91
92
# File 'lib/html.rb', line 90

def self.tag(type, attributes={})
  Fragment.new("<#{type}#{attributes(attributes)}/>")
end