* @license PHP License * @package wb * @subpackage Markup */ WBClass::load('WBMarkup_Validator'); /** * Markup Validator: Html * * Validator for simple HTML elements * * @version 0.1.0 * @package wb * @subpackage Markup */ class WBMarkup_Validator_Html extends WBMarkup_Validator { /** * Validator's config * * @var array */ protected $config = array( 'heading' => array( 1, 2, 3, 4, 5, 6 ), 'markup' => array(), 'paragraph' => array('p', 'br'), 'list' => array('ul', 'ol') ); /** * Validate tags * * @param string $ns * @param string $tag * @param array $attributes * @param bool $isEmpty * @return bool */ public function validateStartElement($ns, $tag, $attributes, $isEmpty) { $this->flushError(); if (!empty($ns)) { return false; } // internal HTML validator if ($this->validateHeading($tag, $attributes)) { return true; } if ($this->validateParagraph($tag, $attributes)) { return true; } if ($this->validateMarkup($tag, $attributes)) { return true; } if ($this->validateList($tag, $attributes)) { return true; } return false; } /** * Validate Heading Tag * * * * @param string $tag named tag * @param array $attributes * @return true if tag is a relevant tag for this validator */ private function validateHeading($tag, $attributes) { if (2 != strlen($tag) || 'h' != $tag[0]) { return false; } if (!is_array($this->config['heading']) || empty($this->config['heading'])) { $this->setError(1, 'Headings are not allowed.'); return true; } $n = $tag[1]; if (in_array($tag[1], $this->config['heading'])) { return true; } $this->setError(intval($tag[1]) + 1, 'Heading level ' . $tag[1] . ' is not allowed.'); return true; } /** * Validate Paragraph * *
and
are valid tags.
*
* @param string $tag named tag
* @param array $attributes
* @return true if tag is a relevant tag for this validator
*/
private function validateParagraph($tag, $attributes)
{
if ('p' == $tag) {
if (in_array('p', $this->config['paragraph'])) {
return true;
}
$this->setError(11, 'Paragraphs are not allowed.');
return true;
}
if ('br' == $tag) {
if (in_array('br', $this->config['paragraph'])) {
return true;
}
$this->setError(12, 'Line breaks are not allowed.');
return true;
}
return false;
}
/**
* Validate Markup Tag
*
*
*
* @param string $tag named tag
* @param array $attributes
* @return true if tag is a relevant tag for this validator
*/
private function validateMarkup($tag, $attributes)
{
// $this->setError(21, 'Paragraphs are not allowed.');
return false;
}
/**
* Validate List Tags
*
*
*
* @param string $tag named tag
* @param array $attributes
* @return true if tag is a relevant tag for this validator
*/
private function validateList($tag, $attributes)
{
if (!in_array($tag, array('ul', 'ol', 'li'))) {
return false;
}
if ('ul' == $tag) {
if (in_array('ul', $this->config['list'])) {
return true;
}
$this->setError(31, 'Unordered lists are not allowed');
return true;
}
if ('ol' == $tag) {
if (in_array('ol', $this->config['list'])) {
return true;
}
$this->setError(32, 'Ordered lists are not allowed');
return true;
}
if ('li' == $tag) {
if (empty($this->config['list'])) {
$this->setError(32, 'Lists and list items are not allowed');
}
return true;
}
return true;
}
}