Class: XML::DOM::Node

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable
Defined in:
lib/xml/dom/core.rb,
lib/xml/dom2/node.rb,
lib/xml/dom/digest.rb,
lib/xml/dom2/xpath.rb,
lib/xml/dom/visitor.rb

Overview

Class XML::DOM::Node

XML::Grove::Visitor like interfaces.

Constant Summary collapse

NODE_NODE =
DOM
0
ELEMENT_NODE =
1
ATTRIBUTE_NODE =
2
TEXT_NODE =
3
CDATA_SECTION_NODE =
4
ENTITY_REFERENCE_NODE =
5
ENTITY_NODE =
6
PROCESSING_INSTRUCTION_NODE =
7
COMMENT_NODE =
8
DOCUMENT_NODE =
9
DOCUMENT_TYPE_NODE =
10
DOCUMENT_FRAGMENT_NODE =
11
NOTATION_NODE =
12

Instance Method Summary collapse

Constructor Details

#initialize(*children) ⇒ Node

new([child1, child2, …]) or new(child1, child2, …)

child?: String or Node


224
225
226
227
228
# File 'lib/xml/dom/core.rb', line 224

def initialize(*children)
  @parent = nil
  @children = nil
  self.childNodes = children if children.length > 0
end

Instance Method Details

#+(node) ⇒ Object

— Node#+(node)

concat node to Node.



393
394
395
# File 'lib/xml/dom/core.rb', line 393

def +(node)
  [self, node]
end

#<=>(node) ⇒ Object



561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
# File 'lib/xml/dom2/node.rb', line 561

def <=>(node)
  ancestors1 = [self]
  ancestors2 = [node]
  p = self
  while p = p.parentNode
    ancestors1.unshift(p)
  end
  p = node
  while p = p.parentNode
    ancestors2.unshift(p)
  end
  raise "different document" unless ancestors1[0].equal?(ancestors2[0])
  ret = 0
  i = 0
  for i in 1...ancestors1.size
    next if ancestors1[i].equal?(ancestors2[i])
    return 1 if ancestors2[i].nil?
    children = ancestors1[i - 1].childNodes.to_a
    return children.index(ancestors1[i]) - children.index(ancestors2[i])
  end
  return -1 if ancestors2.size > i + 1
  0
end

#==(node) ⇒ Object



557
558
559
# File 'lib/xml/dom2/node.rb', line 557

def ==(node)
  equal?(node)
end

#[](index) ⇒ Object

— Node#[](index)

get children node with []-style.



384
385
386
# File 'lib/xml/dom/core.rb', line 384

def [](index)
  @children[index]
end

#[]=(index, nodes) ⇒ Object

— Node#[]=(index, nodes)

set children node as nodes with []-style.



372
373
374
375
376
377
# File 'lib/xml/dom/core.rb', line 372

def []=(index, nodes)
  @children[index..index] = nodes
  @children.each do |child|
    child.parentNode = self
  end if @children
end

#__collectAncestorNS(ns = {}) ⇒ Object



284
285
286
287
288
289
290
291
292
# File 'lib/xml/dom2/xpath.rb', line 284

def __collectAncestorNS(ns = {})
  node = self
  while node
    prefix = node.prefix
    uri = node.namespaceURI
    ns[prefix] = uri unless ns.has_key?(prefix)
    node = node.parentNode
  end
end

#__collectDescendatNS(ns = {}) ⇒ Object



274
275
276
277
278
279
280
281
282
# File 'lib/xml/dom2/xpath.rb', line 274

def __collectDescendatNS(ns = {})
  childNodes.each do |node|
    next if node.nodeType != ELEMENT_NODE
    prefix = node.prefix
    uri = node.namespaceURI
    ns[prefix] = uri unless ns.has_key?(prefix)
    node.__collectDescendatNS(ns)
  end
end

#__sibling(reverse, only_appeared_before_self) ⇒ Object

Masaki Fukushima


703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
# File 'lib/xml/dom/core.rb', line 703

def __sibling(reverse, only_appeared_before_self)
  return if @parent.nil?
  self_appeared = false
  @parent.childNodes.reversible_each(reverse) do |node|
    if node == self
      self_appeared = true
      next
    end
    if only_appeared_before_self
      break if self_appeared
      yield node
    else # only appeared after self
      yield node if self_appeared
    end
  end
