Beispiel #1
0
def whitelister_element_rules():
    return {
        'u':
        allow_without_attributes,
        'table':
        attribute_rule({
            'cellspacing': True,
            'cellpadding': True,
            'border': True
        }),
        'td':
        attribute_rule({
            'valign': True,
            'style': True
        }),
        'tr':
        allow_without_attributes,
        'th':
        allow_without_attributes,
        'tbody':
        allow_without_attributes,
        'tfoot':
        allow_without_attributes,
        'thead':
        allow_without_attributes,
        'p':
        attribute_rule({'align': True}),
    }
Beispiel #2
0
def whitelister_element_rules():
    return {
        'h2': attribute_rule({'style': True}),
        'h3': attribute_rule({'style': True}),
        'h4': attribute_rule({'style': True}),
        'h5': attribute_rule({'style': True}),
        'p': attribute_rule({'style': True}),
    }
Beispiel #3
0
def whitelister_element_rules():
    # Whitelist custom elements to the hallo.js editor
    return {
        'a': attribute_rule({
            'href': check_url,
            'target': True
        }),
        'blockquote': attribute_rule({'class': True})
    }
Beispiel #4
0
def allow_blockquotes():
    return {
        "a": attribute_rule({
            "href": check_url,
            "target": True,
            "class": True
        }),
        "blockquote": attribute_rule({"class": True}),
    }
Beispiel #5
0
def allow_blockquotes():
    return {
        'a': attribute_rule({
            'href': check_url,
            'target': True,
            'class': True
        }),
        'blockquote': attribute_rule({'class': True}),
    }
Beispiel #6
0
def whitelister_element_rules():
    return {
        'span': attribute_rule({'style': True}),
        'p': attribute_rule({'style': True}),
        'sup': allow_without_attributes,
        'sub': allow_without_attributes,
        'code': allow_without_attributes,
        'blockquote': allow_without_attributes,
        'abbr': attribute_rule({'title': True}),
    }
Beispiel #7
0
def whitelister_element_rules():
    return {
        'p': attribute_rule({'class': True}),
        'a': attribute_rule({'href': check_url, 'id': True, 'class': True}),
        'span': attribute_rule({'class': True}),
        'i': attribute_rule({'class': True}),
        'iframe': attribute_rule(
            {'id': True, 'class': True, 'src': True, 'style': True,
             'frameborder': True, 'allowfullscreen': True, 'width': True,
             'height': True}),
    }
Beispiel #8
0
def allow_iframes():
    return {
        'iframe':
        attribute_rule({
            'src': True,
            'width': True,
            'height': True,
            'frameborder': True,
            'marginheight': True,
            'marginwidth': True
        }),
        'tito-widget':
        attribute_rule({'event': True}),
        'tito-button':
        attribute_rule({'event': True}),
    }
Beispiel #9
0
def allow_iframes():
    return {
        "iframe":
        attribute_rule({
            "src": True,
            "width": True,
            "height": True,
            "frameborder": True,
            "marginheight": True,
            "marginwidth": True,
        }),
        "tito-widget":
        attribute_rule({"event": True}),
        "tito-button":
        attribute_rule({"event": True}),
    }
Beispiel #10
0
def whitelist_blockquote(features):
    features.register_converter_rule('editorhtml', 'blockquote', [
        WhitelistRule('style', handler=blacklist_tag()),
        WhitelistRule('style', handler=unwrap_tag()),
        WhitelistRule('style', handler=unwrap_tag()),
        WhitelistRule('blockquote', attribute_rule({'class': True})),
        WhitelistRule('p', attribute_rule({'class': True})),
        WhitelistRule('h2', attribute_rule({'class': True})),
        WhitelistRule('h3', attribute_rule({'class': True})),
        WhitelistRule('h4', attribute_rule({'class': True})),
        WhitelistRule('h5', attribute_rule({'class': True})),
        WhitelistRule(
            'iframe',
            attribute_rule({
                'style': True,
                'src': True,
                'width': True,
                'height': True
            })),
        WhitelistRule(
            'img',
            attribute_rule({
                'srcset': True,
                'class': True,
                'alt': True,
                'sizes': True,
                'width': True,
                'height': True,
                'src': True
            })),
        WhitelistRule(
            'audio',
            attribute_rule({
                'class': True,
                'src': True,
                'controls': True,
            })),
        WhitelistRule(
            'source',
            attribute_rule({
                'class': True,
                'src': True,
                'type': True,
            })),
    ])
    features.default_features.append('blockquote')
Beispiel #11
0
 def test_rule_false_for_attr(self):
     """
     Test that attribute_rule() drops attributes
     when the corresponding rule returns False
     """
     tag = self.soup.b
     fn = attribute_rule({"foo": False})
     fn(tag)
     self.assertEqual(str(tag), "<b>baz</b>")
Beispiel #12
0
 def test_rule_false_for_attr(self):
     """
     Test that attribute_rule() drops atrributes
     when the corresponding rule returns False
     """
     tag = self.soup.b
     fn = attribute_rule({'foo': False})
     fn(tag)
     self.assertEqual(str(tag), '<b>baz</b>')
Beispiel #13
0
 def test_no_rule_for_attr(self):
     """
     Test that attribute_rule() drops attributes for
     which no rule has been defined.
     """
     tag = self.soup.b
     fn = attribute_rule({"snowman": "barbecue"})
     fn(tag)
     self.assertEqual(str(tag), "<b>baz</b>")
Beispiel #14
0
 def test_rule_true_for_attr(self):
     """
     Test that attribute_rule() does not change attributes
     when the corresponding rule returns True
     """
     tag = self.soup.b
     fn = attribute_rule({"foo": True})
     fn(tag)
     self.assertEqual(str(tag), '<b foo="bar">baz</b>')
Beispiel #15
0
 def test_no_rule_for_attr(self):
     """
     Test that attribute_rule() drops attributes for
     which no rule has been defined.
     """
     tag = self.soup.b
     fn = attribute_rule({'snowman': 'barbecue'})
     fn(tag)
     self.assertEqual(str(tag), '<b>baz</b>')
Beispiel #16
0
 def test_rule_true_for_attr(self):
     """
     Test that attribute_rule() does not change atrributes
     when the corresponding rule returns True
     """
     tag = self.soup.b
     fn = attribute_rule({'foo': True})
     fn(tag)
     self.assertEqual(str(tag), '<b foo="bar">baz</b>')
Beispiel #17
0
def register_core_features(features):
    features.register_editor_plugin(
        'hallo', 'hr',
        HalloPlugin(
            name='hallohr',
            js=['wagtailadmin/js/hallo-plugins/hallo-hr.js'],
            order=45,
        ))
    features.register_converter_rule(
        'editorhtml', 'hr', [WhitelistRule('hr', allow_without_attributes)])

    features.register_editor_plugin(
        'hallo', 'link',
        HalloPlugin(
            name='hallowagtaillink',
            js=['wagtailadmin/js/hallo-plugins/hallo-wagtaillink.js'],
        ))
    features.register_converter_rule('editorhtml', 'link', [
        WhitelistRule('a', attribute_rule({'href': check_url})),
        LinkTypeRule('page', PageLinkHandler),
    ])

    features.register_editor_plugin('hallo', 'bold',
                                    HalloFormatPlugin(format_name='bold'))
    features.register_converter_rule('editorhtml', 'bold', [
        WhitelistRule('b', allow_without_attributes),
        WhitelistRule('strong', allow_without_attributes),
    ])

    features.register_editor_plugin('hallo', 'italic',
                                    HalloFormatPlugin(format_name='italic'))
    features.register_converter_rule('editorhtml', 'italic', [
        WhitelistRule('i', allow_without_attributes),
        WhitelistRule('em', allow_without_attributes),
    ])

    for element in ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']:
        features.register_editor_plugin('hallo', element,
                                        HalloHeadingPlugin(element=element))
        features.register_converter_rule(
            'editorhtml', element,
            [WhitelistRule(element, allow_without_attributes)])

    features.register_editor_plugin('hallo', 'ol',
                                    HalloListPlugin(list_type='ordered'))
    features.register_converter_rule('editorhtml', 'ol', [
        WhitelistRule('ol', allow_without_attributes),
        WhitelistRule('li', allow_without_attributes),
    ])

    features.register_editor_plugin('hallo', 'ul',
                                    HalloListPlugin(list_type='unordered'))
    features.register_converter_rule('editorhtml', 'ul', [
        WhitelistRule('ul', allow_without_attributes),
        WhitelistRule('li', allow_without_attributes),
    ])
def blockquote_feature(features):
    # register a feature 'blockquote' which whitelists the <blockquote> element
    features.register_converter_rule('editorhtml', 'blockquote', [
        WhitelistRule('blockquote', allow_without_attributes),
    ])
    # register a feature 'a' which whitelists the <a> element
    features.register_converter_rule('editorhtml', 'a', [
        WhitelistRule(
            'a',
            attribute_rule({
                'href': check_url,
                'target': True,
                'data-toggle': True,
                'data-url': True,
                'data-target': True,
                'id': True
            })),
    ])
    # register a feature 'table' which whitelists the <table> element
    features.register_converter_rule('editorhtml', 'table', [
        WhitelistRule('table', attribute_rule({'style': True})),
    ])
    # register a feature 'table' which whitelists the <table> element
    features.register_converter_rule('editorhtml', 'tbody', [
        WhitelistRule('tbody', attribute_rule({'style': True})),
    ])
    # register a feature 'table' which whitelists the <table> element
    features.register_converter_rule('editorhtml', 'tr', [
        WhitelistRule('tr', attribute_rule({'style': True})),
    ])
    # register a feature 'table' which whitelists the <table> element
    features.register_converter_rule('editorhtml', 'td', [
        WhitelistRule('td', attribute_rule({'style': True})),
    ])
    # register a feature 'table' which whitelists the <table> element
    features.register_converter_rule('editorhtml', 'img', [
        WhitelistRule('img', attribute_rule({'alt': True})),
    ])
    # register a feature 'table' which whitelists the <table> element
    features.register_converter_rule('editorhtml', 'h1', [
        WhitelistRule('h1', attribute_rule({'id': True})),
    ])
    # add 'a' to the default feature set
    features.default_features.append('blockquote')
    # add 'a' to the default feature set
    features.default_features.append('a')
    # add 'table' to the default feature set
    features.default_features.append('table')
    # add 'tbody' to the default feature set
    features.default_features.append('tbody')
    # add 'tr' to the default feature set
    features.default_features.append('tr')
    # add 'td' to the default feature set
    features.default_features.append('td')
    # add 'img' to the default feature set
    features.default_features.append('img')
    # add 'h1' to the default feature set
    features.default_features.append('h1')
Beispiel #19
0
 def test_callable_returns_None(self):
     """
     Test that when the rule returns a callable,
     attribute_rule() replaces the attribute with
     the result of calling the callable on the attribute.
     """
     tag = self.soup.b
     fn = attribute_rule({'foo': lambda x: None})
     fn(tag)
     self.assertEqual(str(tag), '<b>baz</b>')
Beispiel #20
0
def whitelister_element_rules():
    return {
        'sup':
        attribute_rule({
            'data-target': True,
            'data-toggle': True,
            'data-reference': True,
            'class': True
        }),
    }
Beispiel #21
0
 def test_callable_called_on_attr(self):
     """
     Test that when the rule returns a callable,
     attribute_rule() replaces the attribute with
     the result of calling the callable on the attribute.
     """
     tag = self.soup.b
     fn = attribute_rule({'foo': len})
     fn(tag)
     self.assertEqual(str(tag), '<b foo="3">baz</b>')
Beispiel #22
0
 def test_callable_returns_None(self):
     """
     Test that when the rule returns a callable,
     attribute_rule() replaces the attribute with
     the result of calling the callable on the attribute.
     """
     tag = self.soup.b
     fn = attribute_rule({"foo": lambda x: None})
     fn(tag)
     self.assertEqual(str(tag), "<b>baz</b>")
Beispiel #23
0
 def test_callable_called_on_attr(self):
     """
     Test that when the rule returns a callable,
     attribute_rule() replaces the attribute with
     the result of calling the callable on the attribute.
     """
     tag = self.soup.b
     fn = attribute_rule({"foo": len})
     fn(tag)
     self.assertEqual(str(tag), '<b foo="3">baz</b>')
