Exemple #1
0
def html_escape(message):
    """Performs translation and sanitizes any HTML present in the message.

    DO NOT USE THIS DIRECTLY UNLESS YOU ARE SURE YOU NEED TO.

    There is rarely a good reason to use this directly instead of going via
    structured().

    A plain string message will be sanitized ("&", "<" and ">" are
    converted to HTML-safe sequences).  Passing a message that
    provides the `IStructuredString` interface will return a unicode
    string that has been properly escaped.  Passing an instance of a
    Zope internationalized message will cause the message to be
    translated, then santizied.

    :param message: This may be a string, `zope.i18n.Message`,
        `zope.i18n.MessageID`, or an instance of `IStructuredString`.
    """
    if IStructuredString.providedBy(message):
        return message.escapedtext
    else:
        # It is possible that the message is wrapped in an
        # internationalized object, so we need to translate it
        # first. See bug #54987.
        raw = unicode(translate_if_i18n(message))
        for needle, replacement in HTML_REPLACEMENTS:
            raw = raw.replace(needle, replacement)
        return raw
def html_escape(message):
    """Performs translation and sanitizes any HTML present in the message.

    DO NOT USE THIS DIRECTLY UNLESS YOU ARE SURE YOU NEED TO.

    There is rarely a good reason to use this directly instead of going via
    structured().

    A plain string message will be sanitized ("&", "<" and ">" are
    converted to HTML-safe sequences).  Passing a message that
    provides the `IStructuredString` interface will return a unicode
    string that has been properly escaped.  Passing an instance of a
    Zope internationalized message will cause the message to be
    translated, then santizied.

    :param message: This may be a string, `zope.i18n.Message`,
        `zope.i18n.MessageID`, or an instance of `IStructuredString`.
    """
    if IStructuredString.providedBy(message):
        return message.escapedtext
    else:
        # It is possible that the message is wrapped in an
        # internationalized object, so we need to translate it
        # first. See bug #54987.
        raw = unicode(translate_if_i18n(message))
        for needle, replacement in HTML_REPLACEMENTS:
            raw = raw.replace(needle, replacement)
        return raw
    def _setInfoMessage(self, info_message):
        """Property setter for `info_message`.

        Enforces `info_message` values that are either None or
        implement IStructuredString.
        """
        if info_message != self._info_message:
            if (info_message is None or
                IStructuredString.providedBy(info_message)):
                # The supplied value is of a compatible type,
                # assign it to property backing variable.
                self._info_message = info_message
            else:
                raise ValueError(
                    '%s is not a valid value for info_message, only '
                    'None and IStructuredString are allowed.' %
                    type(info_message))