From ce92515c1c949639be001608bacaf86f7778c98d Mon Sep 17 00:00:00 2001 From: Niels Thykier Date: Tue, 23 Feb 2021 19:45:36 +0000 Subject: [PATCH] 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 --- scour/scour.py | 6 +++--- test_scour.py | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/scour/scour.py b/scour/scour.py index 73f8dca..362180e 100644 --- a/scour/scour.py +++ b/scour/scour.py @@ -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) diff --git a/test_scour.py b/test_scour.py index bc9ee67..6637c62 100755 --- a/test_scour.py +++ b/test_scour.py @@ -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')