Commits

Anonymous committed 6f48b46 Merge

merging

Comments (0)

Files changed (6)

 .svn
 *.pyc
 WebHelpers.egg-info
+build
+dist
 * Removed dependency on simplejson and normalized quotes. Patch by Elisha 
   Cook.
 
+COMPATIBILITY CHANGES IN 0.6 DEV VERSION:
+- image(), javascript_link(), stylesheet_link(), and auto_discovery_link()
+  in webhelpers.html.tags do not add prefixes or suffixes to the URL args
+  anymore; they output the exact URL given. Same for button_to() in
+  webhelpers.html.tools.
+- webhelpers.html.tags.javascript_path was deleted.
+
 0.3.4 (03/18/08)
 * Fixed missing javascripts dir.
 

tests/test_tags.py

 
 from webhelpers.html import HTML
 from webhelpers.html.tags import *
-from webhelpers.html.tags import compute_public_path
 
 class TestFormTagHelper(WebHelpersTestCase):
     def test_check_box(self):
         
     def test_image(self):
         self.assertEqual('<img alt="Xml" src="/images/xml.png" />',
-                         image('xml.png'))
+                         image('/images/xml.png', "Xml"))
+        self.assertEqual('<img alt="Xml" src="/images/xml.png" />',
+                         image('/images/xml.png', alt="Xml"))
+        self.assertEqual('<img alt="" src="/images/xml.png" />',
+                         image('/images/xml.png', ""))
+        self.assertEqual('<img alt="" src="/images/xml.png" />',
+                         image('/images/xml.png', None))
         self.assertEqual('<img alt="rss syndication" src="/images/rss.png" />',
-                         image('rss.png', alt='rss syndication'))
-        self.assertEqual('<img alt="Gold" height="70" src="/images/gold.png" width="45" />',
-                         image('gold.png', height=70, width=45))
-        self.assertEqual('<img alt="Symbolize" height="70" src="/images/symbolize.jpg" width="45" />',
-                         image('symbolize.jpg', height=70, width=45))
-        self.assertEqual('<img alt="Pylons-Tower-Dark1" src="http://pylons.tgtg.org/powered/_img/pylons-tower-dark1.png" />',
-                         image('http://pylons.tgtg.org/powered/_img/pylons-tower-dark1.png'))
+                         image('/images/rss.png', 'rss syndication'))
+        self.assertEqual('<img alt="Gold" height="70" src="gold.png" width="45" />',
+                         image('gold.png', "Gold", height=70, width=45))
         self.assertEqual('<img alt="Edit Entry" height="10" src="/images/icon.png" width="16" />',
-                         image("icon.png", height=10, width=16, alt="Edit Entry"))
+                         image("/images/icon.png", height=10, width=16, alt="Edit Entry"))
         self.assertEqual('<img alt="Icon" height="16" src="/icons/icon.gif" width="16" />',
-                         image("/icons/icon.gif", height=16, width=16))
+                         image("/icons/icon.gif", "Icon", height=16, width=16))
         self.assertEqual('<img alt="Icon" src="/icons/icon.gif" width="16" />',
-                         image("/icons/icon.gif", width=16))
+                         image("/icons/icon.gif", "Icon", width=16))
 
     def test_javascript_include_tag(self):
         self.assertEqual("""<script src="/javascripts/prototype.js" type="text/javascript"></script>\n<script src="/other-javascripts/util.js" type="text/javascript"></script>""",
-                         javascript_link('prototype', '/other-javascripts/util.js'))
+                         javascript_link('/javascripts/prototype.js', '/other-javascripts/util.js'))
         self.assertEqual("""<script defer="defer" src="/js/pngfix.js" type="text/javascript"></script>""",
                          javascript_link('/js/pngfix.js', defer=True))
-        self.assertEqual("""<script defer="defer" src="/js/pngfix.js" type="text/javascript"></script>""",
-                         javascript_link('/js/pngfix.js', defer="defer"))
 
     def test_stylesheet_link_tag(self):
