highlighter
;;
+let iter_children ~f (n:Gdome.node) =
+ let rec aux =
+ function
+ None -> ()
+ | Some n ->
+ let sibling = n#get_nextSibling in
+ (f n) ;
+ aux sibling
+ in
+ aux n#get_firstChild
+;;
+
let highlight_nodes ~xrefs (doc:Gdome.document) =
let highlighted = ref [] in
let rec aux (n:Gdome.element) =
highlighted :=
(highlight_node ~color:"#00ff00"(* light green *) doc (n :> Gdome.node))::
!highlighted ;
- let children = n#get_childNodes in
- for i = 0 to children#get_length - 1 do
- match children#item i with
- None -> assert false
- | Some n ->
- if n#get_nodeType = GdomeNodeTypeT.ELEMENT_NODE then
- aux (new Gdome.element_of_node n)
- done
+ iter_children (n :> Gdome.node)
+ ~f:(function n ->
+ if n#get_nodeType = GdomeNodeTypeT.ELEMENT_NODE then
+ aux (new Gdome.element_of_node n))
in
aux doc#get_documentElement ;
!highlighted
done
| _,_ -> assert false
end ;
+(* debugging only
let fchildren = f#get_childNodes in
let tchildren = t#get_childNodes in
+*)
let rec dumb_diff =
function
[],[] -> ()
prerr_endline ("XML_DIFF: cancello i nodi residui " ^ String.concat ", " (List.map (function n -> match n#get_localName with Some s -> s#to_string | None -> "_") tl1)) ;
List.iter (function n -> ignore (f#removeChild n)) tl1
in
- let node_list_of_nodeList nl =
- let rec aux i =
- match nl#item ~index:i with
+ let node_list_of_nodeList n =
+ let rec aux =
+ function
None -> []
| Some n when
n#get_nodeType = GdomeNodeTypeT.ELEMENT_NODE
or n#get_nodeType = GdomeNodeTypeT.TEXT_NODE ->
- n::(aux (i+1))
+ n::(aux n#get_nextSibling)
| Some n ->
prerr_endline ("XML_DIFF: mi sto perdendo i nodi di tipo " ^ string_of_int (Obj.magic n#get_nodeType)) ;
- aux (i+1)
+ aux n#get_nextSibling
in
- aux 0
+ aux n#get_firstChild
in
+(* debugging stuff only
for i = 0 to fchildren#get_length - 1 do
match fchildren#item i with
None -> prerr_endline "EUREKA: ma siamo matti?"
|Some n ->
match n#get_nodeType with GdomeNodeTypeT.ATTRIBUTE_NODE -> prerr_endline "EUREKA attr" | GdomeNodeTypeT.CDATA_SECTION_NODE -> prerr_endline "EUREKA text" | GdomeNodeTypeT.DOCUMENT_FRAGMENT_NODE -> prerr_endline "EUREKA document fragment" | _ -> prerr_endline ("EUREKA " ^ string_of_int (Obj.magic n#get_nodeType))
done ;
-prerr_endline("XML_DIFF: from ha " ^ string_of_int fchildren#get_length ^ " nodi, ovvero " ^ string_of_int (List.length (node_list_of_nodeList fchildren)) ^ " figli");
-prerr_endline("XML_DIFF: to ha " ^ string_of_int tchildren#get_length ^ " nodi, ovvero " ^ string_of_int (List.length (node_list_of_nodeList tchildren)) ^ " figli");
+prerr_endline("XML_DIFF: from ha " ^ string_of_int fchildren#get_length ^ " nodi, ovvero " ^ string_of_int (List.length (node_list_of_nodeList f)) ^ " figli");
+prerr_endline("XML_DIFF: to ha " ^ string_of_int tchildren#get_length ^ " nodi, ovvero " ^ string_of_int (List.length (node_list_of_nodeList t)) ^ " figli");
prerr_endline ("XML_DIFF: chiamo dumb_diff su " ^ String.concat ", " (List.map (function n -> match n#get_localName with Some s -> s#to_string | None -> "_") (node_list_of_nodeList fchildren)) ^ " e " ^ String.concat ", " (List.map (function n -> match n#get_localName with Some s -> s#to_string | None -> "_") (node_list_of_nodeList tchildren))) ;
+*)
dumb_diff
- (node_list_of_nodeList fchildren, node_list_of_nodeList tchildren)
+ (node_list_of_nodeList f, node_list_of_nodeList t)
| t1,t2,_,_,_,_ when
(t1 = GdomeNodeTypeT.ELEMENT_NODE || t1 = GdomeNodeTypeT.TEXT_NODE) &&
(t2 = GdomeNodeTypeT.ELEMENT_NODE || t2 = GdomeNodeTypeT.TEXT_NODE) ->