Beispiel #24
0
def whitelister_element_rules():
    return {
        's':
        allow_without_attributes,
        'u':
        allow_without_attributes,
        'span':
        attribute_rule({
            'style': True,
            "class": True
        }),
        'p':
        attribute_rule({
            'style': True,
            "class": True
        }),
        'div':
        attribute_rule({
            'style': True,
            "class": True
        }),
        'q':
        allow_without_attributes,
        'ins':
        allow_without_attributes,
        'pre':
        allow_without_attributes,
        'address':
        allow_without_attributes,
        'table':
        attribute_rule({
            'align': True,
            "border": True,
            "cellpadding": True,
            "style": True
        }),
        'caption':
        allow_without_attributes,
        'thead':
        allow_without_attributes,
        'tr':
        allow_without_attributes,
        'tbody':
        allow_without_attributes,
        'td':
        attribute_rule({
            'style': True,
            "class": True
        }),
        'hr':
        allow_without_attributes,
        'img':
        attribute_rule({
            'alt': True,
            "src": True,
            'style': True,
            'width': True,
            'height': True
        }),
    }
Beispiel #25
0
def whitelist_table(features):
    cell_attributes = attribute_rule({
        'rowspan': True,
        'colspan': True,
        'width': True,
        'height': True,
        'style': True
    })

    features.register_converter_rule('editorhtml', 'table', [
        WhitelistRule('table', attribute_rule({
            'style': True,
            'border': True
        })),
        WhitelistRule('thead', allow_without_attributes),
        WhitelistRule('tbody', allow_without_attributes),
        WhitelistRule('tfoot', allow_without_attributes),
        WhitelistRule('tr', allow_without_attributes),
        WhitelistRule('th', cell_attributes),
        WhitelistRule('td', cell_attributes),
    ])

    features.default_features.append('table')
Beispiel #26
0
    def register_span_feature(features):
        allow_html_class = attribute_rule({
            'class': True,
            'id': True,
        })

        # register a feature 'span'
        # which whitelists the <span> element
        features.register_converter_rule('editorhtml', 'span', [
            WhitelistRule('span', allow_html_class),
        ])

        # add 'span' to the default feature set
        features.default_features.append('span')
Beispiel #27
0
def whitelister_element_rules():
    allow_html_class = attribute_rule({
        'class': True,
        'itemprop': True,
        'itemscope': True,
        'itemtype': True,
    })

    allowed_tags = [
        'aside', 'h4', 'h3', 'p', 'span', 'table', 'tr', 'th', 'td', 'tbody',
        'thead', 'tfoot', 'col', 'colgroup'
    ]

    return {tag: allow_html_class for tag in allowed_tags}
Beispiel #28
0
class SimpleDbWhitelister(DbWhitelister):
    """
    DbWhitelister to allow/disallow stuff on the text editor
    """
    element_rules = {
        '[document]': allow_without_attributes,
        'a': attribute_rule({'href': check_url, 'id': True, 'linktype': True}),
        'p': allow_without_attributes,
        'b': allow_without_attributes,
        'i': allow_without_attributes,
        'u': allow_without_attributes,
        'ul': allow_without_attributes,
        'ol': allow_without_attributes,
        'li': allow_without_attributes,
    }
def whitelister_element_rules():
    """Doc string."""
    return {
        'div': attribute_rule({'class': True}),
        'p': attribute_rule({'class': True}),
        'a': attribute_rule({'target': True, 'href': check_url,
                             'id': True, 'class': True}),
        'span': attribute_rule({'class': True, 'id': True}),
        'i': attribute_rule({'class': True}),
        'img': attribute_rule({'class': True}),
        'iframe': attribute_rule({'id': True, 'class': True, 'src': True,
                                  'style': True, 'frameborder': True,
                                  'allowfullscreen': True, 'width': True,
                                  'height': True}),
    }
