Module: VCDOM::MiniDOM::ModParentNode

Includes:
ParentNodeManageable
Included in:
Attr, Document, Element
Defined in:
lib/vcdom/minidom/mod_parent_node.rb

Defined Under Namespace

Modules: ParentNodeManageable

Instance Method Summary collapse

Instance Method Details

#append_child(new_child) ⇒ Object

appendChild modified in DOM Level 3

Adds the node newChild to the end of the list of children of this node. 
If the newChild is already in the tree, it is first removed.

Parameters
  newChild of type Node
    The node to add.
    If it is a DocumentFragment object, the entire contents of the document fragment are moved 
    into the child list of this node
Return Value
  Node
    The node added.
Exceptions
  DOMException
    HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not 
              allow children of the type of the newChild node, or if the 
              node to append is one of this node's ancestors or this node 
              itself, or if this node is of type Document and the DOM 
              application attempts to append a second DocumentType or 
              Element node.
      #=> newNode がこのノードの子になれない nodeType である場合や, newNode がこのノードの先祖である場合や自分自身
      #=> である場合, また, もしこのノードのタイプが Document で 2 つ目の DocumentType や Element を追加しようとした場合など
      #=> にこの例外が発生する.
    WRONG_DOCUMENT_ERR: Raised if newChild was created from a different 
              document than the one that created this node.
    (DOMImplementation より
         WRONG_DOCUMENT_ERR: Raised if doctype has already been used with a different 
               document or was created from a different implementation.
    NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly or if the 
              previous parent of the node being inserted is readonly.
    NOT_SUPPORTED_ERR: if the newChild node is a child of the Document node, this exception might 
              be raised if the DOM implementation doesn't support the removal of the DocumentType 
              child or Element child.


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/vcdom/minidom/mod_parent_node.rb', line 145

def append_child( new_child )
  new_child_list = []
  if new_child.node_type == Node::DOCUMENT_FRAGMENT_NODE then
    while new_child.has_child_nodes do
      new_child_list << new_child.remove_child( new_child.first_child )
    end
  else
    new_child_list << new_child
  end
  new_child_list.each do |nc|
    # Hierarchy request err のチェック
    begin
      check_hierarchy( nc )
    rescue => err
      raise err
    end
    # Wrong document err のチェック
    cdoc = ( self.owner_document or self )
    if nc.node_type != Node::DOCUMENT_TYPE_NODE then
      if nc.owner_document != cdoc then
        raise DOMException.new( DOMException::WRONG_DOCUMENT_ERR, 
                'The new child was created from a different document than the one that created this node.' )
      end
    end
  end
  new_child_list.each do |nc|
    # 親が居る場合, まず取り除く
    if ! nc.parent_node.nil? then
      nc.parent_node.remove_child( nc )
    end
    @child_nodes << nc
    nc.parent_node = self
  end
  #$stdout << "ModParentNode#append_child: " << new_child << "\n"
  #$stdout << "ModParentNode#append_child: " << new_child.node_name << "\n"
  return new_child
end

#child_nodesObject

childNodes of type NodeList, readonly

A NodeList that contains all children of this node. If there are no children, this is a NodeList containing no nodes.


31
# File 'lib/vcdom/minidom/mod_parent_node.rb', line 31

def child_nodes; return @child_nodes_wrapper end

#first_childObject

firstChild of type Node, readonly

The first child of this node. If there is no such node, this returns null.


35
# File 'lib/vcdom/minidom/mod_parent_node.rb', line 35

def first_child; return @child_nodes[0] end

#has_child_nodesObject

hasChildNodes

Returns whether this node has any children.

Return Value
  boolean
    Returns true if this node has any children, false otherwise.
No Parameters
No Exceptions


50
51
52
# File 'lib/vcdom/minidom/mod_parent_node.rb', line 50

def has_child_nodes()
  return ( @child_nodes.length > 0 ) ? true : false
end

#insert_before(new_child, ref_child) ⇒ Object

insertBefore modified in DOM Level 3

Inserts the node newChild before the existing child node refChild. If refChild is null, 
insert newChild at the end of the list of children.
If newChild is a DocumentFragment object, all of its children are inserted, in the same 
order, before refChild. If the newChild is already in the tree, it is first removed.

Note: Inserting a node before itself is implementation dependent.

Parameters
  newChild of type Node
    The node to insert.
  refChild of type Node
    The reference node, i.e., the node before which the new node must be inserted.
Return Value
  Node
    The node being inserted.
Exceptions
  DOMException
    HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not allow children 
              of the type of the newChild node, or if the node to insert is one of this node's 
              ancestors or this node itself, or if this node is of type Document and the DOM 
              application attempts to insert a second DocumentType or Element node.
    WRONG_DOCUMENT_ERR: Raised if newChild was created from a different document than the 
              one that created this node.
    NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly or if the parent of the node 
              being inserted is readonly.
    NOT_FOUND_ERR: Raised if refChild is not a child of this node.
    NOT_SUPPORTED_ERR: if this node is of type Document, this exception might be raised if the 
              DOM implementation doesn't support the insertion of a DocumentType or Element node.


213
214
215
216
217
218
219
# File 'lib/vcdom/minidom/mod_parent_node.rb', line 213

def insert_before( new_child, ref_child )
  # aRefChild の index 取得. なければ例外発生
  ( idx = @child_nodes.index( ref_child ) )\
     or raise DOMException.new( DOMException::NOT_FOUND_ERR, "NOT_FOUND_ERR" )
  # 追加処理
  return add_child_at( idx, new_child )
end

#last_childObject

lastChild of type Node, readonly

The last child of this node. If there is no such node, this returns null.


39
# File 'lib/vcdom/minidom/mod_parent_node.rb', line 39

def last_child; return @child_nodes[-1] end

#remove_child(old_child) ⇒ Object

removeChild modified in DOM Level 3

Removes the child node indicated by oldChild from the list of children, and returns it.

Parameters
  oldChild of type Node
    The node being removed.
Return Value
  Node
    The node removed.
Exceptions
  DOMException
    NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
    NOT_FOUND_ERR: Raised if oldChild is not a child of this node.
    NOT_SUPPORTED_ERR: if this node is of type Document, this exception 
              might be raised if the DOM implementation doesn't support 
              the removal of the DocumentType child or the Element child.


238
239
240
241
242
243
244
245
246
# File 'lib/vcdom/minidom/mod_parent_node.rb', line 238

def remove_child( old_child )
  # aOldChild の index を取得. 子ノードに含まれなければ例外発生
  ( idx = get_index_of( old_child ) ) or 
          raise DOMException.new( DOMException::NOT_FOUND_ERR, "The old child is not a child of this node." )
  # 削除
  @child_nodes.delete_at( idx )
  old_child.parent_node = nil
  return old_child
end

#replace_child(new_child, old_child) ⇒ Object

replaceChild modified in DOM Level 3

Replaces the child node oldChild with newChild in the list of children, and returns the oldChild node.
  #=> 子ノードのうち、oldChild を newChild で置き換え、oldChild を返す.
If newChild is a DocumentFragment object, oldChild is replaced by all of the DocumentFragment children, 
which are inserted in the same order. 
  #=> もし newChild が DocumentFragment オブジェクトならば, oldChild は newChild の全ての子をそのままの順で置き換えられる.
If the newChild is already in the tree, it is first removed.
  #=> もし newChild が既に DOM 木の中に存在するならば, まず最初に取り除かれる.

Note: Replacing a node with itself is implementation dependent.
  #=> Note: 自分自身を自分自身で置き換えようとしたときの動作は, 実装に依存する.

Parameters
  newChild of type Node
    The new node to put in the child list.
  oldChild of type Node
    The node being replaced in the list.
Return Value
  Node
    The node replaced.
Exceptions
  DOMException
    HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not allow children of the type 
            of the newChild node, or if the node to put in is one of this node's ancestors or this 
            node itself, or if this node is of type Document and the result of the replacement 
            operation would add a second DocumentType or Element on the Document node.
    WRONG_DOCUMENT_ERR: Raised if newChild was created from a different document than the one that 
            created this node.
    NO_MODIFICATION_ALLOWED_ERR: Raised if this node or the parent of the new node is readonly.
    NOT_FOUND_ERR: Raised if oldChild is not a child of this node.
    NOT_SUPPORTED_ERR: if this node is of type Document, this exception might be raised if the DOM 
            implementation doesn't support the replacement of the DocumentType child or Element child.


281
282
283
# File 'lib/vcdom/minidom/mod_parent_node.rb', line 281

def replace_child( new_child, old_child )
# 未実装
end

#text_contentObject

textContent of type DOMString, introduced in DOM Level 3

This attribute returns the text content of this node and its descendants. 
When it is defined to be null, setting it has no effect. 
On setting, any possible children this node may have are removed and, 
if it the new string is not empty or null, replaced by a single Text node 
containing the string this attribute is set to.
On getting, no serialization is performed, the returned string does not 
contain any markup. No whitespace normalization is performed and the 
returned string does not contain the white spaces in element content 
(see the attribute Text.isElementContentWhitespace). 
Similarly, on setting, no parsing is performed either, the input string 
is taken as pure textual content.

The string returned is made of the text content of this node depending on 
its type, as defined below:

     Node type                        Content
     ---------------------------------------------------
     ELEMENT_NODE, ATTRIBUTE_NODE,    concatenation of the textContent attribute value
      ENTITY_NODE,                     of every child node, excluding COMMENT_NODE and 
      ENTITY_REFERENCE_NODE,           PROCESSING_INSTRUCTION_NODE nodes. This is the  
      DOCUMENT_FRAGMENT_NODE           empty string if the node has no children.

Exceptions on setting
  DOMException
    NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly.
Exceptions on retrieval
  DOMException
    DOMSTRING_SIZE_ERR: Raised when it would return more characters 
              than fit in a DOMString variable on the implementation platform.

Document node でオーバーライド



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/vcdom/minidom/mod_parent_node.rb', line 87

def text_content
  value = String.new()
  @child_nodes.each do |node|
    case node.node_type
      when Node::COMMENT_NODE, Node::PROCESSING_INSTRUCTION_NODE then
        # do nothing
      else
        value << node.text_content
    end
  end
  return value
end

#text_content=(value) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/vcdom/minidom/mod_parent_node.rb', line 99

def text_content=( value )
  if self.is_readonly then
    raise DOMException.new( NO_MODIFICATION_ALLOWED_ERR, 'This node is readonly.' )
  end
  while self.has_child_nodes do
    self.remove_child( self.first_child )
  end
  unless value.nil? or value.length == 0 then
    self.append_child( self.owner_document.create_text_node( value ) )
  end
end