end

#_ancestor(reverse = false) {|@parent| ... } ⇒ Object

Masaki Fukushima

Yields:

  • (@parent)


695
696
697
698
699
700
# File 'lib/xml/dom/core.rb', line 695

def _ancestor(reverse = false)
  return if @parent.nil?
  yield @parent if !reverse
  @parent._ancestor(reverse) do |node| yield node end
  yield @parent if reverse
end

#_checkNode(node) ⇒ Object

Raises:



504
505
506
# File 'lib/xml/dom/core.rb', line 504

def _checkNode(node)
  raise DOMException.new(DOMException::HIERARCHY_REQUEST_ERR)
end

#_child(reverse = false) ⇒ Object

Masaki Fukushima


676
677
678
679
680
681
# File 'lib/xml/dom/core.rb', line 676

def _child(reverse = false)
  return if @children.nil?
  @children.reversible_each(reverse) do |child|
    yield child
  end
end

#_descendant(reverse = false) ⇒ Object

Masaki Fukushima


684
685
686
687
688
689
690
691
692
# File 'lib/xml/dom/core.rb', line 684

def _descendant(reverse = false)
  return if @children.nil?
  @children.reversible_each(reverse) do |child|
    yield child
    child._descendant(reverse) do |node|
      yield node
    end
  end
end

#_following(reverse = false) ⇒ Object

Masaki Fukushima


751
752
753
754
755
756
757
758
759
760
761
762
763
764
# File 'lib/xml/dom/core.rb', line 751

def _following(reverse = false)
  return if @parent.nil?
  next_sib = nextSibling
  if next_sib
    next_sib._following(reverse)  {|node| yield node} if reverse
    yield next_sib
    next_sib._descendant(reverse) {|node| yield node}
    next_sib._following(reverse)  {|node| yield node} if !reverse
  else
    @parent._following(reverse) {|node| yield node} if reverse
    yield @parent
    @parent._following(reverse) {|node| yield node} if !reverse
  end
end

#_fsibling(reverse = false) ⇒ Object

Masaki Fukushima


728
729
730
731
732
# File 'lib/xml/dom/core.rb', line 728

def _fsibling(reverse = false)
  __sibling(reverse, reverse) do |sib|
    yield sib
  end
end

#_getChildIndex(node) ⇒ Object



486
487
488
489
490
491
492
493
494
495
# File 'lib/xml/dom/core.rb', line 486

def _getChildIndex(node)
  index = 0
  @children.each do |child|
    if child == node
      return index
    end
    index += 1
  end
  nil
end

#_getMyLocation(parent) ⇒ Object



651
652
653
654
655
656
657
658
# File 'lib/xml/dom/core.rb', line 651

def _getMyLocation(parent)
  index = parent._getChildIndex(self)
  if !index.nil?
    "child(#{index + 1},#all)"
  else
    nil
  end
end

#_getMyLocationInXPath(parent) ⇒ Object



306
307
308
309
# File 'lib/xml/dom2/xpath.rb', line 306

def _getMyLocationInXPath(parent)
  n = parent.childNodes.index(self)
  "node()[#{n + 1}]"
end

#_getNodeByAbsoluteLocationTerm(location) ⇒ Object

Masaki Fukushima


954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
# File 'lib/xml/dom/core.rb', line 954

def _getNodeByAbsoluteLocationTerm(location)
  case location
  when 'root()', ''
    if nodeType == DOCUMENT_NODE
      root = documentElement
    elsif !ownerDocument.nil?
      root = ownerDocument.documentElement
    end
    root = self if root.nil?
    return root
  when 'origin()'
    return self
  when /^id\(([^\)]*)\)$/
    value = $1
    raise "invalid id value: #{value}" if value !~ Spec::Name
    return _searchID(value)
  when /^html\(([^\)]*)\)$/
    value = $1
    return getNodesByXPointer("root().descendant(1,A,NAME,\"#{value}\")")[0]
  else
    raise "unknown keyword: #{location}"
  end
end

#_insertNodes(index, node) ⇒ Object



508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# File 'lib/xml/dom/core.rb', line 508

