Base module for HTML elements
Enumerable
init_body(&block) {|| ...}
|
set the block 's result to body
# File lib/amrita/node.rb, line 260
def init_body(&block)
if block_given?
@body = to_node(yield)
else
@body = Null
end
end
# File lib/amrita/node.rb, line 269
def body
if defined? @body
@body
else
Null
end
end
test if it has any children
# File lib/amrita/node.rb, line 278
def no_child?
body == Null
end
return an Array of child Node or an empty Array if
it does not have a body
# File lib/amrita/node.rb, line 283
def children
if no_child?
[]
else
[ body ]
end
end
apply_to_children(&block)
|
# File lib/amrita/node.rb, line 291
def apply_to_children(&block)
self
end
delete_if(&block) {|self| ...}
|
# File lib/amrita/node.rb, line 295
def delete_if(&block)
if yield(self)
Null
else
self
end
end
# File lib/amrita/node.rb, line 304
def to_node(n)
case n
when nil, false
Null
when Node
n
when Array
case n.size()
when 0
Null
when 1
to_node(n[0])
else
NodeArray.new(*n)
end
else
TextElement.new(n.to_s)
end
end
# File lib/amrita/node.rb, line 325
def amrita_id
nil
end
# File lib/amrita/node.rb, line 329
def inspect
to_ruby
end
# File lib/amrita/node.rb, line 334
def +(node)
NodeArray.new(self, to_node(node))
end
# File lib/amrita/node.rb, line 339
def *(n)
raise "can't #{self.class} * #{n}(#{n.class})" unless n.kind_of?(Integer)
a = (0...n).collect { |i| self }
NodeArray.new(*a)
end
each_node(&block) {|self| ...}
|
iterate on self and children
# File lib/amrita/node.rb, line 346
def each_node(&block)
c = children # save children before yield
yield(self)
c.each do |n|
n.each_node(&block)
end
end
each_element(&block) {|node| ...}
|
iterate on child Elements
# File lib/amrita/node.rb, line 355
def each_element(&block)
each_node do |node|
yield(node) if node.kind_of?(Element)
end
end
each_element_with_id(recursive=false, &block)
|
iterate on child Elements with id. If recursive == false, don't go to
children of an Element with id.
# File lib/amrita/node.rb, line 364
def each_element_with_id(recursive=false, &block)
children.each do |node|
node.each_element_with_id(recursive, &block)
end
end
test if an Element or children has any id
# File lib/amrita/node.rb, line 371
def has_id_element?
each_node do |n|
next unless n.kind_of?(Element)
return true if n.amrita_id
return true if n.get_expandable_attrs.size > 0
end
false
end