Class: VCDOM::MiniDOM::Element

Inherits:
Node
  • Object
show all
Includes:
Attr::OwnerElementManageable, ModChildNode, ModElementsGetter, ModParentNode
Defined in:
lib/vcdom/minidom/element.rb

Direct Known Subclasses

ElementNS

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::NOTATION_NODE, Node::PROCESSING_INSTRUCTION_NODE, Node::TEXT_NODE

Instance Method Summary collapse

Methods included from ModElementsGetter

#get_elements_by_tagname, #get_elements_by_tagname_ns

Methods included from ModParentNode

#append_child, #child_nodes, #first_child, #has_child_nodes, #insert_before, #last_child, #remove_child, #replace_child, #text_content, #text_content=

Methods included from ModChildNode

#init_mod_child_node, #next_sibling, #parent_node, #previous_sibling

Methods inherited from Node

#append_child, #child_nodes, #first_child, #has_child_nodes, #insert_before, #last_child, #local_name, #namespace_uri, #next_sibling, #node_value, #node_value=, #owner_document, #parent_node, #prefix, #prefix=, #previous_sibling, #remove_child, #replace_child, #text_content, #text_content=

Constructor Details

#initialize(owner_document, tagname) ⇒ Element

Exceptions

DOMException
  INVALID_CHARACTER_ERR: Raised if the specified name is not an XML name according to 
            the XML version in use specified in the Document.xmlVersion attribute.


433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/vcdom/minidom/element.rb', line 433

def initialize( owner_document, tagname )
  if XMLRegExp::NAME !~ tagname then
    # the specified qualified name is not an XML name
    raise DOMException.new( DOMException::INVALID_CHARACTER_ERR, 
            'The specified name "' + tagname + '" is not an XML name.' )
  end
  super( owner_document )
  init_mod_child_node()
  init_mod_parent_node()
  @attributes = []
  @attributes_wrapper = NamedNodeMapAttr.new( @attributes, self )
  @tagname = tagname
end

Instance Method Details

#attributesObject

Element でオーバーライド



34
35
36
# File 'lib/vcdom/minidom/element.rb', line 34

def attributes
  return @attributes_wrapper
end

#get_attribute(name) ⇒ Object

getAttribute

Retrieves an attribute value by name.

Parameters
  name of type DOMString
    The name of the attribute to retrieve.
Return Value
  DOMString
    The Attr value as a string, or the empty string if that attribute does not have a specified 
    or default value.
No Exceptions


278
279
280
281
# File 'lib/vcdom/minidom/element.rb', line 278

def get_attribute( name )
  index = get_index_of_attr( name )
  return ( index.nil? ? '' : @attributes[index].node_value )
end

#get_attribute_node(name) ⇒ Object

getAttributeNode

Retrieves an attribute node by name.
To retrieve an attribute node by qualified name and namespace URI, use the getAttributeNodeNS method.

Parameters
  name of type DOMString
    The name (nodeName) of the attribute to retrieve.
Return Value
  Attr
    The Attr node with the specified name (nodeName) or null if there is no such attribute.
No Exceptions


320
321
322
323
# File 'lib/vcdom/minidom/element.rb', line 320

def get_attribute_node( name )
  index = get_index_of_attr( name )
  return ( index.nil? ? nil : @attributes[index] )
end

#get_attribute_node_ns(namespace_uri, local_name) ⇒ Object

getAttributeNodeNS introduced in DOM Level 2

Retrieves an Attr node by local name and namespace URI.
Per [XML Namespaces], applications must use the value null as the namespaceURI parameter 
for methods if they wish to have no namespace.

Parameters
  namespaceURI of type DOMString
    The namespace URI of the attribute to retrieve.
  localName of type DOMString
    The local name of the attribute to retrieve.
Return Value
  Attr
    The Attr node with the specified attribute local name and namespace URI or null 
    if there is no such attribute.
Exceptions
  DOMException
    NOT_SUPPORTED_ERR: May be raised if the implementation does not support the feature "XML" 
              and the language exposed through the Document does not support XML Namespaces 
              (such as [HTML 4.01]).


