File modules/template_engine/element/HtmlElement.class.php

Last commit: Thu Oct 22 12:01:12 2020 +0200	Jan Dankert	Externalize void elements into class constant.
1 <?php 2 3 4 namespace template_engine\element; 5 6 7 use template_engine\AppInfo; 8 use template_engine\components\html\Component; 9 10 class HtmlElement extends Element 11 { 12 /** 13 * HTML "void elements", these are self-closable. 14 * see HTML5 spec https://html.spec.whatwg.org/multipage/syntax.html#void-elements 15 * @var string[] 16 */ 17 private static $VOID_ELEMENTS = ['area','base','br','col','embed','hr','img','input','link','meta','param','source','track','wbr']; 18 19 private $styleClasses = []; 20 21 /** 22 * Auto-escaping the content. 23 * 24 * @var bool 25 */ 26 private $escape = true; 27 28 29 /** 30 * Add one or more style classes to the element. 31 * @param string|array $classes 32 * @return $this 33 */ 34 public function addStyleClass( $classes ) { 35 36 if ( ! is_array($classes) ) 37 $classes = Component::splitByComma($classes); 38 39 $classes = array_map( function($styleClass){ 40 return AppInfo::$styleClassPrefix.$styleClass; 41 },$classes); 42 43 $this->styleClasses = array_merge( $this->styleClasses, $classes ); 44 return $this; 45 } 46 47 48 public function setEscaping( $escape ) { 49 $this->escape = $escape; 50 return $this; 51 } 52 53 54 public function getAttributeValue($name) 55 { 56 return parent::getAttributeValue(htmlspecialchars($name)); 57 } 58 59 60 public function __construct( $name ) 61 { 62 parent::__construct( $name ); 63 64 // Only "void elements" are self-closable 65 if ( in_array($name,HtmlElement::$VOID_ELEMENTS)) 66 $this->selfClosing = true; 67 } 68 69 70 protected function getContent() 71 { 72 $context = $this->escape ? Value::CONTEXT_HTML : Value::CONTEXT_RAW; 73 74 return (new Value(parent::getContent()))->render( $context ); 75 } 76 77 78 public function render( $format ) 79 { 80 if ( $this->styleClasses ) 81 $this->addAttribute('class', implode(' ',$this->styleClasses) ); 82 83 return parent::render( $format ); 84 } 85 86 }
Download modules/template_engine/element/HtmlElement.class.php
History Thu, 22 Oct 2020 12:01:12 +0200 Jan Dankert Externalize void elements into class constant. Sat, 10 Oct 2020 01:29:41 +0200 Jan Dankert Refactoring: Only using CSS classes with the 'or-'-prefix. Sun, 4 Oct 2020 23:53:25 +0200 Jan Dankert New: The tree is now hidable with a dedicated button. No more hover effect in the navigation. Sun, 4 Oct 2020 16:33:53 +0200 Jan Dankert Refactoring: Using the icon font for displaying arrows. Sun, 23 Feb 2020 04:55:35 +0100 Jan Dankert New: Formatter for indenting XML files. Sat, 22 Feb 2020 01:58:20 +0100 Jan Dankert New: Autoloading of classes in all modules.