Commit 8c2f2dd3 authored by lucas.coatanlem's avatar lucas.coatanlem
Browse files

In /runtime/cduce_js.ml :

	* Add load_from_dom correctly implemented (not yet functionnal)
	* Add replace_inner (innerHTML)
	* Add replace_outer (outerHTML)
parent b022b026
let null_js_str s =
Js.Opt.case s
(fun () -> "")
Js.to_string
let load_from_dom start_e end_e text_e id =
(* find the node in the current page with id *)
let elem = Dom_html.document ## getElementById (Js.string id) in
match Js.Opt.to_option elem with
None -> Value.failwith' ("No element with id " ^ id)
None -> Value.failwith' ("No element with id : " ^ id)
| Some elem ->
ignore (elem);
Value.failwith' "DOM Parsing Not implemented"
(* recursively:
if the current node is an element node,
call start_e tag (attribute,value) list
let elem_node = (elem :> Dom.node Js.t) in
let elem_node_opt = Js.Opt.return elem_node in
let rec parse elem =
match Js.Opt.to_option elem with
None -> ()
| Some elem ->
begin
let cont =
match elem ## nodeType with
Dom.CDATA_SECTION | Dom.TEXT ->
let txt = null_js_str (elem ## nodeValue) in
text_e txt;
ignore
| Dom.ELEMENT ->
let name = Js.to_string (elem ## nodeName) in
let elem =
Js.Opt.get (Dom.CoerceTo.element elem)
(fun () -> assert false)
in
let map = elem ## attributes in
let att_list = ref [] in
for i = (map ## length) - 1 downto 0 do
let att =
Js.Opt.get (map ## item (i))
(fun () -> assert false)
in
let name = Js.to_string (att ## name) in
let value = Js.to_string (att ## value) in
att_list := (name, value) :: !att_list
done;
start_e name !att_list;
(fun () -> end_e name)
| _ -> ignore
in
parse (elem ## firstChild);
parse (elem ## nextSibling);
cont ()
end
in
parse elem_node_opt
if the current node is a text node or a CDATA section
call text_e txt
in all cases
traverse the node recursively using firstChild/nextSibling
let replace_inner id str =
(* replace content of id by str *)
let elem = Dom_html.document ## getElementById (Js.string id) in
match Js.Opt.to_option elem with
None -> Value.failwith' ("No element with id : " ^ id)
| Some elem -> elem ## innerHTML = (Js.string str)
after the recursive calls, if the current node is an element node
call end_e tag
*)
(* return unit and in case of error, Value.failwith' "error message" *)
let replace_outer id str =
(* replace id by str *)
let elem = Dom_html.document ## getElementById (Js.string id) in
match Js.Opt.to_option elem with
None -> Value.failwith' ("No element with id : " ^ id)
| Some elem -> elem ## outerHTML = (Js.string str)
(* Tests
let _ = replace_inner "foo" "bar"
let _ = replace_outer "foo" "bar"
let _ = load_from_dom
Load_xml.start_element_handler
Load_xml.end_element_handler
Load_xml.text_handler
"foo"
*)
let use () =
Load_xml.xml_parser :=
load_from_dom Load_xml.start_element_handler
Load_xml.end_element_handler
Load_xml.text_handler
;
let use () =
Load_xml.xml_parser :=
load_from_dom Load_xml.start_element_handler
Load_xml.end_element_handler
Load_xml.text_handler
;
......@@ -5,4 +5,5 @@ let fact (n : Int) : Int =
let x = fact 24
let [] = print (string_of x)
let _ = load_xml "id:foo" (* raises an exception for now *)
let doc = load_xml "foo" (* raises an exception for now *)
let [] = print (print_xml doc) (* besoin de faire print print_xml !!! *)
\ No newline at end of file
......@@ -4,6 +4,7 @@
<script type="text/javascript" src="main.js" > </script>
</head>
Activer la console javascript avec Ctrl-Shif-I -> Console
<div id="foo"/>
<body>
</body>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment