private static @NotNull Document getTopLevelDocument(@NotNull Document document) {
    return document instanceof DocumentWindow ? ((DocumentWindow)document).getDelegate() : document;
  }
private static @NotNull Document getTopLevelDocument(@NotNull Document document)