Anonymous avatar Anonymous committed 8396b9a

0.13dev: Merged from 0.12-stable.

Comments (0)

Files changed (5)

trac/mimeview/rst.py

 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()
         else:
             uri = context.href.wiki(target)
             missing = not WikiSystem(env).has_page(target)
             if missing:
                 reference['classes'].append('missing')
             return reference
-        return None
 
     def trac_directive(name, arguments, options, content, lineno,
                        content_offset, block_text, state, state_machine):

trac/search/web_ui.py

 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)
         else:
-            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', '')

trac/util/html.py

 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.
 

trac/wiki/intertrac.py

 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')
         else:
             href = req.href(link.rstrip(':'))
         req.redirect(href)

trac/wiki/macros.py

 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
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.