Beispiel #30
0
def register_core_features(features):
    features.register_converter_rule(
        "editorhtml",
        "link",
        [
            WhitelistRule("a", attribute_rule({"href": check_url})),
            LinkTypeRule("page", PageLinkHandler),
        ],
    )

    features.register_converter_rule(
        "editorhtml",
        "bold",
        [
            WhitelistRule("b", allow_without_attributes),
            WhitelistRule("strong", allow_without_attributes),
        ],
    )

    features.register_converter_rule(
        "editorhtml",
        "italic",
        [
            WhitelistRule("i", allow_without_attributes),
            WhitelistRule("em", allow_without_attributes),
        ],
    )

    headings_elements = ["h1", "h2", "h3", "h4", "h5", "h6"]
    for order, element in enumerate(headings_elements):
        features.register_converter_rule(
            "editorhtml", element,
            [WhitelistRule(element, allow_without_attributes)])

    features.register_converter_rule(
        "editorhtml",
        "ol",
        [
            WhitelistRule("ol", allow_without_attributes),
            WhitelistRule("li", allow_without_attributes),
        ],
    )

    features.register_converter_rule(
        "editorhtml",
        "ul",
        [
            WhitelistRule("ul", allow_without_attributes),
            WhitelistRule("li", allow_without_attributes),
        ],
    )

    # Draftail
    features.register_editor_plugin(
        "draftail", "hr",
        draftail_features.BooleanFeature("enableHorizontalRule"))
    features.register_converter_rule(
        "contentstate",
        "hr",
        {
            "from_database_format": {
                "hr": HorizontalRuleHandler(),
            },
            "to_database_format": {
                "entity_decorators": {
                    "HORIZONTAL_RULE": lambda props: DOM.create_element("hr")
                }
            },
        },
    )

    features.register_editor_plugin(
        "draftail",
        "h1",
        draftail_features.BlockFeature({
            "label": "H1",
            "type": "header-one",
            "description": gettext("Heading %(level)d") % {
                "level": 1
            },
        }),
    )
    features.register_converter_rule(
        "contentstate",
        "h1",
        {
            "from_database_format": {
                "h1": BlockElementHandler("header-one"),
            },
            "to_database_format": {
                "block_map": {
                    "header-one": "h1"
                }
            },
        },
    )
    features.register_editor_plugin(
        "draftail",
        "h2",
        draftail_features.BlockFeature({
            "label": "H2",
            "type": "header-two",
            "description": gettext("Heading %(level)d") % {
                "level": 2
            },
        }),
    )
    features.register_converter_rule(
        "contentstate",
        "h2",
        {
            "from_database_format": {
                "h2": BlockElementHandler("header-two"),
            },
            "to_database_format": {
                "block_map": {
                    "header-two": "h2"
                }
            },
        },
    )
    features.register_editor_plugin(
        "draftail",
        "h3",
        draftail_features.BlockFeature({
            "label": "H3",
            "type": "header-three",
            "description": gettext("Heading %(level)d") % {
                "level": 3
            },
        }),
    )
    features.register_converter_rule(
        "contentstate",
        "h3",
        {
            "from_database_format": {
                "h3": BlockElementHandler("header-three"),
            },
            "to_database_format": {
                "block_map": {
                    "header-three": "h3"
                }
            },
        },
    )
    features.register_editor_plugin(
        "draftail",
        "h4",
        draftail_features.BlockFeature({
            "label": "H4",
            "type": "header-four",
            "description": gettext("Heading %(level)d") % {
                "level": 4
            },
        }),
    )
    features.register_converter_rule(
        "contentstate",
        "h4",
        {
            "from_database_format": {
                "h4": BlockElementHandler("header-four"),
            },
            "to_database_format": {
                "block_map": {
                    "header-four": "h4"
                }
            },
        },
    )
    features.register_editor_plugin(
        "draftail",
        "h5",
        draftail_features.BlockFeature({
            "label": "H5",
            "type": "header-five",
            "description": gettext("Heading %(level)d") % {
                "level": 5
            },
        }),
    )
    features.register_converter_rule(
        "contentstate",
        "h5",
        {
            "from_database_format": {
                "h5": BlockElementHandler("header-five"),
            },
            "to_database_format": {
                "block_map": {
                    "header-five": "h5"
                }
            },
        },
    )
    features.register_editor_plugin(
        "draftail",
        "h6",
        draftail_features.BlockFeature({
            "label": "H6",
            "type": "header-six",
            "description": gettext("Heading %(level)d") % {
                "level": 6
            },
        }),
    )
    features.register_converter_rule(
        "contentstate",
        "h6",
        {
            "from_database_format": {
                "h6": BlockElementHandler("header-six"),
            },
            "to_database_format": {
                "block_map": {
                    "header-six": "h6"
                }
            },
        },
    )
    features.register_editor_plugin(
        "draftail",
        "ul",
        draftail_features.BlockFeature({
            "type": "unordered-list-item",
            "icon": "list-ul",
            "description": gettext("Bulleted list"),
        }),
    )
    features.register_converter_rule(
        "contentstate",
        "ul",
        {
            "from_database_format": {
                "ul": ListElementHandler("unordered-list-item"),
                "li": ListItemElementHandler(),
            },
            "to_database_format": {
                "block_map": {
                    "unordered-list-item": {
                        "element": "li",
                        "wrapper": "ul"
                    }
                }
            },
        },
    )
    features.register_editor_plugin(
        "draftail",
        "ol",
        draftail_features.BlockFeature({
            "type": "ordered-list-item",
            "icon": "list-ol",
            "description": gettext("Numbered list"),
        }),
    )
    features.register_converter_rule(
        "contentstate",
        "ol",
        {
            "from_database_format": {
                "ol": ListElementHandler("ordered-list-item"),
                "li": ListItemElementHandler(),
            },
            "to_database_format": {
                "block_map": {
                    "ordered-list-item": {
                        "element": "li",
                        "wrapper": "ol"
                    }
                }
            },
        },
    )
    features.register_editor_plugin(
        "draftail",
        "blockquote",
        draftail_features.BlockFeature({
            "type": "blockquote",
            "icon": "openquote",
            "description": gettext("Blockquote"),
        }),
    )
    features.register_converter_rule(
        "contentstate",
        "blockquote",
        {
            "from_database_format": {
                "blockquote": BlockElementHandler("blockquote"),
            },
            "to_database_format": {
                "block_map": {
                    "blockquote": "blockquote"
                }
            },
        },
    )

    features.register_editor_plugin(
        "draftail",
        "bold",
        draftail_features.InlineStyleFeature({
            "type": "BOLD",
            "icon": "bold",
            "description": gettext("Bold"),
        }),
    )
    features.register_converter_rule(
        "contentstate",
        "bold",
        {
            "from_database_format": {
                "b": InlineStyleElementHandler("BOLD"),
                "strong": InlineStyleElementHandler("BOLD"),
            },
            "to_database_format": {
                "style_map": {
                    "BOLD": "b"
                }
            },
        },
    )
    features.register_editor_plugin(
        "draftail",
        "italic",
        draftail_features.InlineStyleFeature({
            "type": "ITALIC",
            "icon": "italic",
            "description": gettext("Italic"),
        }),
    )
    features.register_converter_rule(
        "contentstate",
        "italic",
        {
            "from_database_format": {
                "i": InlineStyleElementHandler("ITALIC"),
                "em": InlineStyleElementHandler("ITALIC"),
            },
            "to_database_format": {
                "style_map": {
                    "ITALIC": "i"
                }
            },
        },
    )

    features.register_editor_plugin(
        "draftail",
        "link",
        draftail_features.EntityFeature(
            {
                "type": "LINK",
                "icon": "link",
                "description": gettext("Link"),
                # We want to enforce constraints on which links can be pasted into rich text.
                # Keep only the attributes Wagtail needs.
                "attributes": ["url", "id", "parentId"],
                "whitelist": {
                    # Keep pasted links with http/https protocol, and not-pasted links (href = undefined).
                    "href": "^(http:|https:|undefined$)",
                },
            },
            js=[
                "wagtailadmin/js/page-chooser-modal.js",
            ],
        ),
    )
    features.register_converter_rule(
        "contentstate",
        "link",
        {
            "from_database_format": {
                "a[href]": ExternalLinkElementHandler("LINK"),
                'a[linktype="page"]': PageLinkElementHandler("LINK"),
            },
            "to_database_format": {
                "entity_decorators": {
                    "LINK": link_entity
                }
            },
        },
    )
    features.register_editor_plugin(
        "draftail",
        "superscript",
        draftail_features.InlineStyleFeature({
            "type":
            "SUPERSCRIPT",
            "icon":
            "superscript",
            "description":
            gettext("Superscript"),
        }),
    )
    features.register_converter_rule(
        "contentstate",
        "superscript",
        {
            "from_database_format": {
                "sup": InlineStyleElementHandler("SUPERSCRIPT"),
            },
            "to_database_format": {
                "style_map": {
                    "SUPERSCRIPT": "sup"
                }
            },
        },
    )
    features.register_editor_plugin(
        "draftail",
        "subscript",
        draftail_features.InlineStyleFeature({
            "type":
            "SUBSCRIPT",
            "icon":
            "subscript",
            "description":
            gettext("Subscript"),
        }),
    )
    features.register_converter_rule(
        "contentstate",
        "subscript",
        {
            "from_database_format": {
                "sub": InlineStyleElementHandler("SUBSCRIPT"),
            },
            "to_database_format": {
                "style_map": {
                    "SUBSCRIPT": "sub"
                }
            },
        },
    )
    features.register_editor_plugin(
        "draftail",
        "strikethrough",
        draftail_features.InlineStyleFeature({
            "type":
            "STRIKETHROUGH",
            "icon":
            "strikethrough",
            "description":
            gettext("Strikethrough"),
        }),
    )
    features.register_converter_rule(
        "contentstate",
        "strikethrough",
        {
            "from_database_format": {
                "s": InlineStyleElementHandler("STRIKETHROUGH"),
            },
            "to_database_format": {
                "style_map": {
                    "STRIKETHROUGH": "s"
                }
            },
        },
    )
    features.register_editor_plugin(
        "draftail",
        "code",
        draftail_features.InlineStyleFeature({
            "type": "CODE",
            "icon": "code",
            "description": gettext("Code"),
        }),
    )
    features.register_converter_rule(
        "contentstate",
        "code",
        {
            "from_database_format": {
                "code": InlineStyleElementHandler("CODE"),
            },
            "to_database_format": {
                "style_map": {
                    "CODE": "code"
                }
            },
        },
    )
