Browse Source

Decoupled Stack from Parser

split-manual
Dustin Wilson 6 years ago
parent
commit
37205029a3
  1. 9
      lib/ActiveFormattingElementsList.php
  2. 2
      lib/DataStream.php
  3. 6
      lib/Parser.php
  4. 9
      lib/Stack.php

9
lib/ActiveFormattingElementsList.php

@ -16,6 +16,11 @@ namespace dW\HTML5;
# can be created for that token if necessary.
class ActiveFormattingElementsList implements \ArrayAccess {
protected $_storage = [];
protected $stack;
public function __construct(Stack $stack) {
$this->stack = $stack;
}
public function offsetSet($offset, $value) {
if ($offset < 0) {
@ -132,7 +137,7 @@ class ActiveFormattingElementsList implements \ArrayAccess {
# 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'], Parser::$instance->stack)) {
if ($entry instanceof ActiveFormattingElementMarker || in_array($entry['element'], $this->stack)) {
return;
}
@ -153,7 +158,7 @@ class ActiveFormattingElementsList implements \ArrayAccess {
# 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'], Parser::$instance->stack)) {
if (!$entry instanceof ActiveFormattingElementMarker && !in_array($entry['element'], $this->stack)) {
goto rewind;
}

2
lib/DataStream.php

@ -109,7 +109,7 @@ class DataStream
// OPTIMIZATION: When this spec states to return a character token of any kind this
// method will just return the character. The token will be emitted from
// Parser::tokenize() instead. Likewise, if the spec states to return nothing this
// Parser::parse() instead. Likewise, if the spec states to return nothing this
// method will instead return '&' because every single use of "tokenizing a
// character reference" in the spec this emits a '&' character token upon failure.

6
lib/Parser.php

@ -98,8 +98,6 @@ class Parser {
protected function __construct() {
$this->insertionMode = static::INITIAL_MODE;
$this->quirksMode = static::QUIRKS_MODE_OFF;
$this->stack = new Stack();
$this->activeFormattingElementsList = new ActiveFormattingElementsList();
static::$instance = $this;
}
@ -129,6 +127,10 @@ class Parser {
// work on basic latin characters. Used extensively when tokenizing.
setlocale(LC_CTYPE, 'en_US.UTF8');
// Initialize the stack of open elements.
static::$instance->stack = new Stack(static::$instance->fragmentCase, static::$instance->fragmentContext);
// Initialize the list of active formatting elements.
static::$instance->activeFormattingElementsList = new ActiveFormattingElementsList(static::$instance->stack);
// Initialize the tokenizer.
static::$instance->tokenizer = new Tokenizer(static::$instance->data, static::$instance->stack);
// Initialize the parse error handler.

9
lib/Stack.php

@ -4,6 +4,13 @@ namespace dW\HTML5;
class Stack implements \ArrayAccess {
protected $_storage = [];
protected $fragmentCase;
protected $fragmentContext;
public function __construct(bool $fragmentCase = false, $fragmentContext = null) {
$this->fragmentCase = $fragmentCase;
$this->fragmentContext = $fragmentContext;
}
public function offsetSet($offset, $value) {
if ($offset < 0) {
@ -76,7 +83,7 @@ class Stack implements \ArrayAccess {
# the HTML fragment parsing algorithm and the stack of open elements has only one
# element in it (fragment case); otherwise, the adjusted current node is the
# current node.
return (Parser::$instance->fragmentCase && $this->length === 1) ? Parser::$instance->fragmentContext : $this->currentNode;
return ($this->fragmentCase && $this->length === 1) ? $this->fragmentContext : $this->currentNode;
break;
case 'adjustedCurrentNodeNamespace':
$adjustedCurrentNode = $this->adjustedCurrentNode;

Loading…
Cancel
Save