1 (** Wrapper around Perl [HTML::Element] class. *)
2 (* Copyright (C) 2003 Merjis Ltd.
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this library; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
19 $Id: pl_HTML_Element.ml,v 1.5 2008-03-01 13:02:21 rich Exp $
24 let _ = eval "use HTML::Element"
26 type 'a content_t = Element of 'a | String of string
28 class html_element sv =
30 let rec assocs_of_svlist = function
32 | [x] -> failwith "HTML::Element all_attr returned odd-length list!"
33 | svname :: svvalue :: xs ->
34 (string_of_sv svname, string_of_sv svvalue) :: assocs_of_svlist xs
37 let rec list_of_svlist = function
40 string_of_sv sv :: list_of_svlist xs
48 string_of_sv (call_method sv "attr" [sv_of_string name])
49 method set_attr name value =
50 call_method_void sv "attr" [sv_of_string name; sv_of_string value]
52 string_of_sv (call_method sv "tag" [])
54 call_method_void sv "tag" [sv_of_string tag]
56 let sv = call_method sv "parent" [] in
58 method set_parent (parent : html_element) =
59 call_method_void sv "parent" [parent#sv]
61 let svlist = call_method_array sv "content_list" [] in
64 (* Not very satisfactory, but sv_type fails to discern the type
65 * for some reason. XXX
67 let str = string_of_sv c in
68 let marker = "HTML::Element=HASH(" in
69 let marker_len = String.length marker in
70 if String.length str > marker_len &&
71 String.sub str 0 marker_len = marker then
72 Element (new html_element c)
74 String (string_of_sv c)
77 let svlist = call_method_array sv "all_attr" [] in
78 assocs_of_svlist svlist
79 method all_attr_names =
80 let svlist = call_method_array sv "all_attr_names" [] in
82 method all_external_attr =
83 let svlist = call_method_array sv "all_external_attr" [] in
84 assocs_of_svlist svlist
85 method all_external_attr_names =
86 let svlist = call_method_array sv "all_external_attr_names" [] in
91 (* Note that "new" is a reserved word, so I've appended an _ character. *)
93 let rec loop = function
95 | (name, value) :: xs -> sv_of_string name :: sv_of_string value :: loop xs
97 let sv = call_class_method "HTML::Element" "new"
98 (sv_of_string tag :: loop attrs) in