summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/html/src/BaseHtmlElement.php
blob: 5dc01ce74ca99a5be29363a33531a2b943fa9e49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<?php

namespace ipl\Html;

use InvalidArgumentException;
use RuntimeException;

/**
 * Base class for HTML elements
 *
 * Extend this class in order to provide concrete HTML elements or series of HTML elements, e.g. widgets.
 * When extending this class you should provide the element's tag with {@link $tag}. Setting default attributes is
 * possible via {@link $defaultAttributes}. And the content of the element is provided in {@link assemble()}.
 *
 * # Example Usage
 * ```
 * namespace Acme\Widgets;
 *
 * use ipl\Html\BaseHtmlElement;
 *
 * class Dashboard extends BaseHtmlElement
 * {
 *     protected $defaultAttributes = ['class' => 'acme-dashboard'];
 *
 *     protected $tag = 'div';
 *
 *     protected function assemble()
 *     {
 *         // ...
 *         $this->add($content);
 *     }
 * }
 * ```
 */
abstract class BaseHtmlElement extends HtmlDocument
{
    /**
     * List of void elements which must not contain end tags or content
     *
     * If {@link $isVoid} is null, this property should be used to decide whether the content and end tag has to be
     * rendered.
     *
     * @var array
     *
     * @see https://www.w3.org/TR/html5/syntax.html#void-elements
     */
    protected static $voidElements = [
        'area'   => 1,
        'base'   => 1,
        'br'     => 1,
        'col'    => 1,
        'embed'  => 1,
        'hr'     => 1,
        'img'    => 1,
        'input'  => 1,
        'link'   => 1,
        'meta'   => 1,
        'param'  => 1,
        'source' => 1,
        'track'  => 1,
        'wbr'    => 1
    ];

    /** @var Attributes */
    protected $attributes;

    /** @var bool Whether possible attribute callbacks have been registered */
    protected $attributeCallbacksRegistered = false;

    /** @var bool|null Whether the element is void. If null, void check should use {@link $voidElements} */
    protected $isVoid;

    /** @var array<string, mixed> You may want to set default attributes when extending this class */
    protected $defaultAttributes;

    /** @var string Tag of element. Set this property in order to provide the element's tag when extending this class */
    protected $tag;

    /**
     * Get the attributes of the element
     *
     * @return Attributes
     */
    public function getAttributes()
    {
        if ($this->attributes === null) {
            $default = $this->getDefaultAttributes();
            if (empty($default)) {
                $this->attributes = new Attributes();
            } else {
                $this->attributes = Attributes::wantAttributes($default);
            }

            $this->ensureAttributeCallbacksRegistered();
        }

        return $this->attributes;
    }

    /**
     * Set the attributes of the element
     *
     * @param Attributes|array|null $attributes
     *
     * @return $this
     */
    public function setAttributes($attributes)
    {
        $this->attributes = Attributes::wantAttributes($attributes);

        $this->attributeCallbacksRegistered = false;
        $this->ensureAttributeCallbacksRegistered();

        return $this;
    }

    /**
     * Return true if the attribute with the given name exists, false otherwise
     *
     * @param string $name
     *
     * @return bool
     */
    public function hasAttribute(string $name): bool
    {
        return $this->getAttributes()->has($name);
    }

    /**
     * Get the attribute with the given name
     *
     * If the attribute does not already exist, an empty one is automatically created and added to the attributes.
     *
     * @param string $name
     *
     * @return Attribute
     *
     * @throws InvalidArgumentException If the attribute does not yet exist and its name contains special characters
     */
    public function getAttribute(string $name): Attribute
    {
        return $this->getAttributes()->get($name);
    }

    /**
     * Set the attribute with the given name and value
     *
     * If the attribute with the given name already exists, it gets overridden.
     *
     * @param string            $name  The name of the attribute
     * @param string|bool|array $value The value of the attribute
     *
     * @return $this
     */
    public function setAttribute($name, $value)
    {
        $this->getAttributes()->set($name, $value);

        return $this;
    }

    /**
     * Remove the attribute with the given name or remove the given value from the attribute
     *
     * @param string $name  The name of the attribute
     * @param null|string|array $value The value to remove if specified
     *
     * @return ?Attribute The removed or changed attribute, if any, otherwise null
     */
    public function removeAttribute(string $name, $value = null): ?Attribute
    {
        return $this->getAttributes()->remove($name, $value);
    }

    /**
     * Add the given attributes
     *
     * @param Attributes|array $attributes
     *
     * @return $this
     */
    public function addAttributes($attributes)
    {
        $this->getAttributes()->add($attributes);

        return $this;
    }

