Skip to main content

Wagtail Hallo - The legacy richtext editor for Wagtail.

Project description

Wagtail Hallo - Rich Text Editor

License: MIT Wagtail Hallo CI

This is the legacy rich text editor for the Wagtail CMS. Based on Hallo.js.

As of Wagtail 2.0, the hallo.js editor is deprecated.

Status This package should be compatible with Wagtail 2.17 and earlier versions, however, it will no longer receive bug fixes or be actively maintained. Pull requests will be accepted and if maintainers wish to support this outside of the Core Wagtail team please raise an Issue to discuss this.

Major risks of using this package

  • Please be aware of the known hallo.js issues should you want to keep using it.
  • Hallo’s has inappropriate handling of HTML and editor input – it is not reliable, has browser-specific inconsistent behavior, is not a good user experience and is not accessible.
  • This package is a source of security concerns (XSS injections, not CSP compatible) and allows injection of undesirable content or formatting (e.g. images in headings, or headings in lists).
  • There is no guarantee that this package will be compatible with Wagtail beyond the supported versions listed above.

Release Notes

Supported Versions

  • Python 3.7, 3.8, 3.9
  • Django 3.1, 3.2. 4.0
  • Wagtail 2.14, 2.15, 2.16, 2.17

Installing the Hallo Editor

  • Important: Requires jQuery and jQueryUI - which are not included and may not always be included with Wagtail.
  • pip install wagtail-hallo
  • Add 'wagtail_hallo' to your settings.py INSTALLED_APPS

To use Wagtail hallo on Wagtail 2.x, add the following to your settings:

WAGTAILADMIN_RICH_TEXT_EDITORS = {
    'hallo': {
        'WIDGET': 'wagtail_hallo.hallo.HalloRichTextArea'
    }
}

Using the Hallo Editor in RichTextField

# models.py
from wagtail.admin.edit_handlers import FieldPanel
from wagtail.core.fields import RichTextField
from wagtail.core.models import Page

class MyHalloPage(Page):
    body = RichTextField(editor='hallo')

    content_panels = Page.content_panels + [
        FieldPanel('body', classname='full'),
    ]
{% extends "base.html" %}
{% load wagtailcore_tags wagtailimages_tags %}

{% block content %}
    {% include "base/include/header.html" %}
    <div class="container">
        <div class="row">
            <div class="col-md-7">{{ page.body|richtext }}</div>
        </div>
    </div>
{% endblock content %}

Using the Hallo Editor in StreamField via RichTextBlock

# models.py
from wagtail.core.models import Page
from wagtail.core.blocks import CharBlock, RichTextBlock
from wagtail.admin.edit_handlers import StreamFieldPanel
from wagtail.core.fields import StreamField

class MyOtherHalloPage(Page):
    body = StreamField([
        ('heading', CharBlock(form_classname="full title")),
        ('paragraph', RichTextBlock(editor='hallo')),
    ], blank=True)

    content_panels = Page.content_panels + [
        StreamFieldPanel('body'),
    ]
{% extends "base.html" %}
{% load wagtailcore_tags wagtailimages_tags %}

{% block content %}
    {% include "base/include/header.html" %}
    <div class="container">
        <div class="row">
            <div class="col-md-7">{{ page.body }}</div>
        </div>
    </div>
{% endblock content %}

Extending the Hallo Editor

The legacy hallo.js editor’s functionality can be extended through plugins. For information on developing custom hallo.js plugins, see the project's page: https://github.com/bergie/hallo

Once the plugin has been created, it should be registered through the feature registry's register_editor_plugin(editor, feature_name, plugin) method. For a hallo.js plugin, the editor parameter should always be 'hallo'.

A plugin halloblockquote, implemented in myapp/js/hallo-blockquote.js, that adds support for the <blockquote> tag, would be registered under the feature name block-quote as follows:

    from wagtail.core import hooks
    from wagtail_halo.plugins import HalloPlugin

    @hooks.register('register_rich_text_features')
    def register_embed_feature(features):
        features.register_editor_plugin(
            'hallo', 'block-quote',
            HalloPlugin(
                name='halloblockquote',
                js=['myapp/js/hallo-blockquote.js'],
            )
        )

The constructor for HalloPlugin accepts the following keyword arguments:

  • name - the plugin name as defined in the JavaScript code. hallo.js plugin names are prefixed with the "IKS." namespace, but the name passed here should be without the prefix.
  • options - a dictionary (or other JSON-serialisable object) of options to be passed to the JavaScript plugin code on initialisation
  • js - a list of JavaScript files to be imported for this plugin, defined in the same way as a Django form media definition
  • css - a dictionary of CSS files to be imported for this plugin, defined in the same way as a Django form media definition
  • order - an index number (default 100) specifying the order in which plugins should be listed, which in turn determines the order buttons will appear in the toolbar

When writing the front-end code for the plugin, Wagtail’s Hallo implementation offers two extension points:

  • In JavaScript, use the [data-hallo-editor] attribute selector to target the editor, eg. var editor = document.querySelector('[data-hallo-editor]');.
  • In CSS, use the .halloeditor class selector.

Whitelisting rich text elements

