Scour occasionaly produces empty defs

Bug #717254 reported by Jan Thor
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Inkscape
Fix Released
Low
jazzynico
Scour
Fix Released
Low
Unassigned
scour (Ubuntu)
Fix Released
Undecided
Unassigned

Bug Description

It's certainly a minor thing (the possible size reduction is 15 Bytes), but I find it unfortunate if I open a scoured file and the first thing I see is an empty defs.

It happens like this: I have a default svg template file for Inkscape which contains some standard gradients I may or may not be using while creating content. I may end up saving a file where none of them is referenced. Next I start scour.

Scour looks for empty defs', but my defs contains some gradients, so it leaves my defs untouched. Next, it checks for unreferenced gradients, and since all my gradients are unreferenced, it removes them all, as it should. Now it saves the file, with a defs element which is now empty.

I provided a patch which, hopefully, solves this.

Within scourString(), I swapped the order of the removal of empty defs and the call of removeUnreferencedElements(), but that wasn't sufficient: the removal of empty defs is also the same code as the removal of empty metadata and the removal of empty groups. After successfully removing gradients, filters, pattern and symbols from my defs element, those removed elements left some text nodes behind which contain some whitespace. When checking for the presence of children, scour also checks for the presence of text node children, since it preserves metadata with (nothing but) text (it checks for the presence of child nodes of type 1, 3, 4 or 8, with 3 being the type of text nodes).

I handled this by checking whether a child text node contains nothing but whitespace characters; a metadata element with nothing but whitespace as child nodes should be something we should be allowed to delete.

While I was at it, I also tried to slightly update the description of removeUnreferencedElements(), and deleted setting num=0 a second time halfway through the code, which, I think, shouldn't be there. Within scourString(), I deleted a variable elemsToRemove which doesn't do anything.

Finally, I added a new file for unittest with a defs element with nothing but unreferenced removable children and added a test to testscour.py to see whether it gets removed.

Tags: patch

Related branches

Revision history for this message
Jan Thor (jan-janthor) wrote :
Revision history for this message
codedread (codedread) wrote :

Thanks Jan - this looks good to me, including the addition of the unit test, I assume all the other tests pass.

Since Louis has been much more involved with the project lately I'll let him review and merge it in to the trunk.

Thanks for your contribution!

Revision history for this message
Louis Simard (louis-simard-deactivatedaccount) wrote :

Oh, you can do it too; you're still the project creator. It would also be up to you, not to me, to add Jan to the Scouring Pads team if you trust his contributions.

But I'll do the review and merge.

Changed in scour:
assignee: nobody → Louis Simard (louis-simard)
importance: Undecided → Low
status: New → In Progress
Revision history for this message
Louis Simard (louis-simard-deactivatedaccount) wrote :

I'd advise against using str.isspace() and unicode.isspace() because SVG has a well-defined set of characters it considers whitespace. Though, in practice, even a form feed or vertical tab in <defs>, <metadata> or <g> isn't going to need being there.

The single unit test class looks sound, but checks for multiple things. It won't be obvious why the unit test fails, if it fails because of the whitespace-only <defs id="defs_two"> being kept. I'll add another unit test class and another file for the whitespace test.

Revision history for this message
Louis Simard (louis-simard-deactivatedaccount) wrote :

A modified version of your patch is now in Scour trunk as revision 203. Thanks for the bug report!

Changed in scour:
status: In Progress → Fix Committed
Revision history for this message
Jan Thor (jan-janthor) wrote :

Thanks again for the swift reaction.

tags: added: patch
Revision history for this message
Launchpad Janitor (janitor) wrote :

This bug was fixed in the package scour - 0.25+bzr207-1

---------------
scour (0.25+bzr207-1) unstable; urgency=low

  * New upstream bzr snapshot:
    - Add the option --no-renderer-workaround.
    - Fix gradient corruption. (LP: #702423)
    - Add option to only remove autogenerated id's. (LP: #492277)
    - Fix Windows line ending handling. (LP: #717826)
    - Fix occasional production of empty defs. (LP: #717254)
    - Remove more attributes with default values. (LP: #714731)
    - Fix wrong handling of file:// references. (LP: #708515)
    - Delete text attributes from groups with only non-text elements.
      (LP: #714727)
    - Remove unnecessary text-align properties from non-text elements.
      (LP: #714720)
    - Fix wrong optimization of 0-length Bézier curves. (LP: #714717)
    - Fix decimal number processing in rule_elliptical_arc(). (LP: #638764)
    - Fix transform matrix order. (LP: #722544)
  * Drop 01-bzr195.patch, upstream now.
  * debian/cmpsvg: Fix computation of difference to add up the absolute
    difference of each pixel.
  * debian/cmpsvg: Show percentages with three digits after comma.
  * debian/cmpsvg: Lower default treshold to 0.05%.
 -- Martin Pitt <email address hidden> Tue, 15 Mar 2011 09:28:45 +0100

Changed in scour (Ubuntu):
status: New → Fix Released
Revision history for this message
jazzynico (jazzynico) wrote :

Fix committed in the trunk, revision 10180.

Changed in inkscape:
assignee: nobody → JazzyNico (jazzynico)
importance: Undecided → Low
milestone: none → 0.49
status: New → Fix Committed
Revision history for this message
Louis Simard (louis-simard-deactivatedaccount) wrote :

This bug is fixed in release 0.26 of Scour.

Changed in scour:
status: Fix Committed → Fix Released
jazzynico (jazzynico)
Changed in inkscape:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.