Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Ganze Datei anzeigen | Leerzeichen ignorieren | Details | Blame | Letzte Änderung | Log anzeigen | RSS feed

Revision 148 Revision 399
Zeile 236... Zeile 236...
236
	use Nette\SmartObject;
236
	use Nette\SmartObject;
Zeile 237... Zeile 237...
237
 
237
 
238
	/** @var array<string, mixed>  element's attributes */
238
	/** @var array<string, mixed>  element's attributes */
Zeile 239... Zeile -...
239
	public $attrs = [];
-
 
240
 
-
 
241
	/** @var bool  use XHTML syntax? */
-
 
242
	public static $xhtml = false;
239
	public $attrs = [];
243
 
240
 
244
	/** @var array<string, int>  void elements */
241
	/** void elements */
245
	public static $emptyElements = [
242
	public static $emptyElements = [
246
		'img' => 1, 'hr' => 1, 'br' => 1, 'input' => 1, 'meta' => 1, 'area' => 1, 'embed' => 1, 'keygen' => 1,
243
		'img' => 1, 'hr' => 1, 'br' => 1, 'input' => 1, 'meta' => 1, 'area' => 1, 'embed' => 1, 'keygen' => 1,
247
		'source' => 1, 'base' => 1, 'col' => 1, 'link' => 1, 'param' => 1, 'basefont' => 1, 'frame' => 1,
244
		'source' => 1, 'base' => 1, 'col' => 1, 'link' => 1, 'param' => 1, 'basefont' => 1, 'frame' => 1,
Zeile 248... Zeile 245...
248
		'isindex' => 1, 'wbr' => 1, 'command' => 1, 'track' => 1,
245
		'isindex' => 1, 'wbr' => 1, 'command' => 1, 'track' => 1,
249
	];
246
	];
Zeile 250... Zeile 247...
250
 
247
 
251
	/** @var array<int, HtmlStringable|string> nodes */
248
	/** @var array<int, HtmlStringable|string> nodes */
Zeile 252... Zeile -...
252
	protected $children = [];
-
 
253
 
249
	protected $children = [];
Zeile 254... Zeile 250...
254
	/** @var string  element's name */
250
 
255
	private $name;
251
	/** element's name */
256
 
252
	private string $name = '';
257
	/** @var bool  is element empty? */
-
 
258
	private $isEmpty;
253
 
259
 
254
	private bool $isEmpty = false;
260
 
255
 
261
	/**
256
 
262
	 * Constructs new HTML element.
257
	/**
263
	 * @param  array|string $attrs element's attributes or plain text content
258
	 * Constructs new HTML element.
Zeile 287... Zeile 282...
287
 
282
 
288
 
283
 
289
	/**
284
	/**
290
	 * Returns an object representing HTML text.
285
	 * Returns an object representing HTML text.
291
	 */
286
	 */
292
	public static function fromHtml(string $html): self
287
	public static function fromHtml(string $html): static
293
	{
288
	{
Zeile 294... Zeile 289...
294
		return (new static)->setHtml($html);
289
		return (new static)->setHtml($html);
295
	}
290
	}
296
 
291
 
297
 
292
 
298
	/**
293
	/**
299
	 * Returns an object representing plain text.
294
	 * Returns an object representing plain text.
300
	 */
295
	 */
Zeile 331... Zeile 326...
331
	}
326
	}
Zeile 332... Zeile 327...
332
 
327
 
333
 
328
 
334
	/**
-
 
335
	 * Changes element's name.
329
	/**
336
	 * @return static
330
	 * Changes element's name.
337
	 */
331
	 */
338
	final public function setName(string $name, ?bool $isEmpty = null)
332
	final public function setName(string $name, ?bool $isEmpty = null): static
339
	{
333
	{
340
		$this->name = $name;
334
		$this->name = $name;
341
		$this->isEmpty = $isEmpty ?? isset(static::$emptyElements[$name]);
335
		$this->isEmpty = $isEmpty ?? isset(static::$emptyElements[$name]);
Zeile 361... Zeile 355...
361
	}
355
	}
Zeile 362... Zeile 356...
362
 
356
 
363
 
357
 