def _insertNodes(index, node)
  if node.nodeType == DOCUMENT_FRAGMENT_NODE

    node.childNodes.to_a.each_with_index do |n, i|
      if index == -1
        _insertNodes(-1, n)
      else
        _insertNodes(index + i, n)
      end
    end
  elsif node.is_a?(Node)
    ## to be checked
    _checkNode(node)
    node._removeFromTree
    if index == -1
      @children.push(node)
    else
      @children[index, 0] = node
    end
    node.parentNode = self
  else
    raise ArgumentError, "invalid value for Node"
  end
end

#_matchAttribute?(attr, value) ⇒ Boolean

Masaki Fukushima

Returns:

  • (Boolean)


767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
# File 'lib/xml/dom/core.rb', line 767

def _matchAttribute?(attr, value)
  case value
  when '*'
    return !attr.nil?
  when '#IMPLIED'
    return attr.nil?
  else
    return false if attr.nil?
  end

  case value
  when /^"([^"]*)"$/, /^'([^']*)'$/
    ignore_case = false
    value = $1
  when Spec::Name
    ignore_case = true
  else
    raise "invalid attribute value: #{value}"
  end
  if ignore_case
    return attr.nodeValue.downcase == value.downcase
  else
    return attr.nodeValue == value
  end
end

#_matchNode?(node, ntype, attributes) ⇒ Boolean

Masaki Fukushima

Returns:

  • (Boolean)


850
851
852
853
# File 'lib/xml/dom/core.rb', line 850

def _matchNode?(node, ntype, attributes)
  _matchNodeType?(node, ntype) &&
    _matchNodeAttributes?(node, attributes)
end

#_matchNodeAttributes?(node, attributes) ⇒ Boolean

Masaki Fukushima

Returns:

  • (Boolean)

Raises:

  • (TypeError)


794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
# File 'lib/xml/dom/core.rb', line 794

def _matchNodeAttributes?(node, attributes)
  return true     if attributes.nil?
  raise TypeError if !attributes.is_a?(Hash)
  return true     if attributes.length == 0
  return false    if node.nodeType != ELEMENT_NODE

  attributes.each do |name, value|
    case name
    when '*'
      return catch(:match) {
        node.attributes.each do |attr|
          throw(:match, true) if _matchAttribute?(attr, value)
        end
        false
      }
    when Spec::Name
      attr = node.attributes[name] unless node.attributes.nil?
      return _matchAttribute?(attr, value)
    else
      raise "invalid attribute name: '#{name}'"
    end
  end
end

#_matchNodeType?(node, ntype) ⇒ Boolean

Masaki Fukushima

Returns:

  • (Boolean)


819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
# File 'lib/xml/dom/core.rb', line 819

def _matchNodeType?(node, ntype)
  case ntype
  when '#element'
    return (node.nodeType == ELEMENT_NODE)
  when '#pi'
    return (node.nodeType == PROCESSING_INSTRUCTION_NODE)
  when '#comment'
    return (node.nodeType == COMMENT_NODE)
  when '#text'
    return (node.nodeType == TEXT_NODE ||
            node.nodeType == CDATA_SECTION_NODE)
  when '#cdata'
    return (node.nodeType == CDATA_SECTION_NODE)
  when '#all'
    case node.nodeType
    when ELEMENT_NODE, PROCESSING_INSTRUCTION_NODE, COMMENT_NODE,
        TEXT_NODE, CDATA_SECTION_NODE
      return true
    else
      return false
    end
  when /^#/
    raise "unknown node type: '#{ntype}'"
  when Spec::Name
    return (node.nodeType == ELEMENT_NODE && node.nodeName == ntype)
  else
    raise "invalid element type: '#{ntype}'"
  end
end

#_nodesByLocationTerms(location, pre_keyword = nil) ⇒ Object

Masaki Fukushima


906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
# File 'lib/xml/dom/core.rb', line 906

def _nodesByLocationTerms(location, pre_keyword = nil)
  if location !~ /^([a-z]*)\(([^)]*)\)(\.(.+))?$/
    raise "invalid location: \"#{location}\""
  end
  keyword = $1
  args = $2
  rest = $4
  ## omitted keyword
  keyword = pre_keyword if keyword == ''
  if keyword.nil?
    raise "cannot determine preceding keyword: \"#{location}\""
  end

  case keyword
  when 'child', 'descendant', 'ancestor', 'psibling', 'fsibling',
      'preceding', 'following'
    # relative location term
    _nodesByRelativeLocationTerm("#{keyword}(#{args})") do |node|
      if rest.nil?
        yield node
      else
        node._nodesByLocationTerms(rest, keyword) do |n|
          yield n
        end
      end
    end
  when 'attr'
    # attribute location term
    if args !~ Spec::Name
      raise "invalid attribute name: '#{args}'"
    end
    attr = attributes[args]
    value = (attr.nil? ? nil : Text.new(attr.nodeValue))
    if rest.nil?
      yield value
    elsif !value.nil?
      value._nodesByLocationTerms(rest) do |node|
        yield node
      end
    end
  when 'span', 'string'
    raise "unsupported keyword: '#{keyword}'"
  else
    raise "unknown keyword: '#{keyword}'"
  end
end

#_nodesByRelativeLocationTerm(location) ⇒ Object

Masaki Fukushima


856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
# File 'lib/xml/dom/core.rb', line 856

def _nodesByRelativeLocationTerm(location)
  if location !~ /^([a-z]+)\(([^\)]*)\)$/
    raise "invalid relative location: '#{location}'"
  end
  keyword = $1
  args = $2.split(/,/)
  number = args.shift
  ntype = args.shift
  ntype = '#element' if ntype.nil?
  attributes = args

  reverse = false
  # check instance number
  case number
  when nil, ''
    raise "missing instance number: '#{location}'"
  when 'all'
  when Spec::Instance
    number = number.to_i
    if number < 0
      reverse = true
      number = -number
    end
  else
    raise "unknown instance number: '#{number}'"
  end

  # check attributes
  if attributes.length % 2 != 0
    raise " missing attribute value: '#{location}'"
  end
  attributes = Hash[*attributes]

  # iterate over nodes specified with keyword
  i = 0
  self.send("_#{keyword}", reverse) do |node|
    next unless _matchNode?(node, ntype, attributes)
    if number == "all"
      yield node
    else
      i += 1
      if i >= number
        yield node
        break
      end
    end
  end
end

#_preceding(reverse = false) ⇒ Object

Masaki Fukushima


735
736
737
738
739
740
741
742
743
744
745
746
747
748
# File 'lib/xml/dom/core.rb', line 735

def _preceding(reverse = false)
  return if @parent.nil?
  prev_sib = previousSibling
  if prev_sib
    prev_sib._preceding(reverse)   {|node| yield node} if reverse
    yield prev_sib
    prev_sib._descendant(!reverse) {|node| yield node}
    prev_sib._preceding(reverse)   {|node| yield node} if !reverse
  else
    @parent._preceding(reverse) {|node| yield node} if reverse
    yield @parent
    @parent._preceding(reverse) {|node| yield node} if !reverse
  end
end

#_psibling(reverse = false) ⇒ Object

Masaki Fukushima


721
722
723
724
725
# File 'lib/xml/dom/core.rb', line 721

def _psibling(reverse = false)
  __sibling(!reverse, reverse) do |sib|
    yield sib
  end
end

#_removeFromTreeObject



497
498
499
500
501
502
# File 'lib/xml/dom/core.rb', line 497

def _removeFromTree
  parent = parentNode
  if parent
    parent.removeChild(self)
  end
end

#_removeNode(index, node) ⇒ Object



533
534
535
536
# File 'lib/xml/dom/core.rb', line 533

def _removeNode(index, node)
  @children[index, 1] = nil
  node.parentNode = nil
end

#_searchID(value, ids = nil) ⇒ Object

get the Node object by IDs

experimental implement


627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
# File 'lib/xml/dom/core.rb', line 627

def _searchID(value, ids = nil)
  if ids.nil?
    doc = nil
    if nodeType == DOCUMENT_NODE
      doc = self
    elsif !ownerDocument.nil?
      doc = ownerDocument
    else
      return nil
    end
    ids = doc._getIDAttrs
  end
  if nodeType == ELEMENT_NODE && _getIDVals(ids).include?(value)
    return self
  elsif !@children.nil?
    @children.each do |node|
      if !(match = node._searchID(value, ids)).nil?
        return match
      end
    end
  end
  return nil
