Skip to content

Commit

Permalink
Issue #3336616 by Harlor, wengerk: CKEditor5 replaces nbsp with white…
Browse files Browse the repository at this point in the history
…spaces (#33)
  • Loading branch information
WengerK authored Feb 20, 2023
1 parent 7fcfa70 commit 9772d4b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- fix issue #3336616 by Harlor, wengerk: CKEditor5 replaces nbsp with whitespaces

## [3.0.0] - 2022-12-05
### Removed
Expand Down
17 changes: 11 additions & 6 deletions src/Plugin/Filter/NbspCleanerFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
*/
class NbspCleanerFilter extends FilterBase {

/**
* The nbsp character in UFT-8.
*/
const UTF_8_NBSP = "\xc2\xa0";

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -53,15 +58,15 @@ protected function swapNbspHtml($text) {
$xpath = new \DOMXPath($document);

foreach ($xpath->query('//span[@class="nbsp"]') as $node) {
if (!empty($node) && !empty($node->nodeValue)) {
// PHP DOM removing the tag (not content)
$node->parentNode->replaceChild(new \DOMText($node->nodeValue), $node);
if (!empty($node)) {
// PHP DOM replacing the nbsp-span with nbsp character.
$node->parentNode->replaceChild(new \DOMText(self::UTF_8_NBSP), $node);
}
}
foreach ($xpath->query('//nbsp') as $node) {
if (!empty($node) && !empty($node->nodeValue)) {
// PHP DOM removing the tag (not content)
$node->parentNode->replaceChild(new \DOMText($node->nodeValue), $node);
if (!empty($node)) {
// PHP DOM replacing the nbsp-tag with nbsp character.
$node->parentNode->replaceChild(new \DOMText(self::UTF_8_NBSP), $node);
}
}
return Html::serialize($document);
Expand Down

0 comments on commit 9772d4b

Please sign in to comment.