364
	/**
-
 
365
	 * Sets multiple attributes.
358
	/**
366
	 * @return static
359
	 * Sets multiple attributes.
367
	 */
360
	 */
368
	public function addAttributes(array $attrs)
361
	public function addAttributes(array $attrs): static
369
	{
362
	{
370
		$this->attrs = array_merge($this->attrs, $attrs);
363
		$this->attrs = array_merge($this->attrs, $attrs);
Zeile 371... Zeile 364...
371
		return $this;
364
		return $this;
372
	}
365
	}
373
 
-
 
374
 
-
 
375
	/**
-
 
376
	 * Appends value to element's attribute.
366
 
377
	 * @param  mixed  $value
367
 
378
	 * @param  mixed  $option
368
	/**
379
	 * @return static
369
	 * Appends value to element's attribute.
380
	 */
370
	 */
381
	public function appendAttribute(string $name, $value, $option = true)
371
	public function appendAttribute(string $name, mixed $value, mixed $option = true): static
Zeile 398... Zeile 388...
398
	}
388
	}
Zeile 399... Zeile 389...
399
 
389
 
400
 
390
 
401
	/**
-
 
402
	 * Sets element's attribute.
-
 
403
	 * @param  mixed  $value
391
	/**
404
	 * @return static
392
	 * Sets element's attribute.
405
	 */
393
	 */
406
	public function setAttribute(string $name, $value)
394
	public function setAttribute(string $name, mixed $value): static
407
	{
395
	{
408
		$this->attrs[$name] = $value;
396
		$this->attrs[$name] = $value;
Zeile 409... Zeile 397...
409
		return $this;
397
		return $this;
410
	}
398
	}
411
 
-
 
412
 
399
 
413
	/**
400
 
414
	 * Returns element's attribute.
401
	/**
415
	 * @return mixed
402
	 * Returns element's attribute.
416
	 */
403
	 */
Zeile 417... Zeile 404...
417
	public function getAttribute(string $name)
404
	public function getAttribute(string $name): mixed
418
	{
405
	{
419
		return $this->attrs[$name] ?? null;
-
 
420
	}
406
		return $this->attrs[$name] ?? null;
421
 
407
	}
422
 
408
 
423
	/**
409
 
424
	 * Unsets element's attribute.
410
	/**
425
	 * @return static
411
	 * Unsets element's attribute.
Zeile 426... Zeile 412...
426
	 */
412
	 */
427
	public function removeAttribute(string $name)
413
	public function removeAttribute(string $name): static
428
	{
-
 
429
		unset($this->attrs[$name]);
414
	{
430
		return $this;
415
		unset($this->attrs[$name]);
431
	}
416
		return $this;
432
 
417
	}
433
 
418
 
434
	/**
419
 
Zeile 445... Zeile 430...
445
	}
430
	}
Zeile 446... Zeile 431...
446
 
431
 
447
 
432
 
448
	/**
-
 
449
	 * Overloaded setter for element's attribute.
433
	/**
450
	 * @param  mixed  $value
434
	 * Overloaded setter for element's attribute.
451
	 */
435
	 */
452
	final public function __set(string $name, $value): void
436
	final public function __set(string $name, mixed $value): void
453
	{
437
	{
Zeile 454... Zeile 438...
454
		$this->attrs[$name] = $value;
438
		$this->attrs[$name] = $value;
455
	}
439
	}
456
 
-
 
457
 
440
 
458
	/**
441
 
459
	 * Overloaded getter for element's attribute.
442
	/**
460
	 * @return mixed
443
	 * Overloaded getter for element's attribute.
461
	 */
444
	 */
Zeile 483... Zeile 466...
483
	}
466
	}
Zeile 484... Zeile 467...
484
 
467
 
485
 
468
 
486
	/**
-
 
487
	 * Overloaded setter for element's attribute.
469
	/**
488
	 * @return mixed
470
	 * Overloaded setter for element's attribute.
489
	 */
471
	 */
490
	final public function __call(string $m, array $args)
472
	final public function __call(string $m, array $args): mixed
