replace invalid characters from xml
This commit is contained in:
parent
653b0fe276
commit
e5203182bf
2 changed files with 12 additions and 1 deletions
|
|
@ -36,6 +36,10 @@ module DiasporaFederation
|
||||||
extend PropertiesDSL
|
extend PropertiesDSL
|
||||||
include Logging
|
include Logging
|
||||||
|
|
||||||
|
# Invalid XML characters
|
||||||
|
# @see https://www.w3.org/TR/REC-xml/#charsets "Extensible Markup Language (XML) 1.0"
|
||||||
|
INVALID_XML_REGEX = /[^\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD\u{10000}-\u{10FFFF}]/
|
||||||
|
|
||||||
# Initializes the Entity with the given attribute hash and freezes the created
|
# Initializes the Entity with the given attribute hash and freezes the created
|
||||||
# instance it returns.
|
# instance it returns.
|
||||||
#
|
#
|
||||||
|
|
@ -230,7 +234,7 @@ module DiasporaFederation
|
||||||
def simple_node(doc, name, value)
|
def simple_node(doc, name, value)
|
||||||
xml_name = self.class.xml_names[name]
|
xml_name = self.class.xml_names[name]
|
||||||
Nokogiri::XML::Element.new(xml_name ? xml_name.to_s : name, doc).tap do |node|
|
Nokogiri::XML::Element.new(xml_name ? xml_name.to_s : name, doc).tap do |node|
|
||||||
node.content = value unless value.empty?
|
node.content = value.gsub(INVALID_XML_REGEX, "\uFFFD") unless value.empty?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -91,6 +91,13 @@ module DiasporaFederation
|
||||||
expect(%w(test1 test2 test3 test4)).to include(node.name)
|
expect(%w(test1 test2 test3 test4)).to include(node.name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "replaces invalid XML characters" do
|
||||||
|
entity = Entities::TestEntity.new(test: "asdfasdf asdf💩asdf\nasdf")
|
||||||
|
xml = entity.to_xml.to_xml
|
||||||
|
parsed = Entities::TestEntity.from_xml(Nokogiri::XML::Document.parse(xml).root).test
|
||||||
|
expect(parsed).to eq("asdf<EFBFBD>asdf asdf💩asdf\nasdf")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe ".from_xml" do
|
describe ".from_xml" do
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue