johnbintz/rack-livereload

<head> tags not apart of the DOM are being replaced

Opened this issue · 2 comments

The BodyProcessor does a naive regex search for all text looking like <head> and injects it's script tags and code in that location. There are various valid situations where the text <head> can exist in an HTML page without them actually being an element in the DOM (but instead just simply text).

Here are some examples of where the text <head> could validly exist but would be wrong to inject the live reload code into:

  • Template code inside a <script type="text/template"></script> section
  • Mentioning <head> inside a comment
  • Code examples inside <xmp> tags
  • Code examples inside CDATA sections

There may be more but these are the ones I can think of. The first 1 is the worst because it is within a <script> element and the injected code contains <script> tags as well the rule for script tags is the first closing </script> tag discovered always closes any open <script> element (you cannot nest script elements). So this ends up leaving fragments of code afterwards because the browser makes it just past the first script tag in the injected code and then starts printing the rest as text or code fragments and it leaves the template code all over the page as well because the browser doesn't realize it's still inside a <script type="text/template"> section.

Maybe looking into a solution using built in XML parsing in Ruby that will find only <head> tags that are part of the DOM and not just all text that happens to look like a <head> tag would work?

I can research this but I wanted to post the issue first to see if there were any comments.

I just ran into this issue, and had to dig in to the rack-livereload source and scratch my head for a while before figuring out that this was the cause.

I had a javascript comment on my page that described why a script had to be in the <head>. The livereload script was then dutifully injected in the middle of that script tag, thus breaking the page.

I'm sure this isn't a common problem, but it definitely tripped me up.

PR #62 is still open, but it is failing tests now. I don't think it used to. Whatever the reason it is failing tests, that may be why it is still not merged. If you wanted to take my PR and fix those feel free! We no longer need this so don't let me not fixing the tests stop progress!