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 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
|
(* This file is part of Markup.ml, released under the MIT license. See
LICENSE.md for details, or visit https://github.com/aantron/markup.ml. *)
open OUnit2
open Test_support
open Markup__Common
module Error = Markup__Error
let xml_decl = Test_xml_tokenizer.xml_decl
let raw_doctype = Test_xml_tokenizer.raw_doctype
let start_element name =
`Start_element (("", name), [])
let no_custom_entities = fun _ -> None
let no_top_level_namespaces = fun _ -> None
let expect ?context ?(namespace = no_top_level_namespaces) text signals =
let report, iterate, ended =
expect_signals signal_to_string text signals in
text
|> Markup__Stream_io.string
|> Markup__Encoding.utf_8
|> Markup__Input.preprocess is_valid_xml_char Error.ignore_errors
|> Markup__Xml_tokenizer.tokenize Error.ignore_errors no_custom_entities
|> Markup__Xml_parser.parse context namespace report
|> iter iterate;
ended ()
let tests = [
("xml.parser.empty" >:: fun _ ->
expect "" []);
("xml.parser.document" >:: fun _ ->
expect "<root>foo</root>"
[ 1, 1, S (start_element "root");
1, 7, S (`Text ["foo"]);
1, 10, S `End_element];
expect " <root > foo </root > "
[ 1, 3, S (start_element "root");
1, 10, S (`Text [" foo "]);
1, 15, S `End_element];
expect "<!DOCTYPE html><root>foo</root>"
[ 1, 1, S (raw_doctype "html");
1, 16, S (start_element "root");
1, 22, S (`Text ["foo"]);
1, 25, S `End_element];
expect "<?xml version='1.0'?><root>foo</root>"
[ 1, 1, S (xml_decl "1.0" None None);
1, 22, S (start_element "root");
1, 28, S (`Text ["foo"]);
1, 31, S `End_element];
expect "<?xml version='1.0'?> <!DOCTYPE html> <root>foo</root>"
[ 1, 1, S (xml_decl "1.0" None None);
1, 24, S (raw_doctype "html");
1, 41, S (start_element "root");
1, 47, S (`Text ["foo"]);
1, 50, S `End_element]);
("xml.parser.leading-comments" >:: fun _ ->
expect
"<?xml version='1.0'?> <!--foo--><!DOCTYPE html> <!--bar--><root></root>"
[ 1, 1, S (xml_decl "1.0" None None);
1, 23, S (`Comment "foo");
1, 33, S (raw_doctype "html");
1, 49, S (`Comment "bar");
1, 59, S (start_element "root");
1, 65, S `End_element];
expect " <!-- foo --> <root></root>"
[ 1, 2, S (`Comment " foo ");
1, 15, S (start_element "root");
1, 21, S `End_element]);
("xml.parser.trailing-comment" >:: fun _ ->
expect "<root></root> <!-- foo --> "
[ 1, 1, S (start_element "root");
1, 7, S `End_element;
1, 15, S (`Comment " foo ")]);
("xml.parser.leading-processing-instructions" >:: fun _ ->
expect
"<?xml version='1.0'?> <?foo bar?><!DOCTYPE html> <?bar foo?><a></a>"
[ 1, 1, S (xml_decl "1.0" None None);
1, 23, S (`PI ("foo", "bar"));
1, 34, S (raw_doctype "html");
1, 50, S (`PI ("bar", "foo"));
1, 61, S (start_element "a");
1, 64, S `End_element];
expect " <?foo bar?> <root></root>"
[ 1, 2, S (`PI ("foo", "bar"));
1, 14, S (start_element "root");
1, 20, S `End_element]);
("xml.parser.trailing-processing-instruction" >:: fun _ ->
expect "<root></root> <?foo bar?>"
[ 1, 1, S (start_element "root");
1, 7, S `End_element;
1, 15, S (`PI ("foo", "bar"))]);
("xml.parser.junk-before-xml-declaration" >:: fun _ ->
expect " <?xml version='1.0'?><root></root>"
[ 1, 2, E (`Bad_document "XML declaration must be first");
1, 23, S (start_element "root");
1, 29, S `End_element];
expect " <?xml version='1.0'?><!DOCTYPE html><root></root>"
[ 1, 2, E (`Bad_document "XML declaration must be first");
1, 23, S (raw_doctype "html");
1, 38, S (start_element "root");
1, 44, S `End_element];
expect "<!-- foo --><?xml version='1.0'?><!DOCTYPE html><root></root>"
[ 1, 1, S (`Comment " foo ");
1, 13, E (`Bad_document "XML declaration must be first");
1, 34, S (raw_doctype "html");
1, 49, S (start_element "root");
1, 55, S `End_element]);
("xml.parser.junk-before-doctype" >:: fun _ ->
expect "<?xml version='1.0'?>foo<!DOCTYPE html><root></root>"
[ 1, 1, S (xml_decl "1.0" None None);
1, 22, E (`Bad_document "text at top level");
1, 25, S (raw_doctype "html");
1, 40, S (start_element "root");
1, 46, S `End_element]);
("xml.parser.junk-before-root" >:: fun _ ->
expect "<?xml version='1.0'?><!DOCTYPE html>foo<root></root>"
[ 1, 1, S (xml_decl "1.0" None None);
1, 22, S (raw_doctype "html");
1, 37, E (`Bad_document "expected root element");
1, 40, S (start_element "root");
1, 46, S `End_element]);
("xml.parser.junk-after-root" >:: fun _ ->
expect "<root></root>foo"
[ 1, 1, S (start_element "root");
1, 7, S `End_element;
1, 14, S (`Text ["foo"])];
expect "<root></root> <foo></foo> <bar></bar>"
[ 1, 1, S (start_element "root");
1, 7, S `End_element;
1, 15, S (start_element "foo");
1, 20, S `End_element;
1, 26, S (`Text [" "]);
1, 27, S (start_element "bar");
1, 32, S `End_element];
expect "<?xml version='1.0'?><root/>foo"
[ 1, 1, S (xml_decl "1.0" None None);
1, 22, S (start_element "root");
1, 22, S `End_element;
1, 29, E (`Bad_document "not allowed after root element");
1, 29, S (`Text ["foo"])]);
("xml.parser.self-closing-root" >:: fun _ ->
expect "<root/>"
[ 1, 1, S (start_element "root");
1, 1, S `End_element]);
("xml.parser.content" >:: fun _ ->
expect "<root>foo<!--bar--><?baz quux?><<![CDATA[>]]></root>"
[ 1, 1, S (start_element "root");
1, 7, S (`Text ["foo"]);
1, 10, S (`Comment "bar");
1, 20, S (`PI ("baz", "quux"));
1, 32, S (`Text ["<>"]);
1, 52, S `End_element];
expect "<root><nested><more>foo</more></nested><a>bar</a><blah/></root>"
[ 1, 1, S (start_element "root");
1, 7, S (start_element "nested");
1, 15, S (start_element "more");
1, 21, S (`Text ["foo"]);
1, 24, S `End_element;
1, 31, S `End_element;
1, 40, S (start_element "a");
1, 43, S (`Text ["bar"]);
1, 46, S `End_element;
1, 50, S (start_element "blah");
1, 50, S `End_element;
1, 57, S `End_element]);
("xml.parser.prolog-in-content" >:: fun _ ->
expect "<root><?xml version='1.0'?><!DOCTYPE html></root>"
[ 1, 1, S (start_element "root");
1, 7, E (`Bad_document "XML declaration should be at top level");
1, 28, E (`Bad_document "doctype should be at top level");
1, 43, S `End_element]);
("xml.parser.attributes" >:: fun _ ->
expect "<root foo='bar'/>"
[ 1, 1, S (`Start_element (("", "root"), [("", "foo"), "bar"]));
1, 1, S `End_element]);
("xml.parser.bad-attributes" >:: fun _ ->
expect "<root foo='bar' foo='baz'/>"
[ 1, 1, E (`Bad_token ("foo", "tag", "duplicate attribute"));
1, 1, S (`Start_element (("", "root"), [("", "foo"), "bar"]));
1, 1, S `End_element];
expect "<root xmlns:a='some_ns' xmlns:b='some_ns' a:foo='' b:foo=''/>"
[ 1, 1, E (`Bad_token ("foo", "tag", "duplicate attribute"));
1, 1, S (`Start_element (("", "root"),
[(xmlns_ns, "a"), "some_ns";
(xmlns_ns, "b"), "some_ns";
("some_ns", "foo"), ""]));
1, 1, S `End_element]);
("xml.parser.misnested-tags" >:: fun _ ->
expect "<foo>"
[ 1, 1, S (start_element "foo");
1, 1, E (`Unmatched_start_tag "foo");
1, 6, S `End_element];
expect "<foo></bar></foo>"
[ 1, 1, S (start_element "foo");
1, 6, E (`Unmatched_end_tag "bar");
1, 12, S `End_element];
expect "<foo><bar><baz></foo>"
[ 1, 1, S (start_element "foo");
1, 6, S (start_element "bar");
1, 11, S (start_element "baz");
1, 11, E (`Unmatched_start_tag "baz");
1, 16, S `End_element;
1, 6, E (`Unmatched_start_tag "bar");
1, 16, S `End_element;
1, 16, S `End_element]);
("xml.parser.fragment" >:: fun _ ->
expect "foo<bar/>"
[ 1, 1, S (`Text ["foo"]);
1, 4, S (start_element "bar");
1, 4, S `End_element];
expect " <!-- foo --> bar <baz></baz> <quux/>"
[ 1, 1, S (`Text [" "]);
1, 2, S (`Comment " foo ");
1, 14, S (`Text [" bar "]);
1, 19, S (start_element "baz");
1, 24, S `End_element;
1, 30, S (`Text [" "]);
1, 31, S (start_element "quux");
1, 31, S `End_element];
expect "foo"
[ 1, 1, S (`Text ["foo"])]);
("xml.parser.namespaces" >:: fun _ ->
expect
"<root xmlns='some_ns' xmlns:a='other_ns' a:foo='bar' baz='quux'></root>"
[ 1, 1, S (`Start_element (("some_ns", "root"),
[(xmlns_ns, "xmlns"), "some_ns";
(xmlns_ns, "a"), "other_ns";
("other_ns", "foo"), "bar";
("", "baz"), "quux"]));
1, 65, S `End_element];
expect "<a:root xmlns:a='some_ns' xmlns:b='other_ns'></b:root>"
[ 1, 1, S (`Start_element (("some_ns", "root"),
[(xmlns_ns, "a"), "some_ns";
(xmlns_ns, "b"), "other_ns"]));
1, 46, E (`Unmatched_end_tag "b:root");
1, 1, E (`Unmatched_start_tag "a:root");
1, 55, S `End_element];
expect "<a:root xmlns:a='some_ns' xmlns:b='some_ns'></b:root>"
[ 1, 1, S (`Start_element (("some_ns", "root"),
[(xmlns_ns, "a"), "some_ns";
(xmlns_ns, "b"), "some_ns"]));
1, 45, S `End_element];
expect "<root xmlns='some_ns'><foo bar='baz'/></root>"
[ 1, 1, S (`Start_element (("some_ns", "root"),
[(xmlns_ns, "xmlns"), "some_ns"]));
1, 23, S (`Start_element (("some_ns", "foo"),
[("", "bar"), "baz"]));
1, 23, S `End_element;
1, 39, S `End_element];
expect "<root xmlns:a='some_ns'><a:foo bar='baz'/><quux/></root>"
[ 1, 1, S (`Start_element (("", "root"),
[(xmlns_ns, "a"), "some_ns"]));
1, 25, S (`Start_element (("some_ns", "foo"),
[("", "bar"), "baz"]));
1, 25, S `End_element;
1, 43, S (`Start_element (("", "quux"), []));
1, 43, S `End_element;
1, 50, S `End_element];
expect
("<root xmlns:a='some_ns' xmlns:b='other_ns'><foo xmlns:a='another_ns'>" ^
"<a:bar/><b:baz/></foo></root>")
[ 1, 1, S (`Start_element (("", "root"),
[(xmlns_ns, "a"), "some_ns";
(xmlns_ns, "b"), "other_ns"]));
1, 44, S (`Start_element (("", "foo"),
[(xmlns_ns, "a"), "another_ns"]));
1, 70, S (`Start_element (("another_ns", "bar"), []));
1, 70, S `End_element;
1, 78, S (`Start_element (("other_ns", "baz"), []));
1, 78, S `End_element;
1, 86, S `End_element;
1, 92, S `End_element];
expect "<root xmlns='some_ns'><foo xmlns='other_ns'><bar/></foo></root>"
[ 1, 1, S (`Start_element (("some_ns", "root"),
[(xmlns_ns, "xmlns"), "some_ns"]));
1, 23, S (`Start_element (("other_ns", "foo"),
[(xmlns_ns, "xmlns"), "other_ns"]));
1, 45, S (`Start_element (("other_ns", "bar"), []));
1, 45, S `End_element;
1, 51, S `End_element;
1, 57, S `End_element];
expect "<root xmlns='some_ns'></root><foo/>"
[ 1, 1, S (`Start_element (("some_ns", "root"),
[(xmlns_ns, "xmlns"), "some_ns"]));
1, 23, S `End_element;
1, 30, S (start_element "foo");
1, 30, S `End_element];
expect
("<root xmlns:a='some_ns'><foo xmlns:a='other_ns'><a:bar/></foo>" ^
"<a:baz/></root>")
[ 1, 1, S (`Start_element (("", "root"),
[(xmlns_ns, "a"), "some_ns"]));
1, 25, S (`Start_element (("", "foo"),
[(xmlns_ns, "a"), "other_ns"]));
1, 49, S (`Start_element (("other_ns", "bar"), []));
1, 49, S `End_element;
1, 57, S `End_element;
1, 63, S (`Start_element (("some_ns", "baz"), []));
1, 63, S `End_element;
1, 71, S `End_element]);
("xml.parser.bad-namespaces" >:: fun _ ->
expect "<a:root><foo b:bar=''/></a:root>"
[ 1, 1, E (`Bad_namespace "a");
1, 1, S (`Start_element (("a", "root"), []));
1, 9, E (`Bad_namespace "b");
1, 9, S (`Start_element (("", "foo"),
[("b", "bar"), ""]));
1, 9, S `End_element;
1, 24, E (`Bad_namespace "a");
1, 24, S `End_element]);
("xml.parser.custom-namespaces" >:: fun _ ->
let namespace = function
| "a" -> Some "some_ns"
| "b" -> Some "other_ns"
| "xmlns" -> Some "bad"
| _ -> None
in
expect ~namespace "<a:root b:foo='bar' xmlns:c='baz'/>"
[ 1, 1, S (`Start_element (("some_ns", "root"),
[("other_ns", "foo"), "bar";
(xmlns_ns, "c"), "baz"]));
1, 1, S `End_element])
]
|