summaryrefslogtreecommitdiff
path: root/test/HTML5/Serializer/OutputRulesTest.php
blob: 4e1ce92dc1e8ed56045d544d0576889b85897237 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace HTML5\Tests;

use \HTML5\Serializer\OutputRules;
use \HTML5\Serializer\Traverser;
use \HTML5\Parser;

require_once __DIR__ . '/../TestCase.php';

class OutputRulesTest extends \HTML5\Tests\TestCase {

  protected $markup = '<!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <title>Test</title>
      </head>
      <body>
        <p>This is a test.</p>
      </body>
    </html>';

  /**
   * Using reflection we make a protected method accessible for testing.
   * 
   * @param string $name
   *   The name of the method on the Traverser class to test.
   *
   * @return \ReflectionMethod
   *   \ReflectionMethod for the specified method
   */
  function getProtectedMethod($name) {
    $class = new \ReflectionClass('\HTML5\Serializer\OutputRules');
    $method = $class->getMethod($name);
    $method->setAccessible(true);
    return $method;
  }

  function getOutputRules($options = array()) {
    $options = $options + \HTML5::options();
    $stream = fopen('php://temp', 'w');
    $dom = \HTML5::loadHTML($this->markup);
    $t = new Traverser($dom, $stream, $options);

    $o = new OutputRules($t, $stream, $options);

    return array($o, $stream);
  }

  function testDocument() {
    $dom = \HTML5::loadHTML('<!doctype html><html lang="en"><body>foo</body></html>');

    $stream = fopen('php://temp', 'w');
    $t = new Traverser($dom, $stream, \HTML5::options());
    $o = new OutputRules($t, $stream, \HTML5::options());

    $o->document($dom);
    $this->assertEquals("<!DOCTYPE html>\n<html lang=\"en\"><body>foo</body></html>\n", stream_get_contents($stream, -1, 0));
  }

  function testDoctype() {
    $dom = \HTML5::loadHTML('<!doctype html><html lang="en"><body>foo</body></html>');

    $stream = fopen('php://temp', 'w');
    $t = new Traverser($dom, $stream, \HTML5::options());
    $o = new OutputRules($t, $stream, \HTML5::options());

    $m = $this->getProtectedMethod('doctype');
    $m->invoke($o, 'foo');
    $this->assertEquals("<!DOCTYPE html>\n", stream_get_contents($stream, -1, 0));
  }


  function testElement() {
    $dom = \HTML5::loadHTML('<!doctype html>
    <html lang="en">
      <body>
        <div id="foo" class="bar baz">foo bar baz</div>
      </body>
    </html>');

    $stream = fopen('php://temp', 'w');
    $t = new Traverser($dom, $stream, \HTML5::options());
    $o = new OutputRules($t, $stream, \HTML5::options());

    $list = $dom->getElementsByTagName('div');
    $o->element($list->item(0));
    $this->assertEquals('<div id="foo" class="bar baz">foo bar baz</div>', stream_get_contents($stream, -1, 0));
  }

  function testCData() {
    $dom = \HTML5::loadHTML('<!doctype html>
    <html lang="en">
      <body>
        <div><![CDATA[bar]]></div>
      </body>
    </html>');

    $stream = fopen('php://temp', 'w');
    $t = new Traverser($dom, $stream, \HTML5::options());
    $o = new OutputRules($t, $stream, \HTML5::options());

    $list = $dom->getElementsByTagName('div');
    $o->cdata($list->item(0)->childNodes->item(0));
    $this->assertEquals('<![CDATA[bar]]>', stream_get_contents($stream, -1, 0));
  }

  function testComment() {
    $dom = \HTML5::loadHTML('<!doctype html>
    <html lang="en">
      <body>
        <div><!-- foo --></div>
      </body>
    </html>');

    $stream = fopen('php://temp', 'w');
    $t = new Traverser($dom, $stream, \HTML5::options());
    $o = new OutputRules($t, $stream, \HTML5::options());

    $list = $dom->getElementsByTagName('div');
    $o->comment($list->item(0)->childNodes->item(0));
    $this->assertEquals('<!-- foo -->', stream_get_contents($stream, -1, 0));
  }

  function testText() {
    $dom = \HTML5::loadHTML('<!doctype html>
    <html lang="en">
      <head>
        <script>baz();</script>
      </head>
    </html>');

    $stream = fopen('php://temp', 'w');
    $t = new Traverser($dom, $stream, \HTML5::options());
    $o = new OutputRules($t, $stream, \HTML5::options());

    $list = $dom->getElementsByTagName('script');
    $o->text($list->item(0)->childNodes->item(0));
    $this->assertEquals('baz();', stream_get_contents($stream, -1, 0));
  }

  function testNl() {
    list($o, $s) = $this->getOutputRules();

    $m = $this->getProtectedMethod('nl');
    $m->invoke($o);
    $this->assertEquals(PHP_EOL, stream_get_contents($s, -1, 0));
  }

  function testWr() {
    list($o, $s) = $this->getOutputRules();

    $m = $this->getProtectedMethod('wr');
    $m->invoke($o, 'foo');
    $this->assertEquals('foo', stream_get_contents($s, -1, 0));
  }

  function testEnc() {

    // Test basic escaping of text.
    $tests = array(
      '&\'<>"' => '&amp;&#039;&lt;&gt;&quot;',
      'This + is. a < test' => 'This + is. a &lt; test',
    );

    list($o, $s) = $this->getOutputRules();
    $m = $this->getProtectedMethod('enc');
    foreach ($tests as $test => $expected) {
      $this->assertEquals($expected, $m->invoke($o, $test));
    }

    list($o, $s) = $this->getOutputRules(array('encode_entities' => TRUE));
    $m = $this->getProtectedMethod('enc');

    $this->assertEquals('&period;&plus;&num;', $m->invoke($o, '.+#'));
  }

}