Skip to content

Commit ff49ee3

Browse files
committed
Allow nil for external and system IDs when creating a DTD
The DTD initializer required external and system to be strings, making it impossible to create an internal DTD without public/system identifiers. Now nil is accepted for both parameters. Fixes #215
1 parent 0b4f2d4 commit ff49ee3

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

ext/libxml/ruby_xml_dtd.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,17 @@ static VALUE rxml_dtd_initialize(int argc, VALUE *argv, VALUE self)
183183
VALUE name, doc, internal;
184184
rb_scan_args(argc, argv, "23", &external, &system, &name, &doc, &internal);
185185

186-
Check_Type(external, T_STRING);
187-
xpublic = (const xmlChar*) StringValuePtr(external);
186+
if (external != Qnil)
187+
{
188+
Check_Type(external, T_STRING);
189+
xpublic = (const xmlChar*) StringValuePtr(external);
190+
}
188191

189-
Check_Type(system, T_STRING);
190-
xsystem = (const xmlChar*) StringValuePtr(system);
192+
if (system != Qnil)
193+
{
194+
Check_Type(system, T_STRING);
195+
xsystem = (const xmlChar*) StringValuePtr(system);
196+
}
191197

192198
if (name != Qnil)
193199
{

test/test_dtd.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ def test_internal_subset
4444
assert_equal "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", xhtml_dtd.system_id
4545
end
4646

47+
def test_internal_subset_nil_ids
48+
doc = LibXML::XML::Document.new
49+
doc.root = LibXML::XML::Node.new('test')
50+
dtd = LibXML::XML::Dtd.new(nil, nil, 'test', doc, true)
51+
assert_equal('test', dtd.name)
52+
assert_nil(dtd.external_id)
53+
assert_nil(dtd.uri)
54+
assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE test>\n<test/>\n", doc.to_s)
55+
end
56+
4757
def test_external_subset
4858
xhtml_dtd = LibXML::XML::Dtd.new("-//W3C//DTD XHTML 1.0 Transitional//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", nil)
4959
assert xhtml_dtd.name.nil?

0 commit comments

Comments
 (0)