Skip to content

Commit

Permalink
repair bug with substring class names
Browse files Browse the repository at this point in the history
  • Loading branch information
fitnr committed Mar 8, 2016
1 parent d581f0f commit 07ee3fb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
10 changes: 8 additions & 2 deletions svgis/dom.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ def ns(tag):

def apply_rule(doc, rule):
'''
Apply a tinycss Rule to an ElementTree.Element
(only tested on documents created by SVGIS).
Apply a tinycss Rule to an ElementTree.Element (only tested on documents created by SVGIS).
Args:
doc (ElementTree.Element): The svg document to scan.
rule (tinycss rule): Rule to apply.
'''
tokenlist = _build_tokenlist(rule.selector)

Expand Down Expand Up @@ -77,6 +80,9 @@ def _style_string(declaration):

def _match_classes(elem_classes, rule_classes):
'''Check if rule_classes all fall in elem_classes.'''
if isinstance(elem_classes, str):
elem_classes = elem_classes.split(' ')

return all([c in elem_classes for c in rule_classes])


Expand Down
11 changes: 9 additions & 2 deletions tests/test_dom.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,19 @@ def testApplyRule(self):
dom.apply_rule(document, self.rules[0])

polygon = document.find('.//{http://www.w3.org/2000/svg}polygon')
self.assertIn('fill:orange', polygon.attrib['style'])

dom.apply_rule(document, self.rules[6])

polyline = document.find(".//*[@id='meow']")
self.assertIn('stroke-opacity:0.50', polyline.attrib.get('style', ''))

try:
self.assertIn('fill:orange', polygon.attrib['style'])
self.assertIn('stroke-opacity:0.50', polyline.attrib.get('style', ''))

except AssertionError:
print(ElementTree.tostring(polygon, encoding='utf-8'))
print(ElementTree.tostring(polyline, encoding='utf-8'))
raise

def testProcessTokens(self):
document = self.document()
Expand Down
14 changes: 13 additions & 1 deletion tests/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CssTestCase(unittest.TestCase):
<polyline id="baz" class="foo" points="3,2 -2,6 8,-1"></polyline>
</g>
<g id="cat">
<polyline id="meow" points="3,2 -2,6 8,-1"></polyline>
<polyline id="meow" class="long-class-name" points="3,2 -2,6 8,-1"></polyline>
</g>
</g>
</svg>"""
Expand All @@ -40,6 +40,8 @@ class CssTestCase(unittest.TestCase):
#test ~ #foo { fill: purple; }
#cat polyline { fill: red }"""

css1 = '''.class-name { fill: orange;}'''

file = 'tests/test_data/test.svg'

classes = ('apple', 'potato')
Expand Down Expand Up @@ -198,6 +200,16 @@ def testCDATA(self):

self.assertIn(content.encode('utf8'), string)

def testPartialStyleName(self):
doc = ElementTree.fromstring(self.svg).find('./' + dom.ns('g'))
ruleset = style._parse_css(self.css1)

dom.apply_rule(doc, ruleset.rules[0])
svg = ElementTree.tostring(doc, encoding='utf-8').decode('utf-8')
self.assertNotIn('orange', svg)

inlined = style.inline(self.svg, self.css1)
self.assertNotIn('orange', inlined)

if __name__ == '__main__':
unittest.main()

0 comments on commit 07ee3fb

Please sign in to comment.