@@ -29,6 +29,30 @@ def _load_file(self, filename):
2929 def _stripImageData (cls , html ):
3030 return cls .BASE64_REGEX .sub (r'\1%s' % cls .FAKE_IMAGE , html )
3131
32+ FAKE_SVG = '...svg-body...'
33+ SVG_REGEX = re .compile (r'<(?:\w+:)?svg(?:( alt=".*?")|( class=".*?")|( title=".*?")|(?:.*?))+>.*</(?:\w+:)?svg>' )
34+
35+ @classmethod
36+ def _stripSvgData (cls , html ):
37+ """
38+ Simplifies SVG tags to easy comparing.
39+ :param html: source HTML
40+ :return: HTML code with simplified svg tags
41+ """
42+ def sort_attributes (groups ):
43+ """
44+ Sorts attributes in a specific order.
45+ :param groups: matched attributed groups
46+ :return: a SVG tag string source
47+ """
48+ alt = next (x for x in groups if x .startswith (' alt=' ))
49+ title = next (x for x in groups if x .startswith (' title=' ))
50+ classes = next (x for x in groups if x .startswith (' class=' ))
51+
52+ return "<svg{}{}{}>{}</svg>" .format (alt , title , classes , cls .FAKE_SVG )
53+
54+ return cls .SVG_REGEX .sub (lambda x : sort_attributes (x .groups ()), html )
55+
3256 def test_arg_title (self ):
3357 """
3458 Test for the correct parsing of the title argument
@@ -38,6 +62,15 @@ def test_arg_title(self):
3862 '<p><img alt="uml diagram" class="uml" src="data:image/png;base64,%s" title="Diagram test" /></p>' % self .FAKE_IMAGE ,
3963 self ._stripImageData (self .md .convert (text )))
4064
65+ def test_arg_title_inline_svg (self ):
66+ """
67+ Test for setting title attribute in inline SVG
68+ """
69+ text = self .text_builder .diagram ("A --> B" ).format ("svg_inline" ).title ("Diagram test" ).build ()
70+ self .assertEqual (
71+ '<p><svg alt="uml diagram" title="Diagram test" class="uml">%s</svg></p>' % self .FAKE_SVG ,
72+ self ._stripSvgData (self .md .convert (text )))
73+
4174 def test_arg_alt (self ):
4275 """
4376 Test for the correct parsing of the alt argument
@@ -47,6 +80,15 @@ def test_arg_alt(self):
4780 '<p><img alt="Diagram test" class="uml" src="data:image/png;base64,%s" title="" /></p>' % self .FAKE_IMAGE ,
4881 self ._stripImageData (self .md .convert (text )))
4982
83+ def test_arg_alt_inline_svg (self ):
84+ """
85+ Test for setting alt attribute in inline SVG
86+ """
87+ text = self .text_builder .diagram ("A --> B" ).format ("svg_inline" ).alt ("Diagram test" ).build ()
88+ self .assertEqual (
89+ '<p><svg alt="Diagram test" title="" class="uml">%s</svg></p>' % self .FAKE_SVG ,
90+ self ._stripSvgData (self .md .convert (text )))
91+
5092 def test_arg_classes (self ):
5193 """
5294 Test for the correct parsing of the classes argument
@@ -56,6 +98,15 @@ def test_arg_classes(self):
5698 '<p><img alt="uml diagram" class="class1 class2" src="data:image/png;base64,%s" title="" /></p>' % self .FAKE_IMAGE ,
5799 self ._stripImageData (self .md .convert (text )))
58100
101+ def test_arg_classes_inline_svg (self ):
102+ """
103+ Test for setting class attribute in inline SVG
104+ """
105+ text = self .text_builder .diagram ("A --> B" ).format ("svg_inline" ).classes ("class1 class2" ).build ()
106+ self .assertEqual (
107+ '<p><svg alt="uml diagram" title="" class="class1 class2">%s</svg></p>' % self .FAKE_SVG ,
108+ self ._stripSvgData (self .md .convert (text )))
109+
59110 def test_arg_format_png (self ):
60111 """
61112 Test for the correct parsing of the format argument, generating a png image
@@ -72,6 +123,22 @@ def test_arg_format_svg(self):
72123 self .assertEqual (self ._stripImageData (self ._load_file ('svg_diag.html' )),
73124 self ._stripImageData (self .md .convert (text )))
74125
126+ def test_arg_format_svg_object (self ):
127+ """
128+ Test for the correct parsing of the format argument, generating a svg image
129+ """
130+ text = self .text_builder .diagram ("A --> B" ).format ("svg_object" ).build ()
131+ self .assertEqual (self ._stripImageData (self ._load_file ('svg_object_diag.html' )),
132+ self ._stripImageData (self .md .convert (text )))
133+
134+ def test_arg_format_svg_inline (self ):
135+ """
136+ Test for the correct parsing of the format argument, generating a svg image
137+ """
138+ text = self .text_builder .diagram ("A --> B" ).format ("svg_inline" ).build ()
139+ self .assertEqual (self ._stripSvgData (self ._load_file ('svg_inline_diag.html' )),
140+ self ._stripSvgData (self .md .convert (text )))
141+
75142 def test_arg_format_txt (self ):
76143 """
77144 Test for the correct parsing of the format argument, generating a txt image
0 commit comments