diff --git a/wagtail/core/rich_text/__init__.py b/wagtail/core/rich_text/__init__.py index c29683929c..e3291545b5 100644 --- a/wagtail/core/rich_text/__init__.py +++ b/wagtail/core/rich_text/__init__.py @@ -1,11 +1,10 @@ import re # parsing HTML with regexes LIKE A BOSS. from django.utils.functional import cached_property -from django.utils.html import escape from django.utils.safestring import mark_safe from wagtail.core import hooks -from wagtail.core.models import Page +from wagtail.core.rich_text.pages import PageLinkHandler from wagtail.core.whitelist import allow_without_attributes, Whitelister, DEFAULT_ELEMENT_RULES @@ -16,40 +15,6 @@ from wagtail.core.whitelist import allow_without_attributes, Whitelister, DEFAUL # to DB representation and back again. -class PageLinkHandler: - """ - PageLinkHandler will be invoked whenever we encounter an element in HTML content - with an attribute of data-linktype="page". The resulting element in the database - representation will be: - hello world - """ - @staticmethod - def get_db_attributes(tag): - """ - Given an tag that we've identified as a page link embed (because it has a - data-linktype="page" attribute), return a dict of the attributes we should - have on the resulting element. - """ - return {'id': tag['data-id']} - - @staticmethod - def expand_db_attributes(attrs, for_editor): - try: - page = Page.objects.get(id=attrs['id']) - - if for_editor: - editor_attrs = 'data-linktype="page" data-id="%d" ' % page.id - parent_page = page.get_parent() - if parent_page: - editor_attrs += 'data-parent-id="%d" ' % parent_page.id - else: - editor_attrs = '' - - return '' % (editor_attrs, escape(page.specific.url)) - except Page.DoesNotExist: - return "" - - EMBED_HANDLERS = {} LINK_HANDLERS = { 'page': PageLinkHandler, diff --git a/wagtail/core/rich_text/pages.py b/wagtail/core/rich_text/pages.py new file mode 100644 index 0000000000..f454bf1107 --- /dev/null +++ b/wagtail/core/rich_text/pages.py @@ -0,0 +1,37 @@ +from django.utils.html import escape + +from wagtail.core.models import Page + + +class PageLinkHandler: + """ + PageLinkHandler will be invoked whenever we encounter an element in HTML content + with an attribute of data-linktype="page". The resulting element in the database + representation will be: + hello world + """ + @staticmethod + def get_db_attributes(tag): + """ + Given an tag that we've identified as a page link embed (because it has a + data-linktype="page" attribute), return a dict of the attributes we should + have on the resulting element. + """ + return {'id': tag['data-id']} + + @staticmethod + def expand_db_attributes(attrs, for_editor): + try: + page = Page.objects.get(id=attrs['id']) + + if for_editor: + editor_attrs = 'data-linktype="page" data-id="%d" ' % page.id + parent_page = page.get_parent() + if parent_page: + editor_attrs += 'data-parent-id="%d" ' % parent_page.id + else: + editor_attrs = '' + + return '' % (editor_attrs, escape(page.specific.url)) + except Page.DoesNotExist: + return "" diff --git a/wagtail/core/tests/test_rich_text.py b/wagtail/core/tests/test_rich_text.py index 96f6b380e1..fa2417bd9b 100644 --- a/wagtail/core/tests/test_rich_text.py +++ b/wagtail/core/tests/test_rich_text.py @@ -4,7 +4,8 @@ from mock import patch from wagtail.core.models import Page from wagtail.core.rich_text import ( - DbWhitelister, FeatureRegistry, PageLinkHandler, RichText, expand_db_html, extract_attrs) + DbWhitelister, FeatureRegistry, RichText, expand_db_html, extract_attrs) +from wagtail.core.rich_text.pages import PageLinkHandler class TestPageLinkHandler(TestCase):