def allow_all_attributes(attribute_value):
    class Fn_get_true(object):
        def get(self, *args, **kwargs):
            return True

    return attribute_rule(Fn_get_true())
Beispiel #32
0
def whitelister_element_rules():
    return {
        'blockquote': attribute_rule({'class': True}),
    }
Beispiel #33
0
def register_core_features(features):
    # Hallo.js
    features.register_editor_plugin(
        'hallo', 'hr',
        HalloPlugin(
            name='hallohr',
            js=['wagtailadmin/js/hallo-plugins/hallo-hr.js'],
            order=45,
        )
    )
    features.register_converter_rule('editorhtml', 'hr', [
        WhitelistRule('hr', allow_without_attributes)
    ])

    features.register_editor_plugin(
        'hallo', 'link',
        HalloPlugin(
            name='hallowagtaillink',
            js=['wagtailadmin/js/hallo-plugins/hallo-wagtaillink.js'],
        )
    )
    features.register_converter_rule('editorhtml', 'link', [
        WhitelistRule('a', attribute_rule({'href': check_url})),
        LinkTypeRule('page', PageLinkHandler),
    ])

    features.register_editor_plugin(
        'hallo', 'bold', HalloFormatPlugin(format_name='bold')
    )
    features.register_converter_rule('editorhtml', 'bold', [
        WhitelistRule('b', allow_without_attributes),
        WhitelistRule('strong', allow_without_attributes),
    ])

    features.register_editor_plugin(
        'hallo', 'italic', HalloFormatPlugin(format_name='italic')
    )
    features.register_converter_rule('editorhtml', 'italic', [
        WhitelistRule('i', allow_without_attributes),
        WhitelistRule('em', allow_without_attributes),
    ])

    headings_elements = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']
    headings_order_start = HalloHeadingPlugin.default_order + 1
    for order, element in enumerate(headings_elements, start=headings_order_start):
        features.register_editor_plugin(
            'hallo', element, HalloHeadingPlugin(element=element, order=order)
        )
        features.register_converter_rule('editorhtml', element, [
            WhitelistRule(element, allow_without_attributes)
        ])

    features.register_editor_plugin(
        'hallo', 'ol', HalloListPlugin(list_type='ordered')
    )
    features.register_converter_rule('editorhtml', 'ol', [
        WhitelistRule('ol', allow_without_attributes),
        WhitelistRule('li', allow_without_attributes),
    ])

    features.register_editor_plugin(
        'hallo', 'ul', HalloListPlugin(list_type='unordered')
    )
    features.register_converter_rule('editorhtml', 'ul', [
        WhitelistRule('ul', allow_without_attributes),
        WhitelistRule('li', allow_without_attributes),
    ])

    # Draftail
    features.register_editor_plugin(
        'draftail', 'hr', draftail_features.BooleanFeature('enableHorizontalRule')
    )
    features.register_converter_rule('contentstate', 'hr', {
        'from_database_format': {
            'hr': HorizontalRuleHandler(),
        },
        'to_database_format': {
            'entity_decorators': {'HORIZONTAL_RULE': lambda props: DOM.create_element('hr')}
        }
    })

    features.register_editor_plugin(
        'draftail', 'h1', draftail_features.BlockFeature({
            'label': 'H1',
            'type': 'header-one',
            'description': ugettext('Heading {level}').format(level=1),
        })
    )
    features.register_converter_rule('contentstate', 'h1', {
        'from_database_format': {
            'h1': BlockElementHandler('header-one'),
        },
        'to_database_format': {
            'block_map': {'header-one': 'h1'}
        }
    })
    features.register_editor_plugin(
        'draftail', 'h2', draftail_features.BlockFeature({
            'label': 'H2',
            'type': 'header-two',
            'description': ugettext('Heading {level}').format(level=2),
        })
    )
    features.register_converter_rule('contentstate', 'h2', {
        'from_database_format': {
            'h2': BlockElementHandler('header-two'),
        },
        'to_database_format': {
            'block_map': {'header-two': 'h2'}
        }
    })
    features.register_editor_plugin(
        'draftail', 'h3', draftail_features.BlockFeature({
            'label': 'H3',
            'type': 'header-three',
            'description': ugettext('Heading {level}').format(level=3),
        })
    )
    features.register_converter_rule('contentstate', 'h3', {
        'from_database_format': {
            'h3': BlockElementHandler('header-three'),
        },
        'to_database_format': {
            'block_map': {'header-three': 'h3'}
        }
    })
    features.register_editor_plugin(
        'draftail', 'h4', draftail_features.BlockFeature({
            'label': 'H4',
            'type': 'header-four',
            'description': ugettext('Heading {level}').format(level=4),
        })
    )
    features.register_converter_rule('contentstate', 'h4', {
        'from_database_format': {
            'h4': BlockElementHandler('header-four'),
        },
        'to_database_format': {
            'block_map': {'header-four': 'h4'}
        }
    })
    features.register_editor_plugin(
        'draftail', 'h5', draftail_features.BlockFeature({
            'label': 'H5',
            'type': 'header-five',
            'description': ugettext('Heading {level}').format(level=5),
        })
    )
    features.register_converter_rule('contentstate', 'h5', {
        'from_database_format': {
            'h5': BlockElementHandler('header-five'),
        },
        'to_database_format': {
            'block_map': {'header-five': 'h5'}
        }
    })
    features.register_editor_plugin(
        'draftail', 'h6', draftail_features.BlockFeature({
            'label': 'H6',
            'type': 'header-six',
            'description': ugettext('Heading {level}').format(level=6),
        })
    )
    features.register_converter_rule('contentstate', 'h6', {
        'from_database_format': {
            'h6': BlockElementHandler('header-six'),
        },
        'to_database_format': {
            'block_map': {'header-six': 'h6'}
        }
    })
    features.register_editor_plugin(
        'draftail', 'ul', draftail_features.BlockFeature({
            'type': 'unordered-list-item',
            'icon': 'list-ul',
            'description': ugettext('Bulleted list'),
        })
    )
    features.register_converter_rule('contentstate', 'ul', {
        'from_database_format': {
            'ul': ListElementHandler('unordered-list-item'),
            'li': ListItemElementHandler(),
        },
        'to_database_format': {
            'block_map': {'unordered-list-item': {'element': 'li', 'wrapper': 'ul'}}
        }
    })
    features.register_editor_plugin(
        'draftail', 'ol', draftail_features.BlockFeature({
            'type': 'ordered-list-item',
            'icon': 'list-ol',
            'description': ugettext('Numbered list'),
        })
    )
    features.register_converter_rule('contentstate', 'ol', {
        'from_database_format': {
            'ol': ListElementHandler('ordered-list-item'),
            'li': ListItemElementHandler(),
        },
        'to_database_format': {
            'block_map': {'ordered-list-item': {'element': 'li', 'wrapper': 'ol'}}
        }
    })

    features.register_editor_plugin(
        'draftail', 'bold', draftail_features.InlineStyleFeature({
            'type': 'BOLD',
            'icon': 'bold',
            'description': ugettext('Bold'),
        })
    )
    features.register_converter_rule('contentstate', 'bold', {
        'from_database_format': {
            'b': InlineStyleElementHandler('BOLD'),
            'strong': InlineStyleElementHandler('BOLD'),
        },
        'to_database_format': {
            'style_map': {'BOLD': 'b'}
        }
    })
    features.register_editor_plugin(
        'draftail', 'italic', draftail_features.InlineStyleFeature({
            'type': 'ITALIC',
            'icon': 'italic',
            'description': ugettext('Italic'),
        })
    )
    features.register_converter_rule('contentstate', 'italic', {
        'from_database_format': {
            'i': InlineStyleElementHandler('ITALIC'),
            'em': InlineStyleElementHandler('ITALIC'),
        },
        'to_database_format': {
            'style_map': {'ITALIC': 'i'}
        }
    })

    features.register_editor_plugin(
        'draftail', 'link', draftail_features.EntityFeature({
            'type': 'LINK',
            'icon': 'link',
            'description': ugettext('Link'),
            # We want to enforce constraints on which links can be pasted into rich text.
            # Keep only the attributes Wagtail needs.
            'attributes': ['url', 'id', 'parentId'],
            'whitelist': {
                # Keep pasted links with http/https protocol, and not-pasted links (href = undefined).
                'href': "^(http:|https:|undefined$)",
            }
        })
    )
    features.register_converter_rule('contentstate', 'link', {
        'from_database_format': {
            'a[href]': ExternalLinkElementHandler('LINK'),
            'a[linktype="page"]': PageLinkElementHandler('LINK'),
        },
        'to_database_format': {
            'entity_decorators': {'LINK': link_entity}
        }
    })
from django.contrib.staticfiles.templatetags.staticfiles import static
from django.db import models
from django.forms import Media

import wagtail

try:
    from wagtail.admin.rich_text import HalloRichTextArea
    from wagtail.admin.rich_text.converters.editor_html import DbWhitelister
    from wagtail.core.whitelist import attribute_rule, check_url
except ImportError:
    from wagtail.wagtailadmin.rich_text import HalloRichTextArea
    from wagtail.wagtailcore.rich_text import DbWhitelister
    from wagtail.wagtailcore.whitelist import attribute_rule, check_url

allow_without_attributes = attribute_rule({})
ELEMENT_RULES = {
    '[document]': allow_without_attributes,
    'a': attribute_rule({
        'href': check_url,
        'id': True,
        'linktype': True
    }),
    'p': allow_without_attributes,
    'b': allow_without_attributes,
    'i': allow_without_attributes,
    'u': allow_without_attributes,
    'ul': allow_without_attributes,
    'ol': allow_without_attributes,
    'li': allow_without_attributes,
}
def whitelister_element_rules():
    # Whitelist custom elements to the hallo.js editor
    return {
        'a': attribute_rule({'href': check_url, 'target': True}),
        'blockquote': attribute_rule({'class': True})
    }
#         self._type = classtype


def allow_all_attributes(tag):
    pass


def ext_obj(obj, *args):
    _obj = obj.copy()
    for eobj in args:
        _obj.update(eobj)
    return _obj


ext_attributes = {'class': True, 'id': True, 'style': True}
allow_ext_attributes = attribute_rule(allowed_attrs=ext_attributes)
CUSTOM_DEFAULT_ELEMENT_RULES = {
    'a':
    attribute_rule(allowed_attrs=ext_obj(ext_attributes, {
        'href': check_url,
        'target': True
    })),
    'img':
    attribute_rule(
        allowed_attrs=ext_obj(ext_attributes, {
            'src': check_url,
            'width': True,
            'height': True,
            'alt': True
        })),
    'table':
Beispiel #37
0
def whitelister_element_rules():
    return {
        'span': attribute_rule({'data-target': True, 'data-toggle': True, 'class': True}),
    }
Beispiel #38
0
import json

from django.db import models

from wagtail.core.whitelist import attribute_rule, check_url
from wagtail.core.rich_text import DbWhitelister
from wagtail.admin.rich_text import HalloRichTextArea

allow_without_attributes = attribute_rule({})


class SimpleDbWhitelister(DbWhitelister):
    """
    DbWhitelister to allow/disallow stuff on the text editor
    """
    element_rules = {
        '[document]': allow_without_attributes,
        'a': attribute_rule({'href': check_url, 'id': True, 'linktype': True}),
        'p': allow_without_attributes,
        'b': allow_without_attributes,
        'i': allow_without_attributes,
        'u': allow_without_attributes,
        'ul': allow_without_attributes,
        'ol': allow_without_attributes,
        'li': allow_without_attributes,
    }


class SimpleRichTextArea(HalloRichTextArea):
    """
    Customised RichTextArea
Beispiel #39
0
def register_core_features(features):
    # Hallo.js
    features.register_editor_plugin(
        'hallo', 'hr',
        HalloPlugin(
            name='hallohr',
            js=[versioned_static('wagtailadmin/js/hallo-plugins/hallo-hr.js')],
            order=45,
        ))
    features.register_converter_rule(
        'editorhtml', 'hr', [WhitelistRule('hr', allow_without_attributes)])

    features.register_editor_plugin(
        'hallo', 'link',
        HalloPlugin(
            name='hallowagtaillink',
            js=[
                versioned_static('wagtailadmin/js/page-chooser-modal.js'),
                versioned_static(
                    'wagtailadmin/js/hallo-plugins/hallo-wagtaillink.js'),
            ],
        ))
    features.register_converter_rule('editorhtml', 'link', [
        WhitelistRule('a', attribute_rule({'href': check_url})),
        LinkTypeRule('page', PageLinkHandler),
    ])

    features.register_editor_plugin('hallo', 'bold',
                                    HalloFormatPlugin(format_name='bold'))
    features.register_converter_rule('editorhtml', 'bold', [
        WhitelistRule('b', allow_without_attributes),
        WhitelistRule('strong', allow_without_attributes),
    ])

    features.register_editor_plugin('hallo', 'italic',
                                    HalloFormatPlugin(format_name='italic'))
    features.register_converter_rule('editorhtml', 'italic', [
        WhitelistRule('i', allow_without_attributes),
        WhitelistRule('em', allow_without_attributes),
    ])

    headings_elements = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']
    headings_order_start = HalloHeadingPlugin.default_order + 1
    for order, element in enumerate(headings_elements,
                                    start=headings_order_start):
        features.register_editor_plugin(
            'hallo', element, HalloHeadingPlugin(element=element, order=order))
        features.register_converter_rule(
            'editorhtml', element,
            [WhitelistRule(element, allow_without_attributes)])

    features.register_editor_plugin('hallo', 'ol',
                                    HalloListPlugin(list_type='ordered'))
    features.register_converter_rule('editorhtml', 'ol', [
        WhitelistRule('ol', allow_without_attributes),
        WhitelistRule('li', allow_without_attributes),
    ])

    features.register_editor_plugin('hallo', 'ul',
                                    HalloListPlugin(list_type='unordered'))
    features.register_converter_rule('editorhtml', 'ul', [
        WhitelistRule('ul', allow_without_attributes),
        WhitelistRule('li', allow_without_attributes),
    ])

    # Draftail
    features.register_editor_plugin(
        'draftail', 'hr',
        draftail_features.BooleanFeature('enableHorizontalRule'))
    features.register_converter_rule(
        'contentstate', 'hr', {
            'from_database_format': {
                'hr': HorizontalRuleHandler(),
            },
            'to_database_format': {
                'entity_decorators': {
                    'HORIZONTAL_RULE': lambda props: DOM.create_element('hr')
                }
            }
        })

    features.register_editor_plugin(
        'draftail', 'h1',
        draftail_features.BlockFeature({
            'label':
            'H1',
            'type':
            'header-one',
            'description':
            ugettext('Heading {level}').format(level=1),
        }))
    features.register_converter_rule(
        'contentstate', 'h1', {
            'from_database_format': {
                'h1': BlockElementHandler('header-one'),
            },
            'to_database_format': {
                'block_map': {
                    'header-one': 'h1'
                }
            }
        })
    features.register_editor_plugin(
        'draftail', 'h2',
        draftail_features.BlockFeature({
            'label':
            'H2',
            'type':
            'header-two',
            'description':
            ugettext('Heading {level}').format(level=2),
        }))
    features.register_converter_rule(
        'contentstate', 'h2', {
            'from_database_format': {
                'h2': BlockElementHandler('header-two'),
            },
            'to_database_format': {
                'block_map': {
                    'header-two': 'h2'
                }
            }
        })
    features.register_editor_plugin(
        'draftail', 'h3',
        draftail_features.BlockFeature({
            'label':
            'H3',
            'type':
            'header-three',
            'description':
            ugettext('Heading {level}').format(level=3),
        }))
    features.register_converter_rule(
        'contentstate', 'h3', {
            'from_database_format': {
                'h3': BlockElementHandler('header-three'),
            },
            'to_database_format': {
                'block_map': {
                    'header-three': 'h3'
                }
            }
        })
    features.register_editor_plugin(
        'draftail', 'h4',
        draftail_features.BlockFeature({
            'label':
            'H4',
            'type':
            'header-four',
            'description':
            ugettext('Heading {level}').format(level=4),
        }))
    features.register_converter_rule(
        'contentstate', 'h4', {
            'from_database_format': {
                'h4': BlockElementHandler('header-four'),
            },
            'to_database_format': {
                'block_map': {
                    'header-four': 'h4'
                }
            }
        })
    features.register_editor_plugin(
        'draftail', 'h5',
        draftail_features.BlockFeature({
            'label':
            'H5',
            'type':
            'header-five',
            'description':
            ugettext('Heading {level}').format(level=5),
        }))
    features.register_converter_rule(
        'contentstate', 'h5', {
            'from_database_format': {
                'h5': BlockElementHandler('header-five'),
            },
            'to_database_format': {
                'block_map': {
                    'header-five': 'h5'
                }
            }
        })
    features.register_editor_plugin(
        'draftail', 'h6',
        draftail_features.BlockFeature({
            'label':
            'H6',
            'type':
            'header-six',
            'description':
            ugettext('Heading {level}').format(level=6),
        }))
    features.register_converter_rule(
        'contentstate', 'h6', {
            'from_database_format': {
                'h6': BlockElementHandler('header-six'),
            },
            'to_database_format': {
                'block_map': {
                    'header-six': 'h6'
                }
            }
        })
    features.register_editor_plugin(
        'draftail', 'ul',
        draftail_features.BlockFeature({
            'type':
            'unordered-list-item',
            'icon':
            'list-ul',
            'description':
            ugettext('Bulleted list'),
        }))
    features.register_converter_rule(
        'contentstate', 'ul', {
            'from_database_format': {
                'ul': ListElementHandler('unordered-list-item'),
                'li': ListItemElementHandler(),
            },
            'to_database_format': {
                'block_map': {
                    'unordered-list-item': {
                        'element': 'li',
                        'wrapper': 'ul'
                    }
                }
            }
        })
    features.register_editor_plugin(
        'draftail', 'ol',
        draftail_features.BlockFeature({
            'type':
            'ordered-list-item',
            'icon':
            'list-ol',
            'description':
            ugettext('Numbered list'),
        }))
    features.register_converter_rule(
        'contentstate', 'ol', {
            'from_database_format': {
                'ol': ListElementHandler('ordered-list-item'),
                'li': ListItemElementHandler(),
            },
            'to_database_format': {
                'block_map': {
                    'ordered-list-item': {
                        'element': 'li',
                        'wrapper': 'ol'
                    }
                }
            }
        })
    features.register_editor_plugin(
        'draftail', 'blockquote',
        draftail_features.BlockFeature({
            'type': 'blockquote',
            'icon': 'openquote',
            'description': ugettext('Blockquote'),
        }))
    features.register_converter_rule(
        'contentstate', 'blockquote', {
            'from_database_format': {
                'blockquote': BlockElementHandler('blockquote'),
            },
            'to_database_format': {
                'block_map': {
                    'blockquote': 'blockquote'
                }
            }
        })

    features.register_editor_plugin(
        'draftail', 'bold',
        draftail_features.InlineStyleFeature({
            'type': 'BOLD',
            'icon': 'bold',
            'description': ugettext('Bold'),
        }))
    features.register_converter_rule(
        'contentstate', 'bold', {
            'from_database_format': {
                'b': InlineStyleElementHandler('BOLD'),
                'strong': InlineStyleElementHandler('BOLD'),
            },
            'to_database_format': {
                'style_map': {
                    'BOLD': 'b'
                }
            }
        })
    features.register_editor_plugin(
        'draftail', 'italic',
        draftail_features.InlineStyleFeature({
            'type': 'ITALIC',
            'icon': 'italic',
            'description': ugettext('Italic'),
        }))
    features.register_converter_rule(
        'contentstate', 'italic', {
            'from_database_format': {
                'i': InlineStyleElementHandler('ITALIC'),
                'em': InlineStyleElementHandler('ITALIC'),
            },
            'to_database_format': {
                'style_map': {
                    'ITALIC': 'i'
                }
            }
        })

    features.register_editor_plugin(
        'draftail',
        'link',
        draftail_features.EntityFeature(
            {
                'type': 'LINK',
                'icon': 'link',
                'description': ugettext('Link'),
                # We want to enforce constraints on which links can be pasted into rich text.
                # Keep only the attributes Wagtail needs.
                'attributes': ['url', 'id', 'parentId'],
                'whitelist': {
                    # Keep pasted links with http/https protocol, and not-pasted links (href = undefined).
                    'href': "^(http:|https:|undefined$)",
                }
            },
            js=[
                versioned_static('wagtailadmin/js/page-chooser-modal.js'),
            ]))
    features.register_converter_rule(
        'contentstate', 'link', {
            'from_database_format': {
                'a[href]': ExternalLinkElementHandler('LINK'),
                'a[linktype="page"]': PageLinkElementHandler('LINK'),
            },
            'to_database_format': {
                'entity_decorators': {
                    'LINK': link_entity
                }
            }
        })
    features.register_editor_plugin(
        'draftail', 'superscript',
        draftail_features.InlineStyleFeature({
            'type':
            'SUPERSCRIPT',
            'icon':
            'superscript',
            'description':
            ugettext('Superscript'),
        }))
    features.register_converter_rule(
        'contentstate', 'superscript', {
            'from_database_format': {
                'sup': InlineStyleElementHandler('SUPERSCRIPT'),
            },
            'to_database_format': {
                'style_map': {
                    'SUPERSCRIPT': 'sup'
                }
            }
        })
    features.register_editor_plugin(
        'draftail', 'subscript',
        draftail_features.InlineStyleFeature({
            'type':
            'SUBSCRIPT',
            'icon':
            'subscript',
            'description':
            ugettext('Subscript'),
        }))
    features.register_converter_rule(
        'contentstate', 'subscript', {
            'from_database_format': {
                'sub': InlineStyleElementHandler('SUBSCRIPT'),
            },
            'to_database_format': {
                'style_map': {
                    'SUBSCRIPT': 'sub'
                }
            }
        })
    features.register_editor_plugin(
        'draftail', 'strikethrough',
        draftail_features.InlineStyleFeature({
            'type':
            'STRIKETHROUGH',
            'icon':
            'strikethrough',
            'description':
            ugettext('Strikethrough'),
        }))
    features.register_converter_rule(
        'contentstate', 'strikethrough', {
            'from_database_format': {
                's': InlineStyleElementHandler('STRIKETHROUGH'),
            },
            'to_database_format': {
                'style_map': {
                    'STRIKETHROUGH': 's'
                }
            }
        })
    features.register_editor_plugin(
        'draftail', 'code',
        draftail_features.InlineStyleFeature({
            'type': 'CODE',
            'icon': 'code',
            'description': ugettext('Code'),
        }))
    features.register_converter_rule(
        'contentstate', 'code', {
            'from_database_format': {
                'code': InlineStyleElementHandler('CODE'),
            },
            'to_database_format': {
                'style_map': {
                    'CODE': 'code'
                }
            }
        })
def whitelister_element_rules():
    return {
        'blockquote': allow_without_attributes,
        'a': attribute_rule({'href': check_url, 'target': True}),
    }