File: Text2Html.php

package info (click to toggle)
roundcube 1.6.13%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,888 kB
  • sloc: javascript: 195,591; php: 76,917; sql: 3,150; sh: 2,882; pascal: 1,079; makefile: 234; xml: 93; perl: 73; ansic: 48; python: 21
file content (220 lines) | stat: -rw-r--r-- 8,853 bytes parent folder | download | duplicates (3)
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
<?php

namespace Roundcube\Tests\Framework;

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

/**
 * Test class to test rcube_text2html class
 */
class Framework_Text2Html extends TestCase
{
    /**
     * Data for test_text2html()
     */
    static function data_text2html()
    {
        $options = [
            'begin'  => '',
            'end'    => '',
            'break'  => '<br>',
            'links'  => false,
            'flowed' => false,
            'delsp'  => false,
            'wrap'   => false,
            'space'  => '_', // replace UTF-8 non-breaking space for simpler testing
            'nobr_start' => '>',
            'nobr_end'   => '<',
        ];

        $data[] = [" aaaa", ">_aaaa<", $options];
        $data[] = ["aa>aa", ">aa&gt;aa<", $options];
        $data[] = ["aaaa aaaa", ">aaaa_aaaa<", $options];
        $data[] = ["aaaa  aaaa", ">aaaa__aaaa<", $options];
        $data[] = ["aaaa   aaaa", ">aaaa___aaaa<", $options];
        $data[] = ["aaaa\taaaa", ">aaaa____aaaa<", $options];
        $data[] = ["aaaa\naaaa", "aaaa<br>aaaa", $options];
        $data[] = ["aaaa\n aaaa", "aaaa<br>>_aaaa<", $options];
        $data[] = ["aaaa\n  aaaa", "aaaa<br>>__aaaa<", $options];
        $data[] = ["aaaa\n   aaaa", "aaaa<br>>___aaaa<", $options];
        $data[] = ["\n", "<br>", $options];
        $data[] = ["\taaaa", ">____aaaa<", $options];
        $data[] = ["\naaaa", "<br>aaaa", $options];
        $data[] = ["\n aaaa", "<br>>_aaaa<", $options];
        $data[] = ["\n  aaaa", "<br>>__aaaa<", $options];
        $data[] = ["\n   aaaa", "<br>>___aaaa<", $options];
        $data[] = ["aaaa\n\nbbbb", "aaaa<br><br>bbbb", $options];
        $data[] = [">aaaa \n>aaaa", "<blockquote>>aaaa_<<br>aaaa</blockquote>", $options];
        $data[] = [">aaaa\n>aaaa", "<blockquote>aaaa<br>aaaa</blockquote>", $options];
        $data[] = [">aaaa \n>bbbb\ncccc dddd", "<blockquote>>aaaa_<<br>bbbb</blockquote>>cccc_dddd<", $options];
        $data[] = ["aaaa-bbbb/cccc", ">aaaa-bbbb/cccc<", $options];
        $data[] = ["aaaa-bbbb\r\tcccc", ">aaaa-bbbb____cccc<", $options];

        $options['flowed'] = true;

        $data[] = [" aaaa", "aaaa", $options];
        $data[] = ["aaaa aaaa", "aaaa aaaa", $options];
        $data[] = ["aaaa  aaaa", "aaaa _aaaa", $options];
        $data[] = ["aaaa   aaaa", "aaaa _ aaaa", $options];
        $data[] = ["aaaa\taaaa", "aaaa _ _aaaa", $options];
        $data[] = ["aaaa\naaaa", "aaaa<br>aaaa", $options];
        $data[] = ["aaaa\n aaaa", "aaaa<br>aaaa", $options];
        $data[] = ["aaaa\n  aaaa", "aaaa<br>_aaaa", $options];
        $data[] = ["aaaa\n   aaaa", "aaaa<br>_ aaaa", $options];
        $data[] = ["\taaaa", "_ _ aaaa", $options];
        $data[] = ["\naaaa", "<br>aaaa", $options];
        $data[] = ["\n aaaa", "<br>aaaa", $options];
        $data[] = ["\n  aaaa", "<br>_aaaa", $options];
        $data[] = ["\n   aaaa", "<br>_ aaaa", $options];
        $data[] = ["aaaa\n\nbbbb", "aaaa<br><br>bbbb", $options];
        $data[] = [">aaaa \n>aaaa", "<blockquote>aaaa aaaa</blockquote>", $options];
        $data[] = [">aaaa\n>aaaa", "<blockquote>aaaa<br>aaaa</blockquote>", $options];
        $data[] = [">aaaa \n>bbbb\ncccc dddd", "<blockquote>aaaa bbbb</blockquote>cccc dddd", $options];
        $data[] = ["\x02\x03", "\x02\x03", $options];

        $options['flowed'] = true;
        $options['delsp']  = true;

        $data[] = [" aaaa", "aaaa", $options];
        $data[] = ["aaaa aaaa", "aaaa aaaa", $options];
        $data[] = ["aaaa  aaaa", "aaaa _aaaa", $options];
        $data[] = ["aaaa   aaaa", "aaaa _ aaaa", $options];
        $data[] = ["aaaa\taaaa", "aaaa _ _aaaa", $options];
        $data[] = ["aaaa\naaaa", "aaaa<br>aaaa", $options];
        $data[] = ["aaaa\n aaaa", "aaaa<br>aaaa", $options];
        $data[] = ["aaaa\n  aaaa", "aaaa<br>_aaaa", $options];
        $data[] = ["aaaa\n   aaaa", "aaaa<br>_ aaaa", $options];
        $data[] = ["\taaaa", "_ _ aaaa", $options];
        $data[] = ["\naaaa", "<br>aaaa", $options];
        $data[] = ["\n aaaa", "<br>aaaa", $options];
        $data[] = ["\n  aaaa", "<br>_aaaa", $options];
        $data[] = ["\n   aaaa", "<br>_ aaaa", $options];
        $data[] = ["aaaa\n\nbbbb", "aaaa<br><br>bbbb", $options];
        $data[] = [">aaaa \n>aaaa", "<blockquote>aaaaaaaa</blockquote>", $options];
        $data[] = [">aaaa\n>aaaa", "<blockquote>aaaa<br>aaaa</blockquote>", $options];
        $data[] = [">aaaa \n>bbbb\ncccc dddd", "<blockquote>aaaabbbb</blockquote>cccc dddd", $options];

        $options['flowed'] = false;
        $options['delsp']  = false;
        $options['wrap']   = true;

        $data[] = [">>aaaa bbbb\n>>\n>>>\n>cccc\n\ndddd eeee",
            "<blockquote><blockquote>aaaa bbbb<br><br><blockquote><br></blockquote></blockquote>cccc</blockquote><br>dddd eeee", $options];
        $data[] = ["\n>>aaaa\n\ndddd",
            "<br><blockquote><blockquote>aaaa</blockquote></blockquote><br>dddd", $options];
        $data[] = ["aaaa\n>bbbb\n>cccc\n\ndddd\n>>test",
            "aaaa<blockquote>bbbb<br>cccc</blockquote><br>dddd<blockquote><blockquote>test</blockquote></blockquote>", $options];

        return $data;
    }

