Allow elements to be found via Document.getElementById() in the minidom document returned by scourXmlFile()

This commit is contained in:
Eduard Braun 2016-08-25 21:01:08 +02:00
parent 8d6301950b
commit 7613796a62
3 changed files with 35 additions and 3 deletions

View file

@ -3126,7 +3126,20 @@ def scourXmlFile(filename, options=None):
with open(filename, "rb") as f:
in_string = f.read()
out_string = scourString(in_string, options)
return xml.dom.minidom.parseString(out_string.encode('utf-8'))
doc = xml.dom.minidom.parseString(out_string.encode('utf-8'))
# since minidom does not seem to parse DTDs properly
# manually declare all attributes with name "id" to be of type ID
# (otherwise things like doc.getElementById() won't work)
all_nodes = doc.getElementsByTagName("*")
for node in all_nodes:
try:
node.setIdAttribute('id')
except:
pass
return doc

View file

@ -71,18 +71,25 @@ class InvalidOptions(unittest.TestCase):
fail = True
self.assertEqual(fail, False, 'Exception when calling Scour with invalid options')
class GetElementById(unittest.TestCase):
def runTest(self):
doc = scour.scourXmlFile('unittests/ids.svg')
self.assertIsNotNone(doc.getElementById('svg1'), 'Root SVG element not found by ID')
self.assertIsNotNone(doc.getElementById('linearGradient1'), 'linearGradient not found by ID')
self.assertIsNotNone(doc.getElementById('layer1'), 'g not found by ID')
self.assertIsNotNone(doc.getElementById('rect1'), 'rect not found by ID')
self.assertIsNone(doc.getElementById('rect2'), 'Non-existing element found by ID')
class NoInkscapeElements(unittest.TestCase):
def runTest(self):
self.assertNotEqual(walkTree(scour.scourXmlFile('unittests/sodipodi.svg').documentElement,
lambda e: e.namespaceURI != 'http://www.inkscape.org/namespaces/inkscape'), False,
'Found Inkscape elements' )
class NoSodipodiElements(unittest.TestCase):
def runTest(self):
self.assertNotEqual(walkTree(scour.scourXmlFile('unittests/sodipodi.svg').documentElement,
lambda e: e.namespaceURI != 'http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd'), False,
'Found Sodipodi elements' )
class NoAdobeIllustratorElements(unittest.TestCase):
def runTest(self):
self.assertNotEqual(walkTree(scour.scourXmlFile('unittests/adobe.svg').documentElement,

12
unittests/ids.svg Normal file
View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg id="svg1" xmlns="http://www.w3.org/2000/svg" height="100" width="100" viewBox="0 0 100 100">
<defs>
<linearGradient id="linearGradient1">
<stop offset="0"/>
<stop offset="1" stop-color="blue"/>
</linearGradient>
</defs>
<g id="layer1">
<rect id="rect1" height="50" width="50" x="10" y="10" fill="url(#linearGradient1)"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 425 B