Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent empty attribute behaviour #136

Open
NekoiNemo opened this issue Mar 21, 2019 · 4 comments
Open

Inconsistent empty attribute behaviour #136

NekoiNemo opened this issue Mar 21, 2019 · 4 comments

Comments

@NekoiNemo
Copy link

If node attribute is set by binding to empty string on construction - it is omitted from resulting DOM, however if it is set to empty string after DOM is constructed - it results in value-less attribute.

    @dom def header: Binding[Node] = {
    val selected = Var(-1)

    def select(index: Int): Event => Unit = { _ => selected.value = index}

    <ul>
      <li class={ isSelected(0, selected).bind }><a onclick={ select(0) }>Link 1</a></li>
      <li class={ isSelected(1, selected).bind }><a onclick={ select(1) }>Link 2</a></li>
      <li class={ isSelected(2, selected).bind }><a onclick={ select(2) }>Link 3</a></li>
    </ul>
  }

  def isSelected(index: Int, value: Binding[Int]): Binding[String] = Binding {
    if(value.bind == index) "active"
    else ""
  }

Example above will be initially rendered as

<ul>
  <li>...</li>
  <li>...</li>
  <li>...</li>
</ul>

but after pressing buttons it will came to look like this:

<ul>
  <li class="active">...</li>
  <li class>...</li>
  <li class>...</li>
</ul>
@Atry
Copy link
Collaborator

Atry commented Mar 21, 2019

What's your expected behavior?

@NekoiNemo
Copy link
Author

It seems that value-less attribute is how JS reacts to field being set to empty string, so expected behaviour would be above fragment being initially rendered as

<ul>
  <li class>...</li>
  <li class>...</li>
  <li class>...</li>
</ul>

instead of

<ul>
  <li>...</li>
  <li>...</li>
  <li>...</li>
</ul>

@Atry
Copy link
Collaborator

Atry commented Mar 28, 2019

Interesting because we did not give any special treatment between empty and non-empty string attributes.

https://scalafiddle.io/sf/YVOjunl/0

@Atry
Copy link
Collaborator

Atry commented Mar 28, 2019

Fortunately this inconsistency is not found on boolean attributes:
https://scalafiddle.io/sf/YVOjunl/2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants