Class: HTML5::TreeBuilders::Hpricot::Element

Inherits:
Node show all
Defined in:
lib/html5/treebuilders/hpricot.rb

Direct Known Subclasses

DocumentFragment

Defined Under Namespace

Classes: AttributeProxy

Instance Attribute Summary collapse

Attributes inherited from Node

#hpricot

Attributes inherited from Base::Node

#childNodes, #flags, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#appendChild, #hasContent, #insertBefore, #insertText, #removeChild

Methods inherited from Base::Node

#appendChild, #hasContent, #insertBefore, #insertText, #removeChild, #reparentChildren

Constructor Details

#initialize(name, namespace = nil) ⇒ Element

Returns a new instance of Element.



73
74
75
76
77
78
# File 'lib/html5/treebuilders/hpricot.rb', line 73

def initialize(name, namespace=nil)
  super(name)

  @hpricot   = ::Hpricot::Elem.new(::Hpricot::STag.new(name))
  @namespace = namespace
end

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace.



67
68
69
# File 'lib/html5/treebuilders/hpricot.rb', line 67

def namespace
  @namespace
end

Class Method Details

.hpricot_classObject



69
70
71
# File 'lib/html5/treebuilders/hpricot.rb', line 69

def self.hpricot_class
  ::Hpricot::Elem
end

Instance Method Details

#attributesObject



116
117
118
# File 'lib/html5/treebuilders/hpricot.rb', line 116

def attributes
  AttributeProxy.new(@hpricot)
end

#attributes=(attrs) ⇒ Object



120
121
122
# File 'lib/html5/treebuilders/hpricot.rb', line 120

def attributes=(attrs)
  attrs.each { |name, value| @hpricot[name] = value }
end

#cloneNodeObject



84
85
86
87
88
89
# File 'lib/html5/treebuilders/hpricot.rb', line 84

def cloneNode
  attributes.inject(self.class.new(name)) do |node, (name, value)|
    node.hpricot[name] = value
    node
  end
end

#nameObject



80
81
82
# File 'lib/html5/treebuilders/hpricot.rb', line 80

def name
  @hpricot.stag.name
end

#printTree(indent = 0) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/html5/treebuilders/hpricot.rb', line 124

def printTree(indent=0)
  tree = "\n|#{' ' * indent}<#{!@namespace.nil? ? @namespace.to_s + ' ' : ''}#{name}>"
  indent += 2
  attributes.each do |name, value|
    next if name == 'xmlns'
    tree += "\n|#{' ' * indent}#{name}=\"#{value}\""
  end
  childNodes.inject(tree) { |tree, child| tree + child.printTree(indent) }
end