diff --git a/scour/scour.py b/scour/scour.py index 02b4155..614946b 100644 --- a/scour/scour.py +++ b/scour/scour.py @@ -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 diff --git a/testscour.py b/testscour.py index 9c29e91..fcbd837 100755 --- a/testscour.py +++ b/testscour.py @@ -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, diff --git a/unittests/ids.svg b/unittests/ids.svg new file mode 100644 index 0000000..b787343 --- /dev/null +++ b/unittests/ids.svg @@ -0,0 +1,12 @@ + + + + + + + + + + + +