345
346
347
348
# File 'lib/vcdom/minidom/element.rb', line 345

def get_attribute_node_ns( namespace_uri, local_name )
  index = get_index_of_attr_ns( namespace_uri, local_name )
  return ( index.nil? ? nil : @attributes[index] )
end

#get_attribute_ns(namespace_uri, local_name) ⇒ Object

getAttributeNS introduced in DOM Level 2

Retrieves an attribute value by local name and namespace URI.
Per [XML Namespaces], applications must use the value null as the namespaceURI parameter for methods 
if they wish to have no namespace.

Parameters
  namespaceURI of type DOMString
    The namespace URI of the attribute to retrieve.
  localName of type DOMString
    The local name of the attribute to retrieve.
Return Value
  DOMString
    The Attr value as a string, or the empty string if that attribute does not have a specified 
    or default value.
Exceptions
  DOMException
    NOT_SUPPORTED_ERR: May be raised if the implementation does not support the feature "XML" 
              and the language exposed through the Document does not support XML Namespaces 
              (such as [HTML 4.01]).


303
304
305
306
# File 'lib/vcdom/minidom/element.rb', line 303

def get_attribute_ns( namespace_uri, local_name )
  index = get_index_of_attr_ns( namespace_uri, local_name )
  return ( index.nil? ? '' : @attributes[index].node_value )
end

#has_attribute(name) ⇒ Object

hasAttribute introduced in DOM Level 2

Returns true when an attribute with a given name is specified on this element or has a default value, 
false otherwise.

Parameters
  name of type DOMString
    The name of the attribute to look for.
Return Value
  boolean
    true if an attribute with the given name is specified on this element or has a default value, 
    false otherwise.
No Exceptions


59
60
61
62
63
64
65
# File 'lib/vcdom/minidom/element.rb', line 59

def has_attribute( name )
  if get_index_of_attr( name ) then
    return true
  else
    return false
  end
end

#has_attribute_ns(namespace_uri, local_name) ⇒ Object

hasAttributeNS introduced in DOM Level 2

Returns true when an attribute with a given local name and namespace URI is specified on this element or has a default value, false otherwise.
  Per [XML Namespaces], applications must use the value null as the namespaceURI parameter for methods if they wish to have no namespace.

Parameters
  namespaceURI of type DOMString
    The namespace URI of the attribute to look for.
  localName of type DOMString
    The local name of the attribute to look for.
Return Value
  boolean
    true if an attribute with the given local name and namespace URI is specified or has a default value on this element, false otherwise.
Exceptions
  DOMException
    NOT_SUPPORTED_ERR: May be raised if the implementation does not support the feature "XML" and the language exposed through the Document does not support XML Namespaces (such as [HTML 4.01]).


83
84
85
86
87
88
89
# File 'lib/vcdom/minidom/element.rb', line 83

def has_attribute_ns( namespace_uri, local_name )
  if get_index_of_attr_ns( namespace_uri, local_name ) then
    return true
  else
    return false
  end
end

#has_attributesObject



37
38
39
40
41
42
43
# File 'lib/vcdom/minidom/element.rb', line 37

def has_attributes()
  if @attributes.length != 0 then
    return true
  else
    return false
  end
end

#node_typeObject



18
# File 'lib/vcdom/minidom/element.rb', line 18

def node_type; return Node::ELEMENT_NODE end

#remove_attribute(name) ⇒ Object

removeAttribute

Removes an attribute by name. If a default value for the removed attribute is defined in the DTD, a new attribute immediately appears with the default value as well as the corresponding namespace URI, local name, and prefix when applicable. The implementation may handle default values from other schemas similarly but applications should use Document.normalizeDocument() to guarantee this information is up-to-date.
If no attribute with this name is found, this method has no effect.
To remove an attribute by local name and namespace URI, use the removeAttributeNS method.

Parameters
  name of type DOMString
    The name of the attribute to remove.
Exceptions
  DOMException
    NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
No Return Value


363
364
365
366
367
368
369
370
371
372
# File 'lib/vcdom/minidom/element.rb', line 363

def remove_attribute( name )
  if self.is_readonly then
    raise DOMException.new( DOMException::NO_MODIFICATION_ALLOWED_ERR, 
            'This node is readonly.' )
  end
  if index = get_index_of_attr( name ) then
    @attributes.delete_at( index ).owner_element = nil
  end
  return nil
end

#remove_attribute_node(old_attr) ⇒ Object

removeAttributeNode

Removes the specified attribute node. If a default value for the removed Attr node is defined in the DTD, a new node immediately appears with the default value as well as the corresponding namespace URI, local name, and prefix when applicable. The implementation may handle default values from other schemas similarly but applications should use Document.normalizeDocument() to guarantee this information is up-to-date.

Parameters
  oldAttr of type Attr
    The Attr node to remove from the attribute list.
Return Value
  Attr
    The Attr node that was removed.
Exceptions
  DOMException
    NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
    NOT_FOUND_ERR: Raised if oldAttr is not an attribute of the element.


415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/vcdom/minidom/element.rb', line 415

def remove_attribute_node( old_attr )
  if self.is_readonly then
    raise DOMException.new( DOMException::NO_MODIFICATION_ALLOWED_ERR, 
            'This node is readonly.' )
  end
  if index = get_index_of_attr_node( old_attr ) then
    @attributes.delete_at( index ).owner_element = nil
  else
    raise DOMException.new( DOMException::NOT_FOUND_ERR, 
            'The argument oldAttr is not an attribute of the element.' )
  end
  return old_attr
end

#remove_attribute_ns(namespace_uri, local_name) ⇒ Object

removeAttributeNS introduced in DOM Level 2

Removes an attribute by local name and namespace URI. If a default value for the removed attribute is defined in the DTD, a new attribute immediately appears with the default value as well as the corresponding namespace URI, local name, and prefix when applicable. The implementation may handle default values from other schemas similarly but applications should use Document.normalizeDocument() to guarantee this information is up-to-date.
If no attribute with this local name and namespace URI is found, this method has no effect.
Per [XML Namespaces], applications must use the value null as the namespaceURI parameter for methods if they wish to have no namespace.

Parameters
  namespaceURI of type DOMString
    The namespace URI of the attribute to remove.
  localName of type DOMString
    The local name of the attribute to remove.
Exceptions
  DOMException
    NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
    NOT_SUPPORTED_ERR: May be raised if the implementation does not support the feature "XML" and the language exposed through the Document does not support XML Namespaces (such as [HTML 4.01]).
No Return Value


390
391
392
393
394
395
396
397
398
399
# File 'lib/vcdom/minidom/element.rb', line 390

def remove_attribute_ns( namespace_uri, local_name )
  if self.is_readonly then
    raise DOMException.new( DOMException::NO_MODIFICATION_ALLOWED_ERR, 
            'This node is readonly.' )
  end
  if index = get_index_of_attr_ns( namespace_uri, local_name ) then
    @attributes.delete_at( index ).owner_element = nil
  end
  return nil
end

#set_attribute(name, value) ⇒ Object

setAttribute

Adds a new attribute. 
If an attribute with that name is already present in the element, its value is changed to be 
that of the value parameter. This value is a simple string; it is not parsed as it is being set. 
So any markup (such as syntax to be recognized as an entity reference) is treated as literal text, 
and needs to be appropriately escaped by the implementation when it is written out. 
In order to assign an attribute value that contains entity references, the user must create 
an Attr node plus any Text and EntityReference nodes, build the appropriate subtree, and use 
setAttributeNode to assign it as the value of an attribute.
To set an attribute with a qualified name and namespace URI, use the setAttributeNS method.

Parameters
  name of type DOMString
    The name of the attribute to create or alter.
  value of type DOMString
    Value to set in string form.
Exceptions
  DOMException
    INVALID_CHARACTER_ERR: Raised if the specified name is not an XML name according to the XML version 
              in use specified in the Document.xmlVersion attribute.
    NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
