summaryrefslogtreecommitdiff
path: root/test/HTML5/Html5Test.php
blob: 88875a71fc88c3c52417fde15b9102715b8b9cee (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
<?php
namespace HTML5\Tests;

require_once 'TestCase.php';

class Html5Test extends TestCase {

  public function testLoad() {
    $dom = \HTML5::load(__DIR__ . '/Html5Test.html');
    $this->assertInstanceOf('\DOMDocument', $dom);
    $this->assertEmpty($dom->errors);
  }

  public function testLoadHTML() {
    $contents = file_get_contents(__DIR__ . '/Html5Test.html');
    $dom = \HTML5::loadHTML($contents);
    $this->assertInstanceOf('\DOMDocument', $dom);
    $this->assertEmpty($dom->errors);
  }

  public function testSaveHTML() {
    $dom = \HTML5::load(__DIR__ . '/Html5Test.html');
    $this->assertInstanceOf('\DOMDocument', $dom);
    $this->assertEmpty($dom->errors);

    $saved = \HTML5::saveHTML($dom);
    $this->assertRegExp('|<p>This is a test.</p>|', $saved);
  }

  public function testSave() {
    $dom = \HTML5::load(__DIR__ . '/Html5Test.html');
    $this->assertInstanceOf('\DOMDocument', $dom);
    $this->assertEmpty($dom->errors);

    // Test resource
    $file = fopen('php://temp', 'w');
    \HTML5::save($dom, $file);
    $content = stream_get_contents($file, -1, 0);
    $this->assertRegExp('|<p>This is a test.</p>|', $content);

    // Test file
    $tmpfname = tempnam(sys_get_temp_dir(), "html5-php");
    \HTML5::save($dom, $tmpfname);
    $content = file_get_contents($tmpfname);
    $this->assertRegExp('|<p>This is a test.</p>|', $content);
    unlink($tmpfname);
  }

  // This test reads a document into a dom, turn the dom into a document,
  // then tries to read that document again. This makes sure we are reading,
  // and generating a document that works at a high level.
  public function testItWorks() {
    $dom = \HTML5::load(__DIR__ . '/Html5Test.html');
    $this->assertInstanceOf('\DOMDocument', $dom);
    $this->assertEmpty($dom->errors);

    $saved = \HTML5::saveHTML($dom);

    $dom2 = \HTML5::loadHTML($saved);
    $this->assertInstanceOf('\DOMDocument', $dom2);
    $this->assertEmpty($dom2->errors);
  }

  public function testConfig() {
    $options = \HTML5::options();
    $this->assertEquals(FALSE, $options['encode_entities']);
    $this->assertEquals('\HTML5\Serializer\OutputRules', $options['output_rules']);

    \HTML5::setOption('foo', 'bar');
    \HTML5::setOption('encode_entities', TRUE);
    $options = \HTML5::options();
    $this->assertEquals('bar', $options['foo']);
    $this->assertEquals(TRUE, $options['encode_entities']);

    // Need to reset to original so future tests pass as expected.
    \HTML5::setOption('encode_entities', FALSE);
  }

  public function testSvg() {
    $dom = \HTML5::loadHTML('<!doctype html>
      <html lang="en">
        <body>
          <div id="foo" class="bar baz">foo bar baz</div>
          <svg width="150" height="100" viewBox="0 0 3 2">
            <rect width="1" height="2" x="0" fill="#008d46" />
            <rect width="1" height="2" x="1" fill="#ffffff" />
            <rect width="1" height="2" x="2" fill="#d2232c" />
            <text font-family="Verdana" font-size="32">
              <textPath xlink:href="#Foo">
                Test Text.
              </textPath>
            </text>
          </svg>
        </body>
      </html>');

    $this->assertEmpty($dom->errors);

    // Test a mixed case attribute.
    $list = $dom->getElementsByTagName('svg');
    $this->assertNotEmpty($list);
    $svg = $list->item(0);
    $this->assertEquals("0 0 3 2", $svg->getAttribute('viewBox'));
    $this->assertFalse($svg->hasAttribute('viewbox'));

    // Test a mixed case tag.
    // Note: getElementsByTagName is not case sensetitive.
    $list = $dom->getElementsByTagName('textPath');
    $this->assertNotEmpty($list);
    $textPath = $list->item(0);
    $this->assertEquals('textPath', $textPath->tagName);
    $this->assertNotEquals('textpath', $textPath->tagName);

    $html = \HTML5::saveHTML($dom);
    $this->assertRegExp('|<svg width="150" height="100" viewBox="0 0 3 2">|',$html);
    $this->assertRegExp('|<rect width="1" height="2" x="0" fill="#008d46" />|',$html);

  }

}