class XML::DOM::Element

Class XML::DOM::Element

superclass

Node

Class XML::DOM::Element

superclass

Node

Public Class Methods

new(tag = nil, attr = nil, *children) click to toggle source

Class Methods

Calls superclass method XML::DOM::Node::new
# File lib/xml/dom/core.rb, line 1940
def initialize(tag = nil, attr = nil, *children)
  super(*children)
  raise "parameter error" if !tag
  @name = tag.freeze
  if attr.nil?
    @attr = NamedNodeMap.new([])
  elsif attr.is_a?(Hash)
    nodes = []
    attr.each do |key, value|
      nodes.push(Attr.new(key, value))
    end
    @attr = NamedNodeMap.new(nodes)
  elsif attr.is_a?(Array)
    @attr = NamedNodeMap.new(attr)
  elsif attr.is_a?(Attr)
    @attr = NamedNodeMap.new([attr])
  else
    raise "parameter error: #{attr}"
  end
end

Public Instance Methods

_checkNode(node) click to toggle source
# File lib/xml/dom/core.rb, line 2264
def _checkNode(node)
  unless node.nodeType == ELEMENT_NODE ||
      node.nodeType == TEXT_NODE ||
      node.nodeType == COMMENT_NODE ||
      node.nodeType == PROCESSING_INSTRUCTION_NODE ||
      node.nodeType == CDATA_SECTION_NODE ||
      node.nodeType == ENTITY_REFERENCE_NODE
    raise DOMException.new(DOMException::HIERARCHY_REQUEST_ERR)
  end
end
_getIDVals(ids = nil) click to toggle source

get the list of nodeValues by IDs

experimental implement
# File lib/xml/dom/core.rb, line 2216
def _getIDVals(ids = nil)
  if ids.nil?
    doc = ownerDocument
    return [] if doc.nil?
    ids = doc._getIDAttrs
  end

  idelem = []
  if !ids[nodeName].nil?
    return attributes._getValues(ids[nodeName])
  elsif !ids['*'].nil?
    return attributes._getValues(ids['*'])
  end
  return []
end
_getMyLocation(parent) click to toggle source
# File lib/xml/dom/core.rb, line 2159
def _getMyLocation(parent)
  index = 1
  parent.childNodes do |child|
    if child == self
      return "child(#{index},#{@name})"
    end
    if child.nodeType == ELEMENT_NODE && child.nodeName == @name
      index += 1
    end
  end
  nil
end
_getMyLocationInXPath(parent) click to toggle source
# File lib/xml/dom2/xpath.rb, line 327
def _getMyLocationInXPath(parent)
  name = nodeName
  n = parent.childNodes.to_a.select { |i|
    i.nodeType == ELEMENT_NODE and i.nodeName == name
  }.index(self)
  "#{name}[#{n + 1}]"
end
_getNamespaces(parentNamespaces = {}, all = false) click to toggle source
# File lib/xml/dom2/element.rb, line 120
def _getNamespaces(parentNamespaces = {}, all = false)
  if !parentNamespaces
    parentNamespaces = parentNode._getNamespaces(nil, true)
  end
  namespaces = {}
  attributes.each do |a|
    namespaces[a.prefix] = a.namespaceURI if a.prefix
  end
  if @localname
    namespaces[@prefix] = @uri
  end
  parentNamespaces.each do |prefix, uri|
    if all
      if !namespaces.include?(prefix)
        namespaces[prefix] = uri
      end
    else
      if namespaces[prefix] == parentNamespaces[prefix]
        namespaces.delete(prefix)
      end
    end
  end
  namespaces
end
attributes() { |value| ... } click to toggle source
# File lib/xml/dom/core.rb, line 1992
def attributes
  if iterator?
    @attr.each do |key, value|
      yield(value)
    end if @attr
  else
    @attr
  end
end
cloneNode(deep = true) click to toggle source
Calls superclass method XML::DOM::Node#cloneNode
# File lib/xml/dom/core.rb, line 2206
def cloneNode(deep = true)
  attrs = []
  @attr.each do |attr|
    attrs.push(attr.cloneNode(true))
  end
  super(deep, @name, attrs)
end
dump(depth = 0) click to toggle source
# File lib/xml/dom/core.rb, line 2027
def dump(depth = 0)
  attr = ''
  @attr.each do |a|  ## self.attributes do |a|
    attr += a.to_s + ", "
  end if @attr
  attr.chop!
  attr.chop!
  print ' ' * depth * 2
  print "#{@name}(#{attr})\n"
  @children.each do |child|
    child.dump(depth + 1)
  end if @children
