haacked / aspnet-client-validation

A client validation library for ASP.NET MVC that does not require jQuery

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`bootstrap` should run immediately if `document.readyState === "interactive"`

analogrelay opened this issue · comments

The bootstrap method has some logic to defer loading until readyState === "complete":

bootstrap() {
this.addMvcProviders();
let document = window.document;
if(document.readyState === 'complete') {
this.scan();
}
else {
window.document.addEventListener('DOMContentLoaded', event => {
this.scan();
});
}
}

However, this can cause problems if bootstrap is called from within the DOMContentLoaded event. There are three readyState values:

  • loading - The document is loading
  • interactive - The document has finished loading and the document has been parsed but sub-resources such as scripts, images, stylesheets and frames are still loading.
  • complete - The document and all sub-resources have finished loading. The state indicates that the load event is about to fire.

When DOMContentLoaded fires, the current document.readyState is interactive (at least in Chromium-based Edge). Calling bootstrap here causes it to no-op because readyState !== "complete" AND DOMContentLoaded has already fired, so the handler it attaches will never be raised.

The simplest fix is probably to allow document.readyState === "interactive" to trigger an immediate scan. Also, it might be useful to make scan public. If it were public, I could just call that in my own DOMContentLoaded handler, since there's no reason for the defensive logic in bootstrap.

I would accept a PR with both the proposed changes. 😊

Yeah, but it's Friday and I am le tired 🤣

Fair. How about you review my PR then when you're well rested. 😄 #22

Deal. Although, we could probably rewrite the scanning to use MutationObserver so it just works automatically 🤔.

You have my attention.