Skip to content

Commit

Permalink
Document#create_element (and so Builder) now handle non-word characte…
Browse files Browse the repository at this point in the history
…rs in namespace names. Closes #531.
  • Loading branch information
flavorjones committed Feb 20, 2012
1 parent e44e1ef commit a4826a2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.ja.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
* Document#add_child now accepts a Node, NodeSet, DocumentFragment,
or String. #546.

* Document#create_element now recognizes namespaces containing
non-word characters (like "SOAP-ENV"). This is mostly relevant to
users of Builder, which calls Document#create_element for nearly
everything. #531.

== 1.5.0 / 2011年7月1日

* 註
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
* Document#add_child now accepts a Node, NodeSet, DocumentFragment,
or String. #546.

* Document#create_element now recognizes namespaces containing
non-word characters (like "SOAP-ENV"). This is mostly relevant to
users of Builder, which calls Document#create_element for nearly
everything. #531.

== 1.5.0 / 2011-07-01

* Notes
Expand Down
8 changes: 7 additions & 1 deletion lib/nokogiri/xml/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ module XML
# For searching a Document, see Nokogiri::XML::Node#css and
# Nokogiri::XML::Node#xpath
class Document < Nokogiri::XML::Node
# I'm ignoring unicode characters here.
# See http://www.w3.org/TR/REC-xml-names/#ns-decl for more details.
NCNAME_START_CHAR = "A-Za-z_"
NCNAME_CHAR = NCNAME_START_CHAR + "\\-.0-9"
NCNAME_RE = /^xmlns(:[#{NCNAME_START_CHAR}][#{NCNAME_CHAR}]*)?$/

##
# Parse an XML file. +string_or_io+ may be a String, or any object that
# responds to _read_ and _close_ such as an IO, or StringIO.
Expand Down Expand Up @@ -60,7 +66,7 @@ def create_element name, *args, &block
when Hash
arg.each { |k,v|
key = k.to_s
if key =~ /^xmlns(:\w+)?$/
if key =~ NCNAME_RE
ns_name = key.split(":", 2)[1]
elm.add_namespace_definition ns_name, v
next
Expand Down
5 changes: 5 additions & 0 deletions test/xml/test_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ def test_create_element_with_namespace
assert_equal 'http://tenderlovemaking.com', elm.namespaces['xmlns:foo']
end

def test_create_element_with_hyphenated_namespace
elm = @xml.create_element('foo',:'xmlns:SOAP-ENC' => 'http://tenderlovemaking.com')
assert_equal 'http://tenderlovemaking.com', elm.namespaces['xmlns:SOAP-ENC']
end

def test_create_element_with_content
elm = @xml.create_element('foo',"needs more xml/violence")
assert_equal "needs more xml/violence", elm.content
Expand Down

0 comments on commit a4826a2

Please sign in to comment.