end
getAttribute(name) click to toggle source
# File lib/xml/dom/core.rb, line 2057
def getAttribute(name)
  attr = getAttributeNode(name)
  if attr.nil?
    ''
  else
    attr.nodeValue
  end
end
getAttributeNS(nsuri, localname) click to toggle source
DOM2
# File lib/xml/dom2/element.rb, line 441
def getAttributeNS(nsuri, localname)
  attr = getAttributeNodeNS(nsuri, localname)
  if attr.nil?
    ""
  else
    attr.nodeValue
  end
end
getAttributeNode(name) click to toggle source
# File lib/xml/dom/core.rb, line 2103
def getAttributeNode(name)
  @attr.getNamedItem(name)
end
getAttributeNodeNS(nsuri, localname) click to toggle source
DOM2
# File lib/xml/dom2/element.rb, line 475
def getAttributeNodeNS(nsuri, localname)
  attributes.each do |attr|
    return attr if
      attr.namespaceURI == nsuri && attr.localname == localname
  end
  nil
end
getDigest(algorithm = Digest::MD5, force = false) click to toggle source
# File lib/xml/dom/digest.rb, line 75
def getDigest(algorithm = Digest::MD5, force = false)
  return @digest if (!force && @digest)
  attr = attributes
  children = childNodes
  attr_digests = ""
  children_digests = ""
  if attr
    attr_array = attr.sort {|a, b|
      DOM.tou16(a.nodeName) <=> DOM.tou16(b.nodeName)}
    attr_array.each {|a|
      attr_digests << a.getDigest(algorithm, force)
    }
  end
  children_num = 0
  children.each {|c|
    next if c.nodeType == COMMENT_NODE
    children_num += 1
    children_digests << c.getDigest(algorithm, force)
  }
  @digest = algorithm.digest([ELEMENT_NODE].pack("N") +
                             DOM.tou16(nodeName) +
                             "\0\0" +
                             [attr.length].pack("N") +
                             attr_digests +
                             [children_num].pack("N") +
                             children_digests)
end
getElementsByTagName(tagname) click to toggle source
# File lib/xml/dom/core.rb, line 2146
def getElementsByTagName(tagname)
  ret = NodeList.new
  @children.each do |node|
    if node.nodeType == ELEMENT_NODE
      if tagname == '*' || node.nodeName == tagname
        ret << node
      end
      ret << node.getElementsByTagName(tagname)
    end
  end if @children
  ret
end
getElementsByTagNameNS(nsuri, localname) click to toggle source
DOM2
# File lib/xml/dom2/element.rb, line 492
def getElementsByTagNameNS(nsuri, localname)
  ret = NodeList.new
  @children.each do |node|
    if node.nodeType == ELEMENT_NODE
      if (localname == '*' || node.localname == localname) and
          (nsuri == '*' || node.namespaceURI == nsuri)
        ret << node
      end
      ret << node.getElementsByTagNameNS(nsuri, localname)
    end
  end if @children
  ret
end
hasAttribute(name) click to toggle source
DOM2
# File lib/xml/dom2/element.rb, line 507
def hasAttribute(name)
  !getAttributeNode(name).nil?
end
hasAttributeNS(nsuri, localname) click to toggle source
DOM2
# File lib/xml/dom2/element.rb, line 512
def hasAttributeNS(nsuri, localname)
  !getAttributeNodeNS(nsuri, localname).nil?
end
hasAttributes() click to toggle source
DOM2
# File lib/xml/dom2/element.rb, line 436
def hasAttributes()
  attributes.length > 0
end
idAttribute() click to toggle source
# File lib/xml/dom2/element.rb, line 516
def idAttribute; @idAttribute; end
idAttribute=(name) click to toggle source
# File lib/xml/dom2/element.rb, line 517
def idAttribute=(name); @idAttribute = name; end
localname() click to toggle source
DOM2
# File lib/xml/dom2/element.rb, line 433
def localname; @localname; end
namespaceURI() click to toggle source
DOM2
# File lib/xml/dom2/element.rb, line 418
def namespaceURI; @uri; end
nodeName() click to toggle source
# File lib/xml/dom/core.rb, line 1981
def nodeName
  @name
end
Also aliased as: tagName, tagName
nodeType() click to toggle source

Methods

# File lib/xml/dom/core.rb, line 1970
def nodeType
  ELEMENT_NODE
end
normalize() click to toggle source
# File lib/xml/dom/core.rb, line 2181
def normalize
  return if @children.nil?
  old = nil
  children = @children.to_a.dup
  children.each do |child|
    if !old.nil? && old.nodeType == TEXT_NODE &&
        child.nodeType == TEXT_NODE
      old.appendData(child.nodeValue)
      self.removeChild(child)
    else
      if child.nodeType == ELEMENT_NODE
        child.normalize
      end
      old = child
    end
  end
