From ac8ad2941f2b95d2aed5cd66c703e74227d8e38e Mon Sep 17 00:00:00 2001 From: Tom Elliott Date: Sat, 11 Feb 2017 19:46:26 -0500 Subject: [PATCH 1/4] Many tweaks. Adds many more comments. Structures to follow more of 'PSR' style of information and layout. Enables the addClass and removeClass to operate on arrays. Enables usage of separating and operating on arrays when string for class elements contains spaces. --- HtmlTag.php | 318 ++++++++++++++++++++++++++++++-- Markup.php | 412 ++++++++++++++++++++++++++++-------------- phpunit.xml | 2 +- tests/HtmlTagTest.php | 32 ++-- tests/MarkupTest.php | 150 +++++++-------- tests/autoloader.php | 58 ++++-- 6 files changed, 714 insertions(+), 258 deletions(-) diff --git a/HtmlTag.php b/HtmlTag.php index 28dca71..690ec56 100755 --- a/HtmlTag.php +++ b/HtmlTag.php @@ -1,57 +1,335 @@ + * @license http://opensource.org/licenses/mit-license.php MIT + * @link https://github.com/airmanbzh/php-html-generator */ namespace HtmlGenerator; - -if (!defined('ENT_HTML5')) -{ - define('ENT_HTML5', 48); +/** + * Defines our HTML5 element integer. + */ +if (!defined('ENT_HTML5')) { + define('ENT_HTML5', 48); } - +/** + * Add's the tags information more dynamically. + * + * PHP version 5.3 + * + * @category HtmlTag + * @package HtmlGenerator + * @author Airmanbzh + * @license http://opensource.org/licenses/mit-license.php MIT + * @link https://github.com/airmanbzh/php-html-generator + */ class HtmlTag extends Markup { - /** @var int The language convention used for XSS avoiding */ + /** + * The language convention used for XSS avoiding. + * + * @var int + */ public static $outputLanguage = ENT_HTML5; - + /** + * The auto closed tags list (or void elements.) + * + * @var array + */ protected $autocloseTagsList = array( - 'img', 'br', 'hr', 'input', 'area', 'link', 'meta', 'param' + 'area', + 'base', + 'basefont', + 'bgsound', + 'br', + 'col', + 'command', + 'embed', + 'frame', + 'hr', + 'image', + 'img', + 'input', + 'isindex', + 'keygen', + 'link', + 'menuitem', + 'meta', + 'nextid', + 'param', + 'source', + 'track', + 'wbr' ); - /** * Shortcut to set('id', $value) - * @param string $value + * + * @param string $value The value to set. + * * @return HtmlTag instance */ public function id($value) { return $this->set('id', $value); } - /** * Add a class to classList - * @param string $value + * + * @param string|array $value The value to set. + * * @return HtmlTag instance */ public function addClass($value) { - if (!isset($this->attributeList['class']) || is_null($this->attributeList['class'])) { - $this->attributeList['class'] = array(); + /** + * If the attribute is not set or is null, + * initialize into an array. + */ + if (!isset($this->attributeList['class']) + || is_null($this->attributeList['class']) + ) { + $this->attributeList['class'] = []; + } + /** + * Classes are separated by spaces. + * Attempt exploding the values on just a space. + */ + if (!is_array($value)) { + if (false !== strpos($value, ' ')) { + $value = explode(' ', $value); + } } + /** + * If value is an array of values perform actions. + */ + if (is_array($value)) { + /** + * Trim all values of the array. + */ + $value = array_map( + 'trim', + $value + ); + /** + * Filter our values. + */ + $value = array_filter($value); + /** + * If count of values is 0 return. + */ + if (0 === count($value)) { + return $this; + } + /** + * Ensure all values are unique. + */ + $value = array_unique($value); + /** + * Join the values into our class list. + */ + $this->attributeList['class'] += $value; + /** + * Unique the attribute list. + */ + $this->attributeList['class'] = array_unique( + $this->attributeList['class'] + ); + /** + * Sort nicely. + */ + natsort($this->attributeList['class']); + /** + * Order nicely. + */ + $this->attributeList['class'] = array_values( + $this->attributeList['class'] + ); + /** + * Return. + */ + return $this; + } + /** + * Trim the value. + */ + $value = trim($value); + /** + * If the value is empty, return immediately. + */ + if (empty($value)) { + return $this; + } + /** + * Add the value to the class list. + */ $this->attributeList['class'][] = $value; + /** + * Make sure all values are unique. + */ + $this->attributeList['class'] = array_unique( + $this->attributeList['class'] + ); + /** + * Sort nicely. + */ + natsort($this->attributeList['class']); + /** + * Order nicely. + */ + $this->attributeList['class'] = array_values( + $this->attributeList['class'] + ); + /** + * Return. + */ return $this; } - /** * Remove a class from classList - * @param string $value + * + * @param string|array $value The value to remove. + * * @return HtmlTag instance */ public function removeClass($value) { - if (!is_null($this->attributeList['class'])) { - unset($this->attributeList['class'][array_search($value, $this->attributeList['class'])]); + /** + * If the class list is not set or not an array + * return immediately. + */ + if (!(isset($this->attributeList['class']) + && is_array($this->attributeList['class'])) + ) { + return $this; + } + /** + * If our attributeList has no items in it, we + * don't need to perform any action. Return + * immediately. + */ + if (1 > count($this->attributeList['class'])) { + return $this; + } + /** + * Classes are separated by spaces. + * Attempt exploding the values on just a space. + */ + if (!is_array($value)) { + if (false !== strpos($value, ' ')) { + $value = explode(' ', $value); + } + } + /** + * If value is an array process it as such. + */ + if (is_array($value)) { + /** + * Trim all the entries. + */ + $value = array_map( + 'trim', + $value + ); + /** + * Filter our results to remove null/blanks. + */ + $value = array_filter($value); + /** + * If there is no data return immediately. + */ + if (1 > count($value)) { + return $this; + } + /** + * Ensure the values are all unique. + */ + $value = array_unique($value); + /** + * This just gets the difference between + * the current list and what we want to remove. + */ + $diff = array_diff( + $this->attributeList['class'], + $value + ); + /** + * If there is no data return immediately. + */ + if (1 > count($diff)) { + return $this; + } + /** + * Ensure our diff values are unique. + */ + $diff = array_unique($diff); + /** + * Sort nicely. + */ + natsort($diff); + /** + * Order nicely. + */ + $diff = array_values($diff); + /** + * Set up our new list. + */ + $this->attributeList['class'] = $diff; + /** + * Return. + */ + return $this; + } + /** + * Trim the entry. + */ + $value = trim($value); + /** + * If there is no data return immediately. + */ + if (empty($value)) { + return $this; + } + /** + * Attempt to find the index that the value resides under. + */ + $index = array_search( + $value, + $this->attributeList['class'] + ); + /** + * If the index isn't found, return immediately. + */ + if (false === $index) { + return $this; } + /** + * Unset the element. + */ + unset($this->attributeList['class'][$index]); + /** + * Ensure all items are unique. + */ + $this->attributeList['class'] = array_unique( + $this->attributeList['class'] + ); + /** + * Sort nicely. + */ + natsort($this->attributeList['class']); + /** + * Reorder nicely. + */ + $this->attributeList['class'] = array_values( + $this->attributeList['class'] + ); + /** + * Return. + */ return $this; } } diff --git a/Markup.php b/Markup.php index 06ce10d..fc46b33 100755 --- a/Markup.php +++ b/Markup.php @@ -1,55 +1,121 @@ + * @license http://opensource.org/licenses/mit-license.php MIT + * @link https://github.com/airmanbzh/php-html-generator */ namespace HtmlGenerator; - use ArrayAccess; - -if (!defined('ENT_XML1')) -{ - define('ENT_XML1', 16); +if (!defined('ENT_XML1')) { + define('ENT_XML1', 16); } -if (!defined('ENT_XHTML')) -{ - define('ENT_XHTML', 32); +if (!defined('ENT_XHTML')) { + define('ENT_XHTML', 32); } - - +/** + * Add's the tags information more dynamically. + * + * PHP version 5.3 + * + * @category Markup + * @package HtmlGenerator + * @author Airmanbzh + * @license http://opensource.org/licenses/mit-license.php MIT + * @link https://github.com/airmanbzh/php-html-generator + */ class Markup implements ArrayAccess { - /** @var boolean Specifies if attribute values and text input sould be protected from XSS injection */ + /** + * Specifies if attribute values and text input + * should be protected from XSS injection. + * + * @var boolean + */ public static $avoidXSS = false; - - /** @var int The language convention used for XSS avoiding */ + /** + * The language convention used for XSS avoiding. + * + * @var int + */ public static $outputLanguage = ENT_XML1; - - protected static $_instance = null; - - protected $_top = null; - protected $_parent = null; - + /** + * The working instance. + * + * @var object + */ + protected static $instance = null; + /** + * Top elements. + * + * @var mixed + */ + protected $top = null; + /** + * Parent elements. + * + * @var mixed + */ + protected $parent = null; + /** + * The tag. + * + * @var string + */ protected $tag = null; + /** + * The attribute list. + * + * @var mixed + */ public $attributeList = null; + /** + * The class list. + * + * @var mixed + */ protected $classList = null; - + /** + * The content. + * + * @var string + */ protected $content = null; + /** + * The text. + * + * @var string + */ protected $text = ''; - + /** + * Auto closed. + * + * @var bool + */ protected $autoclosed = false; - + /** + * Void elements list. + * + * @var array + */ protected $autocloseTagsList = array(); - /** * Constructor - * @param mixed $tag - * @param Markup $top - * @return Markup instance + * + * @param mixed $tag The Tag to set. + * @param Markup $top The top to set. + * + * @return Markup */ protected function __construct($tag, $top = null) { $this->tag = $tag; - $this->_top =& $top; + $this->top =& $top; $this->attributeList = array(); $this->classList = array(); $this->content = array(); @@ -59,192 +125,241 @@ protected function __construct($tag, $top = null) } /** - * Builds markup from static context - * @param string $tag The tag name - * @param array $content The content of the current tag, first argument can be an array containing the attributes + * Builds markup from static context. + * + * @param string $tag The tag name. + * @param array $content The content of the current tag, + * first argument can be an array containing + * the attributes + * * @return Markup */ public static function __callStatic($tag, $content) { return self::createElement($tag) - ->attr(count($content) && is_array($content[0]) ? array_pop($content) : array()) - ->text(implode('', $content)); + ->attr( + ( + count($content) && is_array($content[0]) ? + array_pop($content) : + array() + ) + )->text( + implode('', $content) + ); } - /** * Add a children to the current element - * @param string $tag The name of the tag - * @param array $content The content of the current tag, first argument can be an array containing the attributes - * @return Markup instance + * + * @param string $tag The name of the tag + * @param array $content The content of the current tag, + * first argument can be an array + * containing the attributes + * + * @return Markup */ public function __call($tag, $content) { return $this ->addElement($tag) - ->attr(count($content) && is_array($content[0]) ? array_pop($content) : array()) - ->text(implode('', $content)); + ->attr( + ( + count($content) && is_array($content[0]) ? + array_pop($content) : + array() + ) + )->text( + implode('', $content) + ); } - /** - * Alias for getParent() + * Alias for getParent method. + * * @return Markup */ public function __invoke() { return $this->getParent(); } - /** * Create a new Markup - * @param string $tag - * @return Markup instance + * + * @param string $tag The tag to create. + * + * @return Markup */ public static function createElement($tag = '') { - self::$_instance = new static($tag); - return self::$_instance; + self::$instance = new static($tag); + return self::$instance; } /** + * Add element at an existing Markup. + * + * @param Markup|string $tag The element to add. * - * Add element at an existing Markup - * @param Markup|string $tag * @return Markup instance */ public function addElement($tag = '') { - $htmlTag = (is_object($tag) && $tag instanceof self) ? $tag : new static($tag); - $htmlTag->_top = $this->getTop(); - $htmlTag->_parent = &$this; - + $htmlTag = ( + is_object($tag) && $tag instanceof self ? + $tag : + new static($tag) + ); + $htmlTag->top = $this->getTop(); + $htmlTag->parent = &$this; $this->content[] = $htmlTag; return $htmlTag; } - /** * (Re)Define an attribute or many attributes - * @param string|array $attribute - * @param string $value + * + * @param string|array $attribute The attribute to add. + * @param string $value The value of the attribute to add. + * * @return Markup instance */ public function set($attribute, $value = null) { - if(is_array($attribute)) { - foreach ($attribute as $key => $value) { + if (is_array($attribute)) { + foreach ($attribute as $key => &$value) { $this[$key] = $value; + unset($value); } } else { $this[$attribute] = $value; } return $this; } - /** - * alias to method "set" - * @param string|array $attribute - * @param string $value + * Alias to method "set". + * + * @param string|array $attribute The attribute to add. + * @param string $value The value of the attribute to add. + * * @return Markup instance */ public function attr($attribute, $value = null) { - return call_user_func_array(array($this, 'set'), func_get_args()); + return call_user_func_array( + array($this, 'set'), + func_get_args() + ); } - /** * Checks if an attribute is set for this tag and not null * * @param string $attribute The attribute to test + * * @return boolean The result of the test */ public function offsetExists($attribute) { return isset($this->attributeList[$attribute]); } - /** - * Returns the value the attribute set for this tag + * Returns the value the attribute set for this tag. + * + * @param string $attribute The attribute to get. * - * @param string $attribute The attribute to get - * @return mixed The stored result in this object + * @return mixed The stored result in this object. */ public function offsetGet($attribute) { - return $this->offsetExists($attribute) ? $this->attributeList[$attribute] : null; + return ( + $this->offsetExists($attribute) ? + $this->attributeList[$attribute] : + null + ); } - /** * Sets the value an attribute for this tag * - * @param string $attribute The attribute to set - * @param mixed $value The value to set + * @param string $attribute The attribute to set. + * @param mixed $value The value to set. + * * @return void */ public function offsetSet($attribute, $value) { $this->attributeList[$attribute] = $value; } - /** * Removes an attribute * * @param mixed $attribute The attribute to unset + * * @return void */ public function offsetUnset($attribute) { - if ($this->offsetExists($attribute)) + if ($this->offsetExists($attribute)) { unset($this->attributeList[$attribute]); + } } - /** + * Define text content. * - * Define text content - * @param string $value - * @return Markup instance + * @param string $value The value of the text. + * + * @return Markup */ public function text($value) { - $this->addElement('')->text = static::$avoidXSS ? static::unXSS($value) : $value; + $this->addElement('')->text = ( + static::$avoidXSS ? + static::unXSS($value) : + $value + ); return $this; } - /** - * Returns the top element + * Returns the top element. + * * @return Markup */ public function getTop() { - return $this->_top===null ? $this : $this->_top; + return ( + $this->top === null ? + $this : + $this->top + ); } - /** - * * Return parent of current element + * + * @return Markup */ public function getParent() { - return $this->_parent; + return $this->parent; } - /** - * Return first child of parent of current object + * Return first child of parent of current object. + * + * @return mixed */ public function getFirst() { - return is_null($this->_parent) ? null : $this->_parent->content[0]; + return ( + is_null($this->parent) ? + null : + $this->parent->content[0] + ); } - /** - * Return previous element or itself - * - * @return Markup instance + * Return previous element or itself. + * + * @return Markup */ public function getPrevious() { $prev = $this; $find = false; - if (!is_null($this->_parent)) { - foreach ($this->_parent->content as $c) { + if (!is_null($this->parent)) { + foreach ($this->parent->content as $c) { if ($c === $this) { $find=true; break; @@ -256,16 +371,17 @@ public function getPrevious() } return $prev; } - /** - * @return Markup last child of parent of current object + * Returns Next child of parent of current object. + * + * @return Markup */ public function getNext() { $next = null; $find = false; - if (!is_null($this->_parent)) { - foreach ($this->_parent->content as $c) { + if (!is_null($this->parent)) { + foreach ($this->parent->content as $c) { if ($find) { $next = &$c; break; @@ -277,21 +393,27 @@ public function getNext() } return $next; } - /** - * @return Markup last child of parent of current object + * Returns Last child of parent of current object. + * + * @return Markup */ public function getLast() { - return is_null($this->_parent) ? null : $this->_parent->content[count($this->_parent->content) - 1]; + return ( + is_null($this->parent) ? + null : + $this->parent->content[count($this->parent->content) - 1] + ); } - /** - * @return Markup return parent or null + * Removes parent. + * + * @return Markup */ public function remove() { - $parent = $this->_parent; + $parent = $this->parent; if (!is_null($parent)) { foreach ($parent->content as $key => $value) { if ($parent->content[$key] == $this) { @@ -302,18 +424,18 @@ public function remove() } return null; } - /** - * Generation method + * Generation method. + * * @return string */ public function __toString() { return $this->getTop()->toString(); } - /** * Generation method + * * @return string */ public function toString() @@ -333,33 +455,50 @@ public function toString() } return $string; } - /** - * return current list of attribute as a string $key="$val" $key2="$val2" + * Returns current list of attributes as string. + * * @return string */ protected function attributesToString() { $string = ''; - $XMLConvention = in_array(static::$outputLanguage, array(ENT_XML1, ENT_XHTML)); + $XMLConvention = in_array( + static::$outputLanguage, + array(ENT_XML1, ENT_XHTML) + ); if (!empty($this->attributeList)) { - foreach ($this->attributeList as $key => $value) { - if ($value!==null && ($value!==false || $XMLConvention)) { - $string.= ' ' . $key; - if($value===true) { + foreach ($this->attributeList as $key => &$value) { + if ($value !== null + && ($value!==false + || $XMLConvention) + ) { + $string .= sprintf( + ' %s', + $key + ); + if ($value === true) { if ($XMLConvention) { $value = $key; } else { continue; } } - $string.= '="' . implode( - ' ', + $string .= sprintf( + '="%s"', array_map( - static::$avoidXSS ? 'static::unXSS' : 'strval', - is_array($value) ? $value : array($value) + ( + static::$avoidXSS ? + 'static:unXSS' : + 'strval' + ), + ( + is_array($value) ? + $value : + array($value) + ) ) - ) . '"'; + ); } } } @@ -367,7 +506,8 @@ protected function attributesToString() } /** - * return current list of content as a string + * Return current list of content as a string. + * * @return string */ protected function contentToString() @@ -381,24 +521,30 @@ protected function contentToString() return $string; } - /** - * Protects value from XSS injection by replacing some characters by XML / HTML entities + * Protects value from XSS injection by replacing some + * characters by XML / HTML entities. + * * @param string $input The unprotected value - * @return string A safe string + * + * @return string */ public static function unXSS($input) { - $return = ''; - if (version_compare(phpversion(), '5.4', '<')) - { - $return = htmlspecialchars($input); - } - else - { - $return = htmlentities($input, ENT_QUOTES | ENT_DISALLOWED | static::$outputLanguage); - } - + $return = ''; + if (version_compare(phpversion(), '5.4', '<')) { + $return = htmlspecialchars( + $input, + ENT_QUOTES | ENT_DISALLOWED | ENT_HTML401, + static::$outputLanguage + ); + } else { + $return = htmlentities( + $input, + ENT_QUOTES | ENT_DISALLOWED | ENT_HTML401, + static::$outputLanguage + ); + } return $return; } } diff --git a/phpunit.xml b/phpunit.xml index 7598e3b..6d4cffd 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -5,4 +5,4 @@ ./tests/ - \ No newline at end of file + diff --git a/tests/HtmlTagTest.php b/tests/HtmlTagTest.php index eaab701..d581729 100755 --- a/tests/HtmlTagTest.php +++ b/tests/HtmlTagTest.php @@ -3,34 +3,34 @@ class HtmlTagTest extends PHPUnit_Framework_TestCase { public function testId() { - $div = HtmlGenerator\HtmlTag::createElement('div'); - $div->id('test'); + $div = HtmlGenerator\HtmlTag::createElement('div'); + $div->id('test'); $this->assertEquals($div, '
'); } public function testAddClass() { - $div = HtmlGenerator\HtmlTag::createElement('div'); - $div->addClass('test'); + $div = HtmlGenerator\HtmlTag::createElement('div'); + $div->addClass('test'); - $this->assertEquals($div, '
'); + $this->assertEquals($div, '
'); } public function testRemoveClass() { - $div = HtmlGenerator\HtmlTag::createElement('div'); - $div->addClass('test'); - $div->addClass('test2'); - $div->removeClass('test'); + $div = HtmlGenerator\HtmlTag::createElement('div'); + $div->addClass('test'); + $div->addClass('test2'); + $div->removeClass('test'); - $this->assertEquals($div, '
'); + $this->assertEquals($div, '
'); } - public function testAutoClose() - { - $br = HtmlGenerator\HtmlTag::createElement('br'); + public function testAutoClose() + { + $br = HtmlGenerator\HtmlTag::createElement('br'); - $this->assertEquals($br, '
'); - } -} \ No newline at end of file + $this->assertEquals($br, '
'); + } +} diff --git a/tests/MarkupTest.php b/tests/MarkupTest.php index 977cb53..631eca9 100755 --- a/tests/MarkupTest.php +++ b/tests/MarkupTest.php @@ -1,13 +1,13 @@ assertEquals(get_class(HtmlGenerator\Markup::createElement()), "HtmlGenerator\Markup"); @@ -22,36 +22,36 @@ public function testToString() public function testAddElement() { $div = HtmlGenerator\Markup::createElement('div'); - $div->addElement('p'); + $div->addElement('p'); $this->assertEquals($div, '

