I was trying to update an anchor tag with an href and the link text
like this:
val xml = <a class="link" href="#">link text</a>
val func =
".link [href]" #> "
http://example.org" &
".link *" #> "example"
println(func.apply(xml))
This doesn't work as only the first selector applies:
// output is <a href="
http://example.org" class="link">link text</
a>
I can see why from the constructor (and fundamental design) of
SelectorMap - it builds a map keyed on the id (and not the subNodes),
and in fact specifically throws away selectors that bind to the same
id.
This is fair enough and a reasonable decision, but should be mentioned
on the wiki page: both myself and two of my colleages independently
attempted to write selectors like this yesterday and got confused as
to why it didn't work. I'm a bit of a lift noob: is it ok for me to
just add this information to the wiki page?
It would have helped to get a warning or error when this happens (just
a log would be fine), and perhaps the catchall "case _ =>" in the
SelectorMap constructor would be a good place to do this?
It looks like the right way to do this is bind a entire replacement
node:
val xml = <a class="link" href="#">link text</a>
val func = ".link" #> <a href={"
http://example.org"}>{"example"}</
a>
println(func.apply(xml))
// output is <a class="link" href="
http://example.org">example</a>
I suspect the main reason for us attempting to write it the first way
is that we haven't quite groked templating that is far more powerful
than the traditional dumb string replacement. The magic of attribute
slurping means we haven't taken any power away from the template
author by taking the second approach, because attributes written in
the template carry through.
g