Always normalize the color name when shortening colors

This enables scour to consistently perform other optimizations that
rely on string equality to determine if two attributes are identical.

Signed-off-by: Niels Thykier <niels@thykier.net>
This commit is contained in:
Niels Thykier 2021-02-23 19:45:36 +00:00
parent 7879ecb23b
commit ce92515c1c
No known key found for this signature in database
GPG key ID: A65B78DBE67C7AAC
2 changed files with 10 additions and 10 deletions

View file

@ -2316,16 +2316,16 @@ def convertColors(element):
newColorValue = convertColor(oldColorValue)
oldBytes = len(oldColorValue)
newBytes = len(newColorValue)
if oldBytes > newBytes:
if oldBytes >= newBytes and oldColorValue != newColorValue:
element.setAttribute(attr, newColorValue)
numBytes += (oldBytes - len(element.getAttribute(attr)))
numBytes += (oldBytes - newBytes)
# colors might also hide in styles
if attr in styles:
oldColorValue = styles[attr]
newColorValue = convertColor(oldColorValue)
oldBytes = len(oldColorValue)
newBytes = len(newColorValue)
if oldBytes > newBytes:
if oldBytes >= newBytes and oldColorValue != newColorValue:
styles[attr] = newColorValue
numBytes += (oldBytes - newBytes)
_setStyle(element, styles)

View file

@ -1242,10 +1242,10 @@ class TranslateColorIntoNameIfShorter(unittest.TestCase):
self.assertEqual(short.getAttribute('stroke'), 'red',
'Not converting color into color name')
self.assertEqual(tied.getAttribute('fill'), 'blue',
'Not keeping the current name when it ties with the shortest match')
self.assertEqual(tied.getAttribute('fill'), '#00f',
'Not converting to hex code when name ties with hex code in length')
self.assertEqual(tied.getAttribute('stroke'), '#00f',
'Not using color hex code when rewriting color where len(hex_code) == len(name)')
'Not converting to hex code when name ties with hex code in length')
class DoNotConvertShortColorNames(unittest.TestCase):
@ -1740,7 +1740,7 @@ class MoveCommonAttributesToParent(unittest.TestCase):
def runTest(self):
g = scourXmlFile('unittests/move-common-attributes-to-parent.svg') \
.getElementsByTagNameNS(SVGNS, 'g')[0]
self.assertEqual(g.getAttribute('fill'), '#0F0',
self.assertEqual(g.getAttribute('fill'), '#0f0',
'Did not move common fill attribute to parent group')
@ -1749,7 +1749,7 @@ class RemoveCommonAttributesFromChild(unittest.TestCase):
def runTest(self):
r = scourXmlFile('unittests/move-common-attributes-to-parent.svg') \
.getElementsByTagNameNS(SVGNS, 'rect')[0]
self.assertNotEqual(r.getAttribute('fill'), '#0F0',
self.assertNotEqual(r.getAttribute('fill'), '#0f0',
'Did not remove common fill attribute from child')
@ -1767,7 +1767,7 @@ class PropagateCommonAttributesUp(unittest.TestCase):
def runTest(self):
g = scourXmlFile('unittests/move-common-attributes-to-grandparent.svg') \
.getElementsByTagNameNS(SVGNS, 'g')[0]
self.assertEqual(g.getAttribute('fill'), '#0F0',
self.assertEqual(g.getAttribute('fill'), '#0f0',
'Did not move common fill attribute to grandparent')
@ -1785,7 +1785,7 @@ class DoNotRemoveCommonAttributesOnParentIfAtLeastOneUsed(unittest.TestCase):
def runTest(self):
g = scourXmlFile('unittests/remove-unused-attributes-on-parent.svg') \
.getElementsByTagNameNS(SVGNS, 'g')[0]
self.assertEqual(g.getAttribute('fill'), '#0F0',
self.assertEqual(g.getAttribute('fill'), '#0f0',
'Used attributes on group were removed')