Richtext.php
Go to the documentation of this file.
1<?php
11
37class Richtext extends Textarea
38{
48 protected function setDefaults()
49 {
50 parent::setDefaults();
51
52 $this->defaults['rows'] = null;
53 $this->defaults['cols'] = null;
54 $this->defaults['stylesheet'] = null;
55 $this->defaults['autogrow'] = true;
56 $this->defaults['allowedTags'] = array(
57 // inline elements
58 "a",
59 "b",
60 "strong",
61 "i",
62 "em",
63 "small",
64
65 // block elements
66 "p",
67 "br",
68 "h1",
69 "h2",
70 "ul",
71 "ol",
72 "li",
73 );
74 }
75
79 protected function htmlDataAttributes()
80 {
81 $options = array();
82 $options['stylesheet'] = $this->stylesheet;
83 $options['allowedTags'] = $this->allowedTags;
84
85 $this->dataAttr['richtext-options'] = json_encode($options);
86
87 return parent::htmlDataAttributes();
88 }
89
98 public function isEmpty()
99 {
100 return empty(trim(strip_tags($this->value)));
101 }
102
108 protected function htmlValue()
109 {
110 if ($this->value === null) {
111 $htmlDOM = $this->parseHtml($this->defaultValue);
112 } else {
113 $htmlDOM = $this->value;
114 }
115
116 $html = "";
117
118 // add content of every node in body
119 foreach ($htmlDOM->documentElement->childNodes as $node) {
120 $html .= $htmlDOM->saveXML($node);
121 }
122
123 return $this->htmlEscape($html);
124 }
130 protected function typeCastValue()
131 {
132 if (is_string($this->value)) {
133 $this->value = $this->parseHtml($this->value);
134 }
135 }
143 protected function parseHtml($html)
144 {
145 if ($this->normalize && class_exists("\\Normalizer")) {
146 $html = \Normalizer::normalize($html);
147 }
148
149 $htmlDOM = new \Depage\HtmlForm\Abstracts\HtmlDom();
150
151 $htmlDOM->loadHTML($html);
152 $htmlDOM->cleanHTML($this->allowedTags);
153
154 if ($this->maxlength) {
155 $htmlDOM->cutToMaxlength($this->maxlength);
156 }
157
158 return $htmlDOM;
159 }
160}
161
162/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
htmlEscape($options=array())
Escapes HTML in strings and arrays of strings.
Definition Element.php:267
$value
Input elements's value.
Definition Input.php:123
HTML richtext element.
Definition Richtext.php:38
parseHtml($html)
Parses html-string into htmlDOM.
Definition Richtext.php:143
typeCastValue()
Converts value into htmlDOM.
Definition Richtext.php:130
htmlDataAttributes()
Returns dataAttr escaped as attribute string.
Definition Richtext.php:79
htmlValue()
Returns HTML-rendered element value.
Definition Richtext.php:108
setDefaults()
collects initial values across subclasses
Definition Richtext.php:48
isEmpty()
says wether the element value is empty
Definition Richtext.php:98
HTML textarea element.
Definition Textarea.php:35
Classes for HTML input-elements.
Definition Address.php:9