Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
- Unset removeEmptyTags, removeInlineStyles, and removeNbsp settings from stored configs
- Drop XHTML formatting from all self-closing tags, not just <br>
- Maintain &nbsp; entities

Fixes #90
  • Loading branch information
brandonkelly committed Apr 29, 2023
1 parent 4693a7f commit 6404f0e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Unreleased

- Link dropdowns now include an “Insert link” option. ([#78](https://github.com/craftcms/ckeditor/discussions/78))
- Fixed a bug where fields with `<br>` tags would always appear dirty. ([#85](https://github.com/craftcms/ckeditor/issues/85))
- Fixed a bug where fields with `<br>` tags and non-breaking spaces would get marked as dirty without making any changes. ([#85](https://github.com/craftcms/ckeditor/issues/85), [#90](https://github.com/craftcms/ckeditor/issues/90))

## 3.2.1 - 2023-04-23

Expand Down
47 changes: 41 additions & 6 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
use craft\elements\Asset;
use craft\elements\Category;
use craft\elements\Entry;
use craft\helpers\ArrayHelper;
use craft\helpers\Html;
use craft\helpers\Json;
use craft\htmlfield\events\ModifyPurifierConfigEvent;
use craft\htmlfield\HtmlField;
use craft\htmlfield\HtmlFieldData;
use craft\models\CategoryGroup;
use craft\models\ImageTransform;
use craft\models\Section;
Expand Down Expand Up @@ -120,7 +120,13 @@ public static function displayName(): string
*/
public function __construct($config = [])
{
ArrayHelper::remove($config, 'initJs');
unset(
$config['initJs'],
$config['removeInlineStyles'],
$config['removeEmptyTags'],
$config['removeNbsp'],
);

parent::__construct($config);
}

Expand Down Expand Up @@ -163,6 +169,23 @@ public function getSettingsHtml(): ?string
]);
}

/**
* @inheritdoc
*/
public function getSettings(): array
{
$settings = parent::getSettings();

// Cleanup
unset(
$settings['removeInlineStyles'],
$settings['removeEmptyTags'],
$settings['removeNbsp'],
);

return $settings;
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -349,10 +372,22 @@ protected function purifierConfig(): HTMLPurifier_Config
*/
protected function prepValueForInput($value, ?ElementInterface $element): string
{
// This is needed because CKEditor5 insists on <br>
// and HTMLPurifier prefers to go with <br />
// That clash causes this issue: https://github.com/craftcms/cms/issues/13112
$value = str_replace(['<br />', '<br/>'], '<br>', $value);
if ($value instanceof HtmlFieldData) {
$value = $value->getRawContent();
}

if ($value !== null) {
// Replace NBSP chars with entities, and remove XHTML formatting from self-closing HTML elements,
// so CKEditor doesn’t need to normalize them and cause the input value to change
// (https://github.com/craftcms/cms/issues/13112)
$pairs = [
' ' => '&nbsp;',
];
foreach (array_keys(Html::$voidElements) as $tag) {
$pairs["<$tag />"] = "<$tag>";
}
$value = strtr($value, $pairs);
}

return parent::prepValueForInput($value, $element);
}
Expand Down
3 changes: 3 additions & 0 deletions src/console/controllers/ConvertController.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ public function actionRedactor(): int
$field['settings']['configSelectionMode'],
$field['settings']['manualConfig'],
$field['settings']['redactorConfig'],
$field['settings']['removeEmptyTags'],
$field['settings']['removeInlineStyles'],
$field['settings']['removeNbsp'],
$field['settings']['showHtmlButtonForNonAdmins'],
$field['settings']['uiMode'],
);
Expand Down

0 comments on commit 6404f0e

Please sign in to comment.