1. Alex Willmer
  2. trac-ticketlinks


rblank  committed 8396b9a

0.13dev: Merged from 0.12-stable.

  • Participants
  • Parent commits e5b81dd
  • Branches trunk

Comments (0)

Files changed (5)

File trac/mimeview/rst.py

View file
 from trac.core import *
 from trac.env import ISystemInfoProvider
 from trac.mimeview.api import IHTMLPreviewRenderer, content_to_unicode
-from trac.util.html import Element, Markup
+from trac.util.html import Element, Fragment, Markup, find_element
 from trac.util.translation import _
 from trac.wiki.api import WikiSystem
 from trac.wiki.formatter import WikiProcessor, Formatter, extract_link
         link = extract_link(env, context, fulltext)
         uri = None
         missing = False
-        if isinstance(link, Element):
+        if isinstance(link, (Element, Fragment)):
             linktext = Markup(link).striptags()
             # the following is a bit hackish, but it takes into account:
             #  - an eventual trailing '?' for missing wiki pages
             #  - space eventually introduced due to split_page_names option
             if linktext.rstrip('?').replace(' ', '') != target:
                 text = linktext
-            uri = link.attrib.get('href', '')
-            missing = 'missing' in link.attrib.get('class', '')
+            elt = find_element(link, 'href', 'missing')
+            if elt is not None:
+                uri = elt.attrib.get('href', '')
+                missing = 'missing' in elt.attrib.get('class', '').split()
             uri = context.href.wiki(target)
             missing = not WikiSystem(env).has_page(target)
             if missing:
             return reference
-        return None
     def trac_directive(name, arguments, options, content, lineno,
                        content_offset, block_text, state, state_machine):

File trac/search/web_ui.py

View file
 import pkg_resources
 import re
-from genshi.builder import tag, Element
+from genshi.builder import tag
 from trac.config import IntOption, ListOption
 from trac.core import *
 from trac.perm import IPermissionRequestor
 from trac.search.api import ISearchSource
 from trac.util.datefmt import format_datetime, user_time
+from trac.util.html import find_element
 from trac.util.presentation import Paginator
 from trac.util.translation import _
 from trac.web import IRequestHandler
             name = kwd
             description = _('Browse repository path %(path)s', path=kwd)
-            link = extract_link(self.env, web_context(req, 'search'), kwd)
-            if isinstance(link, Element):
+            context = web_context(req, 'search')
+            link = find_element(extract_link(self.env, context, kwd), 'href')
+            if link is not None:
                 quickjump_href = link.attrib.get('href')
                 name = link.children
                 description = link.attrib.get('title', '')

File trac/util/html.py

View file
 from genshi.builder import Element, ElementFactory, Fragment
 from genshi.filters.html import HTMLSanitizer
-__all__ = ['escape', 'unescape', 'html', 'plaintext', 'TracHTMLSanitizer',
-           'Deuglifier', 'FormTokenInjector']
+__all__ = ['escape', 'unescape', 'html', 'plaintext', 'find_element',
+           'TracHTMLSanitizer', 'Deuglifier', 'FormTokenInjector']
 class TracHTMLSanitizer(HTMLSanitizer):
     return text
+def find_element(frag, attr=None, cls=None):
+    """Return the first element in the fragment having the given attribute or
+    class, using a preorder depth-first search.
+    """
+    if isinstance(frag, Element):
+        if attr is not None and attr in frag.attrib:
+            return frag
+        if cls is not None and cls in frag.attrib.get('class', '').split():
+            return fragment
+    if isinstance(frag, Fragment):
+        for child in frag.children:
+            elt = find_element(child, attr, cls)
+            if elt is not None:
+                return elt
 def expand_markup(stream, ctxt=None):
     """A Genshi stream filter for expanding `genshi.Markup` events.

File trac/wiki/intertrac.py

View file
 from trac.config import ConfigSection
 from trac.core import *
 from trac.perm import PermissionError
+from trac.util.html import find_element
 from trac.util.translation import _, N_
 from trac.web.api import IRequestHandler
 from trac.wiki.api import IWikiMacroProvider
                 link = '%s:"%s"' % (resolver, target)
         from trac.web.chrome import web_context
         link_frag = extract_link(self.env, web_context(req), link)
-        def get_first_href(item):
-            """Depth-first search for the first `href` attribute."""
-            if isinstance(item, Element):
-                href = item.attrib.get('href')
-                if href is not None:
-                    return href
-            if isinstance(item, Fragment):
-                for each in item.children:
-                    href = get_first_href(each)
-                    if href is not None:
-                        return href
         if isinstance(link_frag, (Element, Fragment)):
-            href = get_first_href(link_frag)
-            if href is None: # most probably no permissions to view
+            elt = find_element(link_frag, 'href')
+            if elt is None: # most probably no permissions to view
                 raise PermissionError(_("Can't view %(link)s:", link=link))
+            href = elt.attrib.get('href')
             href = req.href(link.rstrip(':'))

File trac/wiki/macros.py

View file
 import re
 from StringIO import StringIO
-from genshi.builder import Element, tag
+from genshi.builder import tag
 from genshi.core import Markup
 from trac.core import *
 from trac.resource import Resource, ResourceNotFound, get_resource_name, \
                           get_resource_summary, get_resource_url
 from trac.util.datefmt import format_date, from_utimestamp, user_time
-from trac.util.html import escape
+from trac.util.html import escape, find_element
 from trac.util.presentation import separated
 from trac.util.text import unquote, to_unicode
 from trac.util.translation import _, dgettext, cleandoc_
             elif arg.startswith('link='):
                 val = arg.split('=', 1)[1]
                 elt = extract_link(self.env, formatter.context, val.strip())
+                elt = find_element(elt, 'href')
                 link = None
-                if isinstance(elt, Element):
+                if elt is not None:
                     link = elt.attrib.get('href')
             elif arg in ('left', 'right'):
                 style['float'] = arg