| Class | Texy::Html |
| In: |
lib/texy/html.rb
lib/texy/html_well_form.rb |
| Parent: | Object |
| EMPTY_TAG | = | '/' | ||
| BLOCK | = | [ 'address', 'blockquote', 'caption', 'col', 'colgroup', 'dd', 'div', 'dl', 'dt', 'fieldset', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'iframe', 'legend', 'li', 'object', 'ol', 'p', 'param', 'pre', 'table', 'tbody', 'td', 'tfoot', 'th', 'thead', 'tr', 'ul' | Block elements | |
| INLINE | = | [ 'a', 'abbr', 'acronym', 'area', 'b', 'big', 'br', 'button', 'cite', 'code', 'del', 'dfn', 'em', 'i', 'img', 'input', 'ins', 'kbd', 'label', 'map', 'noscript', 'optgroup', 'option', 'q', 'samp', 'script', 'select', 'small', 'span', 'strong', 'sub', 'sup', 'textarea', 'tt', 'var' | Inline elements | |
| VALID | = | BLOCK.merge INLINE | All elements | |
| EMPTY | = | ['img', 'hr', 'br', 'input', 'meta', 'area', 'base', 'col', 'link', 'param'].to_hash.invert | Empty elements (ones without closing tag) | |
| ACCEPTED_ATTRS | = | [ 'abbr', 'accesskey', 'align', 'alt', 'archive', 'axis', 'bgcolor', 'cellpadding', 'cellspacing', 'char', 'charoff', 'charset', 'cite', 'classid', 'codebase', 'codetype', 'colspan', 'compact', 'coords', 'data', 'datetime', 'declare', 'dir', 'face', 'frame', 'headers', 'href', 'hreflang', 'hspace', 'ismap', 'lang', 'longdesc', 'name', 'noshade', 'nowrap', 'onblur', 'onclick', 'ondblclick', 'onkeydown', 'onkeypress', 'onkeyup', 'onmousedown', 'onmousemove', 'onmouseout', 'onmouseover', 'onmouseup', 'rel', 'rev', 'rowspan', 'rules', 'scope', 'shape', 'size', 'span', 'src', 'standby', 'start', 'summary', 'tabindex', 'target', 'title', 'type', 'usemap', 'valign', 'value', 'vspace' | META = [‘html’, ‘head’, ‘body’, ‘base’, ‘meta’, ‘link’, ‘title’] | |
| ENTITIES | = | { 'Æ' => 'Æ','Á' => 'Á','Â' => 'Â','À' => 'À','Α' => 'Α','Å' => 'Å','Ã' => 'Ã','Ä' => 'Ä', 'Β' => 'Β','Ç' => 'Ç','Χ' => 'Χ','‡' => '‡','Δ' => 'Δ','Ð' => 'Ð','É' => 'É','Ê' => 'Ê', 'È' => 'È','Ε' => 'Ε','Η' => 'Η','Ë' => 'Ë','Γ' => 'Γ','Í' => 'Í','Î' => 'Î','Ì' => 'Ì', 'Ι' => 'Ι','Ï' => 'Ï','Κ' => 'Κ','Λ' => 'Λ','Μ' => 'Μ','Ñ' => 'Ñ','Ν' => 'Ν','Œ' => 'Œ', 'Ó' => 'Ó','Ô' => 'Ô','Ò' => 'Ò','Ω' => 'Ω','Ο' => 'Ο','Ø' => 'Ø','Õ' => 'Õ','Ö' => 'Ö', 'Φ' => 'Φ','Π' => 'Π','″' => '″','Ψ' => 'Ψ','Ρ' => 'Ρ','Š' => 'Š','Σ' => 'Σ','Þ' => 'Þ', 'Τ' => 'Τ','Θ' => 'Θ','Ú' => 'Ú','Û' => 'Û','Ù' => 'Ù','Υ' => 'Υ','Ü' => 'Ü','Ξ' => 'Ξ', 'Ý' => 'Ý','Ÿ' => 'Ÿ','Ζ' => 'Ζ','á' => 'á','â' => 'â','´' => '´','æ' => 'æ','à' => 'à', 'ℵ' => 'ℵ','α' => 'α','&' => '&','∧' => '∧','∠' => '∠',''' => ''','å' => 'å','≈' => '≈', 'ã' => 'ã','ä' => 'ä','„' => '„','β' => 'β','¦' => '¦','•' => '•','∩' => '∩','ç' => 'ç', '¸' => '¸','¢' => '¢','χ' => 'χ','ˆ' => 'ˆ','♣' => '♣','≅' => '≅','©' => '©','↵' => '↵', '∪' => '∪','¤' => '¤','⇓' => '⇓','†' => '†','↓' => '↓','°' => '°','δ' => 'δ','♦' => '♦', '÷' => '÷','é' => 'é','ê' => 'ê','è' => 'è','∅' => '∅',' ' => ' ',' ' => ' ','ε' => 'ε', '≡' => '≡','η' => 'η','ð' => 'ð','ë' => 'ë','€' => '€','∃' => '∃','ƒ' => 'ƒ','∀' => '∀', '½' => '½','¼' => '¼','¾' => '¾','⁄' => '⁄','γ' => 'γ','≥' => '≥','>' => '>','⇔' => '⇔', '↔' => '↔','♥' => '♥','…' => '…','í' => 'í','î' => 'î','¡' => '¡','ì' => 'ì','ℑ' => 'ℑ', '∞' => '∞','∫' => '∫','ι' => 'ι','¿' => '¿','∈' => '∈','ï' => 'ï','κ' => 'κ','⇐' => '⇐', 'λ' => 'λ','⟨' => '〈','«' => '«','←' => '←','⌈' => '⌈','“' => '“','≤' => '≤','⌊' => '⌊', '∗' => '∗','◊' => '◊','‎' => '‎','‹' => '‹','‘' => '‘','<' => '<','¯' => '¯','—' => '—', 'µ' => 'µ','·' => '·','−' => '−','μ' => 'μ','∇' => '∇',' ' => ' ','–' => '–','≠' => '≠', '∋' => '∋','¬' => '¬','∉' => '∉','⊄' => '⊄','ñ' => 'ñ','ν' => 'ν','ó' => 'ó','ô' => 'ô', 'œ' => 'œ','ò' => 'ò','‾' => '‾','ω' => 'ω','ο' => 'ο','⊕' => '⊕','∨' => '∨','ª' => 'ª', 'º' => 'º','ø' => 'ø','õ' => 'õ','⊗' => '⊗','ö' => 'ö','¶' => '¶','∂' => '∂','‰' => '‰', '⊥' => '⊥','φ' => 'φ','π' => 'π','ϖ' => 'ϖ','±' => '±','£' => '£','′' => '′','∏' => '∏', '∝' => '∝','ψ' => 'ψ','"' => '"','⇒' => '⇒','√' => '√','⟩' => '〉','»' => '»','→' => '→', '⌉' => '⌉','”' => '”','ℜ' => 'ℜ','®' => '®','⌋' => '⌋','ρ' => 'ρ','‏' => '‏','›' => '›', '’' => '’','‚' => '‚','š' => 'š','⋅' => '⋅','§' => '§','­' => '­','σ' => 'σ','ς' => 'ς', '∼' => '∼','♠' => '♠','⊂' => '⊂','⊆' => '⊆','∑' => '∑','¹' => '¹','²' => '²','³' => '³', '⊃' => '⊃','⊇' => '⊇','ß' => 'ß','τ' => 'τ','∴' => '∴','θ' => 'θ','ϑ' => 'ϑ',' ' => ' ', 'þ' => 'þ','˜' => '˜','×' => '×','™' => '™','⇑' => '⇑','ú' => 'ú','↑' => '↑','û' => 'û', 'ù' => 'ù','¨' => '¨','ϒ' => 'ϒ','υ' => 'υ','ü' => 'ü','℘' => '℘','ξ' => 'ξ','ý' => 'ý', '¥' => '¥','ÿ' => 'ÿ','ζ' => 'ζ','‍' => '‍','‌' => '‌' | ||
| ALLOWED_ENTITIES | = | {'&' => '&', '"' => '"', '<' => '<', '>' => '>'} |
# File lib/texy/html.rb, line 104 def self.check_entities(html) # decode(!) named entities to numeric ENTITIES.each do |from, to| html.gsub!(from, to) end # preserve numeric entities html.gsub!(/&([a-zA-Z0-9]+);/, '&\1;') # these are only allowed named entites ALLOWED_ENTITIES.each do |from, to| html.gsub!(from, to) end html end
Build string which represents (X)HTML closing tag
# File lib/texy/html.rb, line 168 def self.closing_tags(tags) return '' if tags.empty? tags.reverse.inject('') do |result, (tag, attrs)| next if tag.empty? || EMPTY[tag] || attrs[EMPTY_TAG] result + "</#{tag}>" end.to_s end
Like (PHP) htmlspecialchars, but can preserve entities
Parameters:
| s: | input string |
| in_quotes: | for using inside quotes? |
| entity: | preserve entities? |
# File lib/texy/html.rb, line 81 def self.html_chars(s, in_quotes = false, entity = false) s = CGI.escapeHTML(s) # Unescape double quotes to emulate PHP function htmlspecialchars # with second parameter set to ENT_NOQUOTES s.gsub!('"', '"') unless in_quotes # preserve numeric entities? s.gsub!(/&([a-zA-Z0-9]+|#x[0-9a-fA-F]+|#[0-9]+);/, '&\1;') if entity s end
Build string which represents (X)HTML opening tag
Parameters:
| tags: | array of arrays, where each array contains two elements: the name of tag and hash of it’s attributes. If name id "/", it is empty tag. |
# File lib/texy/html.rb, line 130 def self.opening_tags(tags) return '' if tags.empty? tags.inject('') do |result, (tag, attrs)| next '' if tag.empty? empty = EMPTY[tag] || attrs[EMPTY_TAG] attr_str = '' unless attrs.empty? attrs.delete(EMPTY_TAG) attrs.downcase_keys.each do |name, value| if value.kind_of?(Hash) && name == 'style' value = value.downcase_keys.inject([]) do |style, (key_s, value_s)| style << "#{key_s}:#{value_s}" unless key_s.empty? || value_s.empty? style end.join(';') elsif value.kind_of?(Array) value = value.uniq.join(' ') end next if value.to_s.empty? value.strip! # Freezed spaces will be preserved during reformating. attr_str += %Q( #{html_chars(name)}="#{Texy.freeze_spaces(html_chars(value, true, true))}") end end result + "<#{tag}#{attr_str}#{empty ? ' /' : ''}>" end end