Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Page
Library
Module
Module type
Parameter
Class
Class type
Source
html_tokenizer.ml1 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 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759(* 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 Common open Token_tag type token = [ `Doctype of doctype | `Start of Token_tag.t | `End of Token_tag.t | `Char of int | `Comment of string | `EOF ] type state = [ `Data | `RCDATA | `RAWTEXT | `Script_data | `PLAINTEXT ] let replace_windows_1252_entity = function | 0x80 -> 0x20AC | 0x82 -> 0x201A | 0x83 -> 0x0192 | 0x84 -> 0x201E | 0x85 -> 0x2026 | 0x86 -> 0x2020 | 0x87 -> 0x2021 | 0x88 -> 0x02C6 | 0x89 -> 0x2030 | 0x8A -> 0x0160 | 0x8B -> 0x2039 | 0x8C -> 0x0152 | 0x8E -> 0x017D | 0x91 -> 0x2018 | 0x92 -> 0x2019 | 0x93 -> 0x201C | 0x94 -> 0x201D | 0x95 -> 0x2022 | 0x96 -> 0x2013 | 0x97 -> 0x2014 | 0x98 -> 0x02DC | 0x99 -> 0x2122 | 0x9A -> 0x0161 | 0x9B -> 0x203A | 0x9C -> 0x0153 | 0x9E -> 0x017E | 0x9F -> 0x0178 | c -> c let named_entity_trie = lazy begin let trie = Trie.create () in Array.fold_left (fun trie (name, characters) -> Trie.add name characters trie) trie Entities.entities end type doctype_buffers = {mutable doctype_name : Buffer.t option; mutable public_identifier : Buffer.t option; mutable system_identifier : Buffer.t option; mutable force_quirks : bool} module Doctype_buffers = struct type t = doctype_buffers = {mutable doctype_name : Buffer.t option; mutable public_identifier : Buffer.t option; mutable system_identifier : Buffer.t option; mutable force_quirks : bool} end let add_doctype_char buffer c = let buffer = match buffer with | None -> Buffer.create 32 | Some buffer -> buffer in add_utf_8 buffer c; Some buffer type tag_buffers = {mutable start : bool; tag_name : Buffer.t; mutable self_closing : bool; mutable attributes : (string * string) list} module Tag_buffers = struct type t = tag_buffers = {mutable start : bool; tag_name : Buffer.t; mutable self_closing : bool; mutable attributes : (string * string) list} end let sequence_to_lowercase = List.map (fun (l, c) -> l, to_lowercase c) open Kstream let tokenize report (input, get_location) = let foreign = ref (fun () -> false) in let last_start_tag_name : string option ref = ref None in let is_appropriate_end_tag name_buffer = match !last_start_tag_name with | None -> false | Some name -> Buffer.contents name_buffer = name in let throw = ref (fun _ -> ()) in let ended = ref (fun _ -> ()) in let output = ref (fun _ -> ()) in let rec current_state = ref data_state and emit t s = current_state := s; !output t and emit_character l c s = emit (l, `Char c) s and emit_characters cs s = match cs with | [] -> s () | (l, c)::cs -> emit_character l c (fun () -> emit_characters cs s) and emit_eof () = emit (get_location (), `EOF) (fun () -> !ended ()) and emit_tag l tag' = let rec rev_deduplicate accumulator seen attributes k = match attributes with | [] -> k accumulator | (n, v)::more -> if list_mem_string n seen then report l (`Bad_token (n, "tag", "duplicate attribute")) !throw (fun () -> rev_deduplicate accumulator seen more k) else rev_deduplicate ((n, v)::accumulator) (n::seen) more k in rev_deduplicate [] [] (List.rev tag'.Tag_buffers.attributes) (fun attributes -> let tag = {Token_tag.name = Buffer.contents tag'.tag_name; self_closing = tag'.self_closing; attributes = List.rev attributes} in (fun k -> if tag'.start then begin last_start_tag_name := Some tag.name; k (`Start tag) end else (fun k -> match attributes with | (n, _)::_ -> report l (`Bad_token (n, "tag", "end tag with attributes")) !throw k | _ -> k ()) @@ (fun k () -> if tag.Token_tag.self_closing then report l (`Bad_token ("/>", "tag", "end tag cannot be self-closing")) !throw k else k ()) @@ (fun () -> k (`End tag))) (fun token -> emit (l, token) data_state)) and emit_comment l buffer = emit (l, `Comment (Buffer.contents buffer)) data_state and emit_doctype ?(quirks = false) l doctype = if quirks then doctype.Doctype_buffers.force_quirks <- true; let if_not_missing = function | None -> None | Some buffer -> Some (Buffer.contents buffer) in let doctype = {Common.doctype_name = if_not_missing doctype.doctype_name; public_identifier = if_not_missing doctype.public_identifier; system_identifier = if_not_missing doctype.system_identifier; raw_text = None; force_quirks = doctype.force_quirks} in emit (l, `Doctype doctype) data_state (* Implementation of 8.2.4.69 Tokenizing character references. *) and consume_character_reference in_attribute additional location k = peek_option input !throw (function | Some (_, (0x0009 | 0x000A | 0x000C | 0x0020 | 0x003C | 0x0026)) | None -> k None | Some (_, c) when Some c = additional -> k None | Some (_, 0x0023 as pound) -> let consume_digits filter k = let buffer = Buffer.create 8 in let rec iterate () = next_option input !throw (function | Some (_, c) when filter c -> Buffer.add_char buffer (Char.chr c); iterate () | v -> push_option input v; if Buffer.length buffer = 0 then k None else k (Some (Buffer.contents buffer))) in iterate () in let finish_digits prefix text s = let consume_semicolon k = next_option input !throw begin function | Some (_, 0x003B) -> k ";" | v -> push_option input v; report location (`Bad_token (prefix ^ text, "character reference", "missing ';' at end")) !throw (fun () -> k "") end in let convert s semicolon k' = let maybe_n = try Some (int_of_string s) with Failure _ -> None in match maybe_n with | Some n -> k' n | None -> report location (`Bad_token (prefix ^ text ^ semicolon, "character reference", "out of range")) !throw (fun () -> k (Some (`One u_rep))) in consume_semicolon begin fun semicolon -> convert s semicolon begin fun n' -> let n = replace_windows_1252_entity n' in if n <> n' then report location (`Bad_token (prefix ^ text ^ semicolon, "character reference", "Windows-1252 character")) !throw (fun () -> k (Some (`One n))) else match n with | n when not @@ is_scalar n || n = 0 -> report location (`Bad_token (prefix ^ text ^ semicolon, "character reference", "out of range")) !throw (fun () -> k (Some (`One u_rep))) | n when is_control_character n || is_non_character n -> report location (`Bad_token (prefix ^ text ^ semicolon, "character reference", "invalid HTML character")) !throw (fun () -> k (Some (`One n))) | n -> k (Some (`One n)) end end in next_expected input !throw (fun _ -> peek_option input !throw (function | Some (_, (0x0078 | 0x0058 as c) as x) -> let prefix = Printf.sprintf "&#%c" (Char.chr c) in next_expected input !throw (fun _ -> consume_digits is_hex_digit (function | None -> push_list input [pound; x]; report location (`Bad_token (prefix, "character reference", "expected digits")) !throw (fun () -> k None) | Some s -> finish_digits prefix s ("0x" ^ s))) | _ -> let prefix = "&#" in consume_digits is_digit (function | None -> push input pound; report location (`Bad_token (prefix, "character reference", "expected digits")) !throw (fun () -> k None) | Some s -> finish_digits prefix s s))) | _ -> let is_entity_like k = let finish replace text = push_list input (List.rev replace); k text in let buffer = Buffer.create 16 in let rec iterate replace = next_option input !throw (function | None -> finish replace None | Some ((_, c) as v) when is_alphanumeric c -> Buffer.add_char buffer (Char.chr c); iterate (v::replace) | Some ((_, 0x003B) as v) -> finish (v::replace) (Some (Buffer.contents buffer)) | Some v -> finish (v::replace) None) in iterate [] in let finish best matched replace = push_list input (List.rev replace); match best with | None -> is_entity_like (function | None -> k None | Some s -> report location (`Bad_token ("&" ^ s ^ ";", "entity reference", "no such entity")) !throw (fun () -> k None)) | Some (text, code_points) -> next_option input !throw (function | Some (_, 0x003B) -> k (Some code_points) | maybe_v -> let unterminated () = push_option input maybe_v; report location (`Bad_token ("&" ^ text, "entity reference", "missing ';' at end")) !throw (fun () -> k (Some code_points)) in if not in_attribute then unterminated () else match maybe_v with | Some ((_, c) as v) when is_alphanumeric c -> push_list input (List.rev (v::matched)); k None | Some ((_, 0x003D) as v) -> push_list input (List.rev (v::matched)); report location (`Bad_token ("&" ^ text ^ "=", "attribute", "unterminated entity reference followed by '='")) !throw(fun () -> k None) | _ -> unterminated ()) in let rec match_named best matched replace trie text = next_option input !throw (function | None -> finish best matched replace | Some ((_, c) as v) -> let trie = Trie.advance c trie in add_utf_8 text c; match Trie.matches trie with | Trie.No -> finish best matched (v::replace) | Trie.Prefix -> match_named best matched (v::replace) trie text | Trie.Multiple m -> let w = Buffer.contents text in match_named (Some (w, m)) (v::(replace @ matched)) [] trie text | Trie.Yes m -> let w = Buffer.contents text in finish (Some (w, m)) (v::(replace @ matched)) []) in match_named None [] [] (Lazy.force named_entity_trie) (Buffer.create 16)) (* 8.2.4.1. *) and data_state () = next_option input !throw begin function | Some (l, 0x0026) -> character_reference_state data_state l | Some (l, 0x003C) -> tag_open_state l | Some (l, 0) -> report l (`Bad_token ("U+0000", "content", "null")) !throw (fun () -> emit (l, `Char 0) data_state) | None -> emit_eof () | Some (l, c) -> emit (l, `Char c) data_state end (* 8.2.4.2, 8.2.4.4. *) and character_reference_state state l = consume_character_reference false None l begin function | None -> emit (l, `Char 0x0026) state | Some (`One c) -> emit (l, `Char c) state | Some (`Two (c, c')) -> emit (l, `Char c) (fun () -> emit (l, `Char c') state) end (* 8.2.4.3. *) and rcdata_state () = next_option input !throw begin function | Some (l, 0x0026) -> character_reference_state rcdata_state l | Some (l, 0x003C as v) -> text_less_than_sign_state rcdata_state l [v] | Some (l, 0) -> report l (`Bad_token ("U+0000", "content", "null")) !throw (fun () -> emit (l, `Char u_rep) rcdata_state) | None -> emit_eof () | Some (l, c) -> emit (l, `Char c) rcdata_state end (* 8.2.4.5. *) and rawtext_state () = next_option input !throw begin function | Some (l, 0x003C as v) -> text_less_than_sign_state rawtext_state l [v] | Some (l, 0) -> report l (`Bad_token ("U+0000", "content", "null")) !throw (fun () -> emit (l, `Char u_rep) rawtext_state) | None -> emit_eof () | Some (l, c) -> emit (l, `Char c) rawtext_state end (* 8.2.4.6. *) and script_data_state () = next_option input !throw begin function | Some (l, 0x003C as v) -> script_data_less_than_sign_state l [v] | Some (l, 0) -> report l (`Bad_token ("U+0000", "content", "null")) !throw (fun () -> emit_character l u_rep script_data_state) | None -> emit_eof () | Some (l, c) -> emit_character l c script_data_state end (* 8.2.4.7. *) and plaintext_state () = next_option input !throw begin function | Some (l, 0) -> report l (`Bad_token ("U+0000", "content", "null")) !throw (fun () -> emit (l, `Char u_rep) plaintext_state) | None -> emit_eof () | Some (l, c) -> emit (l, `Char c) plaintext_state end (* 8.2.4.8. *) and tag_open_state l' = let tag = {start = true; tag_name = Buffer.create 16; self_closing = false; attributes = []} in next_option input !throw begin function | Some (_, 0x0021) -> markup_declaration_open_state l' | Some (_, 0x002F) -> end_tag_open_state l' tag | Some (_, c) when is_alphabetic c -> add_utf_8 tag.tag_name (to_lowercase c); tag_name_state l' tag | Some (_, 0x003F) -> report l' (`Bad_token ("<?", "content", "HTML does not have processing instructions")) !throw (fun () -> bogus_comment_state l') | Some ((l, c) as v) -> report l (`Bad_token (char c, "tag", "invalid start character")) !throw (fun () -> push input v; emit_character l' 0x003C data_state) | None -> report (get_location ()) (`Unexpected_eoi "tag") !throw (fun () -> emit_character l' 0x003C data_state) end (* 8.2.4.9. *) and end_tag_open_state l' tag = tag.start <- false; next_option input !throw begin function | Some (_, c) when is_alphabetic c -> add_utf_8 tag.tag_name (to_lowercase c); tag_name_state l' tag | Some (_, 0x003E) -> report l' (`Bad_token ("</>", "tag", "no tag name")) !throw data_state | None -> report (get_location ()) (`Unexpected_eoi "tag") !throw (fun () -> let line, column = l' in emit (l', `Char 0x003C) (fun () -> emit ((line, column + 1), `Char 0x002F) data_state)) | Some (l, c) -> report l (`Bad_token (char c, "tag", "invalid start character")) !throw (fun () -> bogus_comment_state l') end (* 8.2.4.10. *) and tag_name_state l' tag = next_option input !throw begin function | Some (_, (0x0009 | 0x000A | 0x000C | 0x0020)) -> before_attribute_name_state l' tag | Some (_, 0x002F) -> self_closing_start_tag_state l' tag | Some (_, 0x003E) -> emit_tag l' tag | Some (l, 0) -> report l (`Bad_token ("U+0000", "tag name", "null")) !throw (fun () -> add_utf_8 tag.tag_name u_rep; tag_name_state l' tag) | None -> report (get_location ()) (`Unexpected_eoi "tag") !throw data_state | Some (_, c) -> add_utf_8 tag.tag_name (to_lowercase c); tag_name_state l' tag end (* 8.2.4.11, 8.2.4.14. *) and text_less_than_sign_state state l' cs = next_option input !throw begin function | Some (_, 0x002F as v) -> text_end_tag_open_state state l' (v::cs) | maybe_v -> push_option input maybe_v; emit_characters cs state end (* 8.2.4.12, 8.2.4.15, 8.2.4.18, 8.2.4.26. *) and text_end_tag_open_state state l' cs = next_option input !throw begin function | Some (_, c as v) when is_alphabetic c -> let name_buffer = Buffer.create 32 in add_utf_8 name_buffer (to_lowercase c); text_end_tag_name_state state l' (v::cs) name_buffer | maybe_v -> push_option input maybe_v; emit_characters (List.rev cs) state end (* 8.2.4.13, 8.2.4.16, 8.2.4.19, 8.2.4.27. *) and text_end_tag_name_state state l' cs name_buffer = let create_tag () = {start = false; tag_name = name_buffer; self_closing = false; attributes = []} in next_option input !throw begin function | Some (_, (0x0009 | 0x000A | 0x000C | 0x0020)) when is_appropriate_end_tag name_buffer -> before_attribute_name_state l' (create_tag ()) | Some (_, 0x002F) when is_appropriate_end_tag name_buffer -> self_closing_start_tag_state l' (create_tag ()) | Some (_, 0x003E) when is_appropriate_end_tag name_buffer -> emit_tag l' (create_tag ()) | Some ((_, c) as v) when is_alphabetic c -> add_utf_8 name_buffer (to_lowercase c); text_end_tag_name_state state l' (v::cs) name_buffer | maybe_v -> push_option input maybe_v; emit_characters (List.rev cs) state end (* 8.2.4.17. *) and script_data_less_than_sign_state l' cs = next_option input !throw begin function | Some (_, 0x002F as v) -> text_end_tag_open_state script_data_state l' (v::cs) | Some (_, 0x0021 as v) -> emit_characters (List.rev (v::cs)) (fun () -> script_data_escape_start_state l') | maybe_v -> push_option input maybe_v; emit_characters cs script_data_state end (* 8.2.4.20. *) and script_data_escape_start_state l' = next_option input !throw begin function | Some (l, 0x002D) -> emit_character l 0x002D (fun () -> script_data_escape_start_dash_state l') | maybe_v -> push_option input maybe_v; script_data_state () end (* 8.2.4.21. *) and script_data_escape_start_dash_state l' = next_option input !throw begin function | Some (l, 0x002D) -> emit_character l 0x002D (fun () -> script_data_escaped_dash_dash_state l') | maybe_v -> push_option input maybe_v; script_data_state () end (* 8.2.4.22. *) and script_data_escaped_state l' = next_option input !throw begin function | Some (l, 0x002D) -> emit_character l 0x002D (fun () -> script_data_escaped_dash_state l') | Some ((l, 0x003C) as v) -> script_data_escaped_less_than_sign_state l' l [v] | Some (l, 0) -> report l (`Bad_token ("U+0000", "script", "null")) !throw (fun () -> emit_character l u_rep (fun () -> script_data_escaped_state l')) | None -> report (get_location ()) (`Unexpected_eoi "script") !throw data_state | Some (l, c) -> emit_character l c (fun () -> script_data_escaped_state l') end (* 8.2.4.23. *) and script_data_escaped_dash_state l' = next_option input !throw begin function | Some (l, 0x002D) -> emit_character l 0x002D (fun () -> script_data_escaped_dash_dash_state l') | Some (l, 0x003C as v) -> script_data_escaped_less_than_sign_state l' l [v] | Some (l, 0) -> report l (`Bad_token ("U+0000", "script", "null")) !throw (fun () -> emit_character l u_rep (fun () -> script_data_escaped_state l')) | None -> report (get_location ()) (`Unexpected_eoi "script") !throw data_state | Some (l, c) -> emit_character l c (fun () -> script_data_escaped_state l') end (* 8.2.4.24. *) and script_data_escaped_dash_dash_state l' = next_option input !throw begin function | Some (l, 0x002D) -> emit_character l 0x002D (fun () -> script_data_escaped_dash_dash_state l') | Some (l, 0x003C as v) -> script_data_escaped_less_than_sign_state l' l [v] | Some (l, 0x003E) -> emit_character l 0x003E script_data_state | Some (l, 0) -> report l (`Bad_token ("U+0000", "script", "null")) !throw (fun () -> emit_character l u_rep (fun () -> script_data_escaped_state l')) | None -> report (get_location ()) (`Unexpected_eoi "script") !throw data_state | Some (l, c) -> emit_character l c (fun () -> script_data_escaped_state l') end (* 8.2.4.25. *) and script_data_escaped_less_than_sign_state l' l'' cs = next_option input !throw begin function | Some (_, 0x002F as v) -> text_end_tag_open_state (fun () -> script_data_escaped_state l') l'' (v::cs) | Some (_, c as v) when is_alphabetic c -> let tag_buffer = Buffer.create 32 in add_utf_8 tag_buffer (to_lowercase c); emit_characters (List.rev (v::cs)) (fun () -> script_data_double_escape_start_state l' tag_buffer) | maybe_v -> push_option input maybe_v; emit_characters cs (fun () -> script_data_escaped_state l') end (* 8.2.4.28. *) and script_data_double_escape_start_state l' tag_buffer = next_option input !throw begin function | Some (l, (0x0009 | 0x000A | 0x000C | 0x0020 | 0x002F | 0x003E as c)) -> emit_character l c (fun () -> if Buffer.contents tag_buffer = "script" then script_data_double_escaped_state l' else script_data_escaped_state l') | Some (l, c) when is_alphabetic c -> add_utf_8 tag_buffer (to_lowercase c); emit_character l c (fun () -> script_data_double_escape_start_state l' tag_buffer) | maybe_v -> push_option input maybe_v; script_data_escaped_state l' end (* 8.2.4.29. *) and script_data_double_escaped_state l' = next_option input !throw begin function | Some (l, 0x002D) -> emit_character l 0x002D (fun () -> script_data_double_escaped_dash_state l') | Some (l, 0x003C) -> emit_character l 0x003C (fun () -> script_data_double_escaped_less_than_sign_state l') | Some (l, 0) -> report l (`Bad_token ("U+0000", "script", "null")) !throw (fun () -> emit_character l u_rep (fun () -> script_data_double_escaped_state l')) | None -> report (get_location ()) (`Unexpected_eoi "script") !throw data_state | Some (l, c) -> emit_character l c (fun () -> script_data_double_escaped_state l') end (* 8.2.4.30. *) and script_data_double_escaped_dash_state l' = next_option input !throw begin function | Some (l, 0x002D) -> emit_character l 0x002D (fun () -> script_data_double_escaped_dash_dash_state l') | Some (l, 0x003C) -> emit_character l 0x003C (fun () -> script_data_double_escaped_less_than_sign_state l') | Some (l, 0) -> report l (`Bad_token ("U+0000", "script", "null")) !throw (fun () -> emit_character l u_rep (fun () -> script_data_double_escaped_state l')) | None -> report (get_location ()) (`Unexpected_eoi "script") !throw data_state | Some (l, c) -> emit_character l c (fun () -> script_data_double_escaped_state l') end (* 8.2.4.31. *) and script_data_double_escaped_dash_dash_state l' = next_option input !throw begin function | Some (l, 0x002D) -> emit_character l 0x002D (fun () -> script_data_double_escaped_dash_dash_state l') | Some (l, 0x003C) -> emit_character l 0x003C (fun () -> script_data_double_escaped_less_than_sign_state l') | Some (l, 0x003E) -> emit_character l 0x003E script_data_state | Some (l, 0) -> report l (`Bad_token ("U+0000", "script", "null")) !throw (fun () -> emit_character l u_rep (fun () -> script_data_double_escaped_state l')) | None -> report (get_location ()) (`Unexpected_eoi "script") !throw data_state | Some (l, c) -> emit_character l c (fun () -> script_data_double_escaped_state l') end (* 8.2.4.32. *) and script_data_double_escaped_less_than_sign_state l' = next_option input !throw begin function | Some (l, 0x002F) -> let tag_buffer = Buffer.create 32 in emit_character l 0x002F (fun () -> script_data_double_escape_end_state l' tag_buffer) | maybe_v -> push_option input maybe_v; script_data_double_escaped_state l' end (* 8.2.4.33. *) and script_data_double_escape_end_state l' tag_buffer = next_option input !throw begin function | Some (l, (0x0009 | 0x000A | 0x000C | 0x0020 | 0x002F | 0x003E as c)) -> emit_character l c (fun () -> if Buffer.contents tag_buffer = "script" then script_data_escaped_state l' else script_data_double_escaped_state l') | Some (l, c) when is_alphabetic c -> add_utf_8 tag_buffer (to_lowercase c); emit_character l c (fun () -> script_data_double_escape_end_state l' tag_buffer) | maybe_v -> push_option input maybe_v; script_data_double_escaped_state l' end (* 8.2.4.34. *) and before_attribute_name_state l' tag = let start_attribute c = let name_buffer = Buffer.create 32 in add_utf_8 name_buffer c; attribute_name_state l' tag name_buffer in next_option input !throw begin function | Some (_, (0x0009 | 0x000A | 0x000C | 0x0020)) -> before_attribute_name_state l' tag | Some (_, 0x002F) -> self_closing_start_tag_state l' tag | Some (_, 0x003E) -> emit_tag l' tag | None -> report (get_location ()) (`Unexpected_eoi "tag") !throw data_state | Some (l, 0) -> report l (`Bad_token ("U+0000", "attribute name", "null")) !throw (fun () -> start_attribute u_rep) | Some (l, (0x0022 | 0x0027 | 0x003C | 0x003D as c)) -> report l (`Bad_token (char c, "attribute name", "invalid start character")) !throw (fun () -> start_attribute c) | Some (_, c) -> start_attribute (to_lowercase c) end (* 8.2.4.35. *) and attribute_name_state l' tag name_buffer = next_option input !throw begin function | Some (_, (0x0009 | 0x000A | 0x000C | 0x0020)) -> after_attribute_name_state l' tag (Buffer.contents name_buffer) | Some (_, 0x002F) -> tag.attributes <- (Buffer.contents name_buffer, "")::tag.attributes; self_closing_start_tag_state l' tag | Some (_, 0x003D) -> before_attribute_value_state l' tag (Buffer.contents name_buffer) | Some (_, 0x003E) -> tag.attributes <- (Buffer.contents name_buffer, "")::tag.attributes; emit_tag l' tag | Some (l, 0) -> report l (`Bad_token ("U+0000", "attribute name", "null")) !throw (fun () -> add_utf_8 name_buffer u_rep; attribute_name_state l' tag name_buffer) | Some (l, (0x0022 | 0x0027 | 0x003C as c)) -> report l (`Bad_token (char c, "attribute name", "invalid name character")) !throw (fun () -> add_utf_8 name_buffer c; attribute_name_state l' tag name_buffer) | None -> report (get_location ()) (`Unexpected_eoi "tag") !throw data_state | Some (_, c) -> add_utf_8 name_buffer (to_lowercase c); attribute_name_state l' tag name_buffer end (* 8.2.4.36. *) and after_attribute_name_state l' tag name = let start_next_attribute c = tag.attributes <- (name, "")::tag.attributes; let name_buffer = Buffer.create 32 in add_utf_8 name_buffer c; attribute_name_state l' tag name_buffer in next_option input !throw begin function | Some (_, (0x0009 | 0x000A | 0x000C | 0x0020)) -> after_attribute_name_state l' tag name | Some (_, 0x002F) -> tag.attributes <- (name, "")::tag.attributes; self_closing_start_tag_state l' tag | Some (_, 0x003D) -> before_attribute_value_state l' tag name | Some (_, 0x003E) -> tag.attributes <- (name, "")::tag.attributes; emit_tag l' tag | Some (l, 0) -> report l (`Bad_token ("U+0000", "attribute name", "null")) !throw (fun () -> start_next_attribute u_rep) | Some (l, (0x0022 | 0x0027 | 0x003C as c)) -> report l (`Bad_token (char c, "attribute name", "invalid start character")) !throw (fun () -> start_next_attribute c) | None -> report (get_location ()) (`Unexpected_eoi "tag") !throw data_state | Some (_, c) -> start_next_attribute (to_lowercase c) end (* 8.2.4.37. *) and before_attribute_value_state l' tag name = let start_value state maybe_c = let value_buffer = Buffer.create 32 in begin match maybe_c with | None -> () | Some c -> add_utf_8 value_buffer c end; state l' tag name value_buffer in next_option input !throw begin function | Some (_, (0x0009 | 0x000A | 0x000C | 0x0020)) -> before_attribute_value_state l' tag name | Some (_, (0x0022 | 0x0027 as c)) -> start_value (attribute_value_quoted_state c) None | Some (_, 0x0026 as v) -> push input v; start_value attribute_value_unquoted_state None | Some (l, 0) -> report l (`Bad_token ("U+0000", "attribute value", "null")) !throw (fun () -> start_value attribute_value_unquoted_state (Some u_rep)) | Some (l, 0x003E) -> report l (`Bad_token (">", "tag", "expected attribute value after '='")) !throw (fun () -> tag.attributes <- (name, "")::tag.attributes; emit_tag l' tag) | Some (l, (0x003C | 0x003D | 0x0060 as c)) -> report l (`Bad_token (char c, "attribute value", "invalid start character")) !throw (fun () -> start_value attribute_value_unquoted_state (Some c)) | None -> report (get_location ()) (`Unexpected_eoi "tag") !throw data_state | Some (_, c) -> start_value attribute_value_unquoted_state (Some c) end (* 8.2.4.38 and 8.2.4.39. *) and attribute_value_quoted_state quote l' tag name value_buffer = next_option input !throw begin function | Some (_, c) when c = quote -> tag.attributes <- (name, Buffer.contents value_buffer)::tag.attributes; after_attribute_value_quoted_state l' tag | Some (l, 0x0026) -> character_reference_in_attribute quote l value_buffer (fun () -> attribute_value_quoted_state quote l' tag name value_buffer) | Some (l, 0) -> report l (`Bad_token ("U+0000", "attribute value", "null")) !throw (fun () -> add_utf_8 value_buffer u_rep; attribute_value_quoted_state quote l' tag name value_buffer) | None -> report (get_location ()) (`Unexpected_eoi "attribute value") !throw data_state | Some (_, c) -> add_utf_8 value_buffer c; attribute_value_quoted_state quote l' tag name value_buffer end (* 8.2.4.40. *) and attribute_value_unquoted_state l' tag name value_buffer = next_option input !throw begin function | Some (_, (0x0009 | 0x000A | 0x000C | 0x0020)) -> tag.attributes <- (name, Buffer.contents value_buffer)::tag.attributes; before_attribute_name_state l' tag | Some (l, 0x0026) -> character_reference_in_attribute 0x003E l value_buffer (fun () -> attribute_value_unquoted_state l' tag name value_buffer) | Some (_, 0x003E) -> tag.attributes <- (name, Buffer.contents value_buffer)::tag.attributes; emit_tag l' tag | Some (l, 0) -> report l (`Bad_token ("U+0000", "attribute value", "null")) !throw (fun () -> add_utf_8 value_buffer u_rep; attribute_value_unquoted_state l' tag name value_buffer) | Some (l, (0x0022 | 0x0027 | 0x003C | 0x003D | 0x0060 as c)) -> report l (`Bad_token (char c, "attribute value", "invalid character")) !throw (fun () -> add_utf_8 value_buffer c; attribute_value_unquoted_state l' tag name value_buffer) | None -> report (get_location ()) (`Unexpected_eoi "tag") !throw data_state | Some (_, c) -> add_utf_8 value_buffer c; attribute_value_unquoted_state l' tag name value_buffer end (* 8.2.4.41. *) and character_reference_in_attribute allowed l value_buffer k = consume_character_reference true (Some allowed) l begin function | None -> add_utf_8 value_buffer 0x0026; k () | Some (`One c) -> add_utf_8 value_buffer c; k () | Some (`Two (c, c')) -> add_utf_8 value_buffer c; add_utf_8 value_buffer c'; k () end (* 8.2.4.42. *) and after_attribute_value_quoted_state l' tag = next_option input !throw begin function | Some (_, (0x0009 | 0x000A | 0x000C | 0x0020)) -> before_attribute_name_state l' tag | Some (_, 0x002F) -> self_closing_start_tag_state l' tag | Some (_, 0x003E) -> emit_tag l' tag | None -> report (get_location ()) (`Unexpected_eoi "tag") !throw data_state | Some (l, c as v) -> push input v; report l (`Bad_token (char c, "tag", "expected whitespace before attribute")) !throw (fun () -> before_attribute_name_state l' tag) end (* 8.2.4.43. *) and self_closing_start_tag_state l' tag = next_option input !throw begin function | Some (_, 0x003E) -> tag.self_closing <- true; emit_tag l' tag | None -> report (get_location ()) (`Unexpected_eoi "tag") !throw data_state | Some (l, c as v) -> push input v; report l (`Bad_token (char c, "tag", "expected '/>'")) !throw (fun () -> before_attribute_name_state l' tag) end (* 8.2.4.44. *) and bogus_comment_state l' = let buffer = Buffer.create 256 in let rec consume () = next_option input !throw begin function | Some (_, 0x003E) -> emit_comment l' buffer | Some (_, 0) -> add_utf_8 buffer u_rep; consume () | None -> emit_comment l' buffer | Some (_, c) -> add_utf_8 buffer c; consume () end in consume () (* 8.2.4.45. *) and markup_declaration_open_state l' = peek_n 2 input !throw begin function | [_, 0x002D; _, 0x002D] -> next_n 2 input !throw (fun _ -> comment_start_state l' (Buffer.create 64)) | _ -> peek_n 7 input !throw begin fun l -> match sequence_to_lowercase l with | [_, 0x64; _, 0x6F; _, 0x63; _, 0x74; _, 0x79; _, 0x70; _, 0x65] -> next_n 7 input !throw (fun _ -> doctype_state l') | _ -> peek_n 7 input !throw (function | [_, 0x5B; _, 0x43; _, 0x44; _, 0x41; _, 0x54; _, 0x41; _, 0x5B] -> if !foreign () then next_n 7 input !throw (fun _ -> cdata_section_state ()) else report l' (`Bad_token ("<![CDATA[", "content", "CDATA sections not allowed in HTML")) !throw (fun () -> bogus_comment_state l') | _ -> report l' (`Bad_token ("<!", "comment", "should begin with '<!--'")) !throw (fun () -> bogus_comment_state l')) end end (* 8.2.4.46. *) and comment_start_state l' buffer = next_option input !throw begin function | Some (_, 0x002D) -> comment_start_dash_state l' buffer | Some (l, 0) -> report l (`Bad_token ("U+0000", "comment", "null")) !throw (fun () -> add_utf_8 buffer u_rep; comment_state l' buffer) | Some (_, 0x003E) -> report l' (`Bad_token ("<!-->", "comment", "'-->' overlaps '<!--'")) !throw (fun () -> emit_comment l' buffer) | None -> report (get_location ()) (`Unexpected_eoi "comment") !throw (fun () -> emit_comment l' buffer) | Some (_, c) -> add_utf_8 buffer c; comment_state l' buffer end (* 8.2.4.47. *) and comment_start_dash_state l' buffer = next_option input !throw begin function | Some (_, 0x002D) -> comment_end_state l' buffer | Some (l, 0) -> report l (`Bad_token ("U+0000", "comment", "null")) !throw (fun () -> Buffer.add_char buffer '-'; add_utf_8 buffer u_rep; comment_state l' buffer) | Some (_, 0x003E) -> report l' (`Bad_token ("<!--->", "comment", "'-->' overlaps '<!--'")) !throw (fun () -> emit_comment l' buffer) | None -> report (get_location ()) (`Unexpected_eoi "comment") !throw (fun () -> emit_comment l' buffer) | Some (_, c) -> Buffer.add_char buffer '-'; add_utf_8 buffer c; comment_state l' buffer end (* 8.2.4.48. *) and comment_state l' buffer = next_option input !throw begin function | Some (_, 0x002D) -> comment_end_dash_state l' buffer | Some (l, 0) -> report l (`Bad_token ("U+0000", "comment", "null")) !throw (fun () -> add_utf_8 buffer u_rep; comment_state l' buffer) | None -> report (get_location ()) (`Unexpected_eoi "comment") !throw (fun () -> emit_comment l' buffer) | Some (_, c) -> add_utf_8 buffer c; comment_state l' buffer end (* 8.2.4.49. *) and comment_end_dash_state l' buffer = next_option input !throw begin function | Some (_, 0x002D) -> comment_end_state l' buffer | Some (l, 0) -> report l (`Bad_token ("U+0000", "comment", "null")) !throw (fun () -> Buffer.add_char buffer '-'; add_utf_8 buffer u_rep; comment_state l' buffer) | None -> report (get_location ()) (`Unexpected_eoi "comment") !throw (fun () -> emit_comment l' buffer) | Some (_, c) -> Buffer.add_char buffer '-'; add_utf_8 buffer c; comment_state l' buffer end (* 8.2.4.50. *) and comment_end_state l' buffer = next_option input !throw begin function | Some (_, 0x003E) -> emit_comment l' buffer | Some (l, 0) -> report l (`Bad_token ("U+0000", "comment", "null")) !throw (fun () -> Buffer.add_string buffer "--"; add_utf_8 buffer u_rep; comment_state l' buffer) | Some (l, 0x0021) -> report l (`Bad_token ("--!", "comment", "'--' should be in '-->'")) !throw (fun () -> comment_end_bang_state l' buffer) | Some (l, 0x002D) -> report l (`Bad_token ("---", "comment", "'--' should be in '-->'")) !throw (fun () -> Buffer.add_char buffer '-'; comment_end_state l' buffer) | None -> report (get_location ()) (`Unexpected_eoi "comment") !throw (fun () -> emit_comment l' buffer) | Some (l, c) -> report l (`Bad_token ("--" ^ (char c), "comment", "'--' should be in '-->'")) !throw (fun () -> Buffer.add_string buffer "--"; add_utf_8 buffer c; comment_state l' buffer) end (* 8.2.4.51. *) and comment_end_bang_state l' buffer = next_option input !throw begin function | Some (_, 0x002D) -> Buffer.add_string buffer "--!"; comment_end_dash_state l' buffer | Some (_, 0x003E) -> emit_comment l' buffer | Some (l, 0) -> report l (`Bad_token ("U+0000", "comment", "null")) !throw (fun () -> Buffer.add_string buffer "--!"; add_utf_8 buffer u_rep; comment_state l' buffer) | None -> report (get_location ()) (`Unexpected_eoi "comment") !throw (fun () -> emit_comment l' buffer) | Some (_, c) -> Buffer.add_string buffer "--!"; add_utf_8 buffer c; comment_state l' buffer end (* 8.2.5.52. *) and doctype_state l' = let doctype = {doctype_name = None; public_identifier = None; system_identifier = None; force_quirks = false} in next_option input !throw begin function | Some (_, (0x0009 | 0x000A | 0x000C | 0x0020)) -> before_doctype_name_state l' doctype | None -> report (get_location ()) (`Unexpected_eoi "doctype") !throw (fun () -> emit_doctype ~quirks:true l' doctype) | Some (l, c as v) -> report l (`Bad_token (char c, "doctype", "expected whitespace")) !throw (fun () -> push input v; before_doctype_name_state l' doctype) end (* 8.2.5.53. *) and before_doctype_name_state l' doctype = next_option input !throw begin function | Some (_, (0x0009 | 0x000A | 0x000C | 0x0020)) -> before_doctype_name_state l' doctype | Some (l, 0) -> report l (`Bad_token ("U+0000", "doctype", "null")) !throw (fun () -> doctype.doctype_name <- add_doctype_char doctype.doctype_name u_rep; doctype_name_state l' doctype) | Some (l, 0x003E) -> report l (`Bad_token (">", "doctype", "expected name")) !throw (fun () -> emit_doctype ~quirks:true l' doctype) | None -> report (get_location ()) (`Unexpected_eoi "doctype") !throw (fun () -> emit_doctype ~quirks:true l' doctype) | Some (_, c) -> doctype.doctype_name <- add_doctype_char doctype.doctype_name (to_lowercase c); doctype_name_state l' doctype end (* 8.2.5.54. *) and doctype_name_state l' doctype = next_option input !throw begin function | Some (_, (0x0009 | 0x000A | 0x000C | 0x0020)) -> after_doctype_name_state l' doctype | Some (_, 0x003E) -> emit_doctype l' doctype | Some (l, 0) -> report l (`Bad_token ("U+0000", "doctype", "null")) !throw (fun () -> doctype.doctype_name <- add_doctype_char doctype.doctype_name u_rep; doctype_name_state l' doctype) | None -> report (get_location ()) (`Unexpected_eoi "doctype") !throw (fun () -> emit_doctype ~quirks:true l' doctype) | Some (_, c) -> doctype.doctype_name <- add_doctype_char doctype.doctype_name (to_lowercase c); doctype_name_state l' doctype end (* 8.2.4.55. *) and after_doctype_name_state l' doctype = next_option input !throw begin function | Some (_, (0x0009 | 0x000A | 0x000C | 0x0020)) -> after_doctype_name_state l' doctype | Some (_, 0x003E) -> emit_doctype l' doctype | None -> report (get_location ()) (`Unexpected_eoi "doctype") !throw (fun () -> emit_doctype ~quirks:true l' doctype) | Some (l'', c as v) -> push input v; next_n 6 input !throw begin fun l -> match sequence_to_lowercase l with | [_, 0x70; _, 0x75; _, 0x62; _, 0x6C; _, 0x69; _, 0x63] -> after_doctype_public_keyword_state l' doctype | [_, 0x73; _, 0x79; _, 0x73; _, 0x74; _, 0x65; _, 0x6D] -> after_doctype_system_keyword_state l' doctype | vs -> push_list input vs; report l'' (`Bad_token (char c, "doctype", "expected 'PUBLIC' or 'SYSTEM'")) !throw (fun () -> doctype.force_quirks <- true; bogus_doctype_state l' doctype) end end (* Helper. *) and begin_public_identifier quote l' doctype = doctype.Doctype_buffers.public_identifier <- Some (Buffer.create 32); doctype_identifier_quoted_state (fun doctype c -> doctype.Doctype_buffers.public_identifier <- add_doctype_char doctype.Doctype_buffers.public_identifier c) quote after_doctype_public_identifier_state l' doctype (* Helper. *) and begin_system_identifier quote l' doctype = doctype.Doctype_buffers.system_identifier <- Some (Buffer.create 32); doctype_identifier_quoted_state (fun doctype c -> doctype.system_identifier <- add_doctype_char doctype.system_identifier c) quote after_doctype_system_identifier_state l' doctype (* 8.2.4.56. *) and after_doctype_public_keyword_state l' doctype = next_option input !throw begin function | Some (_, (0x0009 | 0x000A | 0x000C | 0x0020)) -> before_doctype_public_identifier_state l' doctype | Some (l, (0x0022 | 0x0027 as c)) -> report l (`Bad_token (char c, "doctype", "expected whitespace")) !throw (fun () -> begin_public_identifier c l' doctype) | Some (l, 0x003E) -> report l (`Bad_token (">", "doctype", "expected public identifier")) !throw (fun () -> emit_doctype ~quirks:true l' doctype) | None -> report (get_location ()) (`Unexpected_eoi "doctype") !throw (fun () -> emit_doctype ~quirks:true l' doctype) | Some (l, c) -> report l (`Bad_token (char c, "doctype", "expected whitespace")) !throw (fun () -> doctype.force_quirks <- true; bogus_doctype_state l' doctype) end (* 8.2.4.57. *) and before_doctype_public_identifier_state l' doctype = next_option input !throw begin function | Some (_, (0x0009 | 0x000A | 0x000C | 0x0020)) -> before_doctype_public_identifier_state l' doctype | Some (_, (0x0022 | 0x0027 as c)) -> begin_public_identifier c l' doctype | Some (l, 0x003E) -> report l (`Bad_token (">", "doctype", "expected public identifier")) !throw (fun () -> emit_doctype ~quirks:true l' doctype) | None -> report (get_location ()) (`Unexpected_eoi "doctype") !throw (fun () -> emit_doctype ~quirks:true l' doctype) | Some (l, c) -> report l (`Bad_token (char c, "doctype", "public identifier must be quoted")) !throw (fun () -> doctype.force_quirks <- true; bogus_doctype_state l' doctype) end (* 8.2.4.58, 8.2.4.59, 8.2.4.64, 8.2.4.65. *) and doctype_identifier_quoted_state add quote next_state l' doctype = next_option input !throw begin function | Some (_, c) when c = quote -> next_state l' doctype | Some (l, 0) -> report l (`Bad_token ("U+0000", "doctype", "null")) !throw (fun () -> add doctype u_rep; doctype_identifier_quoted_state add quote next_state l' doctype) | Some (l, 0x003E) -> report l (`Bad_token (">", "doctype", "'>' in identifier")) !throw (fun () -> emit_doctype ~quirks:true l' doctype) | None -> report (get_location ()) (`Unexpected_eoi "doctype") !throw (fun () -> emit_doctype ~quirks:true l' doctype) | Some (_, c) -> add doctype c; doctype_identifier_quoted_state add quote next_state l' doctype end (* 8.2.4.60. *) and after_doctype_public_identifier_state l' doctype = next_option input !throw begin function | Some (_, (0x0009 | 0x000A | 0x000C | 0x0020)) -> between_doctype_public_and_system_identifiers l' doctype | Some (_, 0x003E) -> emit_doctype l' doctype | Some (l, (0x0022 | 0x0027 as c)) -> report l (`Bad_token (char c, "doctype", "expected whitespace")) !throw (fun () -> begin_system_identifier c l' doctype) | None -> report (get_location ()) (`Unexpected_eoi "doctype") !throw (fun () -> emit_doctype ~quirks:true l' doctype) | Some (l, c) -> report l (`Bad_token (char c, "doctype", "system identifier must be quoted")) !throw (fun () -> doctype.force_quirks <- true; bogus_doctype_state l' doctype) end (* 8.2.4.61. *) and between_doctype_public_and_system_identifiers l' doctype = next_option input !throw begin function | Some (_, (0x0009 | 0x000A | 0x000C | 0x0020)) -> between_doctype_public_and_system_identifiers l' doctype | Some (_, 0x003E) -> emit_doctype l' doctype | Some (_, (0x0022 | 0x0027 as c)) -> begin_system_identifier c l' doctype | None -> report (get_location ()) (`Unexpected_eoi "doctype") !throw (fun () -> emit_doctype ~quirks:true l' doctype) | Some (l, c) -> report l (`Bad_token (char c, "doctype", "system identifier must be quoted")) !throw (fun () -> doctype.force_quirks <- true; bogus_doctype_state l' doctype) end (* 8.2.4.62. *) and after_doctype_system_keyword_state l' doctype = next_option input !throw begin function | Some (_, (0x0009 | 0x000A | 0x000C | 0x0020)) -> before_doctype_system_identifier_state l' doctype | Some (l, (0x0022 | 0x0027 as c)) -> report l (`Bad_token (char c, "doctype", "expected whitespace")) !throw (fun () -> begin_system_identifier c l' doctype) | Some (l, 0x003E) -> report l (`Bad_token (">", "doctype", "expected system identifier")) !throw (fun () -> emit_doctype ~quirks:true l' doctype) | None -> report (get_location ()) (`Unexpected_eoi "doctype") !throw (fun () -> emit_doctype ~quirks:true l' doctype) | Some (l, c) -> report l (`Bad_token (char c, "doctype", "expected whitespace")) !throw (fun () -> doctype.force_quirks <- true; bogus_doctype_state l' doctype) end (* 8.2.4.63. *) and before_doctype_system_identifier_state l' doctype = next_option input !throw begin function | Some (_, (0x0009 | 0x000A | 0x000C | 0x0020)) -> before_doctype_system_identifier_state l' doctype | Some (_, (0x0022 | 0x0027 as c)) -> begin_system_identifier c l' doctype | Some (l, 0x003E) -> report l (`Bad_token (">", "doctype", "expected system identifier")) !throw (fun () -> emit_doctype ~quirks:true l' doctype) | None -> report (get_location ()) (`Unexpected_eoi "doctype") !throw (fun () -> emit_doctype ~quirks:true l' doctype) | Some (l, c) -> report l (`Bad_token (char c, "doctype", "system identifier must be quoted")) !throw (fun () -> doctype.force_quirks <- true; bogus_doctype_state l' doctype) end (* 8.2.4.66. *) and after_doctype_system_identifier_state l' doctype = next_option input !throw begin function | Some (_, (0x0009 | 0x000A | 0x000C | 0x0020)) -> after_doctype_system_identifier_state l' doctype | Some (_, 0x003E) -> emit_doctype l' doctype | None -> report (get_location ()) (`Unexpected_eoi "doctype") !throw (fun () -> emit_doctype ~quirks:true l' doctype) | Some (l, c) -> report l (`Bad_token (char c, "doctype", "junk after system identifier")) !throw (fun () -> bogus_doctype_state l' doctype) end (* 8.2.4.67. *) and bogus_doctype_state l' doctype = next_option input !throw begin function | Some (_, 0x003E) -> emit_doctype l' doctype | None -> emit_doctype l' doctype | _ -> bogus_doctype_state l' doctype end (* 8.2.4.68. *) and cdata_section_state () = next_option input !throw begin function | None -> data_state () | Some (l, 0x005D) -> peek_n 2 input !throw begin function | [_, 0x005D; _, 0x003E] -> next_n 2 input !throw (fun _ -> data_state ()) | _ -> emit (l, `Char 0x005D) cdata_section_state end | Some (l, c) -> emit (l, `Char c) cdata_section_state end in let stream = (fun throw_ e k -> throw := throw_; ended := e; output := k; !current_state ()) |> make in let set_state = function | `Data -> current_state := data_state | `RCDATA -> current_state := rcdata_state | `RAWTEXT -> current_state := rawtext_state | `Script_data -> current_state := script_data_state | `PLAINTEXT -> current_state := plaintext_state in let set_foreign = (:=) foreign in stream, set_state, set_foreign