2 namespace Masterminds\HTML5\Tests\Parser;
4 use Masterminds\HTML5\Elements;
5 use Masterminds\HTML5\Parser\EventHandler;
8 * This testing class gathers events from a parser and builds a stack of events.
9 * It is useful for checking the output of a tokenizer.
13 * The startTag event also kicks the parser into TEXT_RAW when it encounters
14 * script or pre tags. This is to match the behavior required by the HTML5 spec,
15 * which says that the tree builder must tell the tokenizer when to switch states.
17 class EventStack implements EventHandler
22 public function __construct()
24 $this->stack = array();
28 * Get the event stack.
30 public function events()
35 public function depth()
37 return count($this->stack);
40 public function get($index)
42 return $this->stack[$index];
45 protected function store($event, $data = null)
47 $this->stack[] = array(
53 public function doctype($name, $type = 0, $id = null, $quirks = false)
61 $this->store('doctype', $args);
64 public function startTag($name, $attributes = array(), $selfClosing = false)
66 $args = func_get_args();
67 $this->store('startTag', $args);
68 if ($name == 'pre' || $name == 'script') {
69 return Elements::TEXT_RAW;
73 public function endTag($name)
75 $this->store('endTag', array(
80 public function comment($cdata)
82 $this->store('comment', array(
87 public function cdata($data)
89 $this->store('cdata', func_get_args());
92 public function text($cdata)
94 // fprintf(STDOUT, "Received TEXT event with: " . $cdata);
95 $this->store('text', array(
100 public function eof()
105 public function parseError($msg, $line, $col)
107 // throw new EventStackParseError(sprintf("%s (line %d, col %d)", $msg, $line, $col));
108 // $this->store(sprintf("%s (line %d, col %d)", $msg, $line, $col));
109 $this->store('error', func_get_args());
112 public function processingInstruction($name, $data = null)
114 $this->store('pi', func_get_args());