Wiki

The CobraCore class provides methods to encode and decode URLs and HTML. For example:

s = 'x < y'
assert CobraCore.htmlEncode(s) == 'x &lt; y'

Additionally, there is a class called Html for holding text that already represents Html. The .htmlEncode method is cognizant of it:

text = Html('<p>Hello.</p>')
assert CobraCore.htmlEncode(text) == '<p>Hello.</p>'

Here is a summary of what is available:

namespace Cobra.Core

class CobraCore

    shared

        def htmlEncode(obj as Object?) as String
            """
            Return the HTML encoded version of the given object.
            Returns the contents of the object if it is an instance of Html.
            This is useful to display a plain ASCII text string on a web page.
            """
            if obj is nil, return ''
            if obj inherits Html, return obj.contents
            return .htmlEncode(obj.toString)

        def htmlEncode(s as String?) as String
            """
            Return the HTML encoded version of the given string.
            This is useful to display a plain ASCII text string on a web page.
            """
            test
                assert .htmlEncode('foo') == 'foo'
                assert .htmlEncode('3 < 5') == '3 &lt; 5'

        def htmlDecode(s as String?) as String
            """
            Return the HTML decoded version of the given string.
            """
            test
                assert .htmlDecode('foo') == 'foo'
                assert .htmlDecode('3 &lt; 5') == '3 < 5'

        def urlEncode(s as String?) as String
            """
            Return a version of the string encoded for and safe to use in URLs.
            """

        def urlDecode(s as String?) as String


class Html
    """
    Represents HTML that doesn't need to be encoded.
    Html.toString returns the contents as-is.
    """
    cue init(contents as String)
        base.init
        _contents = contents
    get contents from var as String
    def toString as String is override
        return _contents

See also: CobraCore, WebDevelopment, LibraryTopics