Skip to content

Comments

Make live reload script injection more reliable.#746

Open
DominoPivot wants to merge 1 commit intomicrosoft:mainfrom
DominoPivot:fix-livereload-inject
Open

Make live reload script injection more reliable.#746
DominoPivot wants to merge 1 commit intomicrosoft:mainfrom
DominoPivot:fix-livereload-inject

Conversation

@DominoPivot
Copy link

This PR replaces the utility function which injects a live reload script in HTML files, addressing issues mentioned in #711 such as:

  1. The entire document is searched for the <head > before falling back to searching for the <body >, and again before falling back to searching for <!DOCTYPE >. This has a small performance cost which could become worse as we make the search itself more robust.
  2. If <head> is omitted, the <header> tag can be mistaken for it, possibly affecting page layout. This can be solved by changing the regular expression so that the tag name must be followed by > or whitespace.
  3. The text inside attribute values, comments, template elements, and raw text elements such as <script> and <title> can be mistaken for the <head> and <body> tags. This can cause the live reload to stop working, or break parsing for the rest of the page.

The solution I propose is to find the start of the head by scanning from the start of the file and explicitly consuming the DOCTYPE, html start tag and head start tag, along with any whitespace and comments before and between them. The script is injected before any other element or body text.

To follow the spirit of the existing code, I fall back to consuming the body start tag if the head is missing, but in practice all HTML files should have a title in which case the script will be injected before it.

This doesn't solve issues where the > character appears in a quoted attribute value on the html or head start tag. Do tell if you think this case should be handled properly.


⚠ I was unable to run and therefore write my own tests for this, I get an error when runTests tries to download VS code. Can someone familiar with this repository help?
⚠ It would probably be a good idea to add a .gitattributes file to force CRLF line endings, that tripped me up since my global git config does the opposite.

@DominoPivot
Copy link
Author

@microsoft-github-policy-service agree

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

Successfully merging this pull request may close these issues.

1 participant