Ejemplo n.º 1
0
    def test_should_follow_default_behaviour(self):
        # we always have default `page` rules registered.
        rules = {
            "page": lambda attrs: '<a href="/article/{}">'.format(attrs["id"])
        }
        rewriter = LinkRewriter(rules)

        page_type_link = rewriter('<a linktype="page" id="3">')
        self.assertEqual(page_type_link, '<a href="/article/3">')

        # but it should also be able to handle other supported
        # link types (email, external, anchor) even if no rules is provided
        external_type_link = rewriter('<a href="https://wagtail.org/">')
        self.assertEqual(external_type_link, '<a href="https://wagtail.org/">')
        email_type_link = rewriter('<a href="mailto:[email protected]">')
        self.assertEqual(email_type_link, '<a href="mailto:[email protected]">')
        anchor_type_link = rewriter('<a href="#test">')
        self.assertEqual(anchor_type_link, '<a href="#test">')

        # As well as link which don't have any linktypes
        link_without_linktype = rewriter('<a data-link="https://wagtail.org">')
        self.assertEqual(link_without_linktype,
                         '<a data-link="https://wagtail.org">')

        # But should not handle if a custom linktype is mentioned but no
        # associate rules are registered.
        link_with_custom_linktype = rewriter(
            '<a linktype="custom" href="https://wagtail.org">')
        self.assertNotEqual(link_with_custom_linktype,
                            '<a href="https://wagtail.org">')
        self.assertEqual(link_with_custom_linktype, "<a>")
Ejemplo n.º 2
0
def expand_db_html(html):
    """
    Expand database-representation HTML into proper HTML usable on front-end templates
    """
    global FRONTEND_REWRITER

    if FRONTEND_REWRITER is None:
        embed_rules = features.get_embed_types()
        link_rules = features.get_link_types()
        FRONTEND_REWRITER = MultiRuleRewriter(
            [
                LinkRewriter(
                    {
                        linktype: handler.expand_db_attributes
                        for linktype, handler in link_rules.items()
                    }
                ),
                EmbedRewriter(
                    {
                        embedtype: handler.expand_db_attributes
                        for embedtype, handler in embed_rules.items()
                    }
                ),
            ]
        )

    return FRONTEND_REWRITER(html)
Ejemplo n.º 3
0
    def html_rewriter(self):
        embed_rules = {}
        link_rules = {}
        for rule in self.converter_rules:
            if isinstance(rule, EmbedTypeRule):
                embed_rules[rule.embed_type] = rule.handler.expand_db_attributes
            elif isinstance(rule, LinkTypeRule):
                link_rules[rule.link_type] = rule.handler.expand_db_attributes

        return MultiRuleRewriter([LinkRewriter(link_rules), EmbedRewriter(embed_rules)])
Ejemplo n.º 4
0
    def test_supported_type_should_follow_given_rules(self):
        # we always have `page` rules by default
        rules = {
            "page": lambda attrs: '<a href="/article/{}">'.format(attrs["id"]),
            "external": lambda attrs: '<a rel="nofollow" href="{}">'.format(
                attrs["href"]
            ),
            "email": lambda attrs: '<a data-email="true" href="{}">'.format(
                attrs["href"]
            ),
            "anchor": lambda attrs: '<a data-anchor="true" href="{}">'.format(
                attrs["href"]
            ),
            "custom": lambda attrs: '<a data-phone="true" href="{}">'.format(
                attrs["href"]
            ),
        }
        rewriter = LinkRewriter(rules)

        page_type_link = rewriter('<a linktype="page" id="3">')
        self.assertEqual(page_type_link, '<a href="/article/3">')

        # It should call appropriate rule supported linktypes (external or email)
        # based on the href value
        external_type_link = rewriter('<a href="https://wagtail.org/">')
        self.assertEqual(
            external_type_link, '<a rel="nofollow" href="https://wagtail.org/">'
        )
        external_type_link_http = rewriter('<a href="http://wagtail.org/">')
        self.assertEqual(
            external_type_link_http, '<a rel="nofollow" href="http://wagtail.org/">'
        )
        email_type_link = rewriter('<a href="mailto:[email protected]">')
        self.assertEqual(
            email_type_link, '<a data-email="true" href="mailto:[email protected]">'
        )
        anchor_type_link = rewriter('<a href="#test">')
        self.assertEqual(anchor_type_link, '<a data-anchor="true" href="#test">')

        # But not the unsupported ones.
        link_with_no_linktype = rewriter('<a href="tel:+4917640206387">')
        self.assertEqual(link_with_no_linktype, '<a href="tel:+4917640206387">')

        # Also call the rule if a custom linktype is mentioned.
        link_with_custom_linktype = rewriter(
            '<a linktype="custom" href="tel:+4917640206387">'
        )
        self.assertEqual(
            link_with_custom_linktype, '<a data-phone="true" href="tel:+4917640206387">'
        )