diff --git a/svgis/dom.py b/svgis/dom.py index 54a5c3f..f3f6380 100644 --- a/svgis/dom.py +++ b/svgis/dom.py @@ -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) @@ -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]) diff --git a/tests/test_dom.py b/tests/test_dom.py index 7fe9bef..0f0bcb8 100644 --- a/tests/test_dom.py +++ b/tests/test_dom.py @@ -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() diff --git a/tests/test_style.py b/tests/test_style.py index df540e5..5e56bc0 100644 --- a/tests/test_style.py +++ b/tests/test_style.py @@ -28,7 +28,7 @@ class CssTestCase(unittest.TestCase): - + """ @@ -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') @@ -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()