Example #1
0
def js_escaped_string(string_for_js):
    """
    Mako filter that escapes text for use in a JavaScript string.

    If None is provided, returns an empty string.

    Usage:
        Used as follows in a Mako template inside a <SCRIPT> tag::

            var my_string_for_js = "${my_string_for_js | n, js_escaped_string}"

        The surrounding quotes for the string must be included.

        Use the "n" Mako filter above.  It is possible that the default filter
        may include html escaping in the future, and this ensures proper
        escaping.

        Mako's default filter decode.utf8 is applied here since this default
        filter is skipped in the Mako template with "n".

    Arguments:
        string_for_js (string): Text to be properly escaped for use in a
            JavaScript string.

    Returns:
        (string) Text properly escaped for use in a JavaScript string as
        unicode.  Returns empty string if argument is None.

    """
    if string_for_js is None:
        string_for_js = ""
    string_for_js = decode.utf8(string_for_js)
    string_for_js = escapejs(string_for_js)
    return string_for_js
Example #2
0
def js_escaped_string(string_for_js):
    """
    Mako filter that escapes text for use in a JavaScript string.

    If None is provided, returns an empty string.

    Usage:
        Used as follows in a Mako template inside a <SCRIPT> tag::

            var my_string_for_js = "${my_string_for_js | n, js_escaped_string}"

        The surrounding quotes for the string must be included.

        Use the "n" Mako filter above.  It is possible that the default filter
        may include html escaping in the future, and this ensures proper
        escaping.

        Mako's default filter decode.utf8 is applied here since this default
        filter is skipped in the Mako template with "n".

    Arguments:
        string_for_js (string): Text to be properly escaped for use in a
            JavaScript string.

    Returns:
        (string) Text properly escaped for use in a JavaScript string as
        unicode.  Returns empty string if argument is None.

    """
    if string_for_js is None:
        string_for_js = ""
    string_for_js = decode.utf8(string_for_js)
    string_for_js = escapejs(string_for_js)
    return string_for_js
Example #3
0
def escape_js_string(js_string):
    """
    Escape a javascript string that is safe to be embedded in HTML.

    Usage:
        Can be used inside a Mako template inside a <SCRIPT> as follows:
            var my_js_string = "${escape_js_string(my_js_string) | n}"

        Must include the surrounding quotes for the string.

        Use the "n" Mako filter above.  It is possible that the
            default filter may include html escaping in the future, and
            we must make sure to get the proper escaping.

        Mako's default filter decode.utf8 is applied here since this default
            filter is skipped in the Mako template with "n".

    Arguments:
        js_string (string): The javascript string to be escaped

    Returns:
        (string) Escaped javascript as unicode

    """
    js_string = decode.utf8(js_string)
    js_string = escapejs(js_string)
    return js_string
Example #4
0
def strip_all_tags_but_br(string_to_strip):
    """
    Strips all tags from a string except <br/> and marks as HTML.

    Usage:
        <%page expression_filter="h"/>
        <%!
        from openedx.core.djangolib.markup import strip_all_tags_but_br
        %>
        ${accomplishment_course_title | n, strip_all_tags_but_br}
    """

    if string_to_strip is None:
        string_to_strip = ""

    string_to_strip = decode.utf8(string_to_strip)
    string_to_strip = bleach.clean(string_to_strip, tags=['br'], strip=True)

    return HTML(string_to_strip)
Example #5
0
def strip_all_tags_but_br(string_to_strip):
    """
    Strips all tags from a string except <br/> and marks as HTML.

    Usage:
        <%page expression_filter="h"/>
        <%!
        from openedx.core.djangolib.markup import strip_all_tags_but_br
        %>
        ${accomplishment_course_title | n, strip_all_tags_but_br}
    """

    if string_to_strip is None:
        string_to_strip = ""

    string_to_strip = decode.utf8(string_to_strip)
    string_to_strip = bleach.clean(string_to_strip, tags=['br'], strip=True)

    return HTML(string_to_strip)