Fix doc-out-of-sync handling for non-live HTML documents and generalize the logic so it will work for any non-live doc
Add unit test for 5317 (JS portion)
Add status checks to unit tests for cases where the live doc shouldn't be marked dirty