summaryrefslogtreecommitdiff
path: root/test/HTML5/Parser/EventStack.php
blob: 050cb5ae40e7eff08607987bd24413d15e14cf87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
namespace Masterminds\HTML5\Tests\Parser;

use Masterminds\HTML5\Elements;
use Masterminds\HTML5\Parser\EventHandler;

/**
 * This testing class gathers events from a parser and builds a stack of events.
 * It is useful for checking the output of a tokenizer.
 *
 * IMPORTANT:
 *
 * The startTag event also kicks the parser into TEXT_RAW when it encounters
 * script or pre tags. This is to match the behavior required by the HTML5 spec,
 * which says that the tree builder must tell the tokenizer when to switch states.
 */
class EventStack implements EventHandler {
  protected $stack;

  public function __construct() {
    $this->stack = array();
  }

  /**
   * Get the event stack.
   */
  public function events() {
    return $this->stack;
  }

  public function depth() {
    return count($this->stack);
  }

  public function get($index) {
    return $this->stack[$index];
  }

  protected function store($event, $data = NULL) {
    $this->stack[] = array(
      'name' => $event,
      'data' => $data,
    );
  }

  public function doctype($name, $type = 0, $id = NULL, $quirks = FALSE) {
    $args = array($name, $type, $id, $quirks);
    $this->store('doctype', $args);
  }

  public function startTag($name, $attributes = array(), $selfClosing = FALSE) {
    $args = func_get_args();
    $this->store('startTag', $args);
    if ($name == 'pre' || $name == 'script') {
      return Elements::TEXT_RAW;
    }
  }

  public function endTag($name) {
    $this->store('endTag', array($name));
  }

  public function comment($cdata) {
    $this->store('comment', array($cdata));
  }

  public function cdata($data) {
    $this->store('cdata', func_get_args());
  }

  public function text($cdata) {
    //fprintf(STDOUT, "Received TEXT event with: " . $cdata);
    $this->store('text', array($cdata));
  }

  public function eof() {
    $this->store('eof');
  }

  public function parseError($msg, $line, $col) {
    //throw new EventStackParseError(sprintf("%s (line %d, col %d)", $msg, $line, $col));
    //$this->store(sprintf("%s (line %d, col %d)", $msg, $line, $col));
    $this->store('error', func_get_args());
  }

  public function processingInstruction($name, $data = NULL) {
    $this->store('pi', func_get_args());
  }

}