full-text-rss/libraries/html5php/HTML5/Parser/TreeBuildingRules.php

140 lines
3.5 KiB
PHP
Raw Permalink Normal View History

2014-09-15 20:24:06 +00:00
<?php
2015-06-14 00:03:20 +00:00
namespace Masterminds\HTML5\Parser;
2014-09-15 20:24:06 +00:00
/**
* Handles special-case rules for the DOM tree builder.
*
2015-06-14 00:03:20 +00:00
* Many tags have special rules that need to be accomodated on an
2014-09-15 20:24:06 +00:00
* individual basis. This class handles those rules.
*
* See section 8.1.2.4 of the spec.
*
2015-06-14 00:03:20 +00:00
* @todo - colgroup and col special behaviors
* - body and head special behaviors
2014-09-15 20:24:06 +00:00
*/
2015-06-14 00:03:20 +00:00
class TreeBuildingRules
{
2014-09-15 20:24:06 +00:00
2015-06-14 00:03:20 +00:00
protected static $tags = array(
'li' => 1,
'dd' => 1,
'dt' => 1,
'rt' => 1,
'rp' => 1,
'tr' => 1,
'th' => 1,
'td' => 1,
'thead' => 1,
'tfoot' => 1,
'tbody' => 1,
'table' => 1,
'optgroup' => 1,
'option' => 1
);
2014-09-15 20:24:06 +00:00
2015-06-14 00:03:20 +00:00
/**
* Build a new rules engine.
*
* @param \DOMDocument $doc
* The DOM document to use for evaluation and modification.
*/
public function __construct($doc)
{
$this->doc = $doc;
}
2014-09-15 20:24:06 +00:00
2015-06-14 00:03:20 +00:00
/**
* Returns true if the given tagname has special processing rules.
*/
public function hasRules($tagname)
{
return isset(static::$tags[$tagname]);
2014-09-15 20:24:06 +00:00
}
2015-06-14 00:03:20 +00:00
/**
* Evaluate the rule for the current tag name.
*
* This may modify the existing DOM.
*
* @return \DOMElement The new Current DOM element.
*/
public function evaluate($new, $current)
{
switch ($new->tagName) {
case 'li':
return $this->handleLI($new, $current);
case 'dt':
case 'dd':
return $this->handleDT($new, $current);
case 'rt':
case 'rp':
return $this->handleRT($new, $current);
case 'optgroup':
return $this->closeIfCurrentMatches($new, $current, array(
'optgroup'
));
case 'option':
return $this->closeIfCurrentMatches($new, $current, array(
'option',
));
case 'tr':
return $this->closeIfCurrentMatches($new, $current, array(
'tr'
));
case 'td':
case 'th':
return $this->closeIfCurrentMatches($new, $current, array(
'th',
'td'
));
case 'tbody':
case 'thead':
case 'tfoot':
case 'table': // Spec isn't explicit about this, but it's necessary.
return $this->closeIfCurrentMatches($new, $current, array(
'thead',
'tfoot',
'tbody'
));
}
2014-09-15 20:24:06 +00:00
2015-06-14 00:03:20 +00:00
return $current;
}
2014-09-15 20:24:06 +00:00
2015-06-14 00:03:20 +00:00
protected function handleLI($ele, $current)
{
return $this->closeIfCurrentMatches($ele, $current, array(
'li'
));
}
2014-09-15 20:24:06 +00:00
2015-06-14 00:03:20 +00:00
protected function handleDT($ele, $current)
{
return $this->closeIfCurrentMatches($ele, $current, array(
'dt',
'dd'
));
2014-09-15 20:24:06 +00:00
}
2015-06-14 00:03:20 +00:00
protected function handleRT($ele, $current)
{
return $this->closeIfCurrentMatches($ele, $current, array(
'rt',
'rp'
));
2014-09-15 20:24:06 +00:00
}
2015-06-14 00:03:20 +00:00
protected function closeIfCurrentMatches($ele, $current, $match)
{
$tname = $current->tagName;
if (in_array($current->tagName, $match)) {
$current->parentNode->appendChild($ele);
} else {
$current->appendChild($ele);
}
return $ele;
}
2014-09-15 20:24:06 +00:00
}