Module: IQ::HTML
- Defined in:
- lib/iq/html.rb
Constant Summary collapse
- HTML_ESCAPE =
{ '&' => '&', '>' => '>', '<' => '<', '"' => '"' }.freeze
Class Method Summary collapse
-
.escape(value) ⇒ String
Returns HTML escaped version of supplied string.
-
.escape_once(value) ⇒ String
Returns HTML escaped version of supplied string leaving any existing entities intact.
-
.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.
-
.tag(name, *args) ⇒ String
Helper method for creating HTML tags of a specified name, along with optional content and list of attributes.
Class Method Details
.escape(value) ⇒ String
Returns HTML escaped version of supplied string.
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.
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.
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.
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 |