Skip to content

Commit d427fc5

Browse files
authored
Avoid redundant calls for doctype (#264)
We can avoid calling `Document#doctype` by keeping `Document#doctype` result in a local variable.
1 parent 2271fd3 commit d427fc5

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

lib/rexml/element.rb

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2325,11 +2325,11 @@ def get_attribute( name )
23252325
return attr
23262326
end
23272327
end
2328-
element_document = @element.document
2329-
if element_document and element_document.doctype
2328+
doctype = @element.document&.doctype
2329+
if doctype
23302330
expn = @element.expanded_name
2331-
expn = element_document.doctype.name if expn.size == 0
2332-
attr_val = element_document.doctype.attribute_of(expn, name)
2331+
expn = doctype.name if expn.size == 0
2332+
attr_val = doctype.attribute_of(expn, name)
23332333
return Attribute.new( name, attr_val ) if attr_val
23342334
end
23352335
return nil
@@ -2371,8 +2371,9 @@ def []=( name, value )
23712371
end
23722372

23732373
unless value.kind_of? Attribute
2374-
if @element.document and @element.document.doctype
2375-
value = Text::normalize( value, @element.document.doctype )
2374+
doctype = @element.document&.doctype
2375+
if doctype
2376+
value = Text::normalize( value, doctype )
23762377
else
23772378
value = Text::normalize( value, nil )
23782379
end
@@ -2409,10 +2410,11 @@ def prefixes
24092410
each_attribute do |attribute|
24102411
ns << attribute.name if attribute.prefix == 'xmlns'
24112412
end
2412-
if @element.document and @element.document.doctype
2413+
doctype = @element.document&.doctype
2414+
if doctype
24132415
expn = @element.expanded_name
2414-
expn = @element.document.doctype.name if expn.size == 0
2415-
@element.document.doctype.attributes_of(expn).each {
2416+
expn = doctype.name if expn.size == 0
2417+
doctype.attributes_of(expn).each {
24162418
|attribute|
24172419
ns << attribute.name if attribute.prefix == 'xmlns'
24182420
}
@@ -2434,10 +2436,11 @@ def namespaces
24342436
each_attribute do |attribute|
24352437
namespaces[attribute.name] = attribute.value if attribute.prefix == 'xmlns' or attribute.name == 'xmlns'
24362438
end
2437-
if @element.document and @element.document.doctype
2439+
doctype = @element.document&.doctype
2440+
if doctype
24382441
expn = @element.expanded_name
2439-
expn = @element.document.doctype.name if expn.size == 0
2440-
@element.document.doctype.attributes_of(expn).each {
2442+
expn = doctype.name if expn.size == 0
2443+
doctype.attributes_of(expn).each {
24412444
|attribute|
24422445
namespaces[attribute.name] = attribute.value if attribute.prefix == 'xmlns' or attribute.name == 'xmlns'
24432446
}

0 commit comments

Comments
 (0)