Module: IQ::HTML

Defined in:
lib/iq/html.rb

Constant Summary collapse

HTML_ESCAPE =
{ '&' => '&amp;', '>' => '&gt;', '<' => '&lt;', '"' => '&quot;' }.freeze

Class Method Summary collapse

Class Method Details

.escape(value) ⇒ String

Returns HTML escaped version of supplied string.

Examples:

IQ::HTML.escape('Letters, Num83r5, & -- &amp; > -- &gt; < -- &lt; " -- &quot; &#1234;')
#=> 'Letters, Num83r5, &amp; -- &amp;amp; &gt; -- &amp;gt; &lt; -- &amp;lt; &quot; -- &amp;quot; &amp;#1234;'

Parameters:

  • (String)

Returns:

  • (String)

    the escaped string

Raises:

  • (ArgumentError)


13
14
15
16
# File 'lib/iq/html.rb', line 13

def self.escape(value)
  raise ArgumentError, 'Must supply a string' unless value.is_a?(String)
  value.gsub(/[&"><]/) { |special| HTML_ESCAPE[special] }
end

.escape_once(value) ⇒ String

Returns HTML escaped version of supplied string leaving any existing entities intact.

Examples:

IQ::HTML.escape_once('Letters, Num83r5, & -- &amp; > -- &gt; < -- &lt; " -- &quot; &#1234;')
#=> 'Letters, Num83r5, &amp; -- &amp; &gt; -- &gt; &lt; -- &lt; &quot; -- &quot; &#1234;'

Parameters:

  • (String)

Returns:

  • (String)

    the escaped string (with existing entities intact)

Raises:

  • (ArgumentError)


27
28
29
30
# File 'lib/iq/html.rb', line 27

def self.escape_once(value)
  raise ArgumentError, 'Must supply a string' unless value.is_a?(String)
  value.gsub(/[\"><]|&(?!([a-zA-Z]+|(#\d+));)/) { |special| HTML_ESCAPE[special] }
end

.sanitize_as_dom_id(string_to_sanitize) ⇒ String

Takes a string and returns a new hyphenated string that can safely be used as a dom id.

Examples:

IQ::HTML.sanitize_as_dom_id('product[variants][0][stock]')
#=> 'product-variants-0-stock'

Parameters:

  • (String)

Returns:

  • (String)

    the escaped string (leaving existing entities intact)

Raises:

  • (ArgumentError)


41
42
43
44
45
# File 'lib/iq/html.rb', line 41

def self.sanitize_as_dom_id(string_to_sanitize)
  raise ArgumentError, 'Argument must be a string' unless string_to_sanitize.is_a?(String)
  # see http://www.w3.org/TR/html4/types.html#type-name
  string_to_sanitize.to_s.gsub(']','').gsub(/[^-a-zA-Z0-9:.]/, "-")
end

.self.tag(name, attributes = {}) ⇒ String .self.tag(name, content, escape = true) ⇒ String .self.tag(name, content, attributes, escape = true) ⇒ String

Helper method for creating HTML tags of a specified name, along with optional content and list of attributes. All attribute values and content will be escaped, however content escaping may be dissabled by supplying false as the last argument.

Examples:

IQ::HTML.tag('br')                                      #=> "<br />"
IQ::HTML.tag('strong', 'Bill & Ben')                    #=> "<strong>Bill &amp; Ben</strong>"
IQ::HTML.tag('strong', 'Bill & Ben', false)             #=> "<strong>Bill & Ben</strong>"
IQ::HTML.tag('strong', 'B&B', { :id => 'bb' }, false)   #=> "<strong id="bb">Bill & Ben</strong>"
IQ::HTML.tag('input', :title => 'B&B')                  #=> '<input type="text" title="B&amp;B" />'
IQ::HTML.tag('strong', 'B&B', :title => 'Bill & Ben')   #=> "<strong title="Bill &amp; Ben">B&amp;B</strong>"

Overloads:

  • .self.tag(name, attributes = {}) ⇒ String

    Parameters:

    • name (String, Symbol)
    • attributes (Hash) (defaults to: {})
  • .self.tag(name, content, escape = true) ⇒ String

    Parameters:

    • name (String, Symbol)
    • content (String)
    • escape (true, false) (defaults to: true)
  • .self.tag(name, content, attributes, escape = true) ⇒ String

    Parameters:

    • name (String, Symbol)
    • content (String)
    • attributes (Hash)
    • escape (true, false) (defaults to: true)

Returns:

  • (String)

Raises:

  • (ArgumentError)


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
100
101
102
103
104
105
106
107
108
109
# File 'lib/iq/html.rb', line 74

def self.tag(name, *args)
  raise ArgumentError, 'Name must be a symbol or string' unless name.is_a?(Symbol) || name.is_a?(String)

  case args.size
    when 3 then content, attributes, escape = *args
    when 2
      case args.last
        when Hash then escape, content, attributes = true, *args
        when true, false then content, escape = *args
        else
          raise ArgumentError, 'Third argument must be an attribute hash or boolean escape value'
      end
    when 1
      case args.last
        when String then escape, content = true, *args
        when Hash then attributes = args.last
        else
          raise ArgumentError, 'Second argument must be a content string or an attributes hash'
      end
    when 0
    else
      raise ArgumentError, "Too many arguments"
  end
  
  raise ArgumentError, 'Content must be in the form of a string' unless content.nil? || content.is_a?(String)
  raise ArgumentError, 'Attributes must be in the form of a hash' unless attributes.nil? || attributes.is_a?(Hash)
  raise ArgumentError, 'Escape argument must be a boolean' unless escape.nil? || escape == true || escape == false
  raise ArgumentError, 'Escape option supplied, but no content to escape' if escape && content.nil?

  tag = "<#{name}"
  if attributes
    attributes.reject! { |key, value| value.nil? }
    tag << attributes.map { |key, value| %( #{key}="#{escape(value.to_s)}") }.sort.join
  end
  tag << (content ? ">#{escape ? escape(content) : content}</#{name}>" : ' />')
end