1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-16 03:34:33 +02:00

Fix issue ryancramerdesign/InputfieldTinyMCE#20 TinyMCE when used with blank object properties in custom JSON settings

This commit is contained in:
Ryan Cramer
2023-06-13 11:39:36 -04:00
parent dfcbb10f9d
commit c8c5b46e77
2 changed files with 50 additions and 6 deletions

View File

@@ -476,6 +476,12 @@ class InputfieldTinyMCESettings extends InputfieldTinyMCEClass {
} }
*/ */
// ensure blank object properties resolve to {} in JSON rather than []
foreach($this->tools()->jsonBlankObjectProperties as $name) {
if(!isset($settings[$name]) || !empty($settings[$name]) || !is_array($settings[$name])) continue;
$settings[$name] = (object) $settings[$name];
}
return $settings; return $settings;
} }
@@ -755,7 +761,7 @@ class InputfieldTinyMCESettings extends InputfieldTinyMCEClass {
if($inputfield->lazyMode) $features[] = "lazyMode$inputfield->lazyMode"; if($inputfield->lazyMode) $features[] = "lazyMode$inputfield->lazyMode";
$inputfield->wrapAttr('data-configName', $configName); $inputfield->wrapAttr('data-configName', $configName);
$inputfield->wrapAttr('data-settings', json_encode($dataSettings)); $inputfield->wrapAttr('data-settings', $this->tools()->jsonEncode($dataSettings, 'data-settings', false));
$inputfield->wrapAttr('data-features', implode(',', $features)); $inputfield->wrapAttr('data-features', implode(',', $features));
} }

View File

@@ -5,9 +5,11 @@
* *
* Helper tools for InputfieldTinyMCE module. * Helper tools for InputfieldTinyMCE module.
* *
* ProcessWire 3.x, Copyright 2022 by Ryan Cramer * ProcessWire 3.x, Copyright 2023 by Ryan Cramer
* https://processwire.com * https://processwire.com
* *
* @property array $jsonBlankObjectProperties
*
*/ */
class InputfieldTinyMCETools extends InputfieldTinyMCEClass { class InputfieldTinyMCETools extends InputfieldTinyMCEClass {
@@ -33,6 +35,14 @@ class InputfieldTinyMCETools extends InputfieldTinyMCEClass {
*/ */
static protected $purifier = null; static protected $purifier = null;
/**
* Properties found in decoded JSON that were blank objects and should remain when encoded
*
* @var array
*
*/
protected $jsonBlankObjectProperties = array();
/** /**
* Sanitize toolbar or plugin names * Sanitize toolbar or plugin names
* *
@@ -245,6 +255,13 @@ class InputfieldTinyMCETools extends InputfieldTinyMCEClass {
$propertyName, json_last_error_msg() $propertyName, json_last_error_msg()
)); ));
$a = array(); $a = array();
} else if(strpos($json, '{}') !== false) {
if(preg_match_all('/"([_a-z0-9]+)":\s*[{][}]/i', $json, $matches)) {
foreach($matches[1] as $name) {
$this->jsonBlankObjectProperties[$name] = $name;
}
}
} }
return $a; return $a;
} }
@@ -275,12 +292,17 @@ class InputfieldTinyMCETools extends InputfieldTinyMCEClass {
* *
* @param array $a * @param array $a
* @param string $propertyName Name of property JSON is for * @param string $propertyName Name of property JSON is for
* @param bool $pretty
* @return string * @return string
* *
*/ */
public function jsonEncode($a, $propertyName) { public function jsonEncode($a, $propertyName, $pretty = true) {
if(!is_array($a)) return ''; if(!is_array($a)) return '';
$json = json_encode($a, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); if($pretty) {
$json = json_encode($a, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
} else {
$json = json_encode($a);
}
if($json === false) { if($json === false) {
$this->warning(sprintf( $this->warning(sprintf(
$this->_('Error encoding JSON for TinyMCE property "%1$s" - %2$s'), $this->_('Error encoding JSON for TinyMCE property "%1$s" - %2$s'),
@@ -288,6 +310,12 @@ class InputfieldTinyMCETools extends InputfieldTinyMCEClass {
)); ));
$json = ''; $json = '';
} }
if(count($this->jsonBlankObjectProperties) && strpos($json, '[]') !== false) {
// convert JSON arrays [] to objects {}
foreach($this->jsonBlankObjectProperties as $name) {
$json = str_replace(array("\"$name\": []", "\"$name\":[]"), "\"$name\": {}", $json);
}
}
return (string) $json; return (string) $json;
} }
@@ -380,4 +408,14 @@ class InputfieldTinyMCETools extends InputfieldTinyMCEClass {
} }
*/ */
/**
* @param string $name
* @return array|mixed|string|null
*
*/
public function __get($name) {
if($name === 'jsonBlankObjectProperties') return $this->jsonBlankObjectProperties;
return parent::__get($name);
}
} }