end

#accept(visitor, *rest) ⇒ Object

XML::Grove::Visitor like interfaces



110
111
112
113
# File 'lib/xml/dom/visitor.rb', line 110

def accept(visitor, *rest)
  typename = self.class.to_s.sub(/.*?([^:]+)$/, '\1')
  visitor.send("visit_" + typename, self, *rest)
end

#accept_name(visitor, *rest) ⇒ Object

— Node#accept_name(visitor, *rest)

call back visit_name_* method.



120
121
122
123
124
125
126
127
# File 'lib/xml/dom/visitor.rb', line 120

def accept_name(visitor, *rest)
  if nodeType == ELEMENT_NODE
    name_method = "visit_name_" + nodeName
    visitor.send(name_method, self, *rest)
  else
    self.accept(visitor, *rest)
  end
end

#appendChild(newChild) ⇒ Object

DOM


609
610
611
612
# File 'lib/xml/dom/core.rb', line 609

def appendChild(newChild)
  @children = NodeList.new if !@children
  _insertNodes(-1, newChild)
end

#attributesObject

DOM


359
360
361
# File 'lib/xml/dom/core.rb', line 359

def attributes
  nil
end

#childNodesObject

DOM


311
312
313
314
315
316
317
318
319
320
# File 'lib/xml/dom/core.rb', line 311

def childNodes
  if iterator?
    @children.each do |child|
      yield(child)
    end if @children
  else
    return @children if !@children.nil?
    @children = NodeList.new
  end
end

#childNodes=(p) ⇒ Object

— Node#childNodes=(p)

set child node as p.



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/xml/dom/core.rb', line 328

def childNodes=(p)
  if @children.nil?
    @children = NodeList.new
  else
    @children.to_a.clear
  end
  if p.nil? || (p.is_a?(Array) && p.length == 0)
    return
  end
  p.flatten!
  p.each do |child|
    if child.is_a?(String)
      c = Text.new(child)
      @children.push(c)
      c.parentNode = self
    elsif child.is_a?(Node)
      @children.push(child)
      child.parentNode = self
    else
      raise "parameter error"
    end
  end if p
end

#children_accept(visitor, *rest) ⇒ Object

— Node#children_accept(visitor, *rest)

for each children, call back visit_* methods.



134
135
136
137
138
139
140
# File 'lib/xml/dom/visitor.rb', line 134

def children_accept(visitor, *rest)
  ret = []
  @children && @children.each { |node|
    ret.push(node.accept(visitor, *rest))
  }
  ret
end

#children_accept_name(visitor, *rest) ⇒ Object

— Node#children_accept_name(visitor, *rest)

for each children, call back visit_name_* method.



147
148
149
150
151
152
153
# File 'lib/xml/dom/visitor.rb', line 147

def children_accept_name(visitor, *rest)
  ret = []
  @children && @children.each { |node|
    ret.push(node.accept_name(visitor, *rest))
  }
  ret
end

#cloneNode(deep = true, *args) ⇒ Object

DOM


1044
1045
1046
1047
1048
1049
1050
1051
1052
# File 'lib/xml/dom/core.rb', line 1044

def cloneNode(deep = true, *args)
  ret = self.class.new(*args)
  if (deep)
    @children.each do |child|
      ret.appendChild(child.cloneNode(true))
    end
  end if @children
  ret
end

#dump(depth = 0) ⇒ Object

— Node#dump(depth = 0)

dump the Node.



411
412
413
414
415
416
417
# File 'lib/xml/dom/core.rb', line 411

def dump(depth = 0)
  print ' ' * depth * 2
  print nodeName + "\n"
  @children.each do |child|
    child.dump(depth + 1)
  end if @children
end

#eachObject



536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
# File 'lib/xml/dom2/node.rb', line 536

def each
  sibstack = []
  siblings = [ self ]
  while true
    if siblings.length == 0
      break if sibstack.length == 0
      siblings = sibstack.pop
      next
    end
    node = siblings.shift
    yield(node)
    children = node.childNodes
    if !children.nil?
      sibstack.push(siblings)
      siblings = children.to_a.dup
    end
  end
