Whitespace HTML entities break DOM parsing in Drupal 10.2

Created on 5 January 2024, 6 months ago
Updated 6 June 2024, 20 days ago

Problem/Motivation

CKEditor does a somewhat expected feature if you add a space to the end of an <h2> tag, it will preserve the space by converting it to &nbsp;. This is true for both Drupal 10.1 and 10.2.

The difference that is problematic for TOC_api is that that whitespace was previously converted to a space character in Drupal 10.1 is now left alone as a &nbsp; in 10.2.

The result toc_api/src/TocBuilder.php:90

Warning: DOMDocumentFragment::appendXML(): Entity: line 2: parser error : Entity 'nbsp' not defined in Drupal\toc_api\TocBuilder->renderContent() (line 90 of modules/contrib/toc_api/src/TocBuilder.php).

Drupal\toc_api\TocBuilder->renderContent(Object) (Line: 176)
Drupal\toc_filter\Plugin\Filter\TocFilter->process('TEXT HERE', 'en') (Line: 118)
Drupal\filter\Element\ProcessedText::preRenderText(Array)
call_user_func_array(Array, Array) (Line: 111)
Drupal\Core\Render\Renderer->doTrustedCallback(Array, Array, 'Render #pre_render callbacks must be methods of a class that implements \Drupal\Core\Security\TrustedCallbackInterface or be an anonymous function. The callback was %s. See https://www.drupal.org/node/2966725', 'exception', 'Drupal\Core\Render\Element\RenderCallbackInterface') (Line: 858)
Drupal\Core\Render\Renderer->doCallback('#pre_render', Array, Array) (Line: 421)
Drupal\Core\Render\Renderer->doRender(Array, 1) (Line: 240)
Drupal\Core\Render\Renderer->render(Array, 1) (Line: 165)
Drupal\Core\Render\Renderer->Drupal\Core\Render\{closure}() (Line: 627)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 166)
Drupal\Core\Render\Renderer->renderPlain(Array) (Line: 62)
Drupal\text\TextProcessed->getValue() (Line: 154)
Drupal\Core\Field\FieldItemBase->__get('processed') (Line: 143)
node_tokens('node', Array, Array, Array, Object)
call_user_func_array(Object, Array) (Line: 409)
Drupal\Core\Extension\ModuleHandler->Drupal\Core\Extension\{closure}(Object, 'node') (Line: 388)
Drupal\Core\Extension\ModuleHandler->invokeAllWith('tokens', Object) (Line: 416)
Drupal\Core\Extension\ModuleHandler->invokeAll('tokens', Array) (Line: 364)
Drupal\Core\Utility\Token->generate('node', Array, Array, Array, Object) (Line: 241)
Drupal\Core\Utility\Token->doReplace(1, '[node:summary]', Array, Array, Object) (Line: 191)
Drupal\Core\Utility\Token->replace('[node:summary]', Array, Array, NULL) (Line: 66)
Drupal\metatag\MetatagToken->replace('[node:summary]', Array, Array) (Line: 789)
Drupal\metatag\MetatagManager->processTagValue(Object, Array, Array, , 'en') (Line: 628)
Drupal\metatag\MetatagManager->generateRawElements(Array, Object) (Line: 61)
Drupal\metatag\Plugin\Field\MetatagEntityFieldItemList::Drupal\metatag\Plugin\Field\{closure}() (Line: 627)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 62)
Drupal\metatag\Plugin\Field\MetatagEntityFieldItemList->computeValue() (Line: 32)
Drupal\metatag\Plugin\Field\MetatagEntityFieldItemList->ensureComputedValue() (Line: 114)
Drupal\metatag\Plugin\Field\MetatagEntityFieldItemList->isEmpty() (Line: 162)
Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validateNode(Object) (Line: 164)
Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validateNode(Object, Array, 1) (Line: 106)
Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validate(Object, NULL, NULL) (Line: 93)
Drupal\Core\TypedData\Validation\RecursiveValidator->validate(Object) (Line: 132)
Drupal\Core\TypedData\TypedData->validate() (Line: 518)
Drupal\Core\Entity\ContentEntityBase->validate() (Line: 188)
Drupal\Core\Entity\ContentEntityForm->validateForm(Array, Object)
call_user_func_array(Array, Array) (Line: 82)
Drupal\Core\Form\FormValidator->executeValidateHandlers(Array, Object) (Line: 275)
Drupal\Core\Form\FormValidator->doValidateForm(Array, Object, 'node_page_edit_form') (Line: 118)
Drupal\Core\Form\FormValidator->validateForm('node_page_edit_form', Array, Object) (Line: 593)
Drupal\Core\Form\FormBuilder->processForm('node_page_edit_form', Array, Object) (Line: 325)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 73)
Drupal\Core\Controller\FormController->getContentResult(Object, Object) (Line: 39)
Drupal\layout_builder\Controller\LayoutBuilderHtmlEntityFormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 627)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 181)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 76)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 36)
Drupal\Core\StackMiddleware\AjaxPageState->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 704)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Proposed resolution

Options:

  1. Strip it out again at the line again
  2. Sort out why the DOM parser has an issue with appending &nbsp; HTML entities
πŸ› Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

πŸ‡¨πŸ‡¦Canada joelpittet Vancouver

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.69.0 2024