'); } public function testAttr() { $div = HtmlGenerator\Markup::createElement('div'); - $div->attr('id', 'testId'); + $div->attr('id', 'testId'); $this->assertEquals($div, '
'); } public function testSet() { $div = HtmlGenerator\Markup::createElement('div'); - $div->set('id', 'testId'); + $div->set('id', 'testId'); $this->assertEquals($div, '
'); } public function testSetWithArray() { $div = HtmlGenerator\Markup::createElement('div'); - $div->set(array('id' => 'testId', 'class' => 'test')); + $div->set(array('id' => 'testId', 'class' => 'test')); $this->assertEquals($div, '
'); } public function testText() { $div = HtmlGenerator\Markup::createElement('div') - ->addElement('p') - ->text('text'); + ->addElement('p') + ->text('text'); $this->assertEquals($div, '

text

'); } @@ -59,10 +59,10 @@ public function testText() public function testGetParent() { $div = HtmlGenerator\Markup::createElement('div') - ->addElement('p') - ->addElement('a') - ->getParent() - ->addElement('a'); + ->addElement('p') + ->addElement('a') + ->getParent() + ->addElement('a'); $this->assertEquals($div, '

'); } @@ -70,12 +70,12 @@ public function testGetParent() public function testGetFirst() { $div = HtmlGenerator\Markup::createElement('div') - ->addElement('p') - ->addElement('a') - ->getParent() - ->addElement('a') - ->getFirst() - ->text('test'); + ->addElement('p') + ->addElement('a') + ->getParent() + ->addElement('a') + ->getFirst() + ->text('test'); $this->assertEquals($div, '

