Browse Source

Random fixes

• Made names of storage property in Stack and ActiveFormattingElementsList consistent to elsewhere
• Parser has its own public static variable that is an intance of itself, so ActiveFormattingElementsList can use that instead of passing an instance around.
• Added separate exception for ActiveFormattingElementsList
ns
Dustin Wilson 6 years ago
parent
commit
e8bb3a97f7
  1. 65
      lib/ActiveFormattingElementsList.php
  2. 20
      lib/Exception.php
  3. 8
      lib/Parser.php
  4. 22
      lib/Stack.php

65
lib/ActiveFormattingElementsList.php

@ -15,16 +15,11 @@ namespace dW\HTML5;
# associated with the token for which it was created, so that further elements
# can be created for that token if necessary.
class ActiveFormattingElementsList implements \ArrayAccess {
public $parser;
protected $storage = [];
public function __construct(Parser $parser) {
$this->parser = $parser;
}
protected $_storage = [];
public function offsetSet($offset, $value) {
if ($offset < 0) {
throw new Exception(Exception::STACK_INVALID_INDEX);
throw new Exception(Exception::ACTIVE_FORMATTING_ELEMENT_LIST_INVALID_INDEX);
}
if (is_null($offset)) {
@ -76,36 +71,36 @@ class ActiveFormattingElementsList implements \ArrayAccess {
}
# 2. Add element to the list of active formatting elements.
$this->storage[] = $value;
$this->_storage[] = $value;
} else {
$this->storage[$offset] = $value;
$this->_storage[$offset] = $value;
}
}
public function offsetExists($offset) {
return isset($this->storage[$offset]);
return isset($this->_storage[$offset]);
}
public function offsetUnset($offset) {
if ($offset < 0 || $offset > count($this->$storage) - 1) {
throw new Exception(Exception::STACK_INVALID_INDEX);
throw new Exception(Exception::ACTIVE_FORMATTING_ELEMENT_LIST_INVALID_INDEX);
}
unset($this->storage[$offset]);
unset($this->_storage[$offset]);
// Reindex the array.
$this->storage = array_values($this->storage);
$this->_storage = array_values($this->_storage);
}
public function offsetGet($offset) {
if ($offset < 0 || $offset > count($this->$storage) - 1) {
throw new Exception(Exception::STACK_INVALID_INDEX);
throw new Exception(Exception::ACTIVE_FORMATTING_ELEMENT_LIST_INVALID_INDEX);
}
return $this->storage[$offset];
return $this->_storage[$offset];
}
public function insert(StartTagToken $token, \DOMElement $element) {
$this->storage[] = [
$this->_storage[] = [
'token' => $token,
'element' => $element
];
@ -116,7 +111,7 @@ class ActiveFormattingElementsList implements \ArrayAccess {
}
public function pop() {
return array_pop($this->storage);
return array_pop($this->_storage);
}
public function reconstruct() {
@ -129,15 +124,15 @@ class ActiveFormattingElementsList implements \ArrayAccess {
# 1. If there are no entries in the list of active formatting elements, then
# there is nothing to reconstruct; stop this algorithm.
if (count($this->storage) === 0) {
if (count($this->_storage) === 0) {
return;
}
# 2. If the last (most recently added) entry in the list of active formatting
# elements is a marker, or if it is an element that is in the stack of open
# elements, then there is nothing to reconstruct; stop this algorithm.
$entry = end($this->storage);
if ($entry instanceof ActiveFormattingElementMarker || in_array($entry['element'], $this->parser->stack)) {
$entry = end($this->_storage);
if ($entry instanceof ActiveFormattingElementMarker || in_array($entry['element'], Parser::$self->stack)) {
return;
}
@ -148,36 +143,36 @@ class ActiveFormattingElementsList implements \ArrayAccess {
# 4. Rewind: If there are no entries before entry in the list of active
# formatting elements, then jump to the step labeled Create.
rewind:
if (count($this->storage) === 1) {
if (count($this->_storage) === 1) {
goto create;
}
# 5. Let entry be the entry one earlier than entry in the list of active
# formatting elements.
$entry = prev($this->storage);
$entry = prev($this->_storage);
# 6. If entry is neither a marker nor an element that is also in the stack of
# open elements, go to the step labeled Rewind.
if (!$entry instanceof ActiveFormattingElementMarker && !in_array($entry['element'], $this->parser->stack)) {
if (!$entry instanceof ActiveFormattingElementMarker && !in_array($entry['element'], Parser::$self->stack)) {
goto rewind;
}
# 7. Advance: Let entry be the element one later than entry in the list of
# active formatting elements.
advance:
$entry = next($this->storage);
$entry = next($this->_storage);
# 8. Create: Insert an HTML element for the token for which the element entry
# was created, to obtain new element.
create:
$element = $this->parser->insertElement($entry['token']);
$element = Parser::$self->insertElement($entry['token']);
# 9. Replace the entry for entry in the list with an entry for new element.
$this->storage[key($this->storage)]['element'] = $element;
$this->_storage[key($this->_storage)]['element'] = $element;
# 10. If the entry for new element in the list of active formatting elements is
# not the last entry in the list, return to the step labeled Advance.
if ($entry !== $this->storage[count($this->storage) - 1]) {
if ($entry !== $this->_storage[count($this->_storage) - 1]) {
goto advance;
}
}
@ -195,30 +190,30 @@ class ActiveFormattingElementsList implements \ArrayAccess {
// Just going to go backwards through the array until a marker is reached. Does
// the same thing.
foreach (array_reverse($this->storage) as $key => $value) {
if ($value instanceof ActiveFormattingElementMarker) {
for ($end = count($this->_storage) - 1, $i = $end; $i >= 0; $i--) {
if ($this->_storage[$i] instanceof ActiveFormattingElementMarker) {
return;
}
unset($this->storage[$key]);
unset($this->_storage[$i]);
}
// Reindex the array.
$this->storage = array_values($this->storage);
$this->_storage = array_values($this->_storage);
}
public function __get($property) {
switch ($property) {
case 'lastMarker':
foreach (array_reverse($this->storage) as $key => $value) {
if ($value instanceof ActiveFormattingElementMarker) {
return $key;
for ($end = count($this->_storage) - 1, $i = $end; $i >= 0; $i--) {
if ($this->_storage[$i] instanceof ActiveFormattingElementMarker) {
return $i;
}
}
return false;
break;
case 'length': return count($this->storage);
case 'length': return count($this->_storage);
break;
default: return null;
}

20
lib/Exception.php

@ -14,10 +14,12 @@ class Exception extends \Exception {
const STACK_INVALID_INDEX = 10201;
const STACK_DOMNODE_ONLY = 10202;
const DATASTREAM_NODATA = 10301;
const DATASTREAM_INVALID_DATA_CONSUMPTION_LENGTH = 10302;
const ACTIVE_FORMATTING_ELEMENT_LIST_INVALID_INDEX = 10301;
const DOM_DOMELEMENT_STRING_OR_CLOSURE_EXPECTED = 10401;
const DATASTREAM_NODATA = 10401;
const DATASTREAM_INVALID_DATA_CONSUMPTION_LENGTH = 10402;
const DOM_DOMELEMENT_STRING_OR_CLOSURE_EXPECTED = 10501;
protected static $messages = [10000 => 'Invalid error code',
10001 => 'Unknown error; escaping',
@ -27,13 +29,15 @@ class Exception extends \Exception {
10102 => 'DOMElement, DOMDocument, or DOMDocumentFrag expected; found %s',
10103 => 'DOMNode expected; found %s',
10201 => '%s is an invalid index',
10202 => 'Instances of DOMNode are the only types allowed in an HTML5Stack',
10201 => '%s is an invalid Stack index',
10202 => 'Instances of DOMNode are the only types allowed in a Stack',
10301 => '%s is an invalid ActiveFormattingElementsList index',
10301 => 'Data string expected; found %s',
10302 => '%s is an invalid data consumption length; a value of 1 or above is expected',
10401 => 'Data string expected; found %s',
10402 => '%s is an invalid data consumption length; a value of 1 or above is expected',
10401 => 'The first argument must either be an instance of \DOMElement, a string, or a closure; found %s'];
10501 => 'The first argument must either be an instance of \DOMElement, a string, or a closure; found %s'];
public function __construct(int $code, ...$args) {
if (!isset(static::$messages[$code])) {

8
lib/Parser.php

@ -171,7 +171,7 @@ class Parser {
$this->insertionMode = static::INITIAL_MODE;
$this->quirksMode = static::QUIRKS_MODE_OFF;
$this->stack = new Stack();
$this->activeFormattingElementsList = new ActiveFormattingElementsList($this);
$this->activeFormattingElementsList = new ActiveFormattingElementsList();
}
public static function parse(string $data, bool $file = false) {
@ -4235,7 +4235,7 @@ class Parser {
];
}
protected function insertCharacterToken(CharacterToken $token) {
public function insertCharacterToken(CharacterToken $token) {
# 1. Let data be the characters passed to the algorithm, or, if no characters
# were explicitly specified, the character of the character token being
# processed.
@ -4274,7 +4274,7 @@ class Parser {
}
}
protected function insertCommentToken(CommentToken $token, \DOMNode $position = null) {
public function insertCommentToken(CommentToken $token, \DOMNode $position = null) {
# When the steps below require the user agent to insert a comment while
# processing a comment token, optionally with an explicitly insertion position
# position, the user agent must run the following steps:
@ -4307,7 +4307,7 @@ class Parser {
}
}
function insertElement(StartTagToken $token, \DOMNode $intendedParent = null, string $namespace = null) {
public function insertElement(StartTagToken $token, \DOMNode $intendedParent = null, string $namespace = null) {
if (!is_null($namespace)) {
$namespace = $token->namespace;
}

22
lib/Stack.php

@ -3,7 +3,7 @@ declare(strict_types=1);
namespace dW\HTML5;
class Stack implements \ArrayAccess {
protected $storage = [];
protected $_storage = [];
public function offsetSet($offset, $value) {
if ($offset < 0) {
@ -11,14 +11,14 @@ class Stack implements \ArrayAccess {
}
if (is_null($offset)) {
$this->storage[] = $value;
$this->_storage[] = $value;
} else {
$this->storage[$offset] = $value;
$this->_storage[$offset] = $value;
}
}
public function offsetExists($offset) {
return isset($this->storage[$offset]);
return isset($this->_storage[$offset]);
}
public function offsetUnset($offset) {
@ -26,7 +26,7 @@ class Stack implements \ArrayAccess {
throw new Exception(Exception::STACK_INVALID_INDEX);
}
unset($this->storage[$offset]);
unset($this->_storage[$offset]);
}
public function offsetGet($offset) {
@ -34,11 +34,11 @@ class Stack implements \ArrayAccess {
throw new Exception(Exception::STACK_INVALID_INDEX);
}
return $this->storage[$offset];
return $this->_storage[$offset];
}
public function pop() {
return array_pop($this->storage);
return array_pop($this->_storage);
}
public function search(mixed $needle): int {
@ -47,13 +47,13 @@ class Stack implements \ArrayAccess {
}
if ($needle instanceof DOMElement) {
foreach (array_reverse($this->storage) as $key=>$value) {
foreach (array_reverse($this->_storage) as $key=>$value) {
if ($value->isSameNode($needle)) {
return $key;
}
}
} elseif (is_string($needle)) {
foreach (array_reverse($this->storage) as $key=>$value) {
foreach (array_reverse($this->_storage) as $key=>$value) {
if ($value->nodeName === $needle) {
return $key;
}
@ -65,10 +65,10 @@ class Stack implements \ArrayAccess {
public function __get($property) {
switch ($property) {
case 'length': return count($this->storage);
case 'length': return count($this->_storage);
break;
case 'currentNode':
$currentNode = end($this->storage);
$currentNode = end($this->_storage);
return ($currentNode) ? $currentNode : null;
break;
case 'adjustedCurrentNode':

Loading…
Cancel
Save