terezka / elm-charts

Create SVG charts in Elm.
https://www.elm-charts.org
BSD 3-Clause "New" or "Revised" License
711 stars 67 forks source link

New hint api #63

Closed terezka closed 7 years ago

bamorim commented 7 years ago

I know this is for the new api, but I want to mention a bug I fixed here with safari. So, the way the debois/elm-dom implements the approximation for boundingClientRect uses some offset values from the dom node. In Chrome/Firefox everything works fine because those values are not present on the rect/path/g/svg nodes causing the decoder to fail then making the lookup go to its parent.

However, safari, instead of not defining those values, uses a 0 valued property, messing with all hint calculations.

The way I solved it was "skipping" everything that lies on SVG namespace. Like this:

plotPosition : Json.Decoder DOM.Rectangle
plotPosition =
    Json.oneOf
        [ boundingPositionIfNotSvg
        , Json.lazy (\_ -> DOM.parentElement plotPosition)
        ]

boundingPositionIfNotSvg : Json.Decoder DOM.Rectangle
boundingPositionIfNotSvg =
    (Json.field "namespaceURI" Json.string)
        |> Json.andThen
            (\namespace ->
                if (String.toLower namespace) /= "http://www.w3.org/2000/svg" then
                    DOM.boundingClientRect
                else
                    Json.fail "inside svg yet"
            )

I implemented this in our fork at https://github.com/Bractlet/elm-plot/tree/1.0.0

What do you guys think?