Skip to content

Commit

Permalink
[feature] stdlib: add functions to manipulate xhtml attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Heuzard committed Jun 28, 2011
1 parent 97e3f73 commit 7f99541
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions stdlib/core/xhtml/xhtml.opa
Expand Up @@ -667,6 +667,11 @@ Xhtml =
if i.name == attr then some(i.value) else none
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 =
Expand Down Expand Up @@ -1125,6 +1130,54 @@ Xhtml =
end
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
Expand Down

0 comments on commit 7f99541

Please sign in to comment.