Skip to content

Commit

Permalink
fixes #751.
Browse files Browse the repository at this point in the history
  • Loading branch information
jvshahid committed Dec 5, 2012
1 parent 006dfd2 commit 173f5b7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
15 changes: 13 additions & 2 deletions ext/java/nokogiri/XmlDtd.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,20 @@ public static XmlDtd newEmpty(Ruby runtime,
IRubyObject name,
IRubyObject external_id,
IRubyObject system_id) {
Element placeHolder = doc.createElement("dtd_placeholder");

DocumentType placeholder = null;
if (doc.getDoctype() == null) {
String javaName = NokogiriHelpers.rubyStringToString(name);
String javaExternalId = NokogiriHelpers.rubyStringToString(external_id);
String javaSystemId = NokogiriHelpers.rubyStringToString(system_id);
placeholder = doc.getImplementation().createDocumentType(javaName, javaExternalId, javaSystemId);
doc.appendChild(placeholder);
} else {
placeholder = doc.getDoctype();
}
// FIXME: what if the document had a doc type, why are we here ?
XmlDtd dtd = (XmlDtd) NokogiriService.XML_DTD_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::DTD"));
dtd.setNode(runtime, placeHolder);
dtd.setNode(runtime, placeholder);
dtd.name = name;
dtd.pubId = external_id;
dtd.sysId = system_id;
Expand Down
15 changes: 15 additions & 0 deletions test/xml/test_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,21 @@ def test_specify_namespace
assert_equal 'bar', doc.at('foo|baz', 'foo' => 'bar').namespace.href
end

def test_dtd_in_builder_output
builder = Nokogiri::XML::Builder.new do |xml|
xml.doc.create_internal_subset(
'html',
"-//W3C//DTD HTML 4.01 Transitional//EN",
"http://www.w3.org/TR/html4/loose.dtd"
)
xml.root do
xml.foo
end
end
assert_match(/<!DOCTYPE html PUBLIC "-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN" "http:\/\/www.w3.org\/TR\/html4\/loose.dtd">/,
builder.to_xml)
end

def test_specify_namespace_nested
b = Nokogiri::XML::Builder.new { |xml|
xml.root('xmlns:foo' => 'bar') do
Expand Down

0 comments on commit 173f5b7

Please sign in to comment.