Class: XML::DOM::Text

Inherits:
CharacterData show all
Defined in:
lib/xml/dom/core.rb,
lib/xml/dom2/text.rb,
lib/xml/dom/digest.rb,
lib/xml/dom2/xpath.rb

Overview

Class XML::DOM::Text

superclass

Node

Direct Known Subclasses

CDATASection

Constant Summary

Constants inherited from Node

Node::ATTRIBUTE_NODE, Node::CDATA_SECTION_NODE, Node::COMMENT_NODE, Node::DOCUMENT_FRAGMENT_NODE, Node::DOCUMENT_NODE, Node::DOCUMENT_TYPE_NODE, Node::ELEMENT_NODE, Node::ENTITY_NODE, Node::ENTITY_REFERENCE_NODE, Node::NODE_NODE, Node::NOTATION_NODE, Node::PROCESSING_INSTRUCTION_NODE, Node::TEXT_NODE

Instance Method Summary collapse

Methods inherited from CharacterData

#appendData, #cloneNode, #data, #data=, #deleteData, #insertData, #length, #nodeValue, #nodeValue=, #replaceData, #substringData

Methods inherited from Node

#+, #<=>, #==, #[], #[]=, #__collectAncestorNS, #__collectDescendatNS, #__sibling, #_ancestor, #_checkNode, #_child, #_descendant, #_following, #_fsibling, #_getChildIndex, #_getNodeByAbsoluteLocationTerm, #_insertNodes, #_matchAttribute?, #_matchNode?, #_matchNodeAttributes?, #_matchNodeType?, #_nodesByLocationTerms, #_nodesByRelativeLocationTerm, #_preceding, #_psibling, #_removeFromTree, #_removeNode, #_searchID, #accept, #accept_name, #appendChild, #attributes, #childNodes, #childNodes=, #children_accept, #children_accept_name, #cloneNode, #each, #firstChild, #getNodesByXPath, #getNodesByXPointer, #hasAttributes, #hasChildNodes, #insertBefore, #inspect, #isSupported, #lastChild, #localname, #makeXPath, #makeXPointer, #namespaceURI, #nextSibling, #nodeValue, #nodeValue=, #ownerDocument, #ownerDocument=, #parentNode, #parentNode=, #prefix, #prefix=, #previousSibling, #removeChild, #replaceChild

Constructor Details

#initialize(text = nil) ⇒ Text

new(text)

text: String


2461
2462
2463
# File 'lib/xml/dom/core.rb', line 2461

def initialize(text = nil)
  super(text)
end

Instance Method Details

#_getMyLocation(parent) ⇒ Object



2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
# File 'lib/xml/dom/core.rb', line 2523

def _getMyLocation(parent)
  index = 1
  parent.childNodes do |child|
    if child == self
      return "child(#{index},#text)"
    end
    if child.nodeType == TEXT_NODE
      index += 1
    end
  end
  nil
end

#_getMyLocationInXPath(parent) ⇒ Object



340
341
342
343
344
345
# File 'lib/xml/dom2/xpath.rb', line 340

def _getMyLocationInXPath(parent)
  n = parent.childNodes.to_a.select { |i|
    i.nodeType == TEXT_NODE or i.nodeType == CDATA_SECTION_NODE
  }.index(self)
  "text()[#{n + 1}]"
end

#dump(depth = 0) ⇒ Object

— Text#dump(depth = 0)

dumps the Text.



2518
2519
2520
2521
# File 'lib/xml/dom/core.rb', line 2518

def dump(depth = 0)
  print ' ' * depth * 2
  print "#{@value.inspect}\n"
end

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



37
38
39
40
41
# File 'lib/xml/dom/digest.rb', line 37

def getDigest(algorithm = Digest::MD5, force = false)
  (!force && @digest) ||
    @digest = algorithm.digest([TEXT_NODE].pack("N") + DOM.tou16(nodeValue))
  @digest
end

#nodeNameObject

DOM


2485
2486
2487
# File 'lib/xml/dom/core.rb', line 2485

def nodeName
  "#text"
end

#nodeTypeObject

DOM


2474
2475
2476
# File 'lib/xml/dom/core.rb', line 2474

def nodeType
  TEXT_NODE
end

#splitText(offset) ⇒ Object

DOM


2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
# File 'lib/xml/dom/core.rb', line 2543

def splitText(offset)
  if offset > @value.length || offset < 0
    raise DOMException.new(DOMException::INDEX_SIZE_ERR)
  end
  newText = @value[offset, @value.length]
  newNode = Text.new(newText)
  if !self.parentNode.nil?
    self.parentNode.insertAfter(newNode, self)
  end
  @value[offset, @value.length] = ""
  newNode
end

#to_sObject

— Text#to_s

return the string representation of the Text.



2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
# File 'lib/xml/dom/core.rb', line 2494

def to_s
  ret = ""
  @value.each_byte do |code|
    case (code)
    when 13
      ret << sprintf("&#x%X;", code)
    when ?&
      ret << "&amp;"
    when ?<
      ret << "&lt;"
    when ?>
      ret << "&gt;"
    else
      ret << code
    end
  end
  ret
end

#trim(preserve = false) ⇒ Object

— Text#trim(preserve = false)

trim extra whitespaces.



2561
2562
2563
2564
2565
2566
2567
# File 'lib/xml/dom/core.rb', line 2561

def trim(preserve = false)
  if !preserve
    @value.sub!(/\A\s*([\s\S]*?)\s*\Z/, "\\1")
    return @value
  end
  nil
end