File: Html2text.php

package info (click to toggle)
roundcube 1.6.11%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 44,868 kB
  • sloc: javascript: 195,588; php: 76,818; sql: 3,150; sh: 2,882; pascal: 1,079; makefile: 234; xml: 93; perl: 73; ansic: 48; python: 21
file content (346 lines) | stat: -rw-r--r-- 10,684 bytes parent folder | download
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php

namespace Roundcube\Tests\Framework;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

/**
 * Test class to test rcube_html2text class
 */
class Framework_Html2text extends TestCase
{

    static function data_html2text()
    {
        return [
            0 => [
                'title' => 'Test entry',
                'in'    => '',
                'out'   => '',
            ],
            1 => [
                'title' => 'Basic HTML entities',
                'in'    => '&quot;&amp;',
                'out'   => '"&',
            ],
            2 => [
                'title' => 'HTML entity string',
                'in'    => '&amp;quot;',
                'out'   => '&quot;',
            ],
            3 => [
                'title' => 'HTML entity in H1 tag',
                'in'    => '<h1>&#347;</h1>', // ś
                'out'   => "Ś\n\n", // upper ś
            ],
            4 => [
                'title' => 'H1 tag to upper-case conversion',
                'in'    => '<h1>ś</h1>',
                'out'   => "Ś\n\n",
            ],
            5 => [
                'title' => 'H1 inside B tag',
                'in'    => '<b><h1>&#347;</h1></b>',
                'out'   => "Ś\n\n",
            ],
            6 => [
                'title' => 'Don\'t remove non-printable chars',
                'in'    => chr(0x002).chr(0x003),
                'out'   => chr(0x002).chr(0x003),
            ],
            7 => [
                'title' => 'Remove spaces after <br>',
                'in'    => 'test<br>  test',
                'out'   => "test\ntest",
            ],
            8 => [
                'title' => '&nbsp; handling test',
                'in'    => '<div>eye: &nbsp;&nbsp;test<br /> test: &nbsp;&nbsp;test</div>',
                'out'   => "eye:   test\ntest:   test",
            ],
            9 => [
                'title' => 'HTML entity in STRONG tag',
                'in'    => '<strong>&#347;</strong>', // ś
                'out'   => 'ś',
            ],
            10 => [
                'title' => 'STRONG tag to upper-case conversion',
                'in'    => '<strong>ś</strong>',
                'out'   => 'ś',
            ],
            11 => [
                'title' => 'STRONG inside B tag',
                'in'    => '<b><strong>&#347;</strong></b>',
                'out'   => 'ś',
            ],
            12 => [
                'title' => 'Full HTML handling (html tag only)',
                'in'    => "<html>\n<p>test</p></html>",
                'out'   => 'test',
            ],
            13 => [
                'title' => 'Full HTML handling (html+head tags)',
                'in'    => '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head>'
                    . "\n<p>test</p></html>\n",
                'out'   => 'test',
            ],
            14 => [
                'title' => 'Full HTML handling (html+head+body tags)',
                'in'    => '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head>' . "\n"
                    . '<body style="font-size: 10pt; font-family: Verdana,Geneva,sans-serif">' . "\n"
                    . '<p>test</p>'
                    . '</body></html>',
                'out'   => 'test',
            ],
        ];
    }

    /**
     * @dataProvider data_html2text
     */
    #[DataProvider('data_html2text')]
    function test_html2text($title, $in, $out)
    {
        $ht = new \rcube_html2text(null, false, \rcube_html2text::LINKS_NONE);

        $ht->set_html($in);
        $res = $ht->get_text();

        $this->assertEquals($out, $res, $title);
    }

    /**
     * Test blockquote tags handling
     */
    function test_multiple_blockquotes()
    {
        $html = <<<EOF
<br>Begin<br><blockquote>OUTER BEGIN<blockquote>INNER 1<br></blockquote><div><br></div><div>Par 1</div>
<blockQuote>INNER 2</blockquote><div><br></div><div>Par 2</div>
<div><br></div><div>Par 3</div><div><br></div>
<blockquote>INNER 3</blockquote>OUTER END</blockquote>
EOF;
        $ht = new \rcube_html2text($html, false, \rcube_html2text::LINKS_NONE);
        $res = $ht->get_text();

        $this->assertStringContainsString('>> INNER 1', $res, 'Quote inner');
        $this->assertStringContainsString('>> INNER 3', $res, 'Quote inner');
        $this->assertStringContainsString('> OUTER END', $res, 'Quote outer');
    }

    function test_broken_blockquotes()
    {
        // no end tag
        $html = <<<EOF
Begin<br>
<blockquote>QUOTED TEXT
<blockquote>
NO END TAG FOUND
EOF;
        $ht = new \rcube_html2text($html, false, \rcube_html2text::LINKS_NONE);
        $res = $ht->get_text();

        $this->assertStringContainsString('QUOTED TEXT NO END TAG FOUND', $res, 'No quoting on invalid html');

        // with some (nested) end tags
        $html = <<<EOF
Begin<br>
<blockquote>QUOTED TEXT
<blockquote>INNER 1</blockquote>
<blockquote>INNER 2</blockquote>
NO END TAG FOUND
EOF;
        $ht = new \rcube_html2text($html, false, \rcube_html2text::LINKS_NONE);
        $res = $ht->get_text();

        $this->assertStringContainsString('QUOTED TEXT INNER 1 INNER 2 NO END', $res, 'No quoting on invalid html');
    }