end

#firstChildObject

DOM


435
436
437
438
# File 'lib/xml/dom/core.rb', line 435

def firstChild
  return nil if !@children || @children.length == 0
  return @children[0]
end

#getDigest(algorithm = Digest::MD5, force = false) ⇒ Object



31
32
33
# File 'lib/xml/dom/digest.rb', line 31

def getDigest(algorithm = Digest::MD5, force = false)
  nil
end

#getNodesByXPath(xpath, ns = {}) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
# File 'lib/xml/dom2/xpath.rb', line 294

def getNodesByXPath(xpath, ns = {})
  xpath = XMLScan::XPath.compile(xpath) unless xpath.is_a? XMLScan::XPath
  if ns.length == 0
    ## collect namespaces
    __collectAncestorNS(ns)
    __collectDescendatNS(ns)
  end
  ret = xpath.call(XPath::DOM::Context.new(self, ns))
  raise "return value is not NodeSet" unless ret.is_a? Array
  ret
end

#getNodesByXPointer(pointer) ⇒ Object

Masaki Fukushima


984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
# File 'lib/xml/dom/core.rb', line 984

def getNodesByXPointer(pointer)
  if pointer !~ /^([a-z]+)\(([^)]*)\)(\.(.+))?$/
    raise "invalid XPointer: \"#{pointer}\""
  end
  keyword = $1
  args = $2
  rest = $4

  case keyword
  when 'root', 'origin', 'id', 'html'
    src = _getNodeByAbsoluteLocationTerm("#{keyword}(#{args})")
  else
    src = _getNodeByAbsoluteLocationTerm("root()")
    rest = pointer
  end

  ret = NodeList.new
  if src.nil?
    # no match
  elsif rest.nil?
    yield src if iterator?
    ret << src
  else
    src._nodesByLocationTerms(rest) do |node|
      yield node if iterator?
      ret << node
    end
  end
  ret
end

#hasAttributesObject

DOM2


532
# File 'lib/xml/dom2/node.rb', line 532

def hasAttributes(); false; end

#hasChildNodesObject

DOM


621
622
623
# File 'lib/xml/dom/core.rb', line 621

def hasChildNodes
  !@children.nil? && @children.length > 0
end

#insertBefore(newChild, refChild) ⇒ Object

DOM

Raises:



559
560
561
562
563
564
565
566
# File 'lib/xml/dom/core.rb', line 559

def insertBefore(newChild, refChild)
  if @children.nil? || @children.length == 0
    raise DOMException.new(DOMException::NOT_FOUND_ERR)
  end
  index = _getChildIndex(refChild)
  raise DOMException.new(DOMException::NOT_FOUND_ERR) if !index
  _insertNodes(index, newChild)
end

#inspectObject

— Node#inspect()

returns the human-readable string representation.



424
425
426
# File 'lib/xml/dom/core.rb', line 424

def inspect
  "#<#{self.class}: #{self.nodeName}>"
end

#isSupported(feature, version) ⇒ Object

DOM2


509
510
511
512
513
514
515
# File 'lib/xml/dom2/node.rb', line 509

def isSupported(feature, version)
  if (feature =~ /^XML$/i || feature =~ /^Core$/i) &&
      (version.nil? || version == "1.0" || version == "2.0")
    return true
  end
  false
end

#lastChildObject

DOM


447
448
449
450
# File 'lib/xml/dom/core.rb', line 447

def lastChild
  return nil if !@children || @children.length == 0
  return @children[-1]
end

#localnameObject

DOM2


529
# File 'lib/xml/dom2/node.rb', line 529

def localname; nil; end

#makeXPathObject



311
312
313
314
315
316
317
318
319
320
# File 'lib/xml/dom2/xpath.rb', line 311

def makeXPath
  dst = []
  node = self
  while parent = node.parentNode
    dst.push node._getMyLocationInXPath(parent)
    node = parent
  end
  dst.reverse!
  '/' + dst.join('/')
end

#makeXPointer(use_id = true) ⇒ Object

— Node#makeXPointer(use_id = true)

return XPointer’s expression of this node.



665
666
667
668
669
670
671
672
673
# File 'lib/xml/dom/core.rb', line 665

