Skip to content

Commit d5d1e1d

Browse files
committed
correct XML escaping of illegal characters in literals
1 parent 950ae59 commit d5d1e1d

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

lib/rdf/rdfxml/writer/haml_templates.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Writer
5757
= object.value
5858
- elsif object.literal?
5959
- haml_tag(property, :"<", "xml:lang" => object.language, "rdf:datatype" => (object.datatype unless object.plain?)) do
60-
= object.value
60+
= object.value.to_s.encode(xml: :text)
6161
- elsif object.node?
6262
- haml_tag(property, :"/", "rdf:nodeID" => object.id)
6363
- else

spec/writer_spec.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,27 @@ class FOO < RDF::Vocabulary("http://foo/"); end
1818
context "typed resources" do
1919
context "resource without type" do
2020
subject do
21-
@graph << [RDF::URI.new("http://release/"), RDF::DC.title, "foo"]
21+
@graph << [RDF::URI.new("http://release/"), RDF::DC.title, "foo & bar"]
2222
serialize(attributes: :untyped)
2323
end
2424

2525
{
2626
"/rdf:RDF/rdf:Description/@rdf:about" => "http://release/",
27-
"/rdf:RDF/rdf:Description/@dc:title" => "foo"
27+
"/rdf:RDF/rdf:Description[@dc:title='foo & bar']/@rdf:about" => "http://release/",
28+
"/rdf:RDF/rdf:Description/@dc:title" => "foo & bar"
2829
}.each do |path, value|
2930
it "returns #{value.inspect} for xpath #{path}" do
3031
expect(subject).to have_xpath(path, value, {}, @debug)
3132
end
3233
end
34+
[
35+
'<dc:title>foo &amp; bar</dc:title>',
36+
'dc:title=\'foo &amp; bar\''
37+
].each do |value|
38+
it "serializes literal value with illegal XML characters to fragment #{value}" do
39+
expect(subject).to include(value)
40+
end
41+
end
3342
end
3443

3544
context "resource with type" do

0 commit comments

Comments
 (0)