    /**
     * Get the default attributes of the element
     *
     * @return array
     */
    public function getDefaultAttributes()
    {
        return $this->defaultAttributes;
    }

    /**
     * Get the tag of the element
     *
     * Since HTML Elements must have a tag, this method throws an exception if the element does not have a tag.
     *
     * @return string
     *
     * @throws RuntimeException If the element does not have a tag
     */
    final public function getTag()
    {
        $tag = $this->tag();

        if (! $tag) {
            throw new RuntimeException('Element must have a tag');
        }

        return $tag;
    }

    /**
     * Set the tag of the element
     *
     * @param string $tag
     *
     * @return $this
     */
    public function setTag($tag)
    {
        $this->tag = $tag;

        return $this;
    }

    /**
     * Get whether the element is void
     *
     * The default void detection which checks whether the element's tag is in the list of void elements according to
     * https://www.w3.org/TR/html5/syntax.html#void-elements.
     *
     * If you want to override this behavior, use {@link setVoid()}.
     *
     * @return bool
     */
    public function isVoid()
    {
        if ($this->isVoid !== null) {
            return $this->isVoid;
        }

        $tag = $this->getTag();

        return isset(self::$voidElements[$tag]);
    }

    /**
     * Set whether the element is void
     *
     * You may use this method to override the default void detection which checks whether the element's tag is in the
     * list of void elements according to https://www.w3.org/TR/html5/syntax.html#void-elements.
     *
     * If you specify null, void detection is reset to its default behavior.
     *
     * @param bool|null $void
     *
     * @return $this
     */
    public function setVoid($void = true)
    {
        $this->isVoid = $void === null ?: (bool) $void;

        return $this;
    }

    /**
     * Ensure that possible attribute callbacks have been registered
     *
     * Note that this method is automatically called in {@link getAttributes()} and {@link setAttributes()}.
     *
     * @return $this
     */
    public function ensureAttributeCallbacksRegistered()
    {
        if (! $this->attributeCallbacksRegistered) {
            $this->attributeCallbacksRegistered = true;
            $this->registerAttributeCallbacks($this->attributes);
        }

        return $this;
    }

    /**
     * Render the content of the element to HTML
     *
     * @return string
     */
    public function renderContent()
    {
        return parent::renderUnwrapped();
    }

    /**
     * Get whether the closing tag should be rendered
     *
     * @return bool True for void elements, false otherwise
     */
    public function wantsClosingTag()
    {
        // TODO: There is more. SVG and MathML namespaces
        return ! $this->isVoid();
    }

    /**
     * Use this element to wrap the given document
     *
     * @param HtmlDocument $document
     *
     * @return $this
     */
    public function wrap(HtmlDocument $document)
    {
        $document->addWrapper($this);

        return $this;
    }

    /**
     * Internal method for accessing the tag
     *
     * You may override this method in order to provide the tag dynamically
     *
     * @return string
     */
    protected function tag()
    {
        return $this->tag;
    }

    /**
     * Register attribute callbacks
     *
     * Override this method in order to register attribute callbacks in concrete classes.
     */
    protected function registerAttributeCallbacks(Attributes $attributes)
    {
    }

    public function addHtml(ValidHtml ...$content)
    {
        $this->ensureAssembled();

        parent::addHtml(...$content);

        return $this;
    }

    /**
     * @inheritdoc
     *
     * @throws RuntimeException If the element does not have a tag or is void but has content
     */
    public function renderUnwrapped()
    {
        $this->ensureAssembled();

        $tag = $this->getTag();
        $content = $this->renderContent();
        $attributes = $this->getAttributes()->render();

        if (strlen($this->contentSeparator)) {
            $length = strlen($content);
            if ($length > 0) {
                if ($content[0] === '<') {
                    $content = $this->contentSeparator . $content;
                    $length++;
                }
                if ($content[$length - 1] === '>') {
                    $content .= $this->contentSeparator;
                }
            }
        }

        if (! $this->wantsClosingTag()) {
            if (strlen($content)) {
                throw new RuntimeException('Void elements must not have content');
            }

            return sprintf('<%s%s />', $tag, $attributes);
        }

        return sprintf(
            '<%s%s>%s</%s>',
            $tag,
            $attributes,
            $content,
            $tag
        );
    }

    public function __clone()
    {
        parent::__clone();

        if ($this->attributes !== null) {
            $this->attributes = clone $this->attributes;
        }
    }
}