No Return Value


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/vcdom/minidom/element.rb', line 114

def set_attribute( name, value )
  if self.is_readonly then
    raise DOMException.new( DOMException::NO_MODIFICATION_ALLOWED_ERR, 
            'This node is readonly.' )
  end
  if index = get_index_of_attr( name ) then
    @attributes[index].node_value = value
  else
    new_attr = self.owner_document.create_attribute( name )
    new_attr.node_value = value
    @attributes << new_attr
    new_attr.owner_element = self
  end
  return nil
end

#set_attribute_node(new_attr) ⇒ Object

setAttributeNode

Adds a new attribute node.
  #=> 新しい属性を追加する. 
If an attribute with that name (nodeName) is already present in the element, it is replaced by the new one.
  #=> nodeName が同じ属性が既に要素内にある場合, それは新しいものに置き換えられる. 
Replacing an attribute node by itself has no effect.
  #=> 自分自身との入れ替えは何の効果もない.
To add a new attribute node with a qualified name and namespace URI, use the setAttributeNodeNS method.
  #=> qualified name と namespace URI を持つ新しい属性を追加するためには, setAttributeNodeNS メソッドを使用すること.

Parameters
  newAttr of type Attr
    The Attr node to add to the attribute list.
Return Value
  Attr
    If the newAttr attribute replaces an existing attribute, the replaced Attr node is returned, 
    otherwise null is returned.
Exceptions
  DOMException
    WRONG_DOCUMENT_ERR: Raised if newAttr was created from a different document than the one 
              that created the element.
    NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
    INUSE_ATTRIBUTE_ERR: Raised if newAttr is already an attribute of another Element object. 
              The DOM user must explicitly clone Attr nodes to re-use them in other elements.


209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/vcdom/minidom/element.rb', line 209

def set_attribute_node( new_attr )
  if self.is_readonly then
    raise DOMException.new( DOMException::NO_MODIFICATION_ALLOWED_ERR, 
            'This node is readonly.' )
  elsif new_attr.owner_document != self.owner_document then
    raise DOMException.new( DOMException::WRONG_DOCUMENT_ERR, 
            'The argument new_attr was created from a different document than the one that created the element.' )
  elsif ! new_attr.owner_element.nil? then
    raise DOMException.new( DOMException::INUSE_ATTRIBUTE_ERR, 
            'The argument new_attr is already an attribute of another Element object.' )
  end
  return set_attribute_node_inner( new_attr, get_index_of_attr( new_attr.node_name ) )
end

#set_attribute_node_ns(new_attr) ⇒ Object

setAttributeNodeNS introduced in DOM Level 2

Adds a new attribute. 
  #=> 新しい属性を追加する.
If an attribute with that local name and that namespace URI is already present in the element, 
it is replaced by the new one. 
  #=> local name と namespace URI が同じ属性が既に要素内にある場合, それは新しいものに置き換えられる.
Replacing an attribute node by itself has no effect.
  #=> 自分自身と置き換えられる場合は, 何の効果もない. 
Per [XML Namespaces], applications must use the value null as the namespaceURI parameter for methods 
if they wish to have no namespace.

Parameters
  newAttr of type Attr
    The Attr node to add to the attribute list.
Return Value
  Attr
    If the newAttr attribute replaces an existing attribute with the same local name and namespace URI, 
    the replaced Attr node is returned, otherwise null is returned.
Exceptions
  DOMException
    WRONG_DOCUMENT_ERR: Raised if newAttr was created from a different document than the one 
              that created the element.
    NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
    INUSE_ATTRIBUTE_ERR: Raised if newAttr is already an attribute of another Element object. 
              The DOM user must explicitly clone Attr nodes to re-use them in other elements.
    NOT_SUPPORTED_ERR: May be raised if the implementation does not support the feature "XML" 
              and the language exposed through the Document does not support XML Namespaces 
              (such as [HTML 4.01]).


252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/vcdom/minidom/element.rb', line 252

