Permalink
Browse files

[fix] xhtml.opa,parser: use pedestrian verbatim approach with namespace

CHANGELOG [fix] fix anonymous namespace in xhtml
CHANGELOG [fix] fix namespace name conservation in xhtml
  • Loading branch information...
1 parent 716e24d commit c9648accd2bf02f639289d25f5581a35ff492560 @OpaOnWindowsNow OpaOnWindowsNow committed Feb 8, 2012
Showing with 29 additions and 55 deletions.
  1. +24 −12 opalang/classic_syntax/parser_utils.ml
  2. +2 −2 stdlib/core/web/resource/resource.opa
  3. +3 −41 stdlib/core/xhtml/xhtml.opa
@@ -1,5 +1,5 @@
(*
- Copyright © 2011 MLstate
+ Copyright © 2011, 2012 MLstate
This file is part of OPA.
@@ -1166,11 +1166,21 @@ let sassoc ((ns:(_,_) expr),name) (value:(_,_) expr) : (_,_) expr =
let create_element (ns,tag) args children =
(* Adapt tag and attributes *)
- let xmlns s annot = if s="" then string s annot else ident ("xmlns:"^s) annot in
- let tag_ns : (string, 'a) SurfaceAst.expr =
- let (name, annot) = ns in xmlns name annot
+ let bind_xmlns_check = false in
+ let tag_ns =
+ (* Use namespace.
+ Use [<name:div/>] is converted into a binding <`xmlns:name`:div/>. *)
+ let (prefix,annot) = ns in
+ if bind_xmlns_check && prefix<>"" then ident ("xmlns:"^prefix) annot
+ else string prefix annot
in (* Note: [name] can be empty, it's ok *)
- let other_attributes = List.map (fun (prefix, name, value) -> sassoc (xmlns prefix (nlabel tag), (name, nlabel tag)) value) args.args in
+
+ let other_attributes = List.map (fun (prefix, name, value) -> sassoc (string prefix (nlabel tag), (name, nlabel tag)) value) args.args in
+ let xmlns_attributes = List.map (fun (prefix, value) ->
+ let name = (if prefix = "" then "xmlns" else "xmlns:"^prefix),nlabel tag in
+ let prefix = string "" (nlabel tag) in
+ sassoc (prefix,name) value
+ ) args.xmlns_declaration in
(* Create element *)
let record =
if xhtml_mode () && not (is_empty_args args) then (
@@ -1195,26 +1205,28 @@ let create_element (ns,tag) args children =
] in
record [("namespace",tag_ns);
("tag",unc2 string tag);
- ("args", list_expr_of_expr_list other_attributes (label tag));
+ ("args", list_expr_of_expr_list (xmlns_attributes@other_attributes) (label tag));
("specific_attributes", some specific_attributes);
("content",list_expr_of_expr_list children (label tag));
]
) else (
record [("namespace",tag_ns);
("tag",unc2 string tag);
- ("args", list_expr_of_expr_list other_attributes (label tag));
+ ("args", list_expr_of_expr_list (xmlns_attributes@other_attributes) (label tag));
("content",list_expr_of_expr_list children (label tag));
("specific_attributes", none (label tag));
]
) in
(* Declare namespaces.
- Declaration [xmlns = "foo"] is converted into a binding <<`xmlns:` = foo>. *)
- let record = List.fold_left
- (fun record (prefix, uri) -> letin ("xmlns:"^prefix) uri record) record
- args.xmlns_declaration in
-
+ Declaration [xmlns:name = "foo"] is converted into a binding <<`xmlns:name` = name>. *)
+ let record = if bind_xmlns_check then
+ List.fold_left
+ (fun record (prefix, uri) -> letin ("xmlns:"^prefix) (string prefix (nlabel uri)) record) record
+ args.xmlns_declaration
+ else record
+ in
coerce_name_expr record (xml_typename ())
let nstag_to_string ns tag =
@@ -1,5 +1,5 @@
/*
- Copyright © 2011 MLstate
+ Copyright © 2011, 2012 MLstate
This file is part of OPA.
@@ -447,7 +447,7 @@ static_styled_page(title:string, styles:list(string), body: xmlns): resource =
</body>
</html>
)
- string_content = Xmlns.serialize_to_string_with_nsmap([], "http://www.w3.org/1999/xhtml", xml_content)
+ string_content = Xmlns.serialize_to_string(xml_content)
raw_response(string_content, "text/html", {success})
static_page(title:string, body: xmlns): resource =
@@ -1,5 +1,5 @@
/*
- Copyright © 2011 MLstate
+ Copyright © 2011, 2012 MLstate
This file is part of OPA.
@@ -66,7 +66,7 @@ type xml('attributes,'extensions) =
{ text : string } /**Text meant to be escaped before any insertion*/
/ { content_unsafe: string } /**Text meant to be inserted without any check or escaping. Absolutely unsafe, of course.*/
/ { fragment : list(xml('attributes,'extensions)) }
-/ { namespace : string /** A unique URI characterizing the namespace */
+/ { namespace : string /** The namespace name */
tag : string
args : list(Xml.attribute)
content : list(xml('attributes,'extensions))
@@ -163,7 +163,6 @@ Xmlns =
* Convert a xmlns structure into a string
*/
to_string : xmlns -> string = serialize_to_string
- to_string_with_nsmap : list((string,string)), string, xmlns -> string = serialize_to_string_with_nsmap
/**
* Convert a xhtml structure into a xmlns
@@ -387,11 +386,6 @@ Xmlns =
do @assert(js_code == "")
html_code
- serialize_to_string_with_nsmap(ns_map,default_ns,xmlns: xmlns): string =
- xhtml = to_xhtml(xmlns)
- ~{js_code html_code} = Xhtml.prepare_for_export_with_map(ns_map,default_ns,xhtml,false)
- do @assert(js_code == "")
- html_code
}}
verbatim_expr(_)=""
@@ -714,8 +708,7 @@ Xhtml =
)
@private Buf = Buffer2_private
- prepare_for_export(default_ns_uri,xhtml,style_inline) = prepare_for_export_with_map([],default_ns_uri,xhtml,style_inline)
- prepare_for_export_with_map(namespace_map:list((string,string)), default_ns_uri:string, xhtml: xhtml, style_inline : bool): {js_code: string; html_code:string} =
+ prepare_for_export(_default_ns_uri, xhtml: xhtml, style_inline : bool): {js_code: string; html_code:string} =
(
html_buffer = Buf.create(1024)//A buffer for storing the HTML source code
js_buffer = Buf.create(1024)//A buffer for storing the JS source code -- at the last step, it is inserted in [html_buffer]
@@ -724,32 +717,6 @@ Xhtml =
| { ~value } -> value
| { ~expr } -> FunAction.serialize(expr)
Buf.add(js_buffer,code)
- ns_counter = Mutable.make(0)
- ns_bindings = Mutable.make([("","")|namespace_map])
- ns_buffer = Buf.create(1024)
- do if default_ns_uri != "" then
- do Buf.add(ns_buffer, " xmlns=\"")
- do Buf.add(ns_buffer, default_ns_uri)
- Buf.add(ns_buffer, "\"")
- bind_namespace(uri,name) =
- do Buf.add(ns_buffer, " xmlns:")
- do Buf.add(ns_buffer, name)
- do Buf.add(ns_buffer, "=\"")
- do Buf.add(ns_buffer, uri)
- Buf.add(ns_buffer, "\"")
- do List.iter((x,y) -> bind_namespace(x,y),namespace_map)
- get_ns_prefix(ns_uri) = (
- if ns_uri == default_ns_uri then "" else
- match find_assoc(ns_uri,ns_bindings.get()) with
- | ~{some} -> some
- | _ ->
- counter = ns_counter.get()
- do ns_counter.set(counter + 1)
- prefix = "ns{counter}"
- do ns_bindings.set([(ns_uri, prefix)|ns_bindings.get()])
- do bind_namespace(ns_uri,prefix)
- prefix
- )
/**
* @param depth The current depth in the tree. Used both for pretty-printing and to insert scripts at the correct place
@@ -773,7 +740,6 @@ Xhtml =
| ~{ namespace tag args content specific_attributes } ->
tag =
- namespace = get_ns_prefix(namespace)
if String.is_empty(namespace) then tag else
namespace ^ ":" ^ tag
@@ -782,7 +748,6 @@ Xhtml =
do Buf.add(html_buffer,tag)
print_arg(~{name namespace=tagns value}) =
- tagns = get_ns_prefix(tagns)
do Buf.add(html_buffer," ")
do if String.is_empty(tagns) then Buf.add(html_buffer,name)
else
@@ -932,8 +897,6 @@ Xhtml =
| "abbr" | "br" | "col" | "img" | "input" | "link" | "meta" | "param" | "hr" | "area" | "embed" -> true
| _ -> false)
then
- do if depth == 0 then
- Buf.add(html_buffer,Buf.contents(ns_buffer))
Buf.add(html_buffer,"/>")
else
do if depth == 0 then
@@ -947,7 +910,6 @@ Xhtml =
do Buf.reset(html_buffer,1024)
// putting back everything into the buffer
do Buf.add(html_buffer,start)
- do Buf.add(html_buffer,Buf.contents(ns_buffer))
do Buf.add(html_buffer,">")
Buf.add(html_buffer,content)
else

0 comments on commit c9648ac

Please sign in to comment.