CssSel chaining: unexpected behavior

56 views
Skip to first unread message

Denis Nevmerzhitsky

unread,
Aug 23, 2011, 11:57:10 AM8/23/11
to lif...@googlegroups.com
Hi!

I have such html:
...
<div class="parent">
  <div class="first">1</div>
  <div class="second">2</div>
</div>
....

and snippet:
...
object Test {
  def test1 = common & additional

  def common = ".parent" #> (".first *" #> "first")
  def additional = ".second *" #> "second"

  def test2 = ".parent" #> (".first *" #> "first" & ".second *" #> "second")
}

For Test.test1 i've expected such behavior:
1. Insite element ".parent" do: replace content of ".first" with string "first"
2. and then replace content of .second with string "second"
But i get:
...
<div class="parent">
  <div class="first">first</div>
  <div class="second">2</div>
</div>
....

Test.common and Test.additional separately work as expected.
Also I've noticed that Test.test2 works fine. But i need separate Css bindings. On one page i use only Test.common's bindings, and both on another. How can i get expected result?
Thank you.

Diego Medina

unread,
Aug 23, 2011, 2:47:26 PM8/23/11
to lif...@googlegroups.com
Hi,

Here:

scala> import net.liftweb.util.Helpers._
import net.liftweb.util.Helpers._

scala> val myxml= <div class="parent">


| <div class="first">1</div>
| <div class="second">2</div>
| </div>

myxml: scala.xml.Elem =


<div class="parent">
<div class="first">1</div>
<div class="second">2</div>
</div>

scala> def common = ".parent" #> (".first *" #> "first")
common: net.liftweb.util.CssSel

scala> def additional= ".parent ^^" #> "ignore" & ".second *" #> "second"
additional: net.liftweb.util.CssSel

scala> def test1 = common & additional
test1: net.liftweb.util.CssSel

scala> test1(myxml)
res8: scala.xml.NodeSeq =
NodeSeq(<div class="parent">


<div class="first">first</div>
<div class="second">2</div>

</div>)

scala> def test1 = common andThen additional
test1: (scala.xml.NodeSeq) => scala.xml.NodeSeq

scala> test1(myxml)
res9: scala.xml.NodeSeq =
NodeSeq(<div class="parent">


<div class="first">first</div>

<div class="second">second</div>
</div>)

scala>


Regards,

Diego

> --
> You received this message because you are subscribed to the Google Groups
> "Lift" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/liftweb/-/4WrW6Dsklx0J.
> To post to this group, send email to lif...@googlegroups.com.
> To unsubscribe from this group, send email to
> liftweb+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/liftweb?hl=en.
>

--
Diego Medina
Web Developer
(305) 788-4954
di...@fmpwizard.com
http://www.fmpwizard.com

Denis Nevmerzhitsky

unread,
Aug 24, 2011, 4:39:31 AM8/24/11
to lif...@googlegroups.com
Hi

Thank you for answer.
Ah, of course andThen, method of Function.
Hm, so why chaining by & doesn't work? It has another meaning?
I've noticed that &-chaining works fine if:
object Test {
  def test1 = common & additional

  def common = ".parent" #> (".first *" #> "first")
  def additional = ".parent" #> (".second *" #> "second")
}
It would be good to know correct behavior of &-chaining. Thank you.

David Pollak

unread,
Aug 24, 2011, 6:43:10 AM8/24/11
to lif...@googlegroups.com

The defined behavior of CSS Selector Chaining is that if you replace a node or replace all the children of a node (e.g., ".parent" #> something or ".parent *" #> something), the right hand side of the replacement defines the entire scope of the replacement.  Put another way, the children of the RHS of the replacement are not run through the entire set of rules chained by the &.  This is done for two reasons:
  • Efficiency.  I presume that if you are replacing all the subnodes, you know what you're doing and applying a pile of rules to something that the developer has localized control over doesn't make sense.
  • Not being confusing another way.  ".node_with_error" #> ("div [style+]" #> "error") & "div [style]" #> "no_error".  In this case, you're saying "take the node with the error and add an error style and all the other nodes should have a no_error style."  But you'll get "no_error" applied to even the <div>s that are in the function you've already modified.
Hope this helps.
 

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To view this discussion on the web visit https://groups.google.com/d/msg/liftweb/-/OVGK2YcJ5uoJ.

To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.



--
Lift, the simply functional web framework http://liftweb.net

Denis Nevmerzhitsky

unread,
Aug 24, 2011, 7:22:35 AM8/24/11
to lif...@googlegroups.com
Hi!

Now i see. It makes sense. Thank you for your answer!

David Pollak

unread,
Aug 24, 2011, 10:22:11 AM8/24/11
to lif...@googlegroups.com
On Wed, Aug 24, 2011 at 7:22 AM, Denis Nevmerzhitsky <kastig...@gmail.com> wrote:
Hi!

Now i see. It makes sense. Thank you for your answer!

Sure thing.  And thanks for asking a good question and following through to make this an excellent thread!
 

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To view this discussion on the web visit https://groups.google.com/d/msg/liftweb/-/muER6ZQ4aj8J.

To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.
Reply all
Reply to author
Forward
0 new messages