test

'); } @@ -83,15 +83,15 @@ public function testGetFirst() public function testGetLast() { $div = HtmlGenerator\Markup::createElement('div') - ->addElement('p') - ->addElement('a') - ->getParent() - ->addElement('a') - ->getParent() - ->addElement('a') - ->getFirst() - ->getLast() - ->text('test'); + ->addElement('p') + ->addElement('a') + ->getParent() + ->addElement('a') + ->getParent() + ->addElement('a') + ->getFirst() + ->getLast() + ->text('test'); $this->assertEquals($div, '

test

'); } @@ -99,14 +99,14 @@ public function testGetLast() public function testGetPrevious() { $div = HtmlGenerator\Markup::createElement('div') - ->addElement('p') - ->addElement('a') - ->getParent() - ->addElement('a') - ->getParent() - ->addElement('a') - ->getPrevious() - ->text('test'); + ->addElement('p') + ->addElement('a') + ->getParent() + ->addElement('a') + ->getParent() + ->addElement('a') + ->getPrevious() + ->text('test'); $this->assertEquals($div, '

test

'); } @@ -114,15 +114,15 @@ public function testGetPrevious() public function testGetNext() { $div = HtmlGenerator\Markup::createElement('div') - ->addElement('p') - ->addElement('a') - ->getParent() - ->addElement('a') - ->getParent() - ->addElement('a') - ->getFirst() - ->getNext() - ->text('test'); + ->addElement('p') + ->addElement('a') + ->getParent() + ->addElement('a') + ->getParent() + ->addElement('a') + ->getFirst() + ->getNext() + ->text('test'); $this->assertEquals($div, '

