diff --git a/lib/rexml/child.rb b/lib/rexml/child.rb index cc6e9a47..2718040f 100644 --- a/lib/rexml/child.rb +++ b/lib/rexml/child.rb @@ -83,13 +83,12 @@ def previous_sibling=(other) # Returns:: the document this child belongs to, or nil if this child # belongs to no document def document - return parent.document unless parent.nil? - nil + parent&.document end # This doesn't yet handle encodings def bytes - document.encoding + document&.encoding to_s end diff --git a/lib/rexml/element.rb b/lib/rexml/element.rb index b62b6cc2..3e9e9d4c 100644 --- a/lib/rexml/element.rb +++ b/lib/rexml/element.rb @@ -473,8 +473,7 @@ def root # Related: #root, #root_node. # def document - rt = root - rt.parent if rt + root&.parent end # :call-seq: @@ -2325,11 +2324,11 @@ def get_attribute( name ) return attr end end - element_document = @element.document - if element_document and element_document.doctype + doctype = @element.document&.doctype + if doctype expn = @element.expanded_name - expn = element_document.doctype.name if expn.size == 0 - attr_val = element_document.doctype.attribute_of(expn, name) + expn = doctype.name if expn.size == 0 + attr_val = doctype.attribute_of(expn, name) return Attribute.new( name, attr_val ) if attr_val end return nil @@ -2371,8 +2370,8 @@ def []=( name, value ) end unless value.kind_of? Attribute - if @element.document and @element.document.doctype - value = Text::normalize( value, @element.document.doctype ) + if @element.document and (doctype = @element.document&.doctype) + value = Text::normalize( value, doctype ) else value = Text::normalize( value, nil ) end @@ -2390,7 +2389,7 @@ def []=( name, value ) else store value.name, value end - return @element + @element end # :call-seq: @@ -2409,10 +2408,10 @@ def prefixes each_attribute do |attribute| ns << attribute.name if attribute.prefix == 'xmlns' end - if @element.document and @element.document.doctype + if @element.document and (doctype = @element.document&.doctype) expn = @element.expanded_name - expn = @element.document.doctype.name if expn.size == 0 - @element.document.doctype.attributes_of(expn).each { + expn = doctype.name if expn.size == 0 + doctype.attributes_of(expn).each { |attribute| ns << attribute.name if attribute.prefix == 'xmlns' } @@ -2434,10 +2433,10 @@ def namespaces each_attribute do |attribute| namespaces[attribute.name] = attribute.value if attribute.prefix == 'xmlns' or attribute.name == 'xmlns' end - if @element.document and @element.document.doctype + if @element.document and (doctype = @element.document&.doctype) expn = @element.expanded_name - expn = @element.document.doctype.name if expn.size == 0 - @element.document.doctype.attributes_of(expn).each { + expn = doctype.name if expn.size == 0 + doctype.attributes_of(expn).each { |attribute| namespaces[attribute.name] = attribute.value if attribute.prefix == 'xmlns' or attribute.name == 'xmlns' } diff --git a/lib/rexml/text.rb b/lib/rexml/text.rb index 6f821472..1e98aec4 100644 --- a/lib/rexml/text.rb +++ b/lib/rexml/text.rb @@ -201,8 +201,7 @@ def <=>( other ) def doctype if @parent - doc = @parent.document - doc.doctype if doc + @parent.document&.doctype end end diff --git a/lib/rexml/xpath.rb b/lib/rexml/xpath.rb index 666d764f..db09b6e1 100644 --- a/lib/rexml/xpath.rb +++ b/lib/rexml/xpath.rb @@ -31,11 +31,7 @@ class XPath def XPath::first(element, path=nil, namespaces=nil, variables={}, options={}) raise "The namespaces argument, if supplied, must be a hash object." unless namespaces.nil? or namespaces.kind_of?(Hash) raise "The variables argument, if supplied, must be a hash object." unless variables.kind_of?(Hash) - parser = XPathParser.new(**options) - parser.namespaces = namespaces - parser.variables = variables - path = "*" unless path - parser.parse(path, element).flatten[0] + match(element, path, namespaces, variables, options).flatten[0] end # Iterates over nodes that match the given path, calling the supplied @@ -59,11 +55,7 @@ def XPath::first(element, path=nil, namespaces=nil, variables={}, options={}) def XPath::each(element, path=nil, namespaces=nil, variables={}, options={}, &block) raise "The namespaces argument, if supplied, must be a hash object." unless namespaces.nil? or namespaces.kind_of?(Hash) raise "The variables argument, if supplied, must be a hash object." unless variables.kind_of?(Hash) - parser = XPathParser.new(**options) - parser.namespaces = namespaces - parser.variables = variables - path = "*" unless path - parser.parse(path, element).each( &block ) + match(element, path, namespaces, variables, options).each( &block ) end # Returns an array of nodes matching a given XPath. @@ -72,7 +64,7 @@ def XPath::match(element, path=nil, namespaces=nil, variables={}, options={}) parser.namespaces = namespaces parser.variables = variables path = "*" unless path - parser.parse(path,element) + parser.parse(path,element) || [] end end end diff --git a/lib/rexml/xpath_parser.rb b/lib/rexml/xpath_parser.rb index 70ae8919..f03fa3c9 100644 --- a/lib/rexml/xpath_parser.rb +++ b/lib/rexml/xpath_parser.rb @@ -84,7 +84,7 @@ def parse path, node node = node.first end - node.document.__send__(:enable_cache) do + node.document&.__send__(:enable_cache) do match( path_stack, node ) end end