Exemplo n.º 1
0
    def _ready(self):

        if self.value:
            self["href"] = "mailto:" + self.value
            self.append(self.value)

        Element._ready(self)
Exemplo n.º 2
0
    def _add_meta(self):

        # Content type and charset should always go first
        self.content_type_metatag = Element("meta")
        self.content_type_metatag["http-equiv"] = "Content-Type"
        self.content_type_metatag["content"] = "%s; charset=%s" % (
            self.metadata.content_type or "text/html", self.metadata.charset
            or "utf-8")
        self.meta_container.append(self.content_type_metatag)

        # Document-wide default base URL for relative URLs
        if self.metadata.base_href:
            base = Element("base")
            base["href"] = self.metadata.base_href
            self.meta_container.append(base)

        # Other meta tags
        for key, value in self.metadata.meta.items():
            meta = Element("meta")

            if key.lower() in HTTP_EQUIV_KEYS:
                attribute = "http-equiv"
            else:
                attribute = "name"

            meta[attribute] = key
            meta["content"] = value
            self.meta_container.append(meta)
Exemplo n.º 3
0
    def create_group_row(self, group):

        row = Element("tr")
        row.add_class("group")

        cell = Element(
            "td",
            colspan=len(self.head_row.children),
            children=[
                Element("span",
                        class_name="grouping_value",
                        children=[
                            self.grouping.translate_grouping_value(group)
                            or self.translate_value(
                                self.data, self.grouping.member, group)
                        ]),
                Element("span",
                        class_name="grouping_member",
                        children=[self._grouping_member_translation]),
                Element("a",
                        href="?" +
                        get_request_query(grouping="", page=0).escape(),
                        class_name="remove_grouping",
                        children=[self._remove_grouping_translation])
            ])
        row.append(cell)

        return row
Exemplo n.º 4
0
    def get_undefined(self):

        from cocktail.html.element import Element
        e = Element()

        # Getting an undefined meta attribute yields None
        self.assertTrue(e.get_meta("desc") is None)
Exemplo n.º 5
0
    def test_resource_set(self):

        from cocktail.html.element import Element
        from cocktail.html.resources import Resource, Script

        e = Element()
        r1 = Resource("foo.js")

        # Adding the same resource object twice only adds the resource once
        e.add_resource(r1)
        e.add_resource(r1)
        self.assertEqual(e.resources, [r1])

        # Adding a different resource object with the same resource URI adds a
        # single resource
        e.add_resource(Resource("foo.js"))
        self.assertEqual(e.resources, [r1])

        # A different resource object with the same URI counts as a different
        # resource
        r2 = Script("foo.js")
        e.add_resource(r2)
        self.assertEqual(e.resources, [r1, r2])

        # Adding the same URI using a string is also ignored
        e.add_resource("foo.js")
        self.assertEqual(e.resources, [r1, r2])
Exemplo n.º 6
0
    def get_undefined(self):

        from cocktail.html.element import Element
        e = Element()

        # Getting an undefined style declaration yields None
        self.assertTrue(e.get_style("font-weight") is None)
Exemplo n.º 7
0
    def test_set_none(self):

        from cocktail.html.element import Element
        e = Element()
        e.set_client_param("foo", None)
        self.assertTrue(e.get_client_param("foo") is None)
        self.assertEqual(e.client_params, {"foo": None})
Exemplo n.º 8
0
    def test_single_elements_with_children(self):

        from cocktail.html.element import Element
        from cocktail.html.renderers import HTML4Renderer, RenderingError

        e = Element("img")
        e.append(Element())
        self.assertRaises(RenderingError, e.render, HTML4Renderer())
Exemplo n.º 9
0
    def test_empty_compound_elements(self):

        from cocktail.html.element import Element

        for tag in ("div", "script", "table", "p", "b"):
            e = Element()
            e.tag = tag
            self.assertEqual(self.get_html(e), "<%s></%s>" % (tag, tag))
Exemplo n.º 10
0
    def test_empty_single_elements(self):

        from cocktail.html.element import Element

        for tag in ("img", "link", "hr", "br"):
            e = Element()
            e.tag = tag
            self.assertEqual(self.get_html(e), "<%s>" % tag)
Exemplo n.º 11
0
 def __init__(self, *args, **kwargs):
     CollectionDisplay.__init__(self)
     self.__column_display = {}
     self.__column_labels = {}
     self.__split_rows = {}
     self.__split_row_values = {}
     self.__split_row_iterators = {}
     Element.__init__(self, *args, **kwargs)
Exemplo n.º 12
0
 def create_entry(self, item):
     entry = Element()
     entry.add_class("entry")
     entry.control = self.create_control(item)
     entry.append(entry.control)
     entry.remove_button = self.create_remove_button(item)
     entry.append(entry.remove_button)
     return entry
Exemplo n.º 13
0
    def _ready(self):
        Element._ready(self)

        self._font_sizes = get_tag_cloud_font_sizes(self.tags,
                                                    self.max_font_increment)

        for tag in self.sorted_tags(self.tags):
            self.append(self.create_tag_entry(tag, self.tags[tag]))
            self.append(" ")
Exemplo n.º 14
0
    def test_empty_single_elements_with_attributes(self):

        from cocktail.html.element import Element

        for tag in ("img", "link", "hr", "br"):
            e = Element()
            e.tag = tag
            e["title"] = "hello world"
            self.assertEqual(self.get_html(e),
                             '<%s title="hello world">' % tag)

        for tag in ("img", "link", "hr", "br"):
            e = Element()
            e.tag = tag
            e["title"] = "hello world"
            e["id"] = "foo"
            html = self.get_html(e)
            self.assertTrue(
                html == '<%s title="hello world" id="foo">' % tag
                or html == '<%s id="foo" title="hello world">' % tag)

        for tag in ("img", "link", "hr", "br"):
            e = Element()
            e.tag = tag
            e["selected"] = True
            e["checked"] = False
            e["id"] = "foo"
            html = self.get_html(e)
            self.assertTrue(html == '<%s selected id="foo">' % tag
                            or html == '<%s id="foo" selected>' % tag)
Exemplo n.º 15
0
    def test_empty_compound_elements_with_attributes(self):

        from cocktail.html.element import Element

        for tag in ("div", "script", "table", "p", "b"):
            e = Element()
            e.tag = tag
            e["title"] = "hello world"
            self.assertEqual(self.get_html(e),
                             '<%s title="hello world"></%s>' % (tag, tag))

        for tag in ("div", "script", "table", "p", "b"):
            e = Element()
            e.tag = tag
            e["selected"] = True
            e["checked"] = False
            e["id"] = "foo"
            html = self.get_html(e)
            self.assertTrue(
                html == '<%s selected="selected" id="foo"></%s>' % (tag, tag)
                or html == '<%s id="foo" selected="selected"></%s>' %
                (tag, tag))

        for tag in ("div", "script", "table", "p", "b"):
            e = Element()
            e.tag = tag
            e["title"] = "hello world"
            e["id"] = "foo"
            html = self.get_html(e)
            self.assertTrue(
                html == '<%s title="hello world" id="foo"></%s>' % (tag, tag)
                or html == '<%s id="foo" title="hello world"></%s>' %
                (tag, tag))
Exemplo n.º 16
0
    def _ready(self):
        Element._ready(self)

        if self.grouped:
            for group, members in self.displayed_members_by_group:
                tbody = self.create_group(group, members)
                if group:
                    setattr(self, group + "_group", tbody)
                self.append(tbody)
        else:
            self._create_rows(self.displayed_members, self)
Exemplo n.º 17
0
    def __test_add_uri(self, uri, expected_type):

        from cocktail.html.element import Element

        e = Element()
        e.add_resource(uri)
        self.assertEqual(len(e.resources), 1)

        resource = e.resources[0]
        self.assertEqual(resource.uri, uri)
        self.assertTrue(isinstance(resource, expected_type))
Exemplo n.º 18
0
    def _render(self, rendering):

        doctype = self.metadata.doctype
        if doctype is None:
            doctype = rendering.renderer.doctype

        if doctype:
            rendering.write(doctype.strip())
            rendering.write("\n")

        Element._render(self, rendering)
Exemplo n.º 19
0
    def test_insert_content(self):

        from cocktail.html.element import Element, Content

        parent = Element()
        parent.insert(0, "hello world")

        self.assertEqual(len(parent.children), 1)
        child = parent.children[0]
        self.assertTrue(isinstance(child, Content))
        self.assertEqual(child.value, "hello world")
        self.assertTrue(child.parent is parent)
Exemplo n.º 20
0
    def test_visible_rendered(self):

        from cocktail.html.element import Element
        e = Element()

        # Visible elements should be rendered
        e.visible = True
        self.assertTrue(e.rendered)

        # Invisible elements shouldn't be rendered
        e.visible = False
        self.assertFalse(e.rendered)
Exemplo n.º 21
0
    def embed(self, document, source):
        from cocktail.html.element import Element
        embed = Element("script")
        embed["type"] = self.mime_type
        embed.append("\n//<![CDATA[\n" + source + "\n//]]>\n")

        if self.ie_condition:
            from cocktail.html.ieconditionalcomment import IEConditionalComment
            embed = IEConditionalComment(self.ie_condition, children=[embed])

        document.scripts_container.append(embed)
        return embed
