changeset 3266:f62792cb2d24

macro.EmbedObject: adjusted to changes of argument parser, escaped output.
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Sun, 16 Mar 2008 17:35:30 +0100
parents bd322703c85e
children 65be8803b8df
files MoinMoin/action/AttachFile.py MoinMoin/macro/EmbedObject.py MoinMoin/macro/_tests/test_EmbedObject.py MoinMoin/parser/text_moin_wiki.py
diffstat 4 files changed, 41 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/action/AttachFile.py	Sun Mar 16 16:36:06 2008 +0100
+++ b/MoinMoin/action/AttachFile.py	Sun Mar 16 17:35:30 2008 +0100
@@ -991,14 +991,15 @@
         request.write("</pre>")
         return
 
-    # reuse class tmp from Despam to define macro
-    from MoinMoin.action.Despam import tmp
-    macro = tmp()
+    from MoinMoin import macro
+    from MoinMoin.parser.text import Parser
+
     macro.request = request
     macro.formatter = request.html_formatter
+    p = Parser("##\n", request)
+    m = macro.Macro(p)
 
     # use EmbedObject to view valid mime types
-    from MoinMoin.macro import EmbedObject
     if mt is None:
         request.write('<p>' + _("Unknown file type, cannot display this attachment inline.") + '</p>')
         link = (fmt.url(1, getAttachUrl(pagename, filename, request)) +
@@ -1006,8 +1007,7 @@
                 fmt.url(0))
         request.write('For using an external program follow this link %s' % link)
         return
-
-    request.write(EmbedObject.macro_EmbedObject(macro, filename, pagename=pagename))
+    request.write(m.execute('EmbedObject', u'target=%s, pagename=%s' % (filename, pagename)))
     return
 
 