    /**
     * Test text to html conversion
     *
     * @dataProvider data_text2html
     */
    #[DataProvider('data_text2html')]
    function test_text2html($input, $output, $options)
    {
        $t2h = new \rcube_text2html($input, false, $options);

        $html = $t2h->get_html();

        $this->assertEquals($output, $html);
    }

    /**
     * Test XSS issue
     */
    function test_text2html_xss()
    {
        $input = "\n[<script>evil</script>]:##str_replacement_0##\n";
        $t2h = new \rcube_text2html($input);

        $html = $t2h->get_html();

        $expected = "<div class=\"pre\"><br>\n"
            . "[&lt;script&gt;evil&lt;/script&gt;]:##str_replacement_0##<br>\n"
            . "</div>";

        $this->assertEquals($expected, $html);
    }

    /**
     * Test XSS issue
     */
    function test_text2html_xss2()
    {
        $input = "\n[<script>evil</script>] https://google.com\n";
        $t2h = new \rcube_text2html($input);

        $html = $t2h->get_html();

        $expected = "<div class=\"pre\"><br>\n[&lt;script&gt;evil&lt;/script&gt;] "
            . "<a rel=\"noreferrer\" target=\"_blank\" href=\"https://google.com\">https://google.com</a><br>\n"
            . "</div>";

        $this->assertEquals($expected, $html);
    }

    /**
     * Test bug #8021
     */
    function test_text2html_8021()
    {
        $input = "Test1 [1]\n\n[1] http://d1.tld\n\nyou wrote:\n> Test2 [1]\n>\n> [1] http://d2.tld";
        $expected = '<div class="pre">Test1 [<a href="http://d1.tld">1</a>]'
            . "<br>\n<br>\n"
            . '[1] <a href="http://d1.tld">http://d1.tld</a>'
            . "<br>\n<br>\n"
            . 'you wrote:<blockquote>Test2 [<a href="http://d2.tld">1</a>]'
            . "<br>\n<br>\n"
            . '[1] <a href="http://d2.tld">http://d2.tld</a></blockquote></div>';

        $t2h = new \rcube_text2html($input);
        $html = $t2h->get_html();
        $html = preg_replace('/ (rel|target)="(noreferrer|_blank)"/', '', $html);

        $this->assertEquals($expected, $html);
    }

    /**
     * Test patches/diffs handling
     */
    function test_text2html_patches_handling()
    {
        $input = "Start\n"
            . "diff --git a/test.txt b/test.txt\n"
            . "index 7642f44b9..6ce0170aa 100644\n"
            . "--- a/test.txt\n"
            . "+++ b/test.txt\n"
            . "@@ -1982,7 +1982,7 @@ class test\n"
            . " test1\n"
            . " test2\n"
            . " test3\n"
            . "-test4\n"
            . "+test5\n"
            . " \n"
            . "End";

        $expected = "<div class=\"pre\">Start<br>\n"
            . "diff --git a/test.txt b/test.txt<br>\n"
            . "index 7642f44b9..6ce0170aa 100644<br>\n"
            . "<span style=\"white-space:nowrap\">---_a/test.txt</span><br>\n"
            . "<span style=\"white-space:nowrap\">+++_b/test.txt</span><br>\n"
            . "<span style=\"white-space:nowrap\">@@_-1982,7_+1982,7_@@_class_test</span><br>\n"
            . "<span style=\"white-space:nowrap\">_test1</span><br>\n"
            . "<span style=\"white-space:nowrap\">_test2</span><br>\n"
            . "<span style=\"white-space:nowrap\">_test3</span><br>\n"
            . "<span style=\"white-space:nowrap\">-test4</span><br>\n"
            . "<span style=\"white-space:nowrap\">+test5</span><br>\n"
            . "<span style=\"white-space:nowrap\">_</span><br>\n"
            . "End</div>";

        $t2h = new \rcube_text2html($input, false, ['space' => '_']);
        $html = $t2h->get_html();

        $this->assertEquals($expected, $html);
    }
}