remapNamespacePrefix: Preserve prefix of attribute names

Preserve prefix of attribute names when copying them over to the new
node.  This fixes an unintentional rewrite of `xml:space` to `space`
that also caused scour to strip whitespace that should have been
preserved.

Closes: #239
Signed-off-by: Niels Thykier <niels@thykier.net>
This commit is contained in:
Niels Thykier 2020-06-10 05:54:55 +00:00
parent 985cb58a26
commit 96d85ee819
No known key found for this signature in database
GPG key ID: A65B78DBE67C7AAC
3 changed files with 12 additions and 2 deletions

View file

@ -3401,7 +3401,7 @@ def remapNamespacePrefix(node, oldprefix, newprefix):
attrList = node.attributes attrList = node.attributes
for i in range(attrList.length): for i in range(attrList.length):
attr = attrList.item(i) attr = attrList.item(i)
newNode.setAttributeNS(attr.namespaceURI, attr.localName, attr.nodeValue) newNode.setAttributeNS(attr.namespaceURI, attr.name, attr.nodeValue)
# clone and add all the child nodes # clone and add all the child nodes
for child in node.childNodes: for child in node.childNodes:

View file

@ -1502,7 +1502,16 @@ class RemoveRedundantSvgNamespacePrefix(unittest.TestCase):
doc = scourXmlFile('unittests/redundant-svg-namespace.svg').documentElement doc = scourXmlFile('unittests/redundant-svg-namespace.svg').documentElement
r = doc.getElementsByTagNameNS(SVGNS, 'rect')[1] r = doc.getElementsByTagNameNS(SVGNS, 'rect')[1]
self.assertEqual(r.tagName, 'rect', self.assertEqual(r.tagName, 'rect',
'Redundant svg: prefix not removed') 'Redundant svg: prefix not removed from rect')
t = doc.getElementsByTagNameNS(SVGNS, 'text')[0]
self.assertEqual(t.tagName, 'text',
'Redundant svg: prefix not removed from text')
# Regression test for #239
self.assertEqual(t.getAttribute('xml:space'), 'preserve',
'Required xml: prefix removed in error')
self.assertEqual(t.getAttribute("space"), '',
'Required xml: prefix removed in error')
class RemoveDefaultGradX1Value(unittest.TestCase): class RemoveDefaultGradX1Value(unittest.TestCase):

View file

@ -5,4 +5,5 @@
<title>Test</title> <title>Test</title>
</svg:rect> </svg:rect>
<vector:rect height="100" width="100"/> <vector:rect height="100" width="100"/>
<svg:text xml:space="preserve"> Hallo World </svg:text>
</svg> </svg>

Before

Width:  |  Height:  |  Size: 331 B

After

Width:  |  Height:  |  Size: 390 B

Before After
Before After