-
Notifications
You must be signed in to change notification settings - Fork 95
Change namespace cache strategy #333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tompng
wants to merge
1
commit into
ruby:master
Choose a base branch
from
tompng:xpath_namespace_lookup_cache
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+134
−73
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -588,12 +588,7 @@ def prefixes | |
| # d.elements['//c'].namespaces # => {"x"=>"1", "y"=>"2", "z"=>"3"} | ||
| # | ||
| def namespaces | ||
| namespaces_cache = document&.__send__(:namespaces_cache) | ||
| if namespaces_cache | ||
| namespaces_cache[self] ||= calculate_namespaces | ||
| else | ||
| calculate_namespaces | ||
| end | ||
| calculate_namespaces | ||
| end | ||
|
|
||
| # :call-seq: | ||
|
|
@@ -618,13 +613,10 @@ def namespaces | |
| # | ||
| def namespace(prefix=nil) | ||
| if prefix.nil? | ||
| prefix = prefix() | ||
| namespace_internal | ||
| else | ||
| namespace_lookup_internal(prefix) | ||
| end | ||
| prefix = (prefix == '') ? 'xmlns' : prefix.delete_prefix("xmlns:") | ||
| ns = namespaces[prefix] | ||
|
|
||
| ns = '' if ns.nil? and prefix == 'xmlns' | ||
| ns | ||
| end | ||
|
|
||
| # :call-seq: | ||
|
|
@@ -1508,12 +1500,34 @@ def write(output=$stdout, indent=-1, transitive=false, ie_hack=false) | |
| end | ||
|
|
||
| private | ||
| def calculate_namespaces | ||
| if parent | ||
| parent.namespaces.merge(attributes.namespaces) | ||
| else | ||
| attributes.namespaces | ||
| end | ||
|
|
||
| # Returns namespace of the element | ||
| def namespace_internal(namespaces = calculate_namespaces) | ||
| namespace_lookup_internal(prefix, namespaces) | ||
| end | ||
|
|
||
| # Lookup namespace for the given prefix in the context of the element | ||
| def namespace_lookup_internal(prefix, namespaces = calculate_namespaces) | ||
| prefix = (prefix == '') ? 'xmlns' : prefix.delete_prefix("xmlns:") | ||
| ns = namespaces[prefix] | ||
| ns = '' if ns.nil? and prefix == 'xmlns' | ||
| ns | ||
| end | ||
|
|
||
| def calculate_namespaces(cache_hash = nil, attlist_element_namespaces = nil) | ||
| return cache_hash[self] if cache_hash && cache_hash.key?(self) | ||
|
|
||
| inherited_namespaces = parent ? parent.send(:calculate_namespaces, cache_hash, attlist_element_namespaces) : {} | ||
| attlist_element_namespaces ||= document&.send(:attlist_per_element_namespaces) | ||
| attlist_namespaces = attlist_element_namespaces&.[](is_a?(Document) ? doctype&.name : expanded_name) | ||
| own_namespaces = attributes.send(:own_namespaces) | ||
|
|
||
| # Inherited namespaces can be overridden by attribute list declaration, and both can be overridden by its own attributes | ||
| namespaces = inherited_namespaces | ||
| namespaces = namespaces.merge(attlist_namespaces) if attlist_namespaces | ||
| namespaces = namespaces.merge(own_namespaces) if own_namespaces.any? | ||
| cache_hash[self] = namespaces if cache_hash | ||
| namespaces | ||
|
Comment on lines
+1525
to
+1530
|
||
| end | ||
|
|
||
| def __to_xpath_helper node | ||
|
|
@@ -2386,6 +2400,15 @@ def []=( name, value ) | |
| @element | ||
| end | ||
|
|
||
| # Returns namespaces directly declared in this attribute set | ||
| private def own_namespaces # :nodoc: | ||
| namespaces = {} | ||
| each_attribute do |attribute| | ||
| namespaces[attribute.name] = attribute.value if attribute.prefix == 'xmlns' or attribute.name == 'xmlns' | ||
| end | ||
| namespaces | ||
| end | ||
|
|
||
| # :call-seq: | ||
| # prefixes -> array_of_prefix_strings | ||
| # | ||
|
|
@@ -2424,19 +2447,12 @@ def prefixes | |
| # d.root.attributes.namespaces # => {"xmlns"=>"foo", "x"=>"bar", "y"=>"twee"} | ||
| # | ||
| def namespaces | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is no longer called except from the added test. |
||
| namespaces = {} | ||
| each_attribute do |attribute| | ||
| namespaces[attribute.name] = attribute.value if attribute.prefix == 'xmlns' or attribute.name == 'xmlns' | ||
| end | ||
| doctype = @element.document&.doctype | ||
| if doctype | ||
| expn = @element.expanded_name | ||
| 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' | ||
| } | ||
| end | ||
| doc = @element.document | ||
| doctype = doc&.doctype | ||
| attlist_element_namespaces = doc&.send(:attlist_per_element_namespaces) | ||
| attlist_namespaces = attlist_element_namespaces&.[](@element.is_a?(Document) ? doctype&.name : @element.expanded_name) | ||
| namespaces = own_namespaces | ||
| namespaces = attlist_namespaces.merge(namespaces) if attlist_namespaces | ||
| namespaces | ||
| end | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a small bug in master: precedence of attlist attributes and own attirbutes was reversed. Test is added for this