def makeXPointer(use_id = true)
  if use_id && !attributes.nil? && !(idvals = _getIDVals).empty?
    "id(#{idvals[0]})"
  elsif @parent.nil? || @parent.nodeType == DOCUMENT_NODE
    "root()"
  else
    @parent.makeXPointer(use_id) + "." + self._getMyLocation(@parent)
  end
end

#namespaceURIObject

DOM2


518
# File 'lib/xml/dom2/node.rb', line 518

def namespaceURI; nil; end

#nextSiblingObject

DOM


476
477
478
479
480
481
482
483
484
# File 'lib/xml/dom/core.rb', line 476

def nextSibling
  return nil if !@parent
  nexts = nil
  @parent.childNodes.reverse.each do |child|
    return nexts if child == self
    nexts = child
  end
  nil
end

#nodeNameObject

DOM


274
275
276
# File 'lib/xml/dom/core.rb', line 274

def nodeName
  "#node"
end

#nodeTypeObject

DOM


262
263
264
# File 'lib/xml/dom/core.rb', line 262

def nodeType
  NODE_NODE
end

#nodeValueObject

DOM


290
# File 'lib/xml/dom/core.rb', line 290

def nodeValue; nil; end

#nodeValue=(p) ⇒ Object

DOM


299
300
301
# File 'lib/xml/dom/core.rb', line 299

def nodeValue=(p)
  ## no effect
end

#ownerDocumentObject

DOM


1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
# File 'lib/xml/dom/core.rb', line 1023

def ownerDocument
  return @ownerDocument if @ownerDocument
  parent = self.parentNode
  return nil if parent.nil?
  if parent.nodeType == DOCUMENT_NODE
    return parent
  else
    return parent.ownerDocument
  end
end

#ownerDocument=(document) ⇒ Object



1034
# File 'lib/xml/dom/core.rb', line 1034

def ownerDocument=(document); @ownerDocument = document; end

#parentNodeObject

DOM


240
241
242
# File 'lib/xml/dom/core.rb', line 240

def parentNode
  @parent
end

#parentNode=(p) ⇒ Object

— Node#parentNode=(p)

set node p as parent.



250
251
252
# File 'lib/xml/dom/core.rb', line 250

def parentNode=(p)
  @parent = p
end

#prefixObject

DOM2


521
# File 'lib/xml/dom2/node.rb', line 521

def prefix; nil; end

#prefix=(prefix) ⇒ Object

DOM2


524
525
526
# File 'lib/xml/dom2/node.rb', line 524

def prefix=(prefix);
  ## no effect
end

#previousSiblingObject

DOM


459
460
461
462
463
464
465
466
467
# File 'lib/xml/dom/core.rb', line 459

def previousSibling
  return nil if !@parent
  prev = nil
  @parent.childNodes do |child|
    return prev if child == self
    prev = child
  end
  nil
end

#removeChild(oldChild) ⇒ Object

DOM

Raises:



592
593
594
595
596
597
598
599
600
# File 'lib/xml/dom/core.rb', line 592

def removeChild(oldChild)
  if @children.nil? || @children.length == 0
    raise DOMException.new(DOMException::NOT_FOUND_ERR)
  end
  index = _getChildIndex(oldChild)
  raise DOMException.new(DOMException::NOT_FOUND_ERR) if !index
  _removeNode(index, oldChild)
  oldChild
end

#replaceChild(newChild, oldChild) ⇒ Object

DOM

Raises:



575
576
577
578
579
580
581
582
583
# File 'lib/xml/dom/core.rb', line 575

def replaceChild(newChild, oldChild)
  if @children.nil? || @children.length == 0
    raise DOMException.new(DOMException::NOT_FOUND_ERR)
  end
  index = _getChildIndex(oldChild)
  raise DOMException.new(DOMException::NOT_FOUND_ERR) if !index
  _removeNode(index, oldChild)
  _insertNodes(index, newChild)
end

#to_sObject

— Node#to_s

returns the string representation of the Node.



402
403
404
# File 'lib/xml/dom/core.rb', line 402

def to_s
  @children.to_s
end

#trim(preserve = false) ⇒ Object

trim extra whitespaces



1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
# File 'lib/xml/dom/core.rb', line 1062

def trim(preserve = false)
  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