491
	{
473
	{
492
		$p = substr($m, 0, 3);
474
		$p = substr($m, 0, 3);
493
		if ($p === 'get' || $p === 'set' || $p === 'add') {
475
		if ($p === 'get' || $p === 'set' || $p === 'add') {
Zeile 514... Zeile 496...
514
	}
496
	}
Zeile 515... Zeile 497...
515
 
497
 
516
 
498
 
517
	/**
-
 
518
	 * Special setter for element's attribute.
499
	/**
519
	 * @return static
500
	 * Special setter for element's attribute.
520
	 */
501
	 */
521
	final public function href(string $path, ?array $query = null)
502
	final public function href(string $path, array $query = []): static
522
	{
503
	{
523
		if ($query) {
504
		if ($query) {
524
			$query = http_build_query($query, '', '&');
505
			$query = http_build_query($query, '', '&');
Zeile 532... Zeile 513...
532
	}
513
	}
Zeile 533... Zeile 514...
533
 
514
 
534
 
515
 
535
	/**
-
 
536
	 * Setter for data-* attributes. Booleans are converted to 'true' resp. 'false'.
-
 
537
	 * @param  mixed  $value
516
	/**
538
	 * @return static
517
	 * Setter for data-* attributes. Booleans are converted to 'true' resp. 'false'.
539
	 */
518
	 */
540
	public function data(string $name, $value = null)
519
	public function data(string $name, mixed $value = null): static
541
	{
520
	{
542
		if (func_num_args() === 1) {
521
		if (func_num_args() === 1) {
543
			$this->attrs['data'] = $name;
522
			$this->attrs['data'] = $name;
Zeile 551... Zeile 530...
551
	}
530
	}
Zeile 552... Zeile 531...
552
 
531
 
553
 
532
 
554
	/**
-
 
555
	 * Sets element's HTML content.
-
 
556
	 * @param  HtmlStringable|string  $html
533
	/**
557
	 * @return static
534
	 * Sets element's HTML content.
558
	 */
535
	 */
559
	final public function setHtml($html)
536
	final public function setHtml(mixed $html): static
560
	{
537
	{
561
		$this->children = [(string) $html];
538
		$this->children = [(string) $html];
Zeile 572... Zeile 549...
572
	}
549
	}
Zeile 573... Zeile 550...
573
 
550
 
574
 
551
 
575
	/**
-
 
576
	 * Sets element's textual content.
-
 
577
	 * @param  HtmlStringable|string|int|float  $text
552
	/**
578
	 * @return static
553
	 * Sets element's textual content.
579
	 */
554
	 */
580
	final public function setText($text)
555
	final public function setText(mixed $text): static
581
	{
556
	{
582
		if (!$text instanceof HtmlStringable) {
557
		if (!$text instanceof HtmlStringable) {
Zeile 597... Zeile 572...
597
	}
572
	}
Zeile 598... Zeile 573...
598
 
573
 
599
 
574
 
600
	/**
-
 
601
	 * Adds new element's child.
-
 
602
	 * @param  HtmlStringable|string  $child  Html node or raw HTML string
575
	/**
603
	 * @return static
576
	 * Adds new element's child.
604
	 */
577
	 */
605
	final public function addHtml($child)
578
	final public function addHtml(mixed $child): static
606
	{
579
	{
Zeile 607... Zeile 580...
607
		return $this->insert(null, $child);
580
		return $this->insert(null, $child);
608
	}
581
	}
609
 
-
 
610
 
-
 
611
	/**
582
 
612
	 * Appends plain-text string to element content.
583
 
613
	 * @param  HtmlStringable|string|int|float  $text
584
	/**
614
	 * @return static
585
	 * Appends plain-text string to element content.
615
	 */
586
	 */
616
	public function addText($text)
587
	public function addText(mixed $text): static
Zeile 623... Zeile 594...
623
	}
594
	}
Zeile 624... Zeile 595...
624
 
595
 
625
 
596
 
626
	/**
-
 
627
	 * Creates and adds a new Html child.
-
 
628
	 * @param  array|string $attrs  element's attributes or raw HTML string
597
	/**
629
	 * @return static  created element
598
	 * Creates and adds a new Html child.
630
	 */
599
	 */
631
	final public function create(string $name, $attrs = null)
600
	final public function create(string $name, array|string|null $attrs = null): static
632
	{
601
	{
633
		$this->insert(null, $child = static::el($name, $attrs));
602
		$this->insert(null, $child = static::el($name, $attrs));
Zeile 634... Zeile 603...
634
		return $child;
603
		return $child;
635
	}
604
	}
636
 
-
 
637
 
-
 
638
	/**
605
 
639
	 * Inserts child node.
606
 
640
	 * @param  HtmlStringable|string $child Html node or raw HTML string
607
	/**
641
	 * @return static
608
	 * Inserts child node.
642
	 */
609
	 */
643
	public function insert(?int $index, $child, bool $replace = false)
610
	public function insert(?int $index, HtmlStringable|string $child, bool $replace = false): static
Zeile 666... Zeile 633...
666
 
633
 
667
 
634
 
668
	/**
635
	/**
669
	 * Returns child node (\ArrayAccess implementation).
-
 
670
	 * @param  int  $index
636
	 * Returns child node (\ArrayAccess implementation).
671
	 * @return HtmlStringable|string
-
 
672
	 */
637
	 * @param  int  $index
673
	#[\ReturnTypeWillChange]
638
	 */
674
	final public function offsetGet($index)
639
	final public function offsetGet($index): HtmlStringable|string
675
	{
640
	{
Zeile 769... Zeile 734...
769
	}
734
	}
Zeile 770... Zeile 735...
770
 
735
 
771
 
736
 
772
	final public function __toString(): string
-
 
773
	{
737
	final public function __toString(): string
774
		try {
-
 
775
			return $this->render();
-
 
776
		} catch (\Throwable $e) {
-
 
777
			if (PHP_VERSION_ID >= 70400) {
-
 
778
				throw $e;
-
 
779
			}
-
 
780
 
-
 
781
			trigger_error('Exception in ' . __METHOD__ . "(): {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}", E_USER_ERROR);
-
 
782
			return '';
738
	{
Zeile 783... Zeile 739...
783
		}
739
		return $this->render();
784
	}
740
	}
785
 
741
 
786
 
742
 
787
	/**
743
	/**
788
	 * Returns element's start tag.
744
	 * Returns element's start tag.
789
	 */
745
	 */
790
	final public function startTag(): string
746
	final public function startTag(): string
791
	{
747
	{
Zeile 792... Zeile 748...
792
		return $this->name
748
		return $this->name
Zeile 819... Zeile 775...
819
		foreach ($attrs as $key => $value) {
775
		foreach ($attrs as $key => $value) {
820
			if ($value === null || $value === false) {
776
			if ($value === null || $value === false) {
821
				continue;
777
				continue;
Zeile 822... Zeile 778...
822
 
778
 
823
			} elseif ($value === true) {
-
 
824
				if (static::$xhtml) {
-
 
825
					$s .= ' ' . $key . '="' . $key . '"';
-
 
826
				} else {
779
			} elseif ($value === true) {
827
					$s .= ' ' . $key;
-
 
Zeile 828... Zeile 780...
828
				}
780
				$s .= ' ' . $key;
Zeile 829... Zeile 781...
829
 
781
 
830
				continue;
782
				continue;
Zeile 855... Zeile 807...
855
 
807
 
856
			} else {
808
			} else {
857
				$value = (string) $value;
809
				$value = (string) $value;
Zeile 858... Zeile 810...
858
			}
810
			}
859
 
811
 
860
			$q = strpos($value, '"') === false ? '"' : "'";
812
			$q = str_contains($value, '"') ? "'" : '"';
861
			$s .= ' ' . $key . '=' . $q
813
			$s .= ' ' . $key . '=' . $q
862
				. str_replace(
814
				. str_replace(
863
					['&', $q, '<'],
815
					['&', $q, '<'],
864
					['&amp;', $q === '"' ? '&quot;' : '&#39;', self::$xhtml ? '&lt;' : '<'],
816
					['&amp;', $q === '"' ? '&quot;' : '&#39;', '<'],
865
					$value
817
					$value,
866
				)
818
				)
867
				. (strpos($value, '`') !== false && strpbrk($value, ' <>"\'') === false ? ' ' : '')
819
				. (str_contains($value, '`') && strpbrk($value, ' <>"\'') === false ? ' ' : '')
Zeile 868... Zeile 820...
868
				. $q;
820
				. $q;
869
		}
821
		}