scriptindex: Fix weird error cases
[xapian.git] / xapian-applications / omega / htmlparsetest.cc
blobcbd89aee30cc984ce8584b1c9ab544995009731b
1 /** @file
2 * @brief test the MyHtmlParser class
3 */
4 /* Copyright (C) 2006,2008,2011,2012,2013,2015,2016,2018,2020 Olly Betts
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19 * USA
22 #include <config.h>
24 #include <cstdlib>
25 #include <cstring>
26 #include <iostream>
27 #include <string>
29 #include "myhtmlparse.h"
31 using namespace std;
33 struct testcase {
34 const char * html;
35 const char * dump;
36 const char * title;
37 const char * keywords;
38 const char * sample;
41 // Wide character test data is signalled by a single leading nul and terminated
42 // by a double nul.
43 #define WIDE(X) "\0" X "\0"
45 static const testcase tests[] = {
46 { "<body>test<!--htdig_noindex-->icle<!--/htdig_noindex-->s</body>",
47 "tests", "", "", "" },
48 { "<body>test<!--htdig_noindex-->ing</body>", "test", "", "", "" },
49 { "hello<!-- bl>ah --> world", "hello world", "", "", "" },
50 { "hello<!-- blah > world", "hello world", "", "", "" },
51 { "<script>\nif (a<b) a = b;</script>test", "test", "", "", "" },
52 // Regression test for bug first noticed in 1.0.0 (but present earlier).
53 { "<b>not</b>\n<b>able</b>", "not able", "", "", "" },
54 // Check that whitespace is handled as intended.
55 { " <b>not </b>\n<b>\table\t</b>\r\n", "not able", "", "", "" },
56 { "<html><head><title>\xc2\xae</title></head><body>\xc2\xa3</body></html>", "\xc3\x82\xc2\xa3", "\xc3\x82\xc2\xae", "", "" },
57 { "<html><head><meta http-equiv=Content-Type content=\"text/html;charset=iso-8859-1\"><title>\xc2\xae</title></head><body>\xc2\xa3</body></html>", "\xc3\x82\xc2\xa3", "\xc3\x82\xc2\xae", "", "" },
58 { "<html><head><meta http-equiv=Content-Type content=\"text/html;charset=utf-8\"><title>\xc2\xae</title></head><body>\xc2\xa3</body></html>", "\xc2\xa3", "\xc2\xae", "", "" },
59 { "<html><head><meta charset='utf-8'><title>\xc2\xae</title></head><body>\xc2\xa3</body></html>", "\xc2\xa3", "\xc2\xae", "", "" },
60 { "<html><head><title>\xc2\xae</title><meta charset=\"utf-8\"></head><body>\xc2\xa3</body></html>", "\xc2\xa3", "\xc2\xae", "", "" },
61 // The UTF-8 "BOM" should also set the charset to utf-8.
62 { "\xef\xbb\xbf<html><head><title>\xc2\xae</title></head><body>\xc2\xa3</body></html>", "\xc2\xa3", "\xc2\xae", "", "" },
63 { "<title>X</title>", "", "X", "", "" },
64 { WIDE("\xff\xfe<\0t\0i\0t\0l\0e\0>\0\x20\x26<\0/\0t\0i\0t\0l\0e\0>\0"), "", "\xe2\x98\xa0", "", "" },
65 { WIDE("\xfe\xff\0<\0t\0i\0t\0l\0e\0>\x26\x20\0<\0/\0t\0i\0t\0l\0e\0>"), "", "\xe2\x98\xa0", "", "" },
66 { "<html><body><p>This is \nthe text</p><p>This is \nthe tex</p></body></html>", "This is the text\rThis is the tex", "", "", "" },
67 // Check we default to UTF-8 for HTML5.
68 { "<!DOCTYPE html><html><head><title>\xc2\xae</title></head><body>\xc2\xa3</body></html>", "\xc2\xa3", "\xc2\xae", "", "" },
69 { "<!Doctype\tHTML ><html><head><title>\xc2\xae</title></head><body>\xc2\xa3</body></html>", "\xc2\xa3", "\xc2\xae", "", "" },
70 { "<!Doctype HTML\t><html><head><title>\xc2\xae</title></head><body>\xc2\xa3</body></html>", "\xc2\xa3", "\xc2\xae", "", "" },
71 { "<!DOCTYPE system 'about:legacy-compat'><html><head><title>\xc2\xae</title></head><body>\xc2\xa3</body></html>", "\xc2\xa3", "\xc2\xae", "", "" },
72 { "<!doctype SyStem \"about:legacy-compat\" ><html><head><title>\xc2\xae</title></head><body>\xc2\xa3</body></html>", "\xc2\xa3", "\xc2\xae", "", "" },
73 // Check we default to UTF-8 for XML.
74 { "<?xml version=\"1.0\"?><html><head><title>\xc2\xae</title></head><body>\xc2\xa3</body></html>", "\xc2\xa3", "\xc2\xae", "", "" },
75 // Check we handle specify a charset for XML.
76 { "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><html><head><title>\xc2\xae</title></head><body>\xc2\xa3</body></html>", "\xc3\x82\xc2\xa3", "\xc3\x82\xc2\xae", "", "" },
77 { "<!--UdmComment-->test<!--/UdmComment--><div id='body'>test</div>", "test", "", "", "" },
78 { "Foo<![CDATA[ & bar <literal>\"]]> ok", "Foo & bar <literal>\" ok", "", "", "" },
79 { "Foo<![CDATA", "Foo", "", "", "" },
80 { "foo<![CDATA[bar", "foobar", "", "", "" },
81 // Test that handling of multiple body tags matches modern browser behaviour (ticket#599).
82 { "a<html>b<head>c<title>bad</title>d</head>e<body>f</body>g<body>h</body>i</html>j<body>k", "abcdefghijk", "bad", "", "" },
83 { "check<object id='foo'>for<applet foo=\"bar\" />spaces<br> in <p>\tout</p>put\r\n", "check for spaces\rin\rout\rput", "", "", "" },
84 { "tab:<table><tr><th>col 1</th><th>col 2</th></tr><tr><td>test</td><td><img src='foo.jpg'> <img src='bar.jpg'></td></tr><tr><td colspan=2>hello world</td></tr></table>done", "tab:\rcol 1\tcol 2\rtest\rhello world\rdone", "", "", "" },
85 // Test entities.
86 { "<html><body>1 &lt; 2, 3 &gt; 2</body></html>", "1 < 2, 3 > 2", "", "", "" },
87 { "<html><body>&amp;amp;</body></html>", "&amp;", "", "", "" },
88 { "<html><body>&lt;Unknown &ent;-ity&gt;</body></html>", "<Unknown &ent;-ity>", "", "", "" },
89 { "<html><body>&#68;oes &#97; &lt; &auml; &#x3f</body></html>", "Does a < รค ?", "", "", "" },
90 { "&#65;&#x40;&gt", "A@>", "", "", "" },
91 // Test empty tags.
93 // First two cases are a regression test - in Omega < 1.4.16 the title
94 // wasn't closed and any body content was put into the title instead.
95 { "<head><title xml:lang=\"en-US\"/></head><body><p>Body</p></body>", "Body", "", "", "" },
96 { "<head><title xml:lang='en-US'/></head><body><p>Body</p></body>", "Body", "", "", "" },
97 { "<head><title xml:lang=\"en-US\" /></head><body><p>Body</p></body>", "Body", "", "", "" },
98 { "<head><title xml:lang='en-US\" /></head><body><p>Body</p></body>", "Body", "", "", "" },
99 { "<head><title/></head><body><p>Body</p></body>", "Body", "", "", "" },
100 { "<head><title /></head><body><p>Body</p></body>", "Body", "", "", "" },
101 // Test handling of PHP tags.
102 { "T<?php $a=PHP_MAJOR_VERSION > 7 ?>\r\ne<? if ($a) new(); ?>\ns<?= $a ?>\rting<? ?>\n\nPHP<?php $a=0;", "Testing PHP", "", "", "" },
103 { 0, 0, 0, 0, 0 }
107 main()
109 for (size_t i = 0; tests[i].html; ++i) {
110 MyHtmlParser p;
111 const char* html_begin = tests[i].html;
112 size_t html_len = strlen(html_begin);
113 if (html_len == 0) {
114 // Wide character test data is signalled by a single leading nul
115 // and terminated by a double nul.
116 ++html_begin;
117 while (html_begin[html_len] || html_begin[html_len + 1]) {
118 html_len += 2;
121 string html(html_begin, html_len);
122 try {
123 p.parse_html(html, "iso-8859-1", false);
124 } catch (const string &newcharset) {
125 p.reset();
126 p.parse_html(html, newcharset, true);
128 if (!p.indexing_allowed) {
129 cout << "indexing disallowed by meta tag - skipping\n";
130 continue;
132 if (tests[i].dump != p.dump) {
133 cout << "DUMP " << i << ": [" << p.dump << "] != [" << tests[i].dump << "]" << endl;
134 exit(1);
136 if (tests[i].title != p.title) {
137 cout << "TITLE " << i << ": [" << p.title << "] != [" << tests[i].title << "]" << endl;
138 exit(1);
140 if (tests[i].keywords != p.keywords) {
141 cout << "KEYWORDS " << i << ": [" << p.keywords << "] != [" << tests[i].keywords << "]" << endl;
142 exit(1);
144 const char *sample = tests[i].sample;
145 if (sample == NULL) sample = tests[i].dump;
146 if (sample != p.sample) {
147 cout << "SAMPLE " << i << ": [" << p.sample << "] != [" << sample << "]" << endl;
148 exit(1);