Class: XML::Smart::Dom::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/xml/smart_domelement.rb

Instance Method Summary collapse

Constructor Details

#initialize(element) ⇒ Element

Returns a new instance of Element.



6
7
8
# File 'lib/xml/smart_domelement.rb', line 6

def initialize(element)
  @element = element
end

Instance Method Details

#==(other) ⇒ Object



197
198
199
200
201
# File 'lib/xml/smart_domelement.rb', line 197

def ==(other)
  return false unless other
  return false unless other.respond_to?(:unique_id)
  unique_id == other.unique_id
end

#===(cls) ⇒ Object



10
# File 'lib/xml/smart_domelement.rb', line 10

def ===(cls); self.is_a? cls; end

#add(*attrs) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/xml/smart_domelement.rb', line 102

def add(*attrs)
  tmp, update = add_helper(attrs)
  res = Dom::smart_helper(@element.add_child tmp)
  if update
    @element.document.custom_namespace_prefixes_update
    @element.document.ns_update
  end  
  res
end

#add_after(*attrs) ⇒ Object



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

def add_after(*attrs)
  tmp, update = add_helper(attrs)
  res = Dom::smart_helper(@element.add_next_sibling tmp)
  if update
    @element.document.custom_namespace_prefixes_update
    @element.document.ns_update
  end  
  res
end

#add_before(*attrs) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/xml/smart_domelement.rb', line 111

def add_before(*attrs)
  tmp, update = add_helper(attrs)
  res = Dom::smart_helper(@element.add_previous_sibling tmp)
  if update
    @element.document.custom_namespace_prefixes_update
    @element.document.ns_update
  end  
  res
end

#attributesObject



172
# File 'lib/xml/smart_domelement.rb', line 172

def attributes; AttributeSet.new @element; end

#childrenObject



177
# File 'lib/xml/smart_domelement.rb', line 177

def children; find('*|text()'); end

#children?Boolean

Returns:

  • (Boolean)


178
# File 'lib/xml/smart_domelement.rb', line 178

def children?; find('*|text()').length > 0 end

#dumpObject



130
# File 'lib/xml/smart_domelement.rb', line 130

def dump; @element.to_s; end

#element_only?Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/xml/smart_domelement.rb', line 191

def element_only?;
  @element.xpath_fast('*').length > 0 &&  @element.xpath_fast("string(text())") == '';
end

#empty?Boolean

Returns:

  • (Boolean)


184
# File 'lib/xml/smart_domelement.rb', line 184

def empty?; @element.blank?; end

#find(xpath) ⇒ Object



12
13
14
# File 'lib/xml/smart_domelement.rb', line 12

def find(xpath)
  Dom::smart_helper(@element.xpath_fast(xpath))
end

#mixed?Boolean

Returns:

  • (Boolean)


185
186
187
# File 'lib/xml/smart_domelement.rb', line 185

def mixed?;
  @element.xpath_fast('*').length > 0 &&  @element.xpath_fast("string(text())") != '';
end

#namespaceObject



136
# File 'lib/xml/smart_domelement.rb', line 136

def namespace; namespace? ? Namespace.new(@element.namespace) : nil; end

#namespace=(n) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/xml/smart_domelement.rb', line 137

def namespace=(n)
  n = case n
    when Namespace
      n.prefix
    when String
      n
    else
      return
  end  
  tmp = @element.document.custom_namespace_prefixes[n] || @element.document.user_custom_namespace_prefixes[n]
  unless tmp.nil?
    @element.namespace_scopes.each do |nss|
      @element.namespace = nss if nss.href == tmp
    end
  end
end

#namespace?Boolean

Returns:

  • (Boolean)


135
# File 'lib/xml/smart_domelement.rb', line 135

def namespace?; !@element.namespace.nil?; end

#namespacesObject



153
# File 'lib/xml/smart_domelement.rb', line 153

def namespaces; NamespaceSet.new(self,@element); end

#parentObject



179
180
181
# File 'lib/xml/smart_domelement.rb', line 179

def parent
  Dom::smart_helper(@element.parent)
end

#parent?Boolean

Returns:

  • (Boolean)


182
# File 'lib/xml/smart_domelement.rb', line 182

def parent?; !@element.parent.nil?; end

#pathObject



195
# File 'lib/xml/smart_domelement.rb', line 195

def path; @element.path; end

#qnameObject



171
# File 'lib/xml/smart_domelement.rb', line 171

def qname; QName.new @element; end

#replace_by(n) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/xml/smart_domelement.rb', line 162

def replace_by(n)
  case n
    when Element; Element.new @element.replace(n.instance_variable_get(:@element))
    when NodeSet; NodeSet.new @element.replace(n.instance_variable_get(:@nodeset))
    else
      nil
  end
end

#textObject



174
# File 'lib/xml/smart_domelement.rb', line 174

def text; @element.xpath_fast("string(text())"); end

#text=(t) ⇒ Object



175
# File 'lib/xml/smart_domelement.rb', line 175

def text=(t); @element.content = t.to_s if t.respond_to? :to_s; end

#text_only?Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/xml/smart_domelement.rb', line 188

def text_only?;
  @element.xpath_fast('*').length == 0 &&  @element.xpath_fast("string(text())") != '';
end

#to_docObject



204
205
206
207
208
# File 'lib/xml/smart_domelement.rb', line 204

def to_doc
  doc = Nokogiri::XML::Document.new
  doc.root = @element
  dom = Dom.new(doc)
end

#to_fObject



133
# File 'lib/xml/smart_domelement.rb', line 133

def to_f; @element.content.to_f; end

#to_iObject



132
# File 'lib/xml/smart_domelement.rb', line 132

def to_i; @element.content.to_i; end

#to_sObject



131
# File 'lib/xml/smart_domelement.rb', line 131

def to_s; @element.content; end

#unique_idObject



202
# File 'lib/xml/smart_domelement.rb', line 202

def unique_id; @element.pointer_id; end

#xinclude!Object



155
156
157
158
159
160
# File 'lib/xml/smart_domelement.rb', line 155

def xinclude!
  @element.do_xinclude
  @element.document.custom_namespace_prefixes_update
  @element.document.ns_update
  true
end