Permalink
Browse files

[feature] stdlib: add functions to manipulate xhtml attribute

  • Loading branch information...
1 parent 97e3f73 commit 7f99541e9730d4533357a29d6b959df899dafb07 Hugo Heuzard committed Jun 28, 2011
Showing with 53 additions and 0 deletions.
  1. +53 −0 stdlib/core/xhtml/xhtml.opa
@@ -668,6 +668,11 @@ Xhtml =
List.find_map(oracle, list)
@private
+ remove_attr(attr, list: list(string_assoc(string))): list(string_assoc(string)) =
+ oracle(i:Xml.attribute) = i.name == attr
+ List.remove_p(oracle, list)
+
+ @private
iter_tell_me_if_i_am_last(f: 'a,bool -> void, l: list('a)):void =
rec aux =
| [hd ] -> f(hd, true)
@@ -1126,6 +1131,54 @@ Xhtml =
aux(id,x)
/**
+ * Add an attribute to an xhtml node if not already defined
+ */
+ add_attribute(name: string, value: string, x:xhtml):xhtml =
+ // aux(id, x) with
+ rec aux(x)=
+ match x : xhtml
+ {fragment=[x]} -> aux(x)
+ {~args namespace=_ tag=_ content=_ specific_attributes=_} as x->
+ args = if exists_attr(name,args) then args
+ else [{~name namespace="" ~value}|args]
+ @opensums({x with ~args})
+ _ -> x
+ end
+ aux(x)
+
+ /**
+ * Set an attribute to an xhtml node. Replace if already_exists
+ */
+ set_attribute(name: string, value: string, x:xhtml):xhtml =
+ // aux(id, x) with
+ rec aux(x)=
+ match x : xhtml
+ {fragment=[x]} -> aux(x)
+ {~args namespace=_ tag=_ content=_ specific_attributes=_} as x->
+ args =
+ l = remove_attr(name,args)
+ [{~name namespace="" ~value}|l]
+ @opensums({x with ~args})
+ _ -> x
+ end
+ aux(x)
+
+ /**
+ * Remove an attribute from an xhtml node.
+ */
+ remove_attribute(name: string, x:xhtml):xhtml =
+ // aux(id, x) with
+ rec aux(x)=
+ match x : xhtml
+ {fragment=[x]} -> aux(x)
+ {~args namespace=_ tag=_ content=_ specific_attributes=_} as x->
+ args = remove_attr(name,args)
+ @opensums({x with ~args})
+ _ -> x
+ end
+ aux(x)
+
+ /**
* Add style to the xhtml (added to pre-exiting style)
* When the future position of the style is not clear (several possible node), it encapsulated everything in a div
*/

0 comments on commit 7f99541

Please sign in to comment.