Exemplo n.º 22
0
    def _fill_head(self):

        # Cache sorted columns
        if self.order:

            self._sorted_columns = sorted_columns = {}

            for criteria in self.order:
                sign = criteria.__class__
                expr = criteria.operands[0]

                if isinstance(expr, TranslationExpression):
                    member = expr.operands[0].name
                    language = expr.operands[1].value
                else:
                    member = expr.name
                    language = None

                sorted_columns[(member, language)] = sign

        # Selection column
        if (self.selection_mode != NO_SELECTION
                and self.use_separate_selection_column):
            selection_header = Element("th")
            selection_header.add_class("selection")
            self.head_row.append(selection_header)

        # Column groups
        if not self.grouped or not self.column_groups_displayed:
            self.column_groups_row.visible = False

        # Regular columns
        if self.grouped:
            self.columns_by_group = list(self.displayed_members_by_group)
        else:
            self.columns_by_group = [(None, list(self.displayed_members))]

        for group, columns in self.columns_by_group:

            if self.grouped and self.column_groups_displayed:
                self.column_groups_row.append(
                    self.create_column_group_header(group, columns))

            for column in columns:
                if column.translated:
                    for language in self.translations or (get_language(), ):
                        header = self.create_header(column, language)
                        self.head_row.append(header)
                else:
                    header = self.create_header(column)
                    self.head_row.append(header)
Exemplo n.º 23
0
    def test_release(self):

        from cocktail.html.element import Element

        # Releasing a top-level element is a no-op
        parent = Element()
        parent.release()

        # Releasing a child element removes it from its parent
        child = Element()
        parent.append(child)
        child.release()
        self.assertTrue(child.parent is None)
        self.assertEqual(parent.children, [])
Exemplo n.º 24
0
    def _ready(self):

        if self.javascript_api:
            self.add_class("scriptable_video_player")
            self.add_resource("cocktail://scripts/youtubeplayer.js")
            self.add_resource(("https" if self.https else "http") +
                              "://youtube.com/player_api",
                              mime_type="text/javascript")

        self["src"] = self.get_video_url()
        self["width"] = self.width
        self["height"] = self.height
        self["allowfullscreen"] = self.allow_fullscreen

        Element._ready(self)
Exemplo n.º 25
0
    def test_invisible_element_not_rendered(self):

        from cocktail.html.element import Element

        e = Element()
        e.visible = False
        self.assertEqual(self.get_html(e), "")

        e.visible = True
        e.collapsible = True
        self.assertEqual(self.get_html(e), "")

        child = Element()
        child.visible = False
        e.append(child)
        self.assertEqual(self.get_html(e), "")
Exemplo n.º 26
0
    def _ready(self):

        if self.javascript_api:
            self.add_class("scriptable_video_player")
            self.add_resource("%s://player.vimeo.com/api/player.js" %
                              ("https" if self.https else "http"))
            self.add_resource("cocktail://scripts/vimeoplayer.js")

        self["src"] = self.get_video_url()
        self["width"] = self.width
        self["height"] = self.height
        self["allowfullscreen"] = self.allow_fullscreen
        self["mozallowfullscreen"] = self.allow_fullscreen
        self["webkitallowfullscreen"] = self.allow_fullscreen

        Element._ready(self)
Exemplo n.º 27
0
    def get_undefined(self):

        from cocktail.html.element import Element
        e = Element()

        # Getting an undefined attribute yields None
        self.assertTrue(e["id"] is None)
Exemplo n.º 28
0
    def test_elements_can_invalidate_cached_content(self):

        from cocktail.html.element import Element

        e = Element()
        e.cached = True
        e.cached_key = "foo"
        e.cache_tags.add("foo")
        e.render(cache=self.cache)

        e.append("foo")
        html = e.render(cache=self.cache)
        assert "foo" not in html

        self.cache.clear(scope=["foo"])
        html = e.render(cache=self.cache)
        assert "foo" in html
Exemplo n.º 29
0
    def test_constructor(self):

        from cocktail.html.element import Element

        # Attributes can be set using the element's constructor
        e = Element(id="foo", title="hello world")
        self.assertEqual(e["id"], "foo")
        self.assertEqual(e["title"], "hello world")
Exemplo n.º 30
0
    def test_delete_undefined(self):

        from cocktail.html.element import Element
        e = Element()

        # Deleting an undefined attribute is a no-op
        del e["id"]
        self.assertFalse("id" in e.attributes)