File: pl_HTML_Element.ml

package info (click to toggle)
perl4caml 0.9.5-8
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 540 kB
  • sloc: ml: 1,572; ansic: 957; makefile: 186; perl: 45
file content (99 lines) | stat: -rw-r--r-- 3,219 bytes parent folder | download | duplicates (4)
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
(** Wrapper around Perl [HTML::Element] class. *)
(*  Copyright (C) 2003 Merjis Ltd.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this library; see the file COPYING.  If not, write to
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.

    $Id: pl_HTML_Element.ml,v 1.5 2008-03-01 13:02:21 rich Exp $
  *)

open Perl

let _ = eval "use HTML::Element"

type 'a content_t = Element of 'a | String of string

class html_element sv =

  let rec assocs_of_svlist = function
      [] -> []
    | [x] -> failwith "HTML::Element all_attr returned odd-length list!"
    | svname :: svvalue :: xs ->
	(string_of_sv svname, string_of_sv svvalue) :: assocs_of_svlist xs
  in

  let rec list_of_svlist = function
      [] -> []
    | sv :: xs ->
	string_of_sv sv :: list_of_svlist xs
  in

object (self)

  method sv = sv

  method attr name =
    string_of_sv (call_method sv "attr" [sv_of_string name])
  method set_attr name value =
    call_method_void sv "attr" [sv_of_string name; sv_of_string value]
  method tag =
    string_of_sv (call_method sv "tag" [])
  method set_tag tag =
    call_method_void sv "tag" [sv_of_string tag]
  method parent =
    let sv = call_method sv "parent" [] in
    new html_element sv
  method set_parent (parent : html_element) =
    call_method_void sv "parent" [parent#sv]
  method content_list =
    let svlist = call_method_array sv "content_list" [] in
    List.map
      (fun c ->
	 (* Not very satisfactory, but sv_type fails to discern the type
	  * for some reason. XXX
	  *)
	 let str = string_of_sv c in
	 let marker = "HTML::Element=HASH(" in
	 let marker_len = String.length marker in
	 if String.length str > marker_len &&
	   String.sub str 0 marker_len = marker then
	     Element (new html_element c)
	 else
	   String (string_of_sv c)
      ) svlist
  method all_attr =
    let svlist = call_method_array sv "all_attr" [] in
    assocs_of_svlist svlist
  method all_attr_names =
    let svlist = call_method_array sv "all_attr_names" [] in
    list_of_svlist svlist
  method all_external_attr =
    let svlist = call_method_array sv "all_external_attr" [] in
    assocs_of_svlist svlist
  method all_external_attr_names =
    let svlist = call_method_array sv "all_external_attr_names" [] in
    list_of_svlist svlist

end

(* Note that "new" is a reserved word, so I've appended an _ character. *)
let new_ tag attrs =
  let rec loop = function
      [] -> []
    | (name, value) :: xs -> sv_of_string name :: sv_of_string value :: loop xs
  in
  let sv = call_class_method "HTML::Element" "new"
	     (sv_of_string tag :: loop attrs) in
  new html_element sv