--- a/MoinMoin/macro/EmbedObject.py	Sun Mar 16 16:36:06 2008 +0100
+++ b/MoinMoin/macro/EmbedObject.py	Sun Mar 16 17:35:30 2008 +0100
@@ -28,7 +28,7 @@
     @param value: value of param
     """
     if value:
-        return '%(param)s="%(value)s"' % {"param": param, "value": value}
+        return '%(param)s="%(value)s"' % {"param": param, "value": wikiutil.escape(value)}
     else:
         return ""
 
@@ -41,29 +41,30 @@
     if value:
         return '''
 <param name="%(param)s" value="%(value)s" valuetype="%(valuetype)s">''' % {"param": param,
-                                                                           "value": value,
+                                                                           "value": wikiutil.escape(value),
                                                                            "valuetype": valuetype}
     else:
         return ""
 
-def macro_EmbedObject(macro, target=None, pagename=None, width=wikiutil.UnitArgument('0px', float, ['px', 'em', 'mm', '%']),
-                      height=wikiutil.UnitArgument('0px', float, ['px', 'em', 'mm', '%']), alt=u'',
+def macro_EmbedObject(macro, target=None, pagename=None, width=wikiutil.UnitArgument(None, float, ['px', 'em', 'mm', '%']),
+                      height=wikiutil.UnitArgument(None, float, ['px', 'em', 'mm', '%']), alt=u'',
                       play=False, stop=True, loop=False, quality=(u'high', u'low', u'medium'),
                       op=True, repeat=False, autostart=False, align=(u'middle', u'top', u'bottom'), hidden=False,
-                      menu=True, wmode='transparent', url_mimetype=None):
+                      menu=True, wmode=u'transparent', url_mimetype=None):
 
     """ This macro is used to embed an object into a wiki page """
-
     # Join unit arguments with their units
-    if width[1] == 'px':
-        width = '%dpx' % int(width[0])
-    else:
-        width = '%f%s' % width
+    if width:
+        if width[1] == 'px':
+            width = '%dpx' % int(width[0])
+        else:
+            width = '%g%s' % width
 
-    if height[1] == 'px':
-        height = '%dpx' % int(height[0])
-    else:
-        height = '%f%s' % height
+    if height:
+        if height[1] == 'px':
+            height = '%dpx' % int(height[0])
+        else:
+            height = '%g%s' % height
 
     request = macro.request
     _ = macro.request.getText
@@ -124,9 +125,10 @@
                       }
     embed_src = ''
     if mt.major == 'video':
-        # default for video otherweise it may be shown in an external viewer
-        # xxx check the argument parser
-        width = width or '400px'
+        if not width and not height:
+            width = '400px'
+            height = '400px'
+
         embed_src = '''
 <object %(ob_data)s %(ob_type)s %(ob_width)s %(ob_height)s %(ob_align)s %(ob_standby)s %(ob_stop)s>
 %(wmode)s%(movie)s%(play)s%(stop)s%(repeat)s%(autostart)s%(op)s%(menu)s
@@ -147,7 +149,7 @@
     "autostart": _check_param_value("autostart", autostart, "data"),
     "op": _check_param_value("op", op, "data"),
     "menu": _check_param_value("menu", menu, "data"),
-    "alt": alt,
+    "alt": wikiutil.escape(alt),
 }
 
     if mt.major in ['image', 'chemical', 'x-world']:
@@ -162,10 +164,13 @@
     "ob_type": _check_object_value("type", mime_type),
     "ob_align": _check_object_value("align", align),
     "name": _check_param_value("name", url, "data"),
-    "alt": alt,
+    "alt": wikiutil.escape(alt),
 }
 
     if mt.major == 'audio':
+        if not width and not height:
+            width = '400px'
+            height = '100px'
         embed_src = '''
 <object %(ob_data)s %(ob_type)s  %(ob_width)s %(ob_height)s %(ob_align)s>
 %(audio)s%(repeat)s%(autostart)s%(op)s%(play)s%(stop)s%(hidden)s<p>%(alt)s</p>
@@ -182,7 +187,7 @@
     "play": _check_param_value("play", play, "data"),
     "stop": _check_param_value("stop", stop, "data"),
     "hidden": _check_param_value("hidden", hidden, "data"),
-    "alt": alt,
+    "alt": wikiutil.escape(alt),
 }
 
     if mt.major == 'application':
@@ -206,8 +211,8 @@
     "play": _check_param_value("play", play, "data"),
     "loop": _check_param_value("loop", loop, "data"),
     "menu": _check_param_value("menu", menu, "data"),
-    "alt": alt,
+    "alt": wikiutil.escape(alt),
 }
 
-    return fmt.rawHTML(embed_src)
+    return embed_src
 
--- a/MoinMoin/macro/_tests/test_EmbedObject.py	Sun Mar 16 16:36:06 2008 +0100
+++ b/MoinMoin/macro/_tests/test_EmbedObject.py	Sun Mar 16 17:35:30 2008 +0100
@@ -63,10 +63,9 @@
         m = self._make_macro()
         filename = 'test.ogg'
         result = m.execute('EmbedObject', u'%s' % filename)
-        assert '<object data="./AutoCreatedMoinMoinTemporaryTestPageForEmbedObject?action=AttachFile&do=get&target=test.ogg"' in result
-        assert 'height="0px"' in result
-        assert 'width="0px"' in result
+        assert '<object data="./AutoCreatedMoinMoinTemporaryTestPageForEmbedObject?action=AttachFile&amp;do=get&amp;target=test.ogg"' in result
         assert 'align="middle"' in result
+        assert 'value="transparent"' in result
 
     def testEmbedObjectPercentHeight(self):
         """ tests default values of macro EmbedObject"""
@@ -76,9 +75,8 @@
         filename = 'test.ogg'
         height = '50 %' # also tests that space is allowed in there
         result = m.execute('EmbedObject', u'target=%s, height=%s' % (filename, height))
-        assert '<object data="./AutoCreatedMoinMoinTemporaryTestPageForEmbedObject?action=AttachFile&do=get&target=test.ogg"' in result
-        assert 'height="50.000000%"' in result
-        assert 'width="0px"' in result
+        assert '<object data="./AutoCreatedMoinMoinTemporaryTestPageForEmbedObject?action=AttachFile&amp;do=get&amp;target=test.ogg"' in result
+        assert 'height="50%"' in result
         assert 'align="middle"' in result
 
 coverage_modules = ['MoinMoin.macro.EmbedObject']
--- a/MoinMoin/parser/text_moin_wiki.py	Sun Mar 16 16:36:06 2008 +0100
+++ b/MoinMoin/parser/text_moin_wiki.py	Sun Mar 16 17:35:30 2008 +0100
@@ -733,16 +733,14 @@
                         #NOT USED CURRENTLY:
 
                         # use EmbedObject for other mimetypes
-                        from MoinMoin.macro import EmbedObject
                         if mt is not None:
-                            # reuse class tmp from Despam to define macro
-                            from MoinMoin.action.Despam import tmp
-                            macro = tmp()
+                            from MoinMoin import macro
                             macro.request = self.request
                             macro.formatter = self.request.html_formatter
+                            p = Parser("##\n", request)
+                            m = macro.Macro(p)
                             pagename = self.formatter.page.page_name
-                            href = AttachFile.getAttachUrl(pagename, url, self.request, escaped=1)
-                            return EmbedObject.macro_EmbedObject(macro, url)
+                            return m.execute('EmbedObject', u'target=%s' % url)
                 elif scheme == 'drawing':
                     desc = self._transclude_description(desc, url)
                     return self.formatter.attachment_drawing(url, desc)