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

The href attribute cannot be removed once added #228

Open
lydell opened this issue Mar 10, 2021 · 0 comments
Open

The href attribute cannot be removed once added #228

lydell opened this issue Mar 10, 2021 · 0 comments

Comments

@lydell
Copy link

lydell commented Mar 10, 2021

SSCCE (Ellie):

module Main exposing (main)

import Browser
import Html exposing (a, div, h1, text)
import Html.Attributes exposing (href)
import Html.Events exposing (onClick)


main : Program () Bool ()
main =
    Browser.sandbox
        { init = False
        , view = view
        , update = always not
        }


view : Bool -> Html.Html ()
view model =
    div []
        [ a
            (if model then
                [ href "#home", onClick () ]

             else
                []
            )
            [ text "Home" ]
        , text " | "
        , a
            (if model then
                []

             else
                [ href "#about", onClick () ]
            )
            [ text "About" ]
        , h1 []
            [ if model then
                text "About"

              else
                text "Home"
            ]
        ]

The HTML spec suggests using an <a> element without href for the current page in a navigation:

https://html.spec.whatwg.org/multipage/text-level-semantics.html#the-a-element

If a site uses a consistent navigation toolbar on every page, then the link that would normally link to the page itself could be marked up using an a element:

<nav>
 <ul>
  <li> <a href="/">Home</a> </li>
  <li> <a href="/news">News</a> </li>
  <li> <a>Examples</a> </li>
  <li> <a href="/legal">Legal</a> </li>
 </ul>
</nav>

The above SSCCE implements that:

image

Notice how Home is in black, while About is blue and underlined – this is the default browser styling of <a> elements with and without href.

But once you click About, you get this:

image

Now both are blue and underlined (but About was supposed to be black)! This is because <a href="#about">About</a> was changed into <a href="">About</a> instead of <a>About</a>.

That happens because the virtual DOM is removing the href by setting it to "". Unfortunately, that doesn’t remove the href attribute.

https://github.com/elm/virtual-dom/blob/5a5bcf48720bc7d53461b3cd42a9f19f119c5503/src/Elm/Kernel/VirtualDom.js#L895

A solution could be to use attribute (.setAttribute and .removeAttirbute) instead of property in this case.

Other string properties than href could have the same problem – I haven’t checked. It could be that everything using stringProperty actually should be using attribute.

Previous issue about this that is closed for unknown reasons: #142

Probably the same issue in the virtual-dom repo: elm/virtual-dom#169

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

1 participant