elm / virtual-dom

The foundation of HTML and SVG in Elm.
https://package.elm-lang.org/packages/elm/virtual-dom/latest
BSD 3-Clause "New" or "Revised" License
209 stars 80 forks source link

fullscreen leading to childNodes crash #128

Closed emtenet closed 6 years ago

emtenet commented 6 years ago

I have encountered the following exception TypeError: Cannot read property 'childNodes' of undefined at _VirtualDom_addDomNodesHelp whilst migrating a project to elm 0.19.

Whilst preparing a SSCCE encountered a related exception TypeError: Cannot set property 'disabled' of undefined at _VirtualDom_applyFacts.

So I have two SSCCEs:

With both examples, click the "Click" button twice. The second click causes the exception.

emtenet commented 6 years ago

The SSCCE where prepared with elm 0.19 compiled on NetBSD from sources on the 17th of May. The SSCCE where tested in Chrome 66 on Windows 10

evancz commented 6 years ago

The issue is very surprising! Here is some HTML that will log 0 on the console:

<!DOCTYPE HTML>
<head>
<script>
  window.addEventListener("load", function() {
    console.log(document.body.childNodes.length);
  });
</script>
</head><body></body></html>

And here is some HTML that will log 1 on the console:

<!DOCTYPE HTML>
<head>
<script>
  window.addEventListener("load", function() {
    console.log(document.body.childNodes.length);
  });
</script>
</head><body></body>
</html>

The only difference is that there is a newline after </body>.

It seems that everything after the </body> and before </html> is added to the body. In this case a text node gets added. Interestingly, this will be after any synchronous code, so I think you can only know exactly what happened after onload. Waiting until onload would result in a flash of blank though, so tricks are needed here.

I have some ideas of how to detect this, but it is pretty tricky!