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:
parent
7879ecb23b
commit
ce92515c1c
2 changed files with 10 additions and 10 deletions
|
|
@ -2316,16 +2316,16 @@ def convertColors(element):
|
||||||
newColorValue = convertColor(oldColorValue)
|
newColorValue = convertColor(oldColorValue)
|
||||||
oldBytes = len(oldColorValue)
|
oldBytes = len(oldColorValue)
|
||||||
newBytes = len(newColorValue)
|
newBytes = len(newColorValue)
|
||||||
if oldBytes > newBytes:
|
if oldBytes >= newBytes and oldColorValue != newColorValue:
|
||||||
element.setAttribute(attr, newColorValue)
|
element.setAttribute(attr, newColorValue)
|
||||||
numBytes += (oldBytes - len(element.getAttribute(attr)))
|
numBytes += (oldBytes - newBytes)
|
||||||
# colors might also hide in styles
|
# colors might also hide in styles
|
||||||
if attr in styles:
|
if attr in styles:
|
||||||
oldColorValue = styles[attr]
|
oldColorValue = styles[attr]
|
||||||
newColorValue = convertColor(oldColorValue)
|
newColorValue = convertColor(oldColorValue)
|
||||||
oldBytes = len(oldColorValue)
|
oldBytes = len(oldColorValue)
|
||||||
newBytes = len(newColorValue)
|
newBytes = len(newColorValue)
|
||||||
if oldBytes > newBytes:
|
if oldBytes >= newBytes and oldColorValue != newColorValue:
|
||||||
styles[attr] = newColorValue
|
styles[attr] = newColorValue
|
||||||
numBytes += (oldBytes - newBytes)
|
numBytes += (oldBytes - newBytes)
|
||||||
_setStyle(element, styles)
|
_setStyle(element, styles)
|
||||||
|
|
|
||||||
|
|
@ -1242,10 +1242,10 @@ class TranslateColorIntoNameIfShorter(unittest.TestCase):
|
||||||
self.assertEqual(short.getAttribute('stroke'), 'red',
|
self.assertEqual(short.getAttribute('stroke'), 'red',
|
||||||
'Not converting color into color name')
|
'Not converting color into color name')
|
||||||
|
|
||||||
self.assertEqual(tied.getAttribute('fill'), 'blue',
|
self.assertEqual(tied.getAttribute('fill'), '#00f',
|
||||||
'Not keeping the current name when it ties with the shortest match')
|
'Not converting to hex code when name ties with hex code in length')
|
||||||
self.assertEqual(tied.getAttribute('stroke'), '#00f',
|
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):
|
class DoNotConvertShortColorNames(unittest.TestCase):
|
||||||
|
|
@ -1740,7 +1740,7 @@ class MoveCommonAttributesToParent(unittest.TestCase):
|
||||||
def runTest(self):
|
def runTest(self):
|
||||||
g = scourXmlFile('unittests/move-common-attributes-to-parent.svg') \
|
g = scourXmlFile('unittests/move-common-attributes-to-parent.svg') \
|
||||||
.getElementsByTagNameNS(SVGNS, 'g')[0]
|
.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')
|
'Did not move common fill attribute to parent group')
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1749,7 +1749,7 @@ class RemoveCommonAttributesFromChild(unittest.TestCase):
|
||||||
def runTest(self):
|
def runTest(self):
|
||||||
r = scourXmlFile('unittests/move-common-attributes-to-parent.svg') \
|
r = scourXmlFile('unittests/move-common-attributes-to-parent.svg') \
|
||||||
.getElementsByTagNameNS(SVGNS, 'rect')[0]
|
.getElementsByTagNameNS(SVGNS, 'rect')[0]
|
||||||
self.assertNotEqual(r.getAttribute('fill'), '#0F0',
|
self.assertNotEqual(r.getAttribute('fill'), '#0f0',
|
||||||
'Did not remove common fill attribute from child')
|
'Did not remove common fill attribute from child')
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1767,7 +1767,7 @@ class PropagateCommonAttributesUp(unittest.TestCase):
|
||||||
def runTest(self):
|
def runTest(self):
|
||||||
g = scourXmlFile('unittests/move-common-attributes-to-grandparent.svg') \
|
g = scourXmlFile('unittests/move-common-attributes-to-grandparent.svg') \
|
||||||
.getElementsByTagNameNS(SVGNS, 'g')[0]
|
.getElementsByTagNameNS(SVGNS, 'g')[0]
|
||||||
self.assertEqual(g.getAttribute('fill'), '#0F0',
|
self.assertEqual(g.getAttribute('fill'), '#0f0',
|
||||||
'Did not move common fill attribute to grandparent')
|
'Did not move common fill attribute to grandparent')
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1785,7 +1785,7 @@ class DoNotRemoveCommonAttributesOnParentIfAtLeastOneUsed(unittest.TestCase):
|
||||||
def runTest(self):
|
def runTest(self):
|
||||||
g = scourXmlFile('unittests/remove-unused-attributes-on-parent.svg') \
|
g = scourXmlFile('unittests/remove-unused-attributes-on-parent.svg') \
|
||||||
.getElementsByTagNameNS(SVGNS, 'g')[0]
|
.getElementsByTagNameNS(SVGNS, 'g')[0]
|
||||||
self.assertEqual(g.getAttribute('fill'), '#0F0',
|
self.assertEqual(g.getAttribute('fill'), '#0f0',
|
||||||
'Used attributes on group were removed')
|
'Used attributes on group were removed')
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue