Changeset - 82198c193c11
[Not reviewed]
default
0 1 0
Mads Kiilerich - 10 years ago 2015-06-19 18:00:42
madski@unity3d.com
helpers: improve html_escape

Repeated string replace is faster than iterating over each character in Python
and doing a dict lookup.
1 file changed with 13 insertions and 15 deletions:
0 comments (0 inline, 0 general)
kallithea/lib/helpers.py
Show inline comments
 
@@ -86,24 +86,23 @@ def canonical_hostname():
 
        parts = CONFIG.get('canonical_url', '').split('://', 1)
 
        return parts[1].split('/', 1)[0]
 
    except IndexError:
 
        parts = url('home', qualified=True).split('://', 1)
 
        return parts[1].split('/', 1)[0]
 

	
 
def html_escape(text, html_escape_table=None):
 
    """Produce entities within text."""
 
    if not html_escape_table:
 
        html_escape_table = {
 
            "&": "&",
 
            '"': """,
 
            "'": "'",
 
            ">": ">",
 
            "<": "&lt;",
 
        }
 
    return "".join(html_escape_table.get(c, c) for c in text)
 

	
 
def html_escape(text):
 
    """Return string with all html escaped.
 
    This is also safe for javascript in html but not necessarily correct.
 
    """
 
    return (text
 
        .replace('&', '&amp;')
 
        .replace(">", "&gt;")
 
        .replace("<", "&lt;")
 
        .replace('"', "&quot;")
 
        .replace("'", "&apos;")
 
        )
 

	
 
def shorter(text, size=20):
 
    postfix = '...'
 
    if len(text) > size:
 
        return text[:size - len(postfix)] + postfix
 
    return text
 
@@ -1317,17 +1316,16 @@ def urlify_commit(text_, repository, lin
 
    if link_ is given, in other case it's a plain text
 

	
 
    :param text_:
 
    :param repository:
 
    :param link_: changeset link
 
    """
 
    def escaper(string):
 
        return string.replace('<', '&lt;').replace('>', '&gt;')
 
    newtext = html_escape(text_)
 

	
 
    # urlify changesets - extract revisions and make link out of them
 
    newtext = urlify_changesets(escaper(text_), repository)
 
    newtext = urlify_changesets(newtext, repository)
 

	
 
    # extract http/https links and make them real urls
 
    newtext = urlify_text(newtext, safe=False)
 

	
 
    newtext = urlify_issues(newtext, repository, link_)
 

	
0 comments (0 inline, 0 general)