Add style argument.
This commit is contained in:
parent
7a83e7148d
commit
ec7d06f925
3 changed files with 72 additions and 7 deletions
|
|
@ -1800,15 +1800,27 @@ def repairStyle(node, options):
|
||||||
del styleMap['overflow']
|
del styleMap['overflow']
|
||||||
num += 1
|
num += 1
|
||||||
|
|
||||||
|
if node.nodeType == Node.ELEMENT_NODE:
|
||||||
|
if options.style_type == "inline":
|
||||||
|
# Prefer inline style
|
||||||
|
# Remove known SVG attributes and store their values in style attribute
|
||||||
|
attributes = [node.attributes.item(i).nodeName for i in range(node.attributes.length)]
|
||||||
|
for attribute in attributes:
|
||||||
|
if attribute in svgAttributes:
|
||||||
|
styleMap[attribute] = node.getAttribute(attribute)
|
||||||
|
node.removeAttribute(attribute)
|
||||||
|
elif options.style_type == "preserve":
|
||||||
|
# Keep whatever style of attribute versus style the file currently has
|
||||||
|
pass
|
||||||
|
elif options.style_to_xml or options.style_type == "attributes":
|
||||||
# now if any of the properties match known SVG attributes we prefer attributes
|
# now if any of the properties match known SVG attributes we prefer attributes
|
||||||
# over style so emit them and remove them from the style map
|
# over style so emit them and remove them from the style map
|
||||||
if options.style_to_xml:
|
for propName in list(styleMap):
|
||||||
for propName in list(styleMap):
|
if propName in svgAttributes:
|
||||||
if propName in svgAttributes:
|
node.setAttribute(propName, styleMap[propName])
|
||||||
node.setAttribute(propName, styleMap[propName])
|
del styleMap[propName]
|
||||||
del styleMap[propName]
|
|
||||||
|
_setStyle(node, styleMap)
|
||||||
_setStyle(node, styleMap)
|
|
||||||
|
|
||||||
# recurse for our child elements
|
# recurse for our child elements
|
||||||
for child in node.childNodes:
|
for child in node.childNodes:
|
||||||
|
|
@ -3985,6 +3997,9 @@ _option_group_optimization.add_option("--disable-simplify-colors",
|
||||||
_option_group_optimization.add_option("--disable-style-to-xml",
|
_option_group_optimization.add_option("--disable-style-to-xml",
|
||||||
action="store_false", dest="style_to_xml", default=True,
|
action="store_false", dest="style_to_xml", default=True,
|
||||||
help="won't convert styles into XML attributes")
|
help="won't convert styles into XML attributes")
|
||||||
|
_option_group_optimization.add_option("--style",
|
||||||
|
action="store", type="string", dest="style_type", default="none", metavar="TYPE",
|
||||||
|
help="styles type (override style_to_xml): none, preserve, inline, attributes (default: %none)")
|
||||||
_option_group_optimization.add_option("--disable-group-collapsing",
|
_option_group_optimization.add_option("--disable-group-collapsing",
|
||||||
action="store_false", dest="group_collapse", default=True,
|
action="store_false", dest="group_collapse", default=True,
|
||||||
help="won't collapse <g> elements")
|
help="won't collapse <g> elements")
|
||||||
|
|
@ -4101,6 +4116,8 @@ def parse_args(args=None, ignore_additional_args=False):
|
||||||
_options_parser.error("Value for --nindent should be positive (or zero), see --help")
|
_options_parser.error("Value for --nindent should be positive (or zero), see --help")
|
||||||
if options.infilename and options.outfilename and options.infilename == options.outfilename:
|
if options.infilename and options.outfilename and options.infilename == options.outfilename:
|
||||||
_options_parser.error("Input filename is the same as output filename")
|
_options_parser.error("Input filename is the same as output filename")
|
||||||
|
if options.style_type not in ['none', 'preserve', 'attributes', 'inline']:
|
||||||
|
_options_parser.error("Invalid value for --style, see --help")
|
||||||
|
|
||||||
return options
|
return options
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2212,6 +2212,44 @@ class StyleToAttr(unittest.TestCase):
|
||||||
self.assertEqual(line.getAttribute('marker-mid'), 'url(#m)')
|
self.assertEqual(line.getAttribute('marker-mid'), 'url(#m)')
|
||||||
self.assertEqual(line.getAttribute('marker-end'), 'url(#m)')
|
self.assertEqual(line.getAttribute('marker-end'), 'url(#m)')
|
||||||
|
|
||||||
|
class AttrToStyle(unittest.TestCase):
|
||||||
|
|
||||||
|
def runTest(self):
|
||||||
|
doc = scourXmlFile('unittests/attr-to-style.svg',
|
||||||
|
parse_args(['--style=inline']))
|
||||||
|
line = doc.getElementsByTagName('line')[0]
|
||||||
|
self.assertEqual(line.getAttribute('stroke'), '')
|
||||||
|
self.assertEqual(line.getAttribute('marker-start'), '')
|
||||||
|
self.assertEqual(line.getAttribute('marker-mid'), '')
|
||||||
|
self.assertEqual(line.getAttribute('marker-end'), '')
|
||||||
|
|
||||||
|
style_attribute = line.getAttribute('style')
|
||||||
|
rawStyles = style_attribute.split(';')
|
||||||
|
self.assertTrue("color:#FF0000" in rawStyles)
|
||||||
|
self.assertTrue("stroke:#000" in rawStyles)
|
||||||
|
self.assertTrue("marker-start:url(#m)" in rawStyles)
|
||||||
|
self.assertTrue("marker-end:url(#m)" in rawStyles)
|
||||||
|
self.assertTrue("marker-mid:url(#m)" in rawStyles)
|
||||||
|
|
||||||
|
class StylePreserve(unittest.TestCase):
|
||||||
|
|
||||||
|
def runTest(self):
|
||||||
|
doc = scourXmlFile('unittests/attr-to-style.svg',
|
||||||
|
parse_args(['--style=preserve']))
|
||||||
|
|
||||||
|
#First line uses attributes.
|
||||||
|
line = doc.getElementsByTagName('line')[0]
|
||||||
|
self.assertNotEqual(line.getAttribute('stroke'), '')
|
||||||
|
self.assertNotEqual(line.getAttribute('marker-start'), '')
|
||||||
|
self.assertNotEqual(line.getAttribute('marker-mid'), '')
|
||||||
|
self.assertNotEqual(line.getAttribute('marker-end'), '')
|
||||||
|
|
||||||
|
#Second line uses style attribute.
|
||||||
|
line = doc.getElementsByTagName('line')[1]
|
||||||
|
self.assertEqual(line.getAttribute('stroke'), '')
|
||||||
|
self.assertEqual(line.getAttribute('marker-start'), '')
|
||||||
|
self.assertEqual(line.getAttribute('marker-mid'), '')
|
||||||
|
self.assertEqual(line.getAttribute('marker-end'), '')
|
||||||
|
|
||||||
class PathCommandRewrites(unittest.TestCase):
|
class PathCommandRewrites(unittest.TestCase):
|
||||||
|
|
||||||
|
|
|
||||||
10
unittests/attr-to-style.svg
Normal file
10
unittests/attr-to-style.svg
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
<?xml version="1.0"?>
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<defs>
|
||||||
|
<marker id="m">
|
||||||
|
<rect width="200" height="100"/>
|
||||||
|
</marker>
|
||||||
|
</defs>
|
||||||
|
<line x2="100" stroke="#000" marker-start="url(#m)" marker-end="url(#m)" marker-mid="url(#m)" style="color:#FF0000" />
|
||||||
|
<line x2="100" style="stroke:#000; marker-start:url(#m); marker-end:url(#m); marker-mid: url(#m)" />
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 372 B |
Loading…
Add table
Add a link
Reference in a new issue