diff --git a/HISTORY.md b/HISTORY.md index 0cbe1a6..fe9ed89 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -11,6 +11,8 @@ * Redirect informational output to `stderr` when SVG is output to `stdout`. ([#67](https://github.com/scour-project/scour/issues/67)) * Allow elements to be found via `Document.getElementById()` in the minidom document returned by scourXmlFile(). ([#68](https://github.com/scour-project/scour/issues/68)) * Improve code to remove default attribute values and add a lot of new default values. ([#70](https://github.com/scour-project/scour/issues/70)) +* Fix: Only attempt to group elements that the content model allows to be children of a `` when `--create-groups` is specified. (#98) + ## Version 0.34 (2016-07-25) diff --git a/scour/scour.py b/scour/scour.py index 5452b7d..abde59b 100644 --- a/scour/scour.py +++ b/scour/scour.py @@ -1032,7 +1032,24 @@ def createGroupsForCommonAttributes(elem): while curChild >= 0: childNode = elem.childNodes.item(curChild) - if childNode.nodeType == 1 and childNode.getAttribute(curAttr) != '': + if childNode.nodeType == 1 and childNode.getAttribute(curAttr) != '' and \ + childNode.nodeName in [ + # only attempt to group elements that the content model allows to be children of a + + # SVG 1.1 (see https://www.w3.org/TR/SVG/struct.html#GElement) + 'animate', 'animateColor', 'animateMotion', 'animateTransform', 'set', # animation elements + 'desc', 'metadata', 'title', # descriptive elements + 'circle', 'ellipse', 'line', 'path', 'polygon', 'polyline', 'rect', # shape elements + 'defs', 'g', 'svg', 'symbol', 'use', # structural elements + 'linearGradient', 'radialGradient', # gradient elements + 'a', 'altGlyphDef', 'clipPath', 'color-profile', 'cursor', 'filter', + 'font', 'font-face', 'foreignObject', 'image', 'marker', 'mask', + 'pattern', 'script', 'style', 'switch', 'text', 'view', + + # SVG 1.2 (see https://www.w3.org/TR/SVGTiny12/elementTable.html) + 'animation', 'audio', 'discard', 'handler', 'listener', + 'prefetch', 'solidColor', 'textArea', 'video' + ]: # We're in a possible run! Track the value and run length. value = childNode.getAttribute(curAttr) runStart, runEnd = curChild, curChild diff --git a/testscour.py b/testscour.py index c31d86a..fa30bb6 100755 --- a/testscour.py +++ b/testscour.py @@ -1222,6 +1222,13 @@ class GroupNoCreation(unittest.TestCase): self.assertEqual(doc.getElementsByTagName('g').length, 0, 'Created a for a run of elements having dissimilar attributes') +class GroupNoCreationForTspan(unittest.TestCase): + def runTest(self): + doc = scour.scourXmlFile('unittests/group-no-creation-tspan.svg', + scour.parse_args(['--create-groups'])) + self.assertEqual(doc.getElementsByTagName('g').length, 0, + 'Created a for a run of s that are not allowed as children according to content model') + class DoNotCommonizeAttributesOnReferencedElements(unittest.TestCase): def runTest(self): doc = scour.scourXmlFile('unittests/commonized-referenced-elements.svg') diff --git a/unittests/group-no-creation-tspan.svg b/unittests/group-no-creation-tspan.svg new file mode 100644 index 0000000..65f3803 --- /dev/null +++ b/unittests/group-no-creation-tspan.svg @@ -0,0 +1,8 @@ + + + + text1 + text2 + text3 + +