Fix removal of common attributes if parent has non-whitespace text nodes
This commit is contained in:
parent
473e18500a
commit
7652fbc76c
6 changed files with 3823 additions and 787 deletions
File diff suppressed because one or more lines are too long
|
Before Width: | Height: | Size: 209 KiB After Width: | Height: | Size: 227 KiB |
|
|
@ -1,5 +1,5 @@
|
|||
#!/bin/bash
|
||||
SCOURVER="0.20"
|
||||
SCOURVER="0.21"
|
||||
cd ..
|
||||
zip scour/tarballs/scour-$SCOURVER.zip scour/scour.py scour/yocto_css.py scour/svg_regex.py scour/LICENSE scour/NOTICE scour/README.txt scour/release-notes.html
|
||||
cd scour
|
||||
|
|
|
|||
|
|
@ -9,6 +9,16 @@
|
|||
|
||||
<p>Copyright 2009, Jeff Schiller</p>
|
||||
|
||||
<section id="0.21">
|
||||
<header>
|
||||
<h2><a href="#0.21">Version 0.21</a></h2>
|
||||
</header>
|
||||
<p>TBD</p>
|
||||
<ul>
|
||||
<li>Fix <a href="https://bugs.launchpad.net/scour/+bug/427309">Bug 427309</a> by updated Scour inkscape extension file to include yocto_css.py</li>
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
<section id="0.20">
|
||||
<header>
|
||||
<h2><a href="#0.20">Version 0.20</a></h2>
|
||||
|
|
|
|||
29
scour.py
29
scour.py
|
|
@ -36,6 +36,9 @@
|
|||
# Next Up:
|
||||
# + remove unused attributes in parent elements
|
||||
# + prevent elements from being stripped if they are referenced in a <style> element
|
||||
# + only move common attributes and remove unused attributes after removing duplicate gradients
|
||||
# + only move common attributes to parent if the parent contains non-whitespace text nodes
|
||||
# - TODO: fix the removal of comment elements (between <?xml?> and <svg>)
|
||||
# (for instance, filter, marker, pattern) - need a crude CSS parser
|
||||
# - add an option to remove ids if they match the Inkscape-style of IDs
|
||||
# - investigate point-reducing algorithms
|
||||
|
|
@ -66,7 +69,7 @@ except ImportError:
|
|||
Decimal = FixedPoint
|
||||
|
||||
APP = 'scour'
|
||||
VER = '0.20'
|
||||
VER = '0.21'
|
||||
COPYRIGHT = 'Copyright Jeff Schiller, 2009'
|
||||
|
||||
NS = { 'SVG': 'http://www.w3.org/2000/svg',
|
||||
|
|
@ -431,7 +434,7 @@ def findReferencedElements(node, ids=None):
|
|||
return ids
|
||||
|
||||
# else if xlink:href is set, then grab the id
|
||||
href = node.getAttributeNS(NS['XLINK'],'href')
|
||||
href = node.getAttributeNS(NS['XLINK'],'href')
|
||||
if href != '' and len(href) > 1 and href[0] == '#':
|
||||
# we remove the hash mark from the beginning of the id
|
||||
id = href[1:]
|
||||
|
|
@ -641,7 +644,8 @@ def moveCommonAttributesToParentGroup(elem):
|
|||
"""
|
||||
This recursively calls this function on all children of the passed in element
|
||||
and then iterates over all child elements and removes common inheritable attributes
|
||||
from the children and places them in the parent group.
|
||||
from the children and places them in the parent group. But only if the parent contains
|
||||
nothing but element children and whitespace.
|
||||
"""
|
||||
num = 0
|
||||
|
||||
|
|
@ -651,6 +655,10 @@ def moveCommonAttributesToParentGroup(elem):
|
|||
if child.nodeType == 1:
|
||||
childElements.append(child)
|
||||
num += moveCommonAttributesToParentGroup(child)
|
||||
# else if the parent has non-whitespace text children, do not
|
||||
# try to move common attributes
|
||||
elif child.nodeType == 3 and child.nodeValue.strip():
|
||||
return num
|
||||
|
||||
# only process the children if there are more than one element
|
||||
if len(childElements) <= 1: return num
|
||||
|
|
@ -913,11 +921,14 @@ def removeDuplicateGradients(doc):
|
|||
referencedIDs = findReferencedElements(doc.documentElement)
|
||||
for masterGrad in gradientsToRemove.keys():
|
||||
master_id = masterGrad.getAttribute('id')
|
||||
# print 'master='+master_id
|
||||
for dupGrad in gradientsToRemove[masterGrad]:
|
||||
# if the duplicate gradient no longer has a parent that means it was
|
||||
# already re-mapped to another master gradient
|
||||
if not dupGrad.parentNode: continue
|
||||
dup_id = dupGrad.getAttribute('id')
|
||||
# print 'dup='+dup_id
|
||||
# print referencedIDs[dup_id]
|
||||
# for each element that referenced the gradient we are going to remove
|
||||
for elem in referencedIDs[dup_id][1]:
|
||||
# find out which attribute referenced the duplicate gradient
|
||||
|
|
@ -2196,12 +2207,6 @@ def scourString(in_string, options=None):
|
|||
while removeNestedGroups(doc.documentElement) > 0:
|
||||
pass
|
||||
|
||||
# move common attributes to parent group
|
||||
numAttrsRemoved += moveCommonAttributesToParentGroup(doc.documentElement)
|
||||
|
||||
# remove unused attributes from parent
|
||||
numAttrsRemoved += removeUnusedAttributesOnParent(doc.documentElement)
|
||||
|
||||
while removeDuplicateGradientStops(doc) > 0:
|
||||
pass
|
||||
|
||||
|
|
@ -2213,6 +2218,12 @@ def scourString(in_string, options=None):
|
|||
while removeDuplicateGradients(doc) > 0:
|
||||
pass
|
||||
|
||||
# move common attributes to parent group
|
||||
numAttrsRemoved += moveCommonAttributesToParentGroup(doc.documentElement)
|
||||
|
||||
# remove unused attributes from parent
|
||||
numAttrsRemoved += removeUnusedAttributesOnParent(doc.documentElement)
|
||||
|
||||
# clean path data
|
||||
for elem in doc.documentElement.getElementsByTagName('path') :
|
||||
if elem.getAttribute('d') == '':
|
||||
|
|
|
|||
|
|
@ -890,6 +890,12 @@ class RemoveCommonAttributesFromChild(unittest.TestCase):
|
|||
r = scour.scourXmlFile('unittests/move-common-attributes-to-parent.svg').getElementsByTagNameNS(SVGNS, 'rect')[0]
|
||||
self.assertNotEquals( r.getAttribute('fill'), '#0F0',
|
||||
'Did not remove common fill attribute from child')
|
||||
|
||||
class DontRemoveCommonAttributesIfParentHasTextNodes(unittest.TestCase):
|
||||
def runTest(self):
|
||||
text = scour.scourXmlFile('unittests/move-common-attributes-to-parent.svg').getElementsByTagNameNS(SVGNS, 'text')[0]
|
||||
self.assertNotEquals( text.getAttribute('font-style'), 'italic',
|
||||
'Removed common attributes when parent contained text elements')
|
||||
|
||||
class PropagateCommonAttributesUp(unittest.TestCase):
|
||||
def runTest(self):
|
||||
|
|
|
|||
|
|
@ -5,4 +5,9 @@
|
|||
<rect fill="#0F0" width="200" height="100" />
|
||||
<circle fill="#0F0" stroke="0F0" cx="50" cy="50" r="20" />
|
||||
</g>
|
||||
<text>Hello
|
||||
<tspan font-style="italic">World!</tspan>
|
||||
Goodbye
|
||||
<tspan font-style="italic">Cruel World!</tspan>
|
||||
</text>
|
||||
</svg>
|
||||
|
Before Width: | Height: | Size: 317 B After Width: | Height: | Size: 435 B |
Loading…
Add table
Add a link
Reference in a new issue