class HtmlElement
Constants
- DECODE
- ELEMENTS_FORMAT
- ELEMENT_TYPES
- ESC
- HTML5_TAGS
- TagFormats
Attributes
children[RW]
parent[RW]
tagname[R]
Public Class Methods
assign_tagformats()
click to toggle source
# File lib/htmlelement.rb, line 87 def self.assign_tagformats tagformats = Hash.new(ELEMENTS_FORMAT[:INLINE]) self::ELEMENT_TYPES.each do |type, names| names.each {|name| tagformats[name] = self::ELEMENTS_FORMAT[type] } end tagformats[""] = "%s%s%s" tagformats end
comment(content)
click to toggle source
# File lib/htmlelement.rb, line 72 def self.comment(content) "<!-- #{content} -->#{$/}" end
create(tagname, content=nil, attributes={})
click to toggle source
# File lib/htmlelement.rb, line 64 def self.create(tagname, content=nil, attributes={}) if self::HTML5_TAGS.include? tagname attributes["class"] = tagname tagname = "div" end new(tagname, content, attributes) end
decode(str)
click to toggle source
# File lib/htmlelement.rb, line 100 def self.decode(str) str.gsub(@char_entity_pat) {|ent| DECODE[ent] } end
doctype(encoding="UTF-8")
click to toggle source
# File lib/htmlelement.rb, line 60 def self.doctype(encoding="UTF-8") format(@doctype, encoding) end
escape(str)
click to toggle source
# File lib/htmlelement.rb, line 96 def self.escape(str) str.gsub(/[&"<>]/o) {|pat| ESC[pat] } end
new(tagname, content=nil, attributes={})
click to toggle source
# File lib/htmlelement.rb, line 106 def initialize(tagname, content=nil, attributes={}) @parent = nil @tagname = tagname @children = Children.new @children.push content if content @attributes = attributes @end_comment_not_added = true end
urldecode(str)
click to toggle source
# File lib/htmlelement.rb, line 80 def self.urldecode(str) utf = str.gsub(/%\w\w/) {|ch| [ch[-2, 2]].pack('H*') }.toutf8 return utf.tosjis if $KCODE =~ /^s/io return utf.toeuc if $KCODE =~ /^e/io utf end
urlencode(str)
click to toggle source
# File lib/htmlelement.rb, line 76 def self.urlencode(str) str.toutf8.gsub(/[^\w\.\-]/o) {|utf8_char| utf8_char.unpack("C*").map {|b| format('%%%02X', b) }.join } end
Public Instance Methods
[](attribute)
click to toggle source
# File lib/htmlelement.rb, line 147 def [](attribute) @attributes[attribute] end
[]=(attribute, value)
click to toggle source
# File lib/htmlelement.rb, line 143 def []=(attribute, value) @attributes[attribute] = value end
add_end_comment_for_div_or_section()
click to toggle source
# File lib/htmlelement.rb, line 158 def add_end_comment_for_div_or_section if @tagname == "div" or @tagname == "section" and @end_comment_not_added id_or_class = self["id"] || self["class"] push HtmlElement.comment("end of #{id_or_class}") if id_or_class @end_comment_not_added = false end end
empty?()
click to toggle source
# File lib/htmlelement.rb, line 115 def empty? @children.empty? end
pop()
click to toggle source
# File lib/htmlelement.rb, line 125 def pop last_child = @children.pop last_child.parent = nil if last_child.kind_of? HtmlElement last_child end
push(child)
click to toggle source
# File lib/htmlelement.rb, line 119 def push(child) @children.push child child.parent = self if child.kind_of? HtmlElement self end
shift()
click to toggle source
# File lib/htmlelement.rb, line 137 def shift first_child = @children.shift first_child.parent = nil if first_child.kind_of? HtmlElement first_child end
to_s()
click to toggle source
# File lib/htmlelement.rb, line 166 def to_s add_end_comment_for_div_or_section format(self.class::TagFormats[@tagname], @tagname, format_attributes, @children) end
Also aliased as: to_str
traverse() { |self| ... }
click to toggle source
# File lib/htmlelement.rb, line 172 def traverse(&block) yield self @children.traverse(&block) end
unshift(child)
click to toggle source
# File lib/htmlelement.rb, line 131 def unshift(child) @children.unshift child child.parent = self if child.kind_of? HtmlElement self end
Private Instance Methods
format_attributes()
click to toggle source
# File lib/htmlelement.rb, line 151 def format_attributes @attributes.collect do |attr, value| " #{attr}=\"#{HtmlElement.escape(value.to_s)}\"" end.sort.join end