1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-12 09:44:38 +02:00

Update PageFrontEdit to behave better with InputfieldTextarea fields that do not support HTML.

This commit is contained in:
Ryan Cramer
2023-11-15 11:58:55 -05:00
parent 8343fd2365
commit adac6a1e30

View File

@@ -1170,27 +1170,31 @@ class PageFrontEdit extends WireData implements Module {
$inputfield = $this->getInputfield($page, $field); $inputfield = $this->getInputfield($page, $field);
$postName = $name; $postName = $name;
// decode entities and strip tags?
$decodeAndStrip = false;
// jQuery HTML function entity encodes things like & to & even if they don't appear in the source // jQuery HTML function entity encodes things like & to & even if they don't appear in the source
// so we determine if it's necessary to decode them here // so we determine if it's necessary to decode them here
if($inputfield instanceof InputfieldTextarea) { if($inputfield instanceof InputfieldTextarea) {
if(wireInstanceOf($inputfield, 'InputfieldCKEditor')) { if(wireInstanceOf($inputfield, 'InputfieldCKEditor')) {
$decode = false; // ok
} else if(wireInstanceOf($inputfield, 'InputfieldTinyMCE')) { } else if(wireInstanceOf($inputfield, 'InputfieldTinyMCE')) {
$postName = "Inputfield_$name"; $postName = "Inputfield_$name";
$decode = false;
} else if($field->get('contentType') >= FieldtypeTextarea::contentTypeHTML) { } else if($field->get('contentType') >= FieldtypeTextarea::contentTypeHTML) {
$decode = false; // ok
} else { } else {
$decode = true; // convert browser added div and break tags to newlines
$value = str_replace(array('<div><br></div>', '<br>', '<br />'), "\n", $value);
$value = str_replace(array('<div>', '</div>'), array("\n", ""), $value);
$decodeAndStrip = true;
} }
} else if($inputfield instanceof InputfieldText) { } else if($inputfield instanceof InputfieldText) {
$decode = true; $decodeAndStrip = true;
} else {
$decode = false;
} }
if($decode) { if($decodeAndStrip) {
$value = $sanitizer->unentities($value); $value = $sanitizer->unentities($value);
$value = strip_tags($value);
} }
$input->post->$postName = $value; $input->post->$postName = $value;
@@ -1294,6 +1298,7 @@ class PageFrontEdit extends WireData implements Module {
if(is_object($unformatted)) $unformatted = (string) $unformatted; if(is_object($unformatted)) $unformatted = (string) $unformatted;
$purifyHTML = true; $purifyHTML = true;
$addBreaks = false;
if($field && $field->type instanceof FieldtypeTextarea) { if($field && $field->type instanceof FieldtypeTextarea) {
$contentType = (int) $field->get('contentType'); $contentType = (int) $field->get('contentType');
@@ -1301,6 +1306,10 @@ class PageFrontEdit extends WireData implements Module {
if($cls === 'InputfieldCKEditor' || $cls === 'InputfieldTinyMCE' || $contentType == 1 || $contentType == 2) { if($cls === 'InputfieldCKEditor' || $cls === 'InputfieldTinyMCE' || $contentType == 1 || $contentType == 2) {
// HTML is expected and allowed // HTML is expected and allowed
$purifyHTML = false; $purifyHTML = false;
} else if($cls === 'InputfieldTextarea') {
$textformatters = $field->get('textformatters');
if(in_array('TextformatterNewlineBR', $textformatters)) $addBreaks = true;
if(in_array('TextformatterMarkdownExtra', $textformatters)) $addBreaks = true;
} }
} }
@@ -1310,6 +1319,8 @@ class PageFrontEdit extends WireData implements Module {
$unformatted = $this->wire()->sanitizer->purify(trim($unformatted)); $unformatted = $this->wire()->sanitizer->purify(trim($unformatted));
} }
if($addBreaks) $unformatted = nl2br($unformatted);
return $unformatted; return $unformatted;
} }
} }