summaryrefslogtreecommitdiffstats
path: root/vendor/fguillot/picofeed/tests/Filter/HtmlFilterTest.php
blob: 8d64cbc1de32b7a628c9d8fde7f5197c86ede321 (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
<?php

namespace PicoFeed\Filter;

use PHPUnit_Framework_TestCase;

class HtmlFilterTest extends PHPUnit_Framework_TestCase
{
    public function testExecute()
    {
        $html = '<!DOCTYPE html><html><head>
                <meta name="title" content="test">
                </head><body><p>boo<br/><strong>foo</strong>.</p></body></html>';

        $filter = new Html($html, 'http://www.google.ca/');

        $this->assertEquals('<p>boo<br/><strong>foo</strong>.</p>', $filter->execute());
    }

    public function testIframe()
    {
        $data = '<iframe src="http://www.kickstarter.com/projects/lefnire/habitrpg-mobile/widget/video.html" height="480" width="640" frameborder="0"></iframe>';

        $f = new Html($data, 'http://blabla');
        $this->assertEmpty($f->execute());

        $data = '<iframe src="http://www.youtube.com/bla" height="480" width="640" frameborder="0"></iframe>';
        $expected = '<iframe src="https://www.youtube.com/bla" height="480" width="640" frameborder="0"></iframe>';

        $f = new Html($data, 'http://blabla');
        $this->assertEquals($expected, $f->execute());
    }

    public function testClearScriptAttributes()
    {
        $data = '<div><script>this is the content</script><script>blubb content</script><p>something</p></div><p>hi</p>';

        $f = new Html($data, 'http://blabla');
        $expected = '<p>something</p><p>hi</p>';
        $this->assertEquals($expected, $f->execute());
    }

    public function testClearStyleAttributes()
    {
        $data = '<div><style>this is the content</style><style>blubb content</style><p>something</p></div><p>hi</p>';

        $f = new Html($data, 'http://blabla');
        $expected = '<p>something</p><p>hi</p>';
        $this->assertEquals($expected, $f->execute());
    }

    public function testEmptyTags()
    {
        $data = <<<EOD
<div>
<a href="file://path/to/a/file">
    <img src="http://example.com/image.png" />
</a>
</div>
EOD;
        $f = new Html($data, 'http://blabla');
        $output = $f->execute();

        $this->assertEquals('<img src="http://example.com/image.png"/>', $output);
    }

    public function testBadAttributes()
    {
        $data = '<iframe src="http://www.youtube.com/bla" height="480px" width="100%" frameborder="0"></iframe>';

        $f = new Html($data, 'http://blabla');
        $this->assertEquals('<iframe src="https://www.youtube.com/bla" frameborder="0"></iframe>', $f->execute());
    }

    public function testRelativeScheme()
    {
        $f = new Html('<a href="//linuxfr.org">link</a>', 'http://blabla');
        $this->assertEquals('<a href="http://linuxfr.org/" rel="noreferrer" target="_blank">link</a>', $f->execute());
    }

    public function testAttributes()
    {
        $f = new Html('<img src="foo" title="\'quote" alt="\'quote" data-src="bar" data-truc="boo"/>', 'http://blabla');
        $this->assertEquals('<img src="http://blabla/foo" title="&#039;quote" alt="&#039;quote"/>', $f->execute());

        $f = new Html('<img src="foo&bar=\'quote"/>', 'http://blabla');
        $this->assertEquals('<img src="http://blabla/foo&amp;bar=&#039;quote"/>', $f->execute());

        $f = new Html("<time datetime='quote\"here'>bla</time>", 'http://blabla');
        $this->assertEquals('<time datetime="quote&quot;here">bla</time>', $f->execute());
    }

    public function testCode()
    {
        $data = '<pre><code>HEAD / HTTP/1.1
Accept: text/html
Accept-Encoding: gzip, deflate, compress
Host: www.amazon.com
User-Agent: HTTPie/0.6.0



<strong>HTTP/1.1 405 MethodNotAllowed</strong>
Content-Encoding: gzip
Content-Type: text/html; charset=ISO-8859-1
Date: Mon, 15 Jul 2013 02:05:59 GMT
Server: Server
Set-Cookie: skin=noskin; path=/; domain=.amazon.com; expires=Mon, 15-Jul-2013 02:05:59 GMT
Vary: Accept-Encoding,User-Agent
<strong>allow: POST, GET</strong>
x-amz-id-1: 11WD3K15FC268R5GBJY5
x-amz-id-2: DDjqfqz2ZJufzqRAcj1mh+9XvSogrPohKHwXlo8IlkzH67G6w4wnjn9HYgbs4uI0
</code></pre>';

        $f = new Html($data, 'http://blabla');
        $this->assertEquals($data, $f->execute());
    }

    public function testRemoveNoBreakingSpace()
    {
        $f = new Html('<p>&nbsp;&nbsp;truc</p>', 'http://blabla');
        $this->assertEquals('<p>  truc</p>', $f->execute());
    }

    public function testRemoveEmptyTags()
    {
        $f = new Html('<p>toto</p><p></p><br/>', 'http://blabla');
        $this->assertEquals('<p>toto</p><br/>', $f->execute());

        $f = new Html('<p> </p>', 'http://blabla');
        $this->assertEquals('', $f->execute());

        $f = new Html('<p>&nbsp;</p>', 'http://blabla');
        $this->assertEquals('', $f->execute());
    }

    public function testRemoveEmptyTable()
    {
        $f = new Html('<table><tr><td>    </td></tr></table>', 'http://blabla');
        $this->assertEquals('', $f->execute());

        $f = new Html('<table><tr></tr></table>', 'http://blabla');
        $this->assertEquals('', $f->execute());
    }
}