end
prefix() click to toggle source
DOM2
# File lib/xml/dom2/element.rb, line 421
def prefix; @prefix; end
prefix=(prefix) click to toggle source
DOM2
# File lib/xml/dom2/element.rb, line 424
def prefix=(prefix);
  ## to be checked
  @prefix = prefix
  @name = @prefix + ':' + @localname
  @prefix.freeze
  @name.freeze
end
removeAttribute(name) click to toggle source
# File lib/xml/dom/core.rb, line 2091
def removeAttribute(name)
  ret = getAttributeNode(name)
  removeAttributeNode(ret) if ret
end
removeAttributeNS(nsuri, localname) click to toggle source
DOM2
# File lib/xml/dom2/element.rb, line 469
def removeAttributeNS(nsuri, localname)
  ret = getAttributeNodeNS(nsuri, localname)
  removeAttributeNode(ret) if ret
end
removeAttributeNode(oldAttr) click to toggle source
# File lib/xml/dom/core.rb, line 2130
def removeAttributeNode(oldAttr)
  ret = getAttributeNode(oldAttr.nodeName)
  if ret.nil? || ret != oldAttr
    raise DOMException.new(DOMException::NOT_FOUND_ERR)
  end
  @attr.removeNamedItem(oldAttr.nodeName)
  ret
end
setAttribute(name, value) click to toggle source
# File lib/xml/dom/core.rb, line 2073
def setAttribute(name, value)
  if @ownerDocument
    attr = @ownerDocument.createAttribute(name)
    attr.appendChild(@ownerDocument.createTextNode(value))
  else
    attr = Attribute.new(name)
    attr.appendChild(Text.new(value))
  end
  setAttributeNode(attr)
end
setAttributeNS(nsuri, qname, value) click to toggle source
DOM2
# File lib/xml/dom2/element.rb, line 451
def setAttributeNS(nsuri, qname, value)
  if qname.index(':')
    prefix, localname = qname.split(':')
    raise DOMException.new(DOMException::NAMESPACE_ERR) if
      nsuri.nil? or
      (prefix == 'xml' and
       nsuri != 'http://www.w3.org/XML/1998/namespace')
  else
    raise DOMException.new(DOMException::NAMESPACE_ERR) if
      qname == 'xmlns' and
      nsuri != 'http://www.w3.org/2000/xmlns/'
  end
  attr = @ownerDocument.createAttributeNS(nsuri, qname)
  attr.appendChild(@ownerDocument.createTextNode(value))
  setAttributeNodeNS(attr)
end
setAttributeNode(newAttr) click to toggle source
# File lib/xml/dom/core.rb, line 2114
def setAttributeNode(newAttr)
  ret = getAttributeNode(newAttr.nodeName)
  if ret == newAttr
    raise DOMException.new(DOMException::INUSE_ATTRIBUTE_ERR)
  end
  @attr.setNamedItem(newAttr)
  ret
end
setAttributeNodeNS(newAttr) click to toggle source
DOM2
# File lib/xml/dom2/element.rb, line 484
def setAttributeNodeNS(newAttr)
  ret = getAttributeNodeNS(newAttr.namespaceURI, newAttr.localname)
  removeAttributeNode(ret) if ret
  setAttributeNode(newAttr)
  ret
end
tagName()
Alias for: nodeName
to_s() click to toggle source
Calls superclass method XML::DOM::Node#to_s
# File lib/xml/dom/core.rb, line 2007
def to_s
  attr = ''
  @attr.each do |a|
    attr << ' ' + a.to_s
  end if @attr
  content = super
  if content != ''
    ret = "<#{@name}#{attr}>#{content}</#{@name}>"
  else
    ret = "<#{@name}#{attr}/>"
  end
  ret << "\n" if parentNode.nodeType == DOCUMENT_NODE
  ret
end
trim(preserve = false) click to toggle source
# File lib/xml/dom/core.rb, line 2240
def trim(preserve = false)
  if !attributes['xml:space'].nil?
    value = attributes['xml:space'].nodeValue
    if value == 'preserve'
      preserve = true
    elsif value == 'default'
      preserve = false
    end
  end
  return nil if @children.nil?
  children = @children.to_a.dup
  children.each do |child|
    if !preserve && (child.nodeType == TEXT_NODE ||
                     child.nodeType == CDATA_SECTION_NODE)
      if child.trim == ""
        self.removeChild(child)
      end
    else
      child.trim(preserve)
    end
  end
  nil
end