-        self.assertEqual('<link href="/stylesheets/style.css" media="screen" rel="Stylesheet" type="text/css" />',
-                         stylesheet_link('style'))
-        self.assertEqual('<link href="/dir/file.css" media="all" rel="Stylesheet" type="text/css" />',
-                         stylesheet_link('/dir/file', media='all'))
-        self.assertEqual('<link href="/stylesheets/dir/file.css" media="screen" rel="Stylesheet" type="text/css" />',
-                         stylesheet_link('dir/file'))
-        self.assertEqual('<link href="/stylesheets/style.css" media="all" rel="Stylesheet" type="text/css" />',
-                         stylesheet_link('style', media='all'))
-        self.assertEqual('<link href="/stylesheets/random.styles" media="screen" rel="Stylesheet" type="text/css" />\n<link href="/css/stylish.css" media="screen" rel="Stylesheet" type="text/css" />',
-                         stylesheet_link('random.styles', '/css/stylish'))
-        self.assertEqual('<link href="/stylesheets/dir/file.css" media="all" rel="Stylesheet" type="text/css" />',
-                         stylesheet_link('dir/file', media='all'))
+        self.assertEqual('<link href="/dir/file.css" media="all" rel="stylesheet" type="text/css" />',
+                         stylesheet_link('/dir/file.css', media='all'))
+        self.assertEqual('<link href="style.css" media="all" rel="stylesheet" type="text/css" />',
+                         stylesheet_link('style.css', media='all'))
+        self.assertEqual('<link href="/random.styles" media="screen" rel="stylesheet" type="text/css" />\n<link href="/css/stylish.css" media="screen" rel="stylesheet" type="text/css" />',
+                         stylesheet_link('/random.styles', '/css/stylish.css'))
 
-    def test_compute_public_path(self):
-        self.assertEqual('/test.js', compute_public_path('/test.js'))
-        self.assertEqual('/test.js', compute_public_path('/test.js', 'javascripts'))
-        self.assertEqual('test.js', compute_public_path('test.js'))
-        self.assertEqual('test.js', compute_public_path('test', ext='js'))
-        self.assertEqual('/javascripts/test.js',
-                         compute_public_path('test.js', 'javascripts'))
-        self.assertEqual('/javascripts/test.js',
-                         compute_public_path('test', 'javascripts', 'js'))
-        self.assertEqual('/javascripts/test.js',
-                         compute_public_path('test.js', 'javascripts', 'js'))
-        self.assertEqual('http://www.pylonshq.com',
-                         compute_public_path('http://www.pylonshq.com'))
-        self.assertEqual('http://www.pylonshq.com',
-                         compute_public_path('http://www.pylonshq.com', 'javascripts'))
-        self.assertEqual('http://www.pylonshq.com',
-                         compute_public_path('http://www.pylonshq.com', 'javascripts', 'js'))
-        self.assertEqual('mailto:bdfl@python.org',
-                         compute_public_path('mailto:bdfl@python.org'))
-        self.assertEqual('mailto:bdfl@python.org',
-                         compute_public_path('mailto:bdfl@python.org', 'javascripts'))
-        self.assertEqual('mailto:bdfl@python.org',
-                         compute_public_path('mailto:bdfl@python.org', 'javascripts', 'js'))
 