test

'); } @@ -130,10 +130,10 @@ public function testGetNext() public function testGetTop() { $div = HtmlGenerator\Markup::createElement('div') - ->addElement('p') - ->addElement('a') - ->getTop() - ->text('test'); + ->addElement('p') + ->addElement('a') + ->getTop() + ->text('test'); $this->assertEquals($div, '

test
'); } @@ -141,37 +141,37 @@ public function testGetTop() public function testRemoveXSS() { $div = HtmlGenerator\Markup::createElement('div') - ->text('test'); + ->text('test'); $this->assertEquals(HtmlGenerator\Markup::unXSS($div), '<div>test</div>'); } - public function testMagicStatic() - { + public function testMagicStatic() + { - $div = HtmlGenerator\Markup::div() - ->text('test'); + $div = HtmlGenerator\Markup::div() + ->text('test'); - $this->assertEquals($div, '
test
'); - } + $this->assertEquals($div, '
test
'); + } - public function testMagic() - { + public function testMagic() + { - $div = HtmlGenerator\Markup::div() - ->b() - ->text('test'); + $div = HtmlGenerator\Markup::div() + ->b() + ->text('test'); - $this->assertEquals($div, '
test
'); - } + $this->assertEquals($div, '
test
'); + } - public function testMagicWithAttributes() - { + public function testMagicWithAttributes() + { - $div = HtmlGenerator\Markup::div() - ->b(array('id' => 'testId', 'tag' => 'tagTest')) - ->text('test'); + $div = HtmlGenerator\Markup::div() + ->b(array('id' => 'testId', 'tag' => 'tagTest')) + ->text('test'); - $this->assertEquals($div, '
test
'); - } -} \ No newline at end of file + $this->assertEquals($div, '
test
'); + } +} diff --git a/tests/autoloader.php b/tests/autoloader.php index ced7c81..bc4cacd 100755 --- a/tests/autoloader.php +++ b/tests/autoloader.php @@ -1,13 +1,45 @@ - + * @license http://opensource.org/licenses/mit-license.php MIT + * @link https://github.com/airmanbzh/php-html-generator + */ +/** + * Autoloader file. + * + * @category Autoloader + * @package HtmlGenerator + * @author Airmanbzh + * @license http://opensource.org/licenses/mit-license.php MIT + * @link https://github.com/airmanbzh/php-html-generator + */ +/** + * Function performs the loading of classes. + * + * @param string $class The class to load. + * + * @return void + */ +function loader($class) +{ + $class = explode('\\', $class); + $class = array_pop($class); + + $file = sprintf( + '%s%s..%s%s.php', + __DIR__, + DIRECTORY_SEPARATOR, + DIRECTORY_SEPARATOR, + $class + ); + if (file_exists($file)) { + include $file; + } +} +spl_autoload_register('loader'); From b16b57a1e4a068a45214c8ee840971a6e2105657 Mon Sep 17 00:00:00 2001 From: Tom Elliott Date: Sat, 11 Feb 2017 20:15:08 -0500 Subject: [PATCH 2/4] Missed imploding markup. PHPUnit tests pass successfully. --- Markup.php | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/Markup.php b/Markup.php index fc46b33..2a2a540 100755 --- a/Markup.php +++ b/Markup.php @@ -486,16 +486,19 @@ protected function attributesToString() } $string .= sprintf( '="%s"', - array_map( - ( - static::$avoidXSS ? - 'static:unXSS' : - 'strval' - ), - ( - is_array($value) ? - $value : - array($value) + implode( + ' ', + array_map( + ( + static::$avoidXSS ? + 'static:unXSS' : + 'strval' + ), + ( + is_array($value) ? + $value : + array($value) + ) ) ) ); @@ -535,14 +538,12 @@ public static function unXSS($input) if (version_compare(phpversion(), '5.4', '<')) { $return = htmlspecialchars( $input, - ENT_QUOTES | ENT_DISALLOWED | ENT_HTML401, - static::$outputLanguage + ENT_QUOTES | ENT_DISALLOWED | static::$outputLanguage ); } else { $return = htmlentities( $input, - ENT_QUOTES | ENT_DISALLOWED | ENT_HTML401, - static::$outputLanguage + ENT_QUOTES | ENT_DISALLOWED | static::$outputLanguage ); } return $return; From 53ed97b68b47c0af2aed7f00493723521182d7e1 Mon Sep 17 00:00:00 2001 From: Tom Elliott Date: Sat, 11 Feb 2017 20:20:38 -0500 Subject: [PATCH 3/4] Switch array setter to use array name. --- HtmlTag.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HtmlTag.php b/HtmlTag.php index 690ec56..1b052e6 100755 --- a/HtmlTag.php +++ b/HtmlTag.php @@ -93,7 +93,7 @@ public function addClass($value) if (!isset($this->attributeList['class']) || is_null($this->attributeList['class']) ) { - $this->attributeList['class'] = []; + $this->attributeList['class'] = array(); } /** * Classes are separated by spaces. From 1bf62d0d45b956281132ad28641d675b28b148ed Mon Sep 17 00:00:00 2001 From: Tom Elliott Date: Sat, 11 Feb 2017 20:23:59 -0500 Subject: [PATCH 4/4] Fixup htmlspecialchars issue --- Markup.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Markup.php b/Markup.php index 2a2a540..6594cdf 100755 --- a/Markup.php +++ b/Markup.php @@ -536,10 +536,7 @@ public static function unXSS($input) { $return = ''; if (version_compare(phpversion(), '5.4', '<')) { - $return = htmlspecialchars( - $input, - ENT_QUOTES | ENT_DISALLOWED | static::$outputLanguage - ); + $return = htmlspecialchars($input); } else { $return = htmlentities( $input,