text: Title: Uncaptured error raised when invalid HTML is stored in RichTestField and processed by `handle_endtag` in edit interface (throws 500 error)
  Body: ### Issue Summary
  
  When invalid HTML is imported programmatically into a `RichTextField` when viewing custom Wagtail Page model in the CMS interface an unhandled exception is thrown when using the default widget for this field type.
  
  ref: `wagtail/admin/rich_text/converters/html_to_contentstate.py`
  
  ### Steps to Reproduce
  
  1. Start a new project with `wagtail start myproject`
  2. Edit `models.py` to include new field as follows...
  3. `example_field = RichTextField(blank=True)`
  4. `FieldPanel('reviews'),`
  5. import some invalid HTML into this field, sample invalid HTML below: `"<p><em>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</em>, 15 August 1897</p>\n<p><em>\n<p><em>Lorem ipsum, </em>12 August&nbsp;1926</p>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>\n</em></p>\n<p>429, September 1906, pp. &nbsp;800-44&nbsp;<em>&nbsp;</em></p>\n<p>&nbsp;</p>\n<p>&nbsp;</p>"`
  6. View page in the CMS edit interface
  7. 500 error will be thrown, sample stacktrace below.
  
  ```
  Environment:
  
  
  Request Method: GET
  Request URL: http://localhost:9000/cms/pages/266/edit/
  
  Django Version: 4.2.15
  Python Version: 3.12.4
  
  Installed Applications:
  [...,
   'wagtailmedia',
   'wagtail.contrib.forms',
   'wagtail.contrib.redirects',
   'wagtail.embeds',
   'wagtail.sites',
   'wagtail.users',
   'wagtail.snippets',
   'wagtail.documents',
   'wagtail.images',
   'wagtail.search',
   'wagtail.admin',
   'wagtail',
   'modelcluster',
   'taggit',
   'import_export',
   'django.contrib.sitemaps',
   'django.contrib.admin',
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.gis',
   'django.contrib.sessions',
   'django.contrib.messages',
   'django.contrib.staticfiles',
   'django.contrib.sites']
  Installed Middleware:
  ['django.middleware.security.SecurityMiddleware',
   'django.contrib.sessions.middleware.SessionMiddleware',
   'django.middleware.common.CommonMiddleware',
   'django.middleware.csrf.CsrfViewMiddleware',
   'django.contrib.auth.middleware.AuthenticationMiddleware',
   'django.contrib.messages.middleware.MessageMiddleware',
   'django.middleware.clickjacking.XFrameOptionsMiddleware',
   'wagtail.contrib.redirects.middleware.RedirectMiddleware']
  
  
  Template error:
  In template /opt/venv/lib/python3.12/site-packages/wagtail/admin/templates/wagtailadmin/panels/object_list.html, error at line 9
     End of block reached without closing inline style elements
     1 : {% load wagtailadmin_tags %}
     2 : 
     3 : <div class="w-form-width" {% include "wagtailadmin/shared/attrs.html" with attrs=self.attrs %}>
     4 :     {% if self.help_text %}
     5 :         {% help_block status="info" %}{{ self.help_text }}{% endhelp_block %}
     6 :     {% endif %}
     7 :     {% for child, identifier in self.visible_children_with_identifiers %}
     8 :         {% panel id_prefix=self.prefix id=identifier classname=child.classes|join:' ' attrs=child.attrs heading=child.heading heading_size="label" icon=child.icon id_for_label=child.id_for_label is_required=child.is_required %}
     9 :              {% component child %} 
     10 :         {% endpanel %}
     11 :     {% endfor %}
     12 : </div>
     13 : 
  
  Traceback (most recent call last):
    File "/opt/venv/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55, in inner
      response = get_response(request)
                 ^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/core/handlers/base.py", line 220, in _get_response
      response = response.render()
                 ^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/wagtail/admin/auth.py", line 171, in overridden_render
      return render()
             ^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/response.py", line 114, in render
      self.content = self.rendered_content
                     ^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/response.py", line 92, in rendered_content
      return template.render(context, self._request)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/backends/django.py", line 61, in render
      return self.template.render(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 175, in render
      return self._render(context)
             ^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 167, in _render
      return self.nodelist.render(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 1005, in render
      return SafeString("".join([node.render_annotated(context) for node in self]))
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 966, in render_annotated
      return self.render(context)
             ^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/loader_tags.py", line 157, in render
      return compiled_parent._render(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 167, in _render
      return self.nodelist.render(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 1005, in render
      return SafeString("".join([node.render_annotated(context) for node in self]))
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 966, in render_annotated
      return self.render(context)
             ^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/loader_tags.py", line 157, in render
      return compiled_parent._render(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 167, in _render
      return self.nodelist.render(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 1005, in render
      return SafeString("".join([node.render_annotated(context) for node in self]))
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 966, in render_annotated
      return self.render(context)
             ^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/loader_tags.py", line 157, in render
      return compiled_parent._render(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 167, in _render
      return self.nodelist.render(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 1005, in render
      return SafeString("".join([node.render_annotated(context) for node in self]))
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 966, in render_annotated
      return self.render(context)
             ^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/loader_tags.py", line 157, in render
      return compiled_parent._render(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 167, in _render
      return self.nodelist.render(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 1005, in render
      return SafeString("".join([node.render_annotated(context) for node in self]))
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 966, in render_annotated
      return self.render(context)
             ^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/loader_tags.py", line 63, in render
      result = block.nodelist.render(context)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 1005, in render
      return SafeString("".join([node.render_annotated(context) for node in self]))
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 966, in render_annotated
      return self.render(context)
             ^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/loader_tags.py", line 63, in render
      result = block.nodelist.render(context)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 1005, in render
      return SafeString("".join([node.render_annotated(context) for node in self]))
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 966, in render_annotated
      return self.render(context)
             ^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/loader_tags.py", line 63, in render
      result = block.nodelist.render(context)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 1005, in render
      return SafeString("".join([node.render_annotated(context) for node in self]))
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 966, in render_annotated
      return self.render(context)
             ^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 1064, in render
      output = self.filter_expression.resolve(context)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 715, in resolve
      obj = self.var.resolve(context)
            ^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 847, in resolve
      value = self._resolve_lookup(context)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 914, in _resolve_lookup
      current = current()
                ^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/wagtail/admin/panels/base.py", line 317, in render_form_content
      return mark_safe(self.render_html() + self.render_missing_fields())
                       ^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/laces/components.py", line 55, in render_html
      return template.render(context_data)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/backends/django.py", line 61, in render
      return self.template.render(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 175, in render
      return self._render(context)
             ^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 167, in _render
      return self.nodelist.render(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 1005, in render
      return SafeString("".join([node.render_annotated(context) for node in self]))
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 966, in render_annotated
      return self.render(context)
             ^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/defaulttags.py", line 238, in render
      nodelist.append(node.render_annotated(context))
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 966, in render_annotated
      return self.render(context)
             ^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/defaulttags.py", line 321, in render
      return nodelist.render(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 1005, in render
      return SafeString("".join([node.render_annotated(context) for node in self]))
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 966, in render_annotated
      return self.render(context)
             ^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 1064, in render
      output = self.filter_expression.resolve(context)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 715, in resolve
      obj = self.var.resolve(context)
            ^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 847, in resolve
      value = self._resolve_lookup(context)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 914, in _resolve_lookup
      current = current()
                ^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/laces/components.py", line 55, in render_html
      return template.render(context_data)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/backends/django.py", line 61, in render
      return self.template.render(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 175, in render
      return self._render(context)
             ^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 167, in _render
      return self.nodelist.render(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 1005, in render
      return SafeString("".join([node.render_annotated(context) for node in self]))
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 966, in render_annotated
      return self.render(context)
             ^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/defaulttags.py", line 238, in render
      nodelist.append(node.render_annotated(context))
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 966, in render_annotated
      return self.render(context)
             ^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/wagtail/admin/templatetags/wagtailadmin_tags.py", line 1070, in render
      children = self.nodelist.render(context) if self.nodelist else ""
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 1005, in render
      return SafeString("".join([node.render_annotated(context) for node in self]))
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/template/base.py", line 966, in render_annotated
      return self.render(context)
             ^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/laces/templatetags/laces.py", line 81, in render
      html = component.render_html(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/laces/components.py", line 50, in render_html
      context_data = self.get_context_data(parent_context)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/wagtail/admin/panels/field_panel.py", line 273, in get_context_data
      context.update(self.get_editable_context_data())
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/wagtail/admin/panels/field_panel.py", line 315, in get_editable_context_data
      rendered_field = self.bound_field.as_widget(attrs=widget_attrs)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/forms/boundfield.py", line 107, in as_widget
      return widget.render(
             
    File "/opt/venv/lib/python3.12/site-packages/django/forms/widgets.py", line 280, in render
      context = self.get_context(name, value, attrs)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/wagtail/admin/rich_text/editors/draftail/__init__.py", line 72, in get_context
      context = super().get_context(name, value, attrs)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/forms/widgets.py", line 333, in get_context
      context = super().get_context(name, value, attrs)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/django/forms/widgets.py", line 272, in get_context
      "value": self.format_value(value),
               ^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/wagtail/admin/rich_text/editors/draftail/__init__.py", line 69, in format_value
      return self.converter.from_database_format(value)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/wagtail/admin/rich_text/converters/contentstate.py", line 141, in from_database_format
      self.html_to_contentstate_handler.feed(html)
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.12/html/parser.py", line 111, in feed
      self.goahead(0)
      ^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.12/html/parser.py", line 173, in goahead
      k = self.parse_endtag(i)
          ^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.12/html/parser.py", line 414, in parse_endtag
      self.handle_endtag(elem)
      ^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/wagtail/admin/rich_text/converters/html_to_contentstate.py", line 396, in handle_endtag
      element_handler.handle_endtag(name, self.state, self.contentstate)
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/venv/lib/python3.12/site-packages/wagtail/admin/rich_text/converters/html_to_contentstate.py", line 125, in handle_endtag
      not state.current_inline_styles
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  
  Exception Type: AssertionError at /cms/pages/266/edit/
  Exception Value: End of block reached without closing inline style elements
  ```
  
  Any other relevant information. For example, why do you consider this a bug and what did you expect to happen instead?
  
  I would have expected that the error would be captured and reported back to the end user in the CMS edit interface near the field(s) that had the problem data.  
  
  Desirable features might include a report of the offending HTML so the field's contents can be amended manually, but I appreciate this is an edge use case.   However, better error handling would be essential.   Thanks.
  
  -   I have confirmed that this issue can be reproduced as described on a fresh Wagtail project: (no)
  
  ### Technical details
  
  - Django Version: 4.2.15
  - Python Version: 3.12.4
  - wagtail 6.2
  - Browser version: Chrome 127
  
  ### Working on this
  
  Requires Python & Wagtail/Django knowledge of exception handling, may require some Wagtail admin template skills to ensure error message is relayed correctly to the end user.
  
  Anyone can contribute to this. View our [contributing guidelines](https://docs.wagtail.org/en/latest/contributing/index.html), add a comment to the issue once you’re ready to start.
  