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 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
|
(* $Id: nethtml.ml,v 1.9 2001/08/31 22:11:56 gerd Exp $
* ----------------------------------------------------------------------
*
*)
open Nethtml_scanner;;
type document =
Element of (string * (string*string) list * document list)
| Data of string
;;
exception End_of_scan;;
exception Found;;
type element_class = (* What is the class of an element? *)
[ `Inline
| `Block
| `Essential_block
| `None
| `Everywhere
]
;;
type model_constraint = (* The constraint the subelements must fulfill *)
[ `Inline
| `Block
| `Flow (* = `Inline or `Block *)
| `Empty
| `Any
| `Special
| `Elements of string list (* Enumeration of allowed elements *)
| `Or of (model_constraint * model_constraint)
| `Except of (model_constraint * model_constraint)
| `Sub_exclusions of (string list * model_constraint)
]
;;
type simplified_dtd =
(string * (element_class * model_constraint)) list
let ( |. ) a b = `Or(a,b);;
let ( -. ) a b = `Except(a,b);;
let block_elements =
(* Only used for exclusions *)
[ "p"; "dl"; "div"; "center"; "noscript"; "noframes"; "blockquote"; "form";
"isindex"; "hr"; "table"; "fieldset"; "address"; "h1"; "h2"; "h3"; "h4";
"h5"; "h6"; "pre"; "ul"; "ol"; "dir"; "menu" ];;
let html40_dtd =
[ (* --------- INLINE ELEMENTS ------------ *)
(* %fontstyle; *)
"tt", (`Inline, `Inline);
"i", (`Inline, `Inline);
"b", (`Inline, `Inline);
"big", (`Inline, `Inline);
"small", (`Inline, `Inline);
(* transitional: *)
"u", (`Inline, `Inline);
"s", (`Inline, `Inline);
"strike", (`Inline, `Inline);
(* %phrase; *)
"em", (`Inline, `Inline);
"strong", (`Inline, `Inline);
"dfn", (`Inline, `Inline);
"code", (`Inline, `Inline);
"samp", (`Inline, `Inline);
"kbd", (`Inline, `Inline);
"var", (`Inline, `Inline);
"cite", (`Inline, `Inline);
"abbr", (`Inline, `Inline);
"acronym", (`Inline, `Inline);
(* %special; *)
"sup", (`Inline, `Inline);
"sub", (`Inline, `Inline);
"span", (`Inline, `Inline);
"bdo", (`Inline, `Inline);
"br", (`Inline, `Empty);
"a", (`Inline, `Sub_exclusions(["a"],`Inline));
"img", (`Inline, `Empty);
"object", (`Inline, (`Flow |. `Elements ["param"]));
"script", (`Inline, `Special);
"map", (`Inline, (`Flow |. `Elements ["area"]));
"q", (`Inline, `Inline);
(* transitional: *)
"applet", (`Inline, (`Flow |. `Elements ["param"]));
"font", (`Inline, `Inline);
"basefont", (`Inline, `Empty);
"iframe", (`Inline, `Flow);
(* %formctrl; *)
"input", (`Inline, `Empty);
"select", (`Inline, `Elements ["optgroup"; "option"]);
"textarea", (`Inline, `Elements []); (* #PCDATA *)
"label", (`Inline, `Sub_exclusions( ["label"],
`Inline));
"button", (`Inline, `Sub_exclusions( ["a"; "input"; "select";
"textarea"; "label";
"button"; "form";
"fieldset"; "isindex";
"iframe"],
`Flow));
(* ------------ BLOCK ELEMENTS ----------*)
"p", (`Block, `Inline);
(* %heading; *)
"h1", (`Block, `Inline);
"h2", (`Block, `Inline);
"h3", (`Block, `Inline);
"h4", (`Block, `Inline);
"h5", (`Block, `Inline);
"h6", (`Block, `Inline);
(* %list; *)
"ul", (`Block, `Elements ["li"]);
"ol", (`Block, `Elements ["li"]);
(* transitional: *)
"dir", (`Block, `Sub_exclusions( block_elements,
`Elements ["li"]));
"menu", (`Block, `Sub_exclusions( block_elements,
`Elements ["li"]));
(* %preformatted; *)
"pre", (`Block, `Sub_exclusions( [ "img"; "object"; "applet";
"big"; "small"; "sub";
"sup"; "font"; "basefont"],
`Inline ));
(* other: *)
"dl", (`Block, `Elements ["dl"; "dd"]);
"div", (`Block, `Flow);
"noscript", (`Block, `Flow);
"blockquote", (`Block, (`Block |. `Elements ["script"]));
"form", (`Block, `Sub_exclusions( ["form"],
`Block |.
`Elements ["script"]));
"hr", (`Block, `Empty);
"table", (`Block, `Elements ["caption"; "col"; "colgroup";
"thead"; "tfoot"; "tbody"; "tr"]);
"fieldset", (`Block, (`Flow |. `Elements ["legend"]));
"address", (`Block, `Inline);
(* transitional: *)
"center", (`Block, `Flow);
"noframes", (`Block, `Flow);
"isindex", (`Block, `Empty);
(* ------------ OTHER ELEMENTS ----------*)
"body", (`None, (`Flow |. `Elements ["script"]));
"area", (`None, `Empty);
"link", (`None, `Empty);
"param", (`None, `Empty);
"ins", (`Everywhere, `Flow);
"del", (`Everywhere, `Flow);
"dt", (`None, `Inline);
"dd", (`None, `Flow);
"li", (`None, `Flow);
"optgroup", (`None, `Elements ["option"]);
"option", (`None, `Elements []); (* #PCDATA *)
"legend", (`None, `Inline);
"caption", (`None, `Inline);
"thead", (`None, `Elements ["tr"]);
"tfoot", (`None, `Elements ["tr"]);
"colgroup", (`None, `Elements ["col"]);
"col", (`None, `Empty);
"tr", (`None, `Elements ["th"; "td"]);
"th", (`None, `Flow);
"td", (`None, `Flow);
"head", (`None, `Elements ["title"; "base"; "script";
"style"; "meta"; "link";
"object"]);
"title", (`None, `Elements []); (* #PCDATA *)
"base", (`None, `Empty);
"meta", (`None, `Empty);
"style", (`None, `Special);
"html", (`None, (`Flow |.
`Elements ["head";
"title"; "base"; "script";
"style"; "meta"; "link";
"object";
"body"; "frameset"]));
(* transitional: *)
"frameset", (`None, `Elements ["frameset"; "frame"; "noframes"]);
"frame", (`None, `Empty);
]
;;
let relax_dtd dtd =
(* Changes (`Inline, `Inline) constraints into (`Inline, `Flow). *)
let rec relax_model m =
match m with
`Inline -> `Flow
| `Sub_exclusions(l,m') -> `Sub_exclusions(l,relax_model m')
| other -> other
in
List.map
(fun (name, (elclass, elconstr)) ->
match elclass with
`Inline ->
(name, (elclass, relax_model elconstr))
| other ->
(name, (elclass, elconstr))
)
dtd
;;
let essential_blocks dtd elements =
(* Changes the passed block elements into essential block elements *)
List.map
(fun (name, (elclass, elconstr)) ->
match elclass with
`Block when List.mem name elements ->
(name, ( `Essential_block, elconstr))
| other ->
(name, (elclass, elconstr))
)
dtd
;;
let relaxed_html40_dtd =
essential_blocks
(relax_dtd html40_dtd)
[ "table"; "ol"; "ul"; "dl" ]
;;
let rec parse_comment buf =
let t = scan_comment buf in
match t with
Mcomment ->
let s = Lexing.lexeme buf in
s ^ parse_comment buf
| Eof ->
raise End_of_scan
| _ ->
(* must be Rcomment *)
""
;;
let rec parse_doctype buf =
let t = scan_doctype buf in
match t with
Mdoctype ->
let s = Lexing.lexeme buf in
s ^ parse_doctype buf
| Eof ->
raise End_of_scan
| _ ->
(* must be Rdoctype *)
""
;;
let rec parse_pi buf =
let t = scan_pi buf in
match t with
Mpi ->
let s = Lexing.lexeme buf in
s ^ parse_pi buf
| Eof ->
raise End_of_scan
| _ ->
(* must be Rpi *)
""
;;
let hashtbl_from_alist l =
let ht = Hashtbl.create (List.length l) in
List.iter
(fun (k, v) ->
Hashtbl.add ht k v)
l;
ht
;;
module S = struct
type t = string
let compare = (Pervasives.compare : string -> string -> int)
end
module Strset = Set.Make(S);;
let parse_document ?(dtd = html40_dtd)
?(return_declarations = false)
?(return_pis = false)
?(return_comments = false) buf =
let current_name = ref "" in
let current_atts = ref [] in
let current_subs = ref [] in
let current_excl = ref Strset.empty in (* current exclusions *)
let stack = Stack.create() in
let dtd_hash = hashtbl_from_alist dtd in
let model_of element_name =
if element_name = "" then
(`Everywhere, `Any)
else
let extract =
function
(eclass, `Sub_exclusions(_,m)) -> eclass, m
| m -> m
in
try
extract(Hashtbl.find dtd_hash element_name)
with
Not_found -> (`Everywhere, `Any)
in
let exclusions_of element_name =
if element_name = "" then
[]
else
let extract =
function
(eclass, `Sub_exclusions(l,_)) -> l
| _ -> []
in
try
extract(Hashtbl.find dtd_hash element_name)
with
Not_found -> []
in
let is_possible_subelement parent_element parent_exclusions sub_element =
let (sub_class, _) = model_of sub_element in
let rec eval m =
match m with
`Inline -> sub_class = `Inline
| `Block -> sub_class = `Block || sub_class = `Essential_block
| `Flow -> sub_class = `Inline || sub_class = `Block ||
sub_class = `Essential_block
| `Elements l -> List.mem sub_element l
| `Any -> true
| `Or(m1,m2) -> eval m1 || eval m2
| `Except(m1,m2) -> eval m1 && not (eval m2)
| `Empty -> false
| `Special -> false
| `Sub_exclusions(_,_) -> assert false
in
(sub_class = `Everywhere) || (
(not (Strset.mem sub_element parent_exclusions)) &&
let (_, parent_model) = model_of parent_element in
eval parent_model
)
in
let unwind_stack sub_name =
(* If the current element is not a possible parent element for sub_name,
* search the parent element in the stack.
* Either the new current element is the parent, or there was no
* possible parent. In the latter case, the current element is the
* same element as before.
*)
let backup = Stack.create() in
let backup_name = !current_name in
let backup_atts = !current_atts in
let backup_subs = !current_subs in
let backup_excl = !current_excl in
try
while not (is_possible_subelement !current_name !current_excl sub_name) do
(* End the current element and remove it from the stack: *)
let grant_parent = Stack.pop stack in
Stack.push grant_parent backup; (* Save it; may we need it *)
let (gp_name, gp_atts, gp_subs, gp_excl) = grant_parent in
(* If gp_name is an essential element, we are not allowed to close
* it implicitly, even if that violates the DTD.
*)
let (gp_class, _) = model_of gp_name in
if gp_class = `Essential_block then raise Stack.Empty;
let current = Element (!current_name, !current_atts,
List.rev !current_subs) in
current_name := gp_name;
current_atts := gp_atts;
current_excl := gp_excl;
current_subs := current :: gp_subs
done;
with
Stack.Empty ->
(* It did not work! Push everything back to the stack, and
* resume the old state.
*)
while Stack.length backup > 0 do
Stack.push (Stack.pop backup) stack
done;
current_name := backup_name;
current_atts := backup_atts;
current_subs := backup_subs;
current_excl := backup_excl
in
let parse_atts() =
let rec next_no_space() =
match scan_element buf with
Space _ -> next_no_space()
| t -> t
in
let rec parse_atts_lookahead next =
match next with
Relement -> []
| Name n ->
begin match next_no_space() with
Is ->
begin match next_no_space() with
Name v ->
(String.lowercase n, v) ::
parse_atts_lookahead (next_no_space())
| Literal v ->
(String.lowercase n,v) ::
parse_atts_lookahead (next_no_space())
| Eof ->
raise End_of_scan
| Relement ->
(* Illegal *)
[]
| _ ->
(* Illegal *)
parse_atts_lookahead (next_no_space())
end
| Eof ->
raise End_of_scan
| Relement ->
(* <tag name> <==> <tag name="name"> *)
[ String.lowercase n, String.lowercase n ]
| next' ->
(* assume <tag name ... > <==> <tag name="name" ...> *)
( String.lowercase n, String.lowercase n ) ::
parse_atts_lookahead next'
end
| Eof ->
raise End_of_scan
| _ ->
(* Illegal *)
parse_atts_lookahead (next_no_space())
in
parse_atts_lookahead (next_no_space())
in
let rec parse_special name =
(* Parse until </name> *)
match scan_special buf with
Lelementend n ->
if String.lowercase n = name then
""
else
"</" ^ n ^ parse_special name
| Eof ->
raise End_of_scan
| Cdata s ->
s ^ parse_special name
| _ ->
(* Illegal *)
parse_special name
in
let rec skip_element() =
(* Skip until ">" *)
match scan_element buf with
Relement ->
()
| Eof ->
raise End_of_scan
| _ ->
skip_element()
in
let rec parse_next() =
let t = scan_document buf in
match t with
Lcomment ->
let comment = parse_comment buf in
if return_comments then
current_subs := (Element("--",["contents",comment],[])) :: !current_subs;
parse_next()
| Ldoctype ->
let decl = parse_doctype buf in
if return_declarations then
current_subs := (Element("!",["contents",decl],[])) :: !current_subs;
parse_next()
| Lpi ->
let pi = parse_pi buf in
if return_pis then
current_subs := (Element("?",["contents",pi],[])) :: !current_subs;
parse_next()
| Lelement name ->
let name = String.lowercase name in
let (_, model) = model_of name in
( match model with
`Empty ->
let atts = parse_atts() in
unwind_stack name;
current_subs := (Element(name, atts, [])) :: !current_subs;
parse_next()
| `Special ->
let atts = parse_atts() in
unwind_stack name;
let data = parse_special name in
(* Read until ">" *)
skip_element();
current_subs := (Element(name, atts, [Data data])) :: !current_subs;
parse_next()
| _ ->
let atts = parse_atts() in
(* Unwind the stack until we find an element which can be
* the parent of the new element:
*)
unwind_stack name;
(* Push the current element on the stack, and this element
* becomes the new current element:
*)
let new_excl = exclusions_of name in
Stack.push
(!current_name, !current_atts, !current_subs, !current_excl)
stack;
current_name := name;
current_atts := atts;
current_subs := [];
List.iter
(fun xel -> current_excl := Strset.add xel !current_excl)
new_excl;
parse_next()
)
| Cdata data ->
current_subs := (Data data) :: !current_subs;
parse_next()
| Lelementend name ->
let name = String.lowercase name in
(* Read until ">" *)
skip_element();
(* Search the element to close on the stack: *)
let found =
(name = !current_name) ||
try
Stack.iter
(fun (old_name, _, _, _) ->
if name = old_name then raise Found;
match model_of old_name with
`Essential_block, _ -> raise Not_found;
(* Don't close essential blocks implicitly *)
| _ -> ())
stack;
false
with
Found -> true
| Not_found -> false
in
(* If not found, the end tag is wrong. Simply ignore it. *)
if not found then
parse_next()
else begin
(* If found: Remove the elements from the stack, and append
* them to the previous element as sub elements
*)
while !current_name <> name do
let old_name, old_atts, old_subs, old_excl = Stack.pop stack in
current_subs := (Element (!current_name, !current_atts,
List.rev !current_subs)) :: old_subs;
current_name := old_name;
current_atts := old_atts;
current_excl := old_excl
done;
(* Remove one more element: the element containing the element
* currently being closed.
*)
let old_name, old_atts, old_subs, old_excl = Stack.pop stack in
current_subs := (Element (!current_name, !current_atts,
List.rev !current_subs)) :: old_subs;
current_name := old_name;
current_atts := old_atts;
current_excl := old_excl;
(* Go on *)
parse_next()
end
| Eof ->
raise End_of_scan
| _ ->
parse_next()
in
try
parse_next();
List.rev !current_subs
with
End_of_scan ->
(* Close all remaining elements: *)
while Stack.length stack > 0 do
let old_name, old_atts, old_subs, old_excl = Stack.pop stack in
current_subs := Element (!current_name,
!current_atts,
List.rev !current_subs) :: old_subs;
current_name := old_name;
current_atts := old_atts;
current_excl := old_excl
done;
List.rev !current_subs
;;
let parse_string ?dtd ?return_declarations ?return_pis ?return_comments s =
let buf = Lexing.from_string s in
parse_document ?dtd ?return_declarations ?return_comments ?return_pis buf
;;
let parse_file ?dtd ?return_declarations ?return_pis ?return_comments fd =
let buf = Lexing.from_channel fd in
parse_document ?dtd ?return_declarations ?return_comments ?return_pis buf
;;
let rec map f doc =
match doc with
Element(name,atts,subdocs) ->
(match name with
"!"
| "?"
| "--" ->
Element(name,atts,map_list f subdocs)
| _ ->
let atts' =
List.map
(fun (aname,aval) ->
aname, f aval
)
atts
in
let subdocs' = map_list f subdocs in
Element(name,atts',subdocs')
)
| Data s ->
Data(f s)
and map_list f l = List.map (map f) l;;
let encode = map_list Netencoding.Html.encode_from_latin1;;
let decode = map_list Netencoding.Html.decode_to_latin1;;
let quote_quot_re = Netstring_str.regexp "\"";;
let write ?(dtd = html40_dtd) os doc =
let write_os =
match os with
`Out_buffer b ->
(fun s -> Buffer.add_string b s)
| `Out_channel ch ->
(fun s -> output_string ch s)
| `Out_function f ->
(fun s -> f s 0 (String.length s))
in
let quote_quot s =
Netstring_str.global_substitute quote_quot_re
(fun _ _ -> """)
s
in
let rec trav doc =
match doc with
Element(name,atts,subdocs) ->
( match name with
"!" ->
write_os "<!";
write_os (List.assoc "contents" atts);
write_os ">";
| "?" ->
write_os "<?";
write_os (List.assoc "contents" atts);
write_os ">";
| "--" ->
write_os "<!--";
write_os (List.assoc "contents" atts);
write_os "-->";
| _ ->
let is_empty =
try
let _, constr = List.assoc name dtd in
constr = `Empty
with
Not_found -> false
in
write_os "<";
write_os name;
List.iter
(fun (aname,aval) ->
write_os " ";
write_os aname;
write_os "=\"";
write_os (quote_quot aval);
write_os "\"";
)
atts;
write_os ">";
List.iter trav subdocs;
if not is_empty then begin
write_os "</";
write_os name;
write_os ">";
end
)
| Data s ->
write_os s
in
try
List.iter trav doc
with
Not_found -> failwith "write"
;;
(* ======================================================================
* History:
*
* $Log: nethtml.ml,v $
* Revision 1.9 2001/08/31 22:11:56 gerd
* Added essential blocks.
*
* Revision 1.8 2001/07/15 15:00:55 gerd
* Relaxed the DTD.
*
* Revision 1.7 2001/07/15 14:20:54 gerd
* Bugfix: Sometimes too many elements were closed when an
* end tag was found. This is fixed now.
* New relaxed_html40_dtd.
* New constraint `Sub_exclusions. html40_dtd uses this
* constraint already.
* Attribute values without quotes are no longer converted
* to uppercase.
*
* Revision 1.6 2001/06/10 23:56:50 gerd
* Fix: 'write' no longer writes end tags of empty elements.
*
* Revision 1.5 2001/06/08 22:21:03 gerd
* Fix: if 'write' is applied to bad documents a Failure will
* be raised instead of Not_found.
*
* Revision 1.4 2001/06/08 22:19:55 gerd
* Added functions encode, decode, write for convenience.
*
* Revision 1.3 2001/06/08 16:25:27 gerd
* Bugfix: </SCRIPT> is now recognized (thanks to David Fox)
* The parser may now return comments, declarations, and processing
* instructions if requested to do so
* The parser accepts xhtml to some extent
* Now exported: parse_document.
*
* Revision 1.2 2001/04/07 23:38:26 gerd
* Added a simplified representation of the DTD. This improves
* the quality of the parser drastically. For example,
* "<p>abc<p>def" is no longer parsed as "<p>abc<p>def</p></p>",
* but as "<p>abc</p><p>def</p>". However, the representation is not
* perfect yet. What's definitly missing are the exclusion lists
* of the DTD. Because of this missing feature, "<a>abc<a>def" is
* still parsed as "<a>abc<a>def</a></a>" although the DTD states
* that anchors cannot contain anchors ( - but it also states that
* end tags of anchors cannot be omitted, so this feature is not
* priority 1).
*
* Revision 1.1 2000/03/03 01:07:25 gerd
* Initial revision.
*
*
*)
|