Skip to content

Commit 35b8258

Browse files
committed
Merge pull request #110 from ocsigen/noescape
Do not escape comments
2 parents 7a05dd6 + e82852f commit 35b8258

File tree

3 files changed

+58
-24
lines changed

3 files changed

+58
-24
lines changed

_oasis

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Library tyxml_f
6464
Html5_sigs,
6565
Html5_types,
6666
Html5_f
67-
BuildDepends: uutf
67+
BuildDepends: uutf, re
6868

6969
Library tyxml_top
7070
FindlibName: top

lib/xml_print.ml

+36-23
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,39 @@ let encode_unsafe_char_and_at s =
4848
Buffer.contents b
4949

5050
let compose_decl ?(version = "1.0") ?(encoding = "UTF-8") () =
51-
"<?xml version=\"" ^ version ^ "\" encoding=\"" ^ encoding ^ "\"?>\n"
51+
Format.sprintf
52+
{|<?xml version="%s" encoding="%s"?>\n|}
53+
version encoding
5254

5355
let compose_doctype dt args =
54-
"<!DOCTYPE " ^ dt
55-
^ (if args = []
56-
then ""
57-
else
58-
" PUBLIC " ^
59-
String.concat " " (List.map (fun a -> "\"" ^ a ^ "\"") args)) ^ ">"
60-
56+
let pp_args fmt = function
57+
| [] -> ()
58+
| l ->
59+
Format.fprintf fmt " PUBLIC %a"
60+
(Format.pp_print_list ~pp_sep:Format.pp_print_space
61+
(fun fmt -> Format.fprintf fmt "%S"))
62+
l
63+
in
64+
Format.asprintf
65+
"<!DOCTYPE %s%a>"
66+
dt
67+
pp_args args
68+
69+
let re_end_comment = Re.(compile @@ alt [
70+
seq [ bos ; str ">" ] ;
71+
seq [ bos ; str "->" ] ;
72+
str "-->" ;
73+
str "--!>" ;
74+
])
75+
let escape_comment s =
76+
let f g = match Re.Group.get g 0 with
77+
| ">" -> "&gt;"
78+
| "->" -> "-&gt;"
79+
| "-->" -> "--&gt;"
80+
| "--!>" -> "--!&gt;"
81+
| s -> s
82+
in
83+
Re.replace ~all:true re_end_comment ~f s
6184

6285
(* copied form js_of_ocaml: compiler/javascript.ml *)
6386
let pp_number fmt v =
@@ -177,16 +200,6 @@ module type TagList = sig val emptytags : string list end
177200

178201
let pp_noop _fmt _ = ()
179202

180-
(* Present only in ocaml >= 4.02 *)
181-
let rec pp_print_list ~pp_sep pp_v ppf = function
182-
| [] -> ()
183-
| [v] -> pp_v ppf v
184-
| v :: vs ->
185-
pp_v ppf v;
186-
pp_sep ppf ();
187-
pp_print_list ~pp_sep pp_v ppf vs
188-
189-
190203
module Make_fmt
191204
(Xml : Xml_sigs.Iterable)
192205
(I : TagList) =
@@ -213,14 +226,14 @@ struct
213226
| AStr s -> Format.fprintf fmt "%S" (encode s)
214227
| AStrL (sep, slist) ->
215228
Format.fprintf fmt "\"%a\""
216-
(pp_print_list ~pp_sep:(pp_sep sep) (pp_encode encode)) slist
229+
(Format.pp_print_list ~pp_sep:(pp_sep sep) (pp_encode encode)) slist
217230

218231
let pp_attrib encode fmt a =
219232
Format.fprintf fmt
220233
" %s=%a" (aname a) (pp_attrib_value encode) a
221234

222235
let pp_attribs encode =
223-
pp_print_list ~pp_sep:pp_noop (pp_attrib encode)
236+
Format.pp_print_list ~pp_sep:pp_noop (pp_attrib encode)
224237

225238
let pp_closedtag encode fmt tag attrs =
226239
if is_emptytag tag then
@@ -240,13 +253,13 @@ struct
240253

241254
and pp_elt encode fmt elt = match content elt with
242255
| Comment texte ->
243-
Format.fprintf fmt "<!--%a-->" (pp_encode encode) texte
256+
Format.fprintf fmt "<!--%s-->" (escape_comment texte)
244257

245258
| Entity e ->
246259
Format.fprintf fmt "&%s;" e
247260

248261
| PCDATA texte ->
249-
Format.pp_print_string fmt (encode texte)
262+
pp_encode encode fmt texte
250263

251264
| EncodedPCDATA texte ->
252265
Format.pp_print_string fmt texte
@@ -260,7 +273,7 @@ struct
260273
| Empty -> ()
261274

262275
and pp_elts encode =
263-
pp_print_list ~pp_sep:pp_noop (pp_elt encode)
276+
Format.pp_print_list ~pp_sep:pp_noop (pp_elt encode)
264277

265278
let pp ?(encode=encode_unsafe_char) () =
266279
pp_elt encode

test/test_html.ml

+21
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,27 @@ let escaping = "html escaping", tyxml_tests Html5.[
5353
/* ]]> */
5454
|} ;
5555

56+
"comment",
57+
tot (Xml.comment
58+
{|[if IE 8]> <html class="no-js lt-ie9" lang="en"> <![endif]|}),
59+
{|<!--[if IE 8]> <html class="no-js lt-ie9" lang="en"> <![endif]-->|} ;
60+
61+
"dodgy comment 1",
62+
tot (Xml.comment {|><script BOUM/>|}),
63+
{|<!--&gt;<script BOUM/>-->|} ;
64+
65+
"dodgy comment 2",
66+
tot (Xml.comment {|-><script BOUM/>|}),
67+
{|<!---&gt;<script BOUM/>-->|} ;
68+
69+
"dodgy comment 3",
70+
tot (Xml.comment {|foo--><script BOUM/>|}),
71+
{|<!--foo--&gt;<script BOUM/>-->|} ;
72+
73+
"dodgy comment 4",
74+
tot (Xml.comment {|foo--!><script BOUM/>|}),
75+
{|<!--foo--!&gt;<script BOUM/>-->|} ;
76+
5677
]
5778

5879

0 commit comments

Comments
 (0)