From b12a7b08bd3a87f0dc26829cb730d2683cb71ca5 Mon Sep 17 00:00:00 2001 From: Loic Teixeira Date: Wed, 4 Apr 2018 13:35:11 +1000 Subject: [PATCH] Ensure order of headings features is consistent for HalloJS --- CHANGELOG.txt | 1 + docs/releases/2.1.rst | 1 + wagtail/admin/rich_text/editors/hallo.py | 4 +++- wagtail/admin/tests/test_rich_text.py | 23 +++++++++++++++++++++++ wagtail/admin/wagtail_hooks.py | 6 ++++-- 5 files changed, 32 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index d5e35a8de1..bf9697df7d 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -38,6 +38,7 @@ Changelog * Fix: Localization of image and apps verbose names * Fix: Draftail editor no longer crashes after deleting image/embed using DEL key (Thibaud Colas) * Fix: Breadcrumb navigation now respects custom `get_admin_display_title` methods (Arthur Holzner, Wietze Helmantel, Matt Westcott) + * Fix: Inconsistent order of heading features when adding h1, h5 or h6 as default feature for Hallo RichText editor (Loic Teixeira) 2.0.1 (04.04.2018) diff --git a/docs/releases/2.1.rst b/docs/releases/2.1.rst index 5a9732e34b..48287962b5 100644 --- a/docs/releases/2.1.rst +++ b/docs/releases/2.1.rst @@ -56,6 +56,7 @@ Bug fixes * Localization of image and apps verbose names * Draftail editor no longer crashes after deleting image/embed using DEL key (Thibaud Colas) * Breadcrumb navigation now respects custom ``get_admin_display_title`` methods (Arthur Holzner, Wietze Helmantel, Matt Westcott) + * Inconsistent order of heading features when adding h1, h5 or h6 as default feature for Hallo RichText editor (Loic Teixeira) Upgrade considerations diff --git a/wagtail/admin/rich_text/editors/hallo.py b/wagtail/admin/rich_text/editors/hallo.py index e95b16b574..d85c7b454d 100644 --- a/wagtail/admin/rich_text/editors/hallo.py +++ b/wagtail/admin/rich_text/editors/hallo.py @@ -41,9 +41,11 @@ class HalloFormatPlugin(HalloPlugin): class HalloHeadingPlugin(HalloPlugin): + default_order = 20 + def __init__(self, **kwargs): kwargs.setdefault('name', 'halloheadings') - kwargs.setdefault('order', 20) + kwargs.setdefault('order', self.default_order) self.element = kwargs.pop('element') super().__init__(**kwargs) diff --git a/wagtail/admin/tests/test_rich_text.py b/wagtail/admin/tests/test_rich_text.py index dfbe7f31db..e70f97d1a1 100644 --- a/wagtail/admin/tests/test_rich_text.py +++ b/wagtail/admin/tests/test_rich_text.py @@ -8,6 +8,7 @@ from wagtail.admin.rich_text import ( DraftailRichTextArea, HalloRichTextArea, get_rich_text_editor_widget) from wagtail.core.blocks import RichTextBlock from wagtail.core.models import Page, get_page_models +from wagtail.core.rich_text import features as feature_registry from wagtail.core.rich_text import RichText from wagtail.tests.testapp.models import SingleEventPage from wagtail.tests.testapp.rich_text import CustomRichTextArea @@ -504,6 +505,28 @@ class TestHalloJsWithCustomFeatureOptions(BaseRichTextEditHandlerTestCase, Wagta self.assertNotIn('wagtaildocs/js/hallo-plugins/hallo-wagtaildoclink.js', media_html) +@override_settings(WAGTAILADMIN_RICH_TEXT_EDITORS={ + 'default': { + 'WIDGET': 'wagtail.admin.rich_text.HalloRichTextArea' + }, +}) +class TestHalloJsHeadingOrder(BaseRichTextEditHandlerTestCase, WagtailTestUtils): + + def test_heading_order(self): + # Using the `register_rich_text_features` doesn't work here, + # probably because the features have already been scanned at that point. + # Extending the registry directly instead. + feature_registry.default_features.extend(['h1', 'h5', 'h6']) + + widget = HalloRichTextArea() + js_init = widget.render_js_init('the_id', 'the_name', '

the value

') + + expected_options = ( + '"halloheadings": {"formatBlocks": ["p", "h1", "h2", "h3", "h4", "h5", "h6"]}' + ) + self.assertIn(expected_options, js_init) + + class TestWidgetWhitelisting(TestCase, WagtailTestUtils): def test_default_whitelist(self): widget = HalloRichTextArea() diff --git a/wagtail/admin/wagtail_hooks.py b/wagtail/admin/wagtail_hooks.py index 95a54faf52..8ca108b667 100644 --- a/wagtail/admin/wagtail_hooks.py +++ b/wagtail/admin/wagtail_hooks.py @@ -287,9 +287,11 @@ def register_core_features(features): WhitelistRule('em', allow_without_attributes), ]) - for element in ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']: + 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) + 'hallo', element, HalloHeadingPlugin(element=element, order=order) ) features.register_converter_rule('editorhtml', element, [ WhitelistRule(element, allow_without_attributes)