1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-12 01:34:31 +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);
$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
// so we determine if it's necessary to decode them here
if($inputfield instanceof InputfieldTextarea) {
if(wireInstanceOf($inputfield, 'InputfieldCKEditor')) {
$decode = false;
// ok
} else if(wireInstanceOf($inputfield, 'InputfieldTinyMCE')) {
$postName = "Inputfield_$name";
$decode = false;
} else if($field->get('contentType') >= FieldtypeTextarea::contentTypeHTML) {
$decode = false;
// ok
} 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) {
$decode = true;
} else {
$decode = false;
$decodeAndStrip = true;
}
if($decode) {
if($decodeAndStrip) {
$value = $sanitizer->unentities($value);
$value = strip_tags($value);
}
$input->post->$postName = $value;
@@ -1294,6 +1298,7 @@ class PageFrontEdit extends WireData implements Module {
if(is_object($unformatted)) $unformatted = (string) $unformatted;
$purifyHTML = true;
$addBreaks = false;
if($field && $field->type instanceof FieldtypeTextarea) {
$contentType = (int) $field->get('contentType');
@@ -1301,7 +1306,11 @@ class PageFrontEdit extends WireData implements Module {
if($cls === 'InputfieldCKEditor' || $cls === 'InputfieldTinyMCE' || $contentType == 1 || $contentType == 2) {
// HTML is expected and allowed
$purifyHTML = false;
}
} else if($cls === 'InputfieldTextarea') {
$textformatters = $field->get('textformatters');
if(in_array('TextformatterNewlineBR', $textformatters)) $addBreaks = true;
if(in_array('TextformatterMarkdownExtra', $textformatters)) $addBreaks = true;
}
}
if(is_string($unformatted) && $purifyHTML && (strpos($unformatted, '<') !== false || strpos($unformatted, '&') !== false)) {
@@ -1310,6 +1319,8 @@ class PageFrontEdit extends WireData implements Module {
$unformatted = $this->wire()->sanitizer->purify(trim($unformatted));
}
if($addBreaks) $unformatted = nl2br($unformatted);
return $unformatted;
}
}