    /**
     * Test links handling
     */
    function test_links()
    {
        $html     = '<a href="http://test.com">content</a>';
        $expected = 'content [1]

Links:
------
[1] http://test.com
';

        $ht = new \rcube_html2text($html, false, \rcube_html2text::LINKS_END);
        $res = $ht->get_text();

        $this->assertSame($expected, $res, 'Links list');

        // href == content (#1490434)
        $html     = '<a href="http://test.com">http://test.com</a>';
        $expected = 'http://test.com';

        $ht = new \rcube_html2text($html, false, \rcube_html2text::LINKS_END);
        $res = $ht->get_text();

        $this->assertSame($expected, $res, 'Skip link with href == content');

        // HTML entities in links
        $html     = '<a href="http://test.com?test1&amp;test2">test3&amp;test4</a>';
        $expected = 'test3&test4 [1]

Links:
------
[1] http://test.com?test1&test2
';

        $ht = new \rcube_html2text($html, false, \rcube_html2text::LINKS_END);
        $res = $ht->get_text();

        $this->assertSame($expected, $res, 'Links with HTML entities');
    }

    /**
     * Test links handling with backward compatibility boolean flag
     */
    function test_links_bc_with_boolean()
    {
        $html     = '<a href="http://test.com">content</a>';
        $expected = 'content [1]

Links:
------
[1] http://test.com
';

        $ht = new \rcube_html2text($html, false, true);
        $res = $ht->get_text();

        $this->assertSame($expected, $res, 'Links list');

        // href == content (#1490434)
        $html     = '<a href="http://test.com">http://test.com</a>';
        $expected = 'http://test.com';

        $ht = new \rcube_html2text($html, false, true);
        $res = $ht->get_text();

        $this->assertSame($expected, $res, 'Skip link with href == content');
    }

    /**
     * Test links inline handling
     */
    function test_links_inline()
    {
        $html     = '<a href="http://test.com">content</a>';
        $expected = 'content <http://test.com>';

        $ht = new \rcube_html2text($html, false, \rcube_html2text::LINKS_INLINE);
        $res = $ht->get_text();

        $this->assertSame($expected, $res, 'Links Inline');

        // href == content (#1490434)
        $html     = '<a href="http://test.com">http://test.com</a>';
        $expected = 'http://test.com';

        $ht = new \rcube_html2text($html, false, \rcube_html2text::LINKS_INLINE);
        $res = $ht->get_text();

        $this->assertSame($expected, $res, 'Skip link with href == content');
    }

    /**
     * Test <a> links handling when not using link list (#5795)
     *
     * @dataProvider data_links_no_list
     */
    #[DataProvider('data_links_no_list')]
    function test_links_no_list($input, $output)
    {
        $h2t = new \rcube_html2text($input, false, \rcube_html2text::LINKS_NONE);
        $res = $h2t->get_text();

        $this->assertSame($output, $res, 'Links handling');
    }

    /**
     * Test <a> links handling when not using link list (#5795) with backward compatibility boolean flag
     *
     * @dataProvider data_links_no_list
     */
    #[DataProvider('data_links_no_list')]
    function test_links_no_list_bc_with_boolean($input, $output)
    {
        $h2t = new \rcube_html2text($input, false, false);
        $res = $h2t->get_text();

        $this->assertSame($output, $res, 'Links handling');
    }

    static function data_links_no_list()
    {
        return [
            [
                'this is <a href="http://test.com">content</a>',
                'this is content',
            ],
            [
                'this is <a href="#test">content&amp;&nbsp;test</a>',
                'this is content& test',
            ],
            [
                'this is <a href="">content</a>',
                'this is content',
            ],
            [
                'this is <a href="http://test.com"><img src=http://test.com/image" alt="image" /></a>',
                'this is http://test.com',
            ],
        ];
    }

    /**
     * Test links fallback to default handling
     */
    function test_links_fallback_to_default_link_list()
    {
        $html     = '<a href="http://test.com">content</a>';
        $expected = 'content [1]

Links:
------
[1] http://test.com
';

        $ht = new \rcube_html2text($html, false);
        $res = $ht->get_text();

        $this->assertSame($expected, $res, 'Links list as default (doLinks not set)');

        $ht = new \rcube_html2text($html, false, mt_rand(3, 9999));
        $res = $ht->get_text();

        $this->assertSame($expected, $res, 'Links list as default (doLinks greater than 3)');

        $ht = new \rcube_html2text($html, false, mt_rand(-9999, -1));
        $res = $ht->get_text();

        $this->assertSame($expected, $res, 'Links list as default (doLinks lower than 0)');
    }

    /**
     * Test huge HTML content (#8137)
     */
    function test_memory_fix_8137()
    {
        // create >1MB input
        $src = 'data:image/png;base64,' . str_repeat('1234567890abcdefghijklmnopqrstuvwxyz', 50000);
        $input = 'test<body><p>test1</p><p>test2</p><img src="' . $src . '" /><p>test3</p>';

        $h2t = new \rcube_html2text($input, false, \rcube_html2text::LINKS_NONE);
        $res = $h2t->get_text();

        $this->assertSame("test1\n\ntest2\n\ntest3", $res, 'Huge input');
    }
}