shortenIDs: Avoid pointless renames of IDs

With the current code, scour could do a pointless remap of an ID,
where there is no benefit in it.  Consider:

```xml
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
 <defs>
  <rect id="a" width="80" height="50" fill="red"/>
  <rect id="b" width="80" height="50" fill="blue"/>
 </defs>
 <use xlink:href="#a"/>
 <use xlink:href="#b"/>
 <use xlink:href="#b"/>
</svg>
```

In this example, there is no point in swapping the IDs - even if "#b"
is used more often than "#a", they have the same length.  Besides a
performance win on an already scour'ed image, it also mean scour will
behave like a function with a fixed-point (i.e. scour eventually stops
altering the image).

To solve this, we no longer check whether an we find exactly the same
ID.  Instead, we look at the length of the new ID compared to the
original.  This gives us a slight complication as we can now "reserve"
a "future" ID to avoid the rename.

Thanks to Eduard "Ede_123" Braun for providing the test case.

Signed-off-by: Niels Thykier <niels@thykier.net>
This commit is contained in:
Niels Thykier 2018-04-15 16:29:44 +00:00
parent 00cf42b554
commit d6406a3470
3 changed files with 43 additions and 5 deletions

View file

@ -1948,6 +1948,19 @@ class ShortenIDsOption(unittest.TestCase):
'Did not update reference to shortened ID')
class ShortenIDsStableOutput(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/shorten-ids-stable-output.svg',
parse_args(['--shorten-ids']))
use_tags = doc.getElementsByTagName('use')
hrefs_ordered = [x.getAttributeNS('http://www.w3.org/1999/xlink', 'href')
for x in use_tags]
expected = ['#a', '#b', '#b']
self.assertEquals(hrefs_ordered, expected,
'--shorten-ids pointlessly reassigned ids')
class MustKeepGInSwitch(unittest.TestCase):
def runTest(self):