After extending the editor to support a new HTML element, you'll need to add it to the whitelist of permitted elements - Wagtail's standard behaviour is to strip out unrecognised elements, to prevent editors from inserting styles and scripts (either deliberately, or inadvertently through copy-and-paste) that the developer didn't account for.

Elements can be added to the whitelist through the feature registry's register_converter_rule(converter, feature_name, ruleset) method. When the hallo.js editor is in use, the converter parameter should always be 'editorhtml'.

The following code will add the <blockquote> element to the whitelist whenever the block-quote feature is active:

    from wagtail.admin.rich_text.converters.editor_html import WhitelistRule
    from wagtail.core.whitelist import allow_without_attributes

    @hooks.register('register_rich_text_features')
    def register_blockquote_feature(features):
        features.register_converter_rule('editorhtml', 'block-quote', [
            WhitelistRule('blockquote', allow_without_attributes),
        ])

WhitelistRule is passed the element name, and a callable which will perform some kind of manipulation of the element whenever it is encountered. This callable receives the element as a BeautifulSoup Tag object.

The wagtail.core.whitelist module provides a few helper functions to assist in defining these handlers: allow_without_attributes, a handler which preserves the element but strips out all of its attributes, and attribute_rule which accepts a dict specifying how to handle each attribute, and returns a handler function. This dict will map attribute names to either True (indicating that the attribute should be kept), False (indicating that it should be dropped), or a callable (which takes the initial attribute value and returns either a final value for the attribute, or None to drop the attribute).

Contributing

All contributions are welcome as the Wagtail core team will no longer be actively maintaining this project.

Development instructions

  • To make changes to this project, first clone this repository git clone git@github.com:wagtail/wagtail-hallo.git.

Python (Django / Wagtail)

  • pip3 install -e ../path/to/wagtail-hallo/ -> this installs the package locally as editable
  • Ensure 'wagtail_hallo' is added to your settings.py INSTALLED_APPS
  • You will need to have a test application (e.g. Bakery Demo) and have a Page model to work with, along with a template.
    • see test/testapp/models.py for a reference model
    • see test/testapp/templates/hallo_test_page.html for a reference template
  • After creating the model, remember to run python manage.py makemigrations and python manage.py migrate
  • Run tests python testmanage.py test
  • Run migrations for test models django-admin makemigrations --settings=wagtail_hallo.test.settings
  • Run linting flake8 wagtail_hallo
  • Run formatting black wagtail_hallo

JavaScript & CSS (Frontend)

Currently the frontend tooling is based on Node & NPM and is only used to format and check code, this repository intentionally does not use any build tools and as such JavaScript and CSS must be written without that requirement.

  • nvm use - Ensures you are on the right node version
  • npm install --no-save - Install NPM packages
  • npm run fix - Parses through JS/CSS files to fix anything it can
  • npm run lint - Runs linting
  • npm run format - Runs Prettier formatting on most files (non-Python)
  • npm test - Runs tests (Jest)
  • npm run preflight - Runs all the linting/formatting/jest checks and must be done before committing code

Release checklist

  • Update tox.ini, setup.py, README.md with new supported Python, Django, or Wagtail versions
  • Update setup.py with new release version
  • Update package.json with new release version (note: npm package not deployed, however good to keep in sync), run npm install after to update package-lock.json
  • Update CHANGELOG.md with the release date
  • Push to Pypi
    • pip install twine
    • python3 setup.py clean --all sdist bdist_wheel
    • twine upload dist/* <-- pushes to PyPI

Thanks

Many thanks to all of our supporters, contributors, and users of Wagtail who built upon the amazing Hallo.js editor. We are thankful to the Wagtail core team and developers at Torchbox who sponsored the majority of the initial development. And a very special thanks to the original creator of the Hallo.js editor.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wagtail-hallo-0.1.0a1.tar.gz (50.9 kB view details)

Uploaded Source

Built Distribution

wagtail_hallo-0.1.0a1-py3-none-any.whl (55.6 kB view details)

Uploaded Python 3

File details

Details for the file wagtail-hallo-0.1.0a1.tar.gz.

File metadata

  • Download URL: wagtail-hallo-0.1.0a1.tar.gz
  • Upload date:
  • Size: 50.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.7

File hashes

Hashes for wagtail-hallo-0.1.0a1.tar.gz
Algorithm Hash digest
SHA256 7cbc3930d68ca949d9a72eba0d9835acfc6c8937f04932bc249c8eb529022870
MD5 1a246ee6b3302d2bf7dfb24de14905b6
BLAKE2b-256 e2e866623858fb38d06de3498545602850a7d5ea1d671b9a9918c86635ff054d

See more details on using hashes here.

File details

Details for the file wagtail_hallo-0.1.0a1-py3-none-any.whl.

File metadata

  • Download URL: wagtail_hallo-0.1.0a1-py3-none-any.whl
  • Upload date:
  • Size: 55.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.7

File hashes

Hashes for wagtail_hallo-0.1.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 fc0b20cccc64be50178bd245bf91d238e234b7e2e374b00605334e0e177fbe0a
MD5 2e792469bd4f6a9d712b406b0867d787
BLAKE2b-256 6991c7113a65d175e78ca7bef50c7b9e5f567141f4bdc5adeab0d7624ff2ce0a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page