depage-forms v1.4.1
html forms made easy
Loading...
Searching...
No Matches
Richtext.php
Go to the documentation of this file.
1<?php
2
10
11namespace Depage\HtmlForm\Elements;
12
38class Richtext extends Textarea
39{
43 public $stylesheet = null;
44
48 public $allowedTags = [];
49
53 public $autogrow = true;
54
64 protected function setDefaults(): void
65 {
66 parent::setDefaults();
67
68 $this->defaults['rows'] = null;
69 $this->defaults['cols'] = null;
70 $this->defaults['stylesheet'] = null;
71 $this->defaults['autogrow'] = true;
72 $this->defaults['allowedTags'] = [
73 // inline elements
74 "a",
75 "b",
76 "strong",
77 "i",
78 "em",
79 "small",
80
81 // block elements
82 "p",
83 "br",
84 "h1",
85 "h2",
86 "ul",
87 "ol",
88 "li",
89 ];
90 }
91
95 protected function htmlDataAttributes(): string
96 {
97 $options = [];
98 $options['stylesheet'] = $this->stylesheet;
99 $options['allowedTags'] = $this->allowedTags;
100
101 $this->dataAttr['richtext-options'] = json_encode($options);
102
103 return parent::htmlDataAttributes();
104 }
105
114 public function isEmpty(): bool
115 {
116 return empty(trim(strip_tags($this->value)));
117 }
118
124 protected function htmlValue(): string
125 {
126 if ($this->value === null) {
127 $htmlDOM = $this->parseHtml($this->defaultValue);
128 } else {
129 $htmlDOM = $this->value;
130 }
131
132 $html = (string) $htmlDOM;
133
134 return $this->htmlEscape($html);
135 }
136
141 protected function typeCastValue(): void
142 {
143 if (is_string($this->value)) {
144 $this->value = $this->parseHtml($this->value);
145 }
146 }
147
154 protected function parseHtml(string $html): \Depage\HtmlForm\Abstracts\HtmlDom
155 {
156 if ($this->normalize && class_exists("\\Normalizer")) {
157 $html = \Normalizer::normalize($html);
158 }
159
160 $htmlDOM = new \Depage\HtmlForm\Abstracts\HtmlDom();
161
162 $htmlDOM->loadHTML($html);
163 $htmlDOM->cleanHTML($this->allowedTags);
164
165 if ($this->maxlength) {
166 $htmlDOM->cutToMaxlength($this->maxlength);
167 }
168
169 return $htmlDOM;
170 }
171}
172
173/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
htmlEscape(array|string $options=[])
Escapes HTML in strings and arrays of strings.
Definition Element.php:261
DOMDocument for html-content.
Definition HtmlDom.php:19
$value
Input elements's value.
Definition Input.php:124
HTML richtext element.
Definition Richtext.php:39
parseHtml(string $html)
Parses html-string into htmlDOM.
Definition Richtext.php:154
typeCastValue()
Converts value into htmlDOM.
Definition Richtext.php:141
htmlDataAttributes()
Returns dataAttr escaped as attribute string.
Definition Richtext.php:95
htmlValue()
Returns HTML-rendered element value.
Definition Richtext.php:124
setDefaults()
collects initial values across subclasses
Definition Richtext.php:64
isEmpty()
says wether the element value is empty
Definition Richtext.php:114
HTML textarea element.
Definition Textarea.php:36
main interface to users
Definition HtmlForm.php:124
Abstract element classes.
Definition Container.php:11
depage cms