-        
 if __name__ == '__main__':
     suite = map(unittest.makeSuite, [
         TestFormTagHelper,

tests/test_tools.py

 
     def test_button_to_with_img(self):
         self.assertEqual(u'<form action="/content/edit/3" class="button-to" method="POST"><div><input alt="Edit" src="/images/icon_delete.gif" type="image" value="Edit" /></div></form>',
-                         button_to("Edit", url_for(action='edit', id=3), type='image', src='icon_delete.gif'))
-        self.assertEqual(u'<form action="/content/submit/3" class="button-to" method="POST"><div><input alt="Complete the form" src="/images/submit.png" type="image" value="Submit" /></div></form>',
-                         button_to("Submit", url_for(action='submit', id=3), type='image', src='submit', alt='Complete the form'))
+                         button_to("Edit", url_for(action='edit', id=3), type='image', src='/images/icon_delete.gif'))
+        self.assertEqual(u'<form action="/content/submit/3" class="button-to" method="POST"><div><input alt="Complete the form" src="submit.png" type="image" value="Submit" /></div></form>',
+                         button_to("Submit", url_for(action='submit', id=3), type='image', src='submit.png', alt='Complete the form'))
 
     def test_mail_to(self):
         self.assertEqual(u'<a href="mailto:justin@example.com">justin@example.com</a>', mail_to("justin@example.com"))

webhelpers/html/tags.py

 import urllib
 import urlparse
 
-try:
-    from routes import request_config
-except ImportError:
-    request_config = None
-
 from webhelpers.html import escape, HTML, literal, url_escape
 
 __all__ = [
            # Head tags
            "auto_discovery_link",
            "javascript_link",
-           "javascript_path",
            "stylesheet_link",
            # Utility functions
            "convert_boolean_attrs",
         selected_values = (selected_values,)
     opts = []
     # Cast integer values to strings
-    selected_values = map(str, selected_values)
+    selected_values = map(unicode, selected_values)
     for option in options:
         if isinstance(option, basestring):
             label = value = option
+        elif isinstance(option, int):
+            label = value = unicode(option)
         else:
-            label = option[0]
-            value = option[1]
+            label = unicode(option[0])
+            value = unicode(option[1])
         if value in selected_values:
             opt = HTML.option(label, value=value, selected="selected")
         else:
 
 #### Non-form tags
 
-def image(source, alt=None, height=None, width=None, **attrs):
+def image(url, alt, width=None, height=None, **attrs):
     """Return an image tag for the specified ``source``.
 
-    ``source``
-        The source URL of the image. The URL is prepended with 
-        '/images/', unless its full path is specified. The URL is
-        ultimately prepended with the environment's ``SCRIPT_NAME``
-        (the root path of the web application), unless the URL is 
-        fully-fledged (e.g. http://example.com).
+    ``url``
+        The URL of the image.  (This must be the exact URL desired.  A
+        previous version of this helper added magic prefixes; this is
+        no longer the case.)
     
     ``alt``
-        The img's alt tag. Defaults to the source's filename, title
-        cased.
+        The img's alt tag. Non-graphical browsers and screen readers will
+        output this instead of the image.  If the image is pure decoration
+        and uninteresting to non-graphical users, pass "".  To omit the
+        alt tag completely, pass None.
+
+    ``width``
+        The width of the image, default is not included
 
     ``height``
         The height of the image, default is not included
         
-    ``width``
-        The width of the image, default is not included
-        
     Examples::
 
-        >>> image('xml.png')
-        literal(u'<img alt="Xml" src="/images/xml.png" />')
-
-        >>> image('rss.png', 'rss syndication')
+        >>> image('/images/rss.png', 'rss syndication')
         literal(u'<img alt="rss syndication" src="/images/rss.png" />')
 
-        >>> image("icon.png", height=16, width=10, alt="Edit Entry")
+        >>> image('/images/xml.png', "")
+        literal(u'<img alt="" src="/images/xml.png" />')
+
+        >>> image("/images/icon.png", height=16, width=10, alt="Edit Entry")
         literal(u'<img alt="Edit Entry" height="16" src="/images/icon.png" width="10" />')
 
-        >>> image("/icons/icon.gif", width=16, height=16)
+        >>> image("/icons/icon.gif", alt="Icon", width=16, height=16)
         literal(u'<img alt="Icon" height="16" src="/icons/icon.gif" width="16" />')
 
-        >>> image("/icons/icon.gif", width=16)
-        literal(u'<img alt="Icon" src="/icons/icon.gif" width="16" />')
+        >>> image("/icons/icon.gif", None, width=16)
+        literal(u'<img alt="" src="/icons/icon.gif" width="16" />')
         
     """
-    attrs['src'] = compute_public_path(source, 'images')
-
     if not alt:
-        alt = os.path.splitext(os.path.basename(source))[0].title()
-    attrs['alt'] = alt
-    
+        alt = ""
     if width is not None:
         attrs['width'] = width
     if height is not None:
         attrs['height'] = height
-        
-    return HTML.img(**attrs)
+    return HTML.img(src=url, alt=alt, **attrs)
 
 #### Tags for the HTML head
 
-# The absolute path of the WebHelpers javascripts directory
-javascript_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
-                               'javascripts')
+def javascript_link(*urls, **attrs):
+    """Return script include tags for the specified javascript URLs.
+    
+    ``urls`` should be the exact URLs desired.  A previous version of this
+    helper added magic prefixes; this is no longer the case.
 
-def javascript_link(*sources, **attrs):
-    """Return script include tags for the specified javascript
-    ``sources``.
-    
     Specify the keyword argument ``defer=True`` to enable the script 
     defer attribute.
 
     """
     convert_boolean_attrs(attrs, ["defer"])
     tags = []
-    for source in sources:
-        content_attrs = dict(type='text/javascript',
-                src=compute_public_path(source, 'javascripts', 'js'))
-        content_attrs.update(attrs)
-        tags.append(HTML.script('',  **content_attrs))
+    for url in urls:
+        tag = HTML.script("", type="text/javascript", src=url, **attrs)
+        tags.append(tag)
+    return literal("\n").join(tags)
+
+
+def stylesheet_link(*urls, **attrs):
+    """Return CSS link tags for the specified stylesheet URLs.
+
+    ``urls`` should be the exact URLs desired.  A previous version of this
+    helper added magic prefixes; this is no longer the case.
+
+    Examples::
+
+        >>> stylesheet_link('/stylesheets/style.css')
+        literal(u'<link href="/stylesheets/style.css" media="screen" rel="stylesheet" type="text/css" />')
+
+        >>> stylesheet_link('/stylesheets/dir/file.css', media='all')
+        literal(u'<link href="/stylesheets/dir/file.css" media="all" rel="stylesheet" type="text/css" />')
+
+    """
+    if "href" in attrs:
+        raise TypeError("keyword arg 'href' not allowed")
+    attrs.setdefault("rel", "stylesheet")
+    attrs.setdefault("type", "text/css")
+    attrs.setdefault("media", "screen")
+    tags = []
+    for url in urls:
+        tag = HTML.link(href=url, **attrs)
+        tags.append(tag)
     return literal('\n').join(tags)
 
 
-def stylesheet_link(*sources, **attrs):
-    """Return CSS link tags for the specified stylesheet ``sources``.
-
-    Each source's URL path is ultimately prepended with the 
-    environment's ``SCRIPT_NAME`` (the root path of the web 
-    application), unless the URL path is a full-fledged URL 
-    (e.g., http://example.com).
-    
-    Examples::
-
-        >>> stylesheet_link('style.css')
-        literal(u'<link href="/stylesheets/style.css" media="screen" rel="Stylesheet" type="text/css" />')
-
-        >>> stylesheet_link('/stylesheets/dir/file.css', media='all')
-        literal(u'<link href="/stylesheets/dir/file.css" media="all" rel="Stylesheet" type="text/css" />')
-
-    """
-    tag_attrs = dict(rel='Stylesheet', type='text/css', media='screen')
-    tag_attrs.update(attrs)
-    tag_attrs.pop('href', None)
-    
-    tags = [HTML.link(
-        **dict(href=compute_public_path(source, 'stylesheets', 'css'), 
-               **tag_attrs)) for source in sources]
-    return literal('\n').join(tags)
-
-
-def auto_discovery_link(source, feed_type='rss', **attrs):
+def auto_discovery_link(url, feed_type="rss", **attrs):
     """Return a link tag allowing auto-detecting of RSS or ATOM feed.
     
     The auto-detection of feed for the current page is only for
     browsers and news readers that support it.
 
-    ``source``
-        The URL of the feed. The URL is ultimately prepended with the
-        environment's ``SCRIPT_NAME`` (the root path of the web 
-        application), unless the URL is fully-fledged 
-        (e.g. http://example.com).
+    ``url``
+        The URL of the feed.  (This should be the exact URLs desired.  A
+        previous version of this helper added magic prefixes; this is no longer
+        the case.)
 
     ``feed_type``
         The type of feed. Specifying 'rss' or 'atom' automatically 
         literal(u'<link href="/app.html" rel="alternate" title="" type="text/html" />')
         
     """
-    title = ''
+    if "href" in attrs:
+        raise TypeError("keyword arg 'href' is not allowed")
+    if "type" in attrs:
+        raise TypeError("keyword arg 'type' is not allowed")
+    title = ""
     if feed_type.lower() in ('rss', 'atom'):
         title = feed_type.upper()
         feed_type = 'application/%s+xml' % feed_type.lower()
-
-    tag_args = dict(rel='alternate', type=feed_type, title=title,
-                    href=compute_public_path(source))
-    attrs.pop('href', None)
-    attrs.pop('type', None)
-    tag_args.update(attrs)
-    return HTML.link(**tag_args)
+    attrs.setdefault("title", title)
+    return HTML.link(rel="alternate", type=feed_type, href=url, **attrs)
 
 
 
 ########## INTERNAL FUNCTIONS ##########
 
-def compute_public_path(source, root_path=None, ext=None):
-    """Format the specified source for publishing.
-    
-    Use the public directory, if applicable.
-    
-    """
-    if ext and not os.path.splitext(os.path.basename(source))[1]:
-        source = '%s.%s' % (source, ext)
-
-    # Avoid munging fully-fledged URLs, including 'mailto:'
-    parsed = urlparse.urlparse(source)
-    if not (parsed[0] and (parsed[1] or parsed[2])):
-        # Prefix apps deployed under any SCRIPT_NAME path
-        if not root_path or source.startswith('/'):
-            source = '%s%s' % (get_script_name(), source)
-        else:
-            source = '%s/%s/%s' % (get_script_name(), root_path, source)
-    return source
-
-
 def convert_boolean_attrs(attrs, bool_attrs):
     """Convert boolean values into proper HTML attributes.
 
     attrs["type"] = type
     attrs["name"] = name
     attrs["value"] = value
-
-
-def get_script_name():
-    """Determine the current web application's ``SCRIPT_NAME``.
-    
-    .. note::
-        This requires Routes to function, and will not pick up the
-        SCRIPT_NAME var without it in use.
-    
-    """
-    script_name = ''
-    if request_config:
-        config = request_config()
-        if hasattr(config, 'environ'):
-            script_name = config.environ.get('SCRIPT_NAME', '')
-    else:
-        script_name = ''
-    return script_name
-
-

webhelpers/html/tools.py

 import urllib
 
 from webhelpers.html import HTML, literal, lit_sub, escape
-from webhelpers.html.tags import compute_public_path, convert_boolean_attrs
+from webhelpers.html.tags import convert_boolean_attrs
 
 __all__ = [
     'button_to', 
 
          type='image', src='icon_delete.gif'
 
-    The ``src`` path will be computed as the image_tag() computes its 
-    ``source`` argument.
+    The ``src`` path should be the exact URL desired.  A previous version of
+    this helper added magical prefixes but this is no longer the case.
 
     Example 1::
     
     submit_type = html_options.get('type')
     img_source = html_options.get('src')
     if submit_type == 'image' and img_source:
-        html_options.update(dict(type=submit_type, value=name,
-                                 alt=html_options.get('alt', name)))
-        html_options['src'] = compute_public_path(img_source, 'images', 'png')
+        html_options["value"] = name
+        html_options.setdefault("alt", name)
     else:
-        html_options.update(dict(type='submit', value=name))
+        html_options["type"] = "submit"
+        html_options["value"] = name
     
     return HTML.form(method=form_method, action=url, class_="button-to",
                      c=[HTML.div(method_tag, HTML.input(**html_options))])
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.