Skip to content

Commit 047dcbf

Browse files
committed
🐛 Properly handle Element#document # => nil
- Fixes #260
1 parent 95b8ef8 commit 047dcbf

File tree

4 files changed

+18
-21
lines changed

4 files changed

+18
-21
lines changed

lib/rexml/child.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,12 @@ def previous_sibling=(other)
8383
# Returns:: the document this child belongs to, or nil if this child
8484
# belongs to no document
8585
def document
86-
return parent.document unless parent.nil?
87-
nil
86+
parent&.document
8887
end
8988

9089
# This doesn't yet handle encodings
9190
def bytes
92-
document.encoding
91+
document&.encoding
9392

9493
to_s
9594
end

lib/rexml/element.rb

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,7 @@ def root
473473
# Related: #root, #root_node.
474474
#
475475
def document
476-
rt = root
477-
rt.parent if rt
476+
root&.parent
478477
end
479478

480479
# :call-seq:
@@ -2325,11 +2324,11 @@ def get_attribute( name )
23252324
return attr
23262325
end
23272326
end
2328-
element_document = @element.document
2329-
if element_document and element_document.doctype
2327+
doctype = @element.document&.doctype
2328+
if doctype
23302329
expn = @element.expanded_name
2331-
expn = element_document.doctype.name if expn.size == 0
2332-
attr_val = element_document.doctype.attribute_of(expn, name)
2330+
expn = doctype.name if expn.size == 0
2331+
attr_val = doctype.attribute_of(expn, name)
23332332
return Attribute.new( name, attr_val ) if attr_val
23342333
end
23352334
return nil
@@ -2371,8 +2370,8 @@ def []=( name, value )
23712370
end
23722371

23732372
unless value.kind_of? Attribute
2374-
if @element.document and @element.document.doctype
2375-
value = Text::normalize( value, @element.document.doctype )
2373+
if @element.document and (doctype = @element.document&.doctype)
2374+
value = Text::normalize( value, doctype )
23762375
else
23772376
value = Text::normalize( value, nil )
23782377
end
@@ -2390,7 +2389,7 @@ def []=( name, value )
23902389
else
23912390
store value.name, value
23922391
end
2393-
return @element
2392+
@element
23942393
end
23952394

23962395
# :call-seq:
@@ -2409,10 +2408,10 @@ def prefixes
24092408
each_attribute do |attribute|
24102409
ns << attribute.name if attribute.prefix == 'xmlns'
24112410
end
2412-
if @element.document and @element.document.doctype
2411+
if @element.document and (doctype = @element.document&.doctype)
24132412
expn = @element.expanded_name
2414-
expn = @element.document.doctype.name if expn.size == 0
2415-
@element.document.doctype.attributes_of(expn).each {
2413+
expn = doctype.name if expn.size == 0
2414+
doctype.attributes_of(expn).each {
24162415
|attribute|
24172416
ns << attribute.name if attribute.prefix == 'xmlns'
24182417
}
@@ -2434,10 +2433,10 @@ def namespaces
24342433
each_attribute do |attribute|
24352434
namespaces[attribute.name] = attribute.value if attribute.prefix == 'xmlns' or attribute.name == 'xmlns'
24362435
end
2437-
if @element.document and @element.document.doctype
2436+
if @element.document and (doctype = @element.document&.doctype)
24382437
expn = @element.expanded_name
2439-
expn = @element.document.doctype.name if expn.size == 0
2440-
@element.document.doctype.attributes_of(expn).each {
2438+
expn = doctype.name if expn.size == 0
2439+
doctype.attributes_of(expn).each {
24412440
|attribute|
24422441
namespaces[attribute.name] = attribute.value if attribute.prefix == 'xmlns' or attribute.name == 'xmlns'
24432442
}

lib/rexml/text.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,7 @@ def <=>( other )
201201

202202
def doctype
203203
if @parent
204-
doc = @parent.document
205-
doc.doctype if doc
204+
@parent.document&.doctype
206205
end
207206
end
208207

lib/rexml/xpath_parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def parse path, node
8484
node = node.first
8585
end
8686

87-
node.document.__send__(:enable_cache) do
87+
node.document&.__send__(:enable_cache) do
8888
match( path_stack, node )
8989
end
9090
end

0 commit comments

Comments
 (0)