class XML::Smart::Dom::Element
Public Class Methods
new(element)
click to toggle source
# File lib/xml/smart_domelement.rb, line 6 def initialize(element) @node = element end
Public Instance Methods
==(other)
click to toggle source
# File lib/xml/smart_domelement.rb, line 216 def ==(other) return false unless other return false unless other.respond_to?(:unique_id) unique_id == other.unique_id end
===(cls)
click to toggle source
# File lib/xml/smart_domelement.rb, line 10 def ===(cls); self.is_a? cls; end
add(*attrs)
click to toggle source
# File lib/xml/smart_domelement.rb, line 101 def add(*attrs) tmp, update = add_helper(attrs) res = Dom::smart_helper(@node.add_child tmp) if update @node.document.custom_namespace_prefixes_update @node.document.ns_update end res end
add_after(*attrs)
click to toggle source
# File lib/xml/smart_domelement.rb, line 126 def add_after(*attrs) tmp, update = add_helper(attrs) res = Dom::smart_helper(@node.add_next_sibling tmp) if update @node.document.custom_namespace_prefixes_update @node.document.ns_update end res end
add_before(*attrs)
click to toggle source
# File lib/xml/smart_domelement.rb, line 117 def add_before(*attrs) tmp, update = add_helper(attrs) res = Dom::smart_helper(@node.add_previous_sibling tmp) if update @node.document.custom_namespace_prefixes_update @node.document.ns_update end res end
append(*attrs)
click to toggle source
# File lib/xml/smart_domelement.rb, line 110 def append(*attrs) add(*attrs) end
attributes()
click to toggle source
# File lib/xml/smart_domelement.rb, line 191 def attributes; AttributeSet.new @node; end
children()
click to toggle source
# File lib/xml/smart_domelement.rb, line 196 def children; find('*|text()'); end
children?()
click to toggle source
# File lib/xml/smart_domelement.rb, line 197 def children?; find('*|text()').length > 0 end
dump()
click to toggle source
# File lib/xml/smart_domelement.rb, line 136 def dump doc = Nokogiri::XML::Document.new doc.root = @node doc.root.to_s end
element_only?()
click to toggle source
# File lib/xml/smart_domelement.rb, line 210 def element_only?; @node.xpath_fast('*').length > 0 && @node.xpath_fast("string(text())") == ''; end
empty?()
click to toggle source
# File lib/xml/smart_domelement.rb, line 203 def empty?; !children?; end
find(xpath)
click to toggle source
# File lib/xml/smart_domelement.rb, line 12 def find(xpath) Dom::smart_helper(@node.xpath_fast(xpath)) end
mixed?()
click to toggle source
# File lib/xml/smart_domelement.rb, line 204 def mixed?; @node.xpath_fast('*').length > 0 && @node.xpath_fast("string(text())") != ''; end
namespace()
click to toggle source
# File lib/xml/smart_domelement.rb, line 147 def namespace; namespace? ? Namespace.new(@node.namespace) : nil; end
namespace=(n)
click to toggle source
# File lib/xml/smart_domelement.rb, line 148 def namespace=(n) n = case n when Namespace n.prefix when String n else return end tmp = @node.document.custom_namespace_prefixes[n] || @node.document.user_custom_namespace_prefixes[n] unless tmp.nil? @node.namespace_scopes.each do |nss| @node.namespace = nss if nss.href == tmp end end end
namespace?()
click to toggle source
# File lib/xml/smart_domelement.rb, line 146 def namespace?; !@node.namespace.nil?; end
namespaces()
click to toggle source
# File lib/xml/smart_domelement.rb, line 164 def namespaces; NamespaceSet.new(self,@node); end
parent()
click to toggle source
# File lib/xml/smart_domelement.rb, line 198 def parent Dom::smart_helper(@node.parent) end
parent?()
click to toggle source
# File lib/xml/smart_domelement.rb, line 201 def parent?; !@node.parent.nil?; end
path()
click to toggle source
# File lib/xml/smart_domelement.rb, line 214 def path; @node.path[-1] != ']' ? @node.path + '[1]' : @node.path; end
prepend(*attrs)
click to toggle source
# File lib/xml/smart_domelement.rb, line 113 def prepend(*attrs) c = children c.empty? ? add(*attrs) : c.first.add_before(*attrs) end
qname()
click to toggle source
# File lib/xml/smart_domelement.rb, line 190 def qname; QName.new @node; end
replace_by(n)
click to toggle source
# File lib/xml/smart_domelement.rb, line 173 def replace_by(n) case n when Element; Element.new @node.replace(n.instance_variable_get(:@node)) when NodeSet; NodeSet.new @node.replace(n.instance_variable_get(:@nodeset)) else nil end end
replace_by_copy(n)
click to toggle source
# File lib/xml/smart_domelement.rb, line 181 def replace_by_copy(n) case n when Element; Element.new @node.replace(n.instance_variable_get(:@node).dup) when NodeSet; NodeSet.new @node.replace(n.instance_variable_get(:@nodeset).dup) else nil end end
text()
click to toggle source
# File lib/xml/smart_domelement.rb, line 193 def text; @node.xpath_fast("string(text())"); end
text=(t)
click to toggle source
# File lib/xml/smart_domelement.rb, line 194 def text=(t); @node.content = t.to_s if t.respond_to? :to_s; end
text_only?()
click to toggle source
# File lib/xml/smart_domelement.rb, line 207 def text_only?; @node.xpath_fast('*').length == 0 && @node.xpath_fast("string(text())") != ''; end
to_doc()
click to toggle source
# File lib/xml/smart_domelement.rb, line 223 def to_doc doc = Nokogiri::XML::Document.new doc.root = @node Dom.new(doc) end
to_f()
click to toggle source
# File lib/xml/smart_domelement.rb, line 144 def to_f; @node.content.to_f; end
to_i()
click to toggle source
# File lib/xml/smart_domelement.rb, line 143 def to_i; @node.content.to_i; end
to_s()
click to toggle source
# File lib/xml/smart_domelement.rb, line 142 def to_s; @node.content; end
unique_id()
click to toggle source
# File lib/xml/smart_domelement.rb, line 221 def unique_id; @node.pointer_id; end
xinclude!(basedir=nil)
click to toggle source
# File lib/xml/smart_domelement.rb, line 166 def xinclude!(basedir=nil) @node.do_xinclude_manual(basedir) @node.document.custom_namespace_prefixes_update @node.document.ns_update true end
Private Instance Methods
add_helper(attrs)
click to toggle source
# File lib/xml/smart_domelement.rb, line 16 def add_helper(attrs) if attrs.length>0 && attrs[0].is_a?(String) && attrs[0][0] != '?' pfx = '' ns = nil attrs[0] = attrs[0].dup if attrs[0].sub!(/([^:]+):/, '') pfx = $1 if @node.document.user_custom_namespace_prefixes.has_key?(pfx) @node.document.custom_namespace_prefixes.each do |k,v| if @node.document.user_custom_namespace_prefixes[pfx] == v ns = @node.document.user_custom_namespace_prefixes[pfx] end end end if ns.nil? if @node.document.custom_namespace_prefixes.has_key?(pfx) ns = @node.document.custom_namespace_prefixes[pfx] else raise Error, 'No valid namespace' end end end tmp = Nokogiri::XML::Node.new attrs[0], @node.document unless ns.nil? @node.namespace_scopes.each do |nss| tmp.namespace = nss if nss.href == ns end end [1,2].each do |i| if attrs.length > i case attrs[i] when Hash attrs[i].each do |k,v| if k.respond_to?(:to_s) tmp[k.to_s] = v.respond_to?(:to_s) ? v.to_s : '' else raise Error, 'use something that has provides #to_s' end end else tmp.content = attrs[i].respond_to?(:to_s) ? attrs[i].to_s : '' end end end return [tmp,false] elsif attrs.length == 1 && attrs[0].is_a?(XML::Smart::Dom::Element) ele = attrs[0].instance_variable_get(:@node) same = ele.document.root.pointer_id == @node.document.root.pointer_id return [same ? ele : ele.dup, !same] elsif attrs.length == 1 && attrs[0].is_a?(XML::Smart::Dom::NodeSet) nos = attrs[0].instance_variable_get(:@nodeset) if nos.length > 0 same = nos.first.document.root.pointer_id == @node.document.root.pointer_id if same return [nos, false] else tnos = nos.map{|e|e.dup} return [Nokogiri::XML::NodeSet.new(nos.first.document,tnos), true] end else return [nos, false] end elsif attrs.length == 2 && attrs[0].is_a?(XML::Smart::Dom::Element) && (attrs[1] == XML::Smart::COPY || attrs[1] == XML::Smart::MOVE) ele = attrs[0].instance_variable_get(:@node) same = ele.document.root.pointer_id == @node.document.root.pointer_id return [attrs[1] == XML::Smart::COPY ? ele.dup : ele, !same] elsif attrs.length == 2 && attrs[0].is_a?(XML::Smart::Dom::NodeSet) && (attrs[1] == XML::Smart::COPY || attrs[1] == XML::Smart::MOVE) nos = attrs[0].instance_variable_get(:@nodeset) if nos.length > 0 same = nos.first.document.root.pointer_id == @node.document.root.pointer_id if attrs[1] == XML::Smart::COPY tnos = nos.map{|e|e.dup} nos = Nokogiri::XML::NodeSet.new(nos.first.document,tnos) end return [nos, !same] else return [nos, false] end elsif attrs.length == 2 && attrs[0].is_a?(String) && attrs[1].is_a?(String) && attrs[0][0] == '?' tmp = Nokogiri::XML::ProcessingInstruction.new @node.document, attrs[0].sub(/./,''), attrs[1] return [tmp,false] end return [nil, false] end