def set_attribute_node_ns( new_attr )
  if self.is_readonly then
    raise DOMException.new( DOMException::NO_MODIFICATION_ALLOWED_ERR, 
            'This node is readonly.' )
  elsif new_attr.owner_document != self.owner_document then
    raise DOMException.new( DOMException::WRONG_DOCUMENT_ERR, 
            'The argument new_attr was created from a different document than the one that created the element.' )
  elsif ! new_attr.owner_element.nil? then
    raise DOMException.new( DOMException::INUSE_ATTRIBUTE_ERR, 
            'The argument new_attr is already an attribute of another Element object.' )
  end
  return set_attribute_node_inner( new_attr, get_index_of_attr_ns( new_attr.namespace_uri, new_attr.local_name ) )
end

#set_attribute_ns(namespace_uri, qualified_name, value) ⇒ Object

setAttributeNS introduced in DOM Level 2

Adds a new attribute. 
If an attribute with the same local name and namespace URI is already present on the element, 
its prefix is changed to be the prefix part of the qualifiedName, and its value is changed to be 
the value parameter. 
This value is a simple string; it is not parsed as it is being set. 
So any markup (such as syntax to be recognized as an entity reference) is treated as literal text, 
and needs to be appropriately escaped by the implementation when it is written out. 
In order to assign an attribute value that contains entity references, the user must create 
an Attr node plus any Text and EntityReference nodes, build the appropriate subtree, 
and use setAttributeNodeNS or setAttributeNode to assign it as the value of an attribute.
Per [XML Namespaces], applications must use the value null as the namespaceURI parameter for methods 
if they wish to have no namespace.

Parameters
  namespaceURI of type DOMString
    The namespace URI of the attribute to create or alter.
  qualifiedName of type DOMString
    The qualified name of the attribute to create or alter.
  value of type DOMString
    The value to set in string form.
Exceptions
  DOMException
    INVALID_CHARACTER_ERR: Raised if the specified qualified name is not an XML name according to 
              the XML version in use specified in the Document.xmlVersion attribute.
    NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
    NAMESPACE_ERR: Raised if the qualifiedName is malformed per the Namespaces in XML specification, 
              if the qualifiedName has a prefix and the namespaceURI is null, if the qualifiedName 
              has a prefix that is "xml" and the namespaceURI is different from 
              "http://www.w3.org/XML/1998/namespace", if the qualifiedName or its prefix is "xmlns" 
              and the namespaceURI is different from "http://www.w3.org/2000/xmlns/", or if 
              the namespaceURI is "http://www.w3.org/2000/xmlns/" and neither the qualifiedName 
              nor its prefix is "xmlns".
    NOT_SUPPORTED_ERR: May be raised if the implementation does not support the feature "XML" 
              and the language exposed through the Document does not support XML Namespaces 
              (such as [HTML 4.01]).
No Return Value


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/vcdom/minidom/element.rb', line 168

def set_attribute_ns( namespace_uri, qualified_name, value )
  if self.is_readonly then
    raise DOMException.new( DOMException::NO_MODIFICATION_ALLOWED_ERR, 
            'This node is readonly.' )
  end
  if index = get_index_of_attr_ns( namespace_uri, qualified_name ) then
    @attributes[index].node_value = value
  else
    new_attr = self.owner_document.create_attribute_ns( namespace_uri, qualified_name )
    new_attr.node_value = value
    @attributes << new_attr
    new_attr.owner_element = self
  end
  return nil
end

#tagnameObject Also known as: node_name

tagName of type DOMString, readonly

The name of the element. 
If Node.localName is different from null, this attribute is a qualified name. For example, in:
          <elementExample id="demo"> 
          ... 
          </elementExample> ,
tagName has the value "elementExample".
Note that this is case-preserving in XML, as are all of the operations of the DOM. 
The HTML DOM returns the tagName of an HTML element in the canonical uppercase form, regardless of
 the case in the source HTML document.


30
# File 'lib/vcdom/minidom/element.rb', line 30

def tagname; return @tagname end