From 574ac9e8854ff8b471183ceafc07659b2e16b835 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Wed, 19 Jul 2017 04:18:56 +0200 Subject: [PATCH] Improved diffs --- scripts/AnimeNotifier.ts | 4 ++++ scripts/Diff.ts | 19 ++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/scripts/AnimeNotifier.ts b/scripts/AnimeNotifier.ts index 759772c5..fbdb4655 100644 --- a/scripts/AnimeNotifier.ts +++ b/scripts/AnimeNotifier.ts @@ -30,6 +30,10 @@ export class AnimeNotifier { this.imageNotFound = new MutationQueue(elem => elem.classList.add("image-not-found")) this.unmount = new MutationQueue(elem => elem.classList.remove("mounted")) + // These classes will never be removed on DOM diffs + Diff.persistentClasses.add("mounted") + Diff.persistentClasses.add("image-found") + if("IntersectionObserver" in window) { // Enable lazy load this.visibilityObserver = new IntersectionObserver( diff --git a/scripts/Diff.ts b/scripts/Diff.ts index 93c162aa..974a0450 100644 --- a/scripts/Diff.ts +++ b/scripts/Diff.ts @@ -1,4 +1,6 @@ export class Diff { + static persistentClasses = new Set() + // Reuse container for diffs to avoid memory allocation static container: HTMLElement @@ -82,7 +84,22 @@ export class Diff { if(attrib.specified) { // Skip mountables - if(attrib.name == "class" && (elemA.classList.contains("mounted") || elemA.classList.contains("image-found"))) { + if(attrib.name == "class") { + let classesA = elemA.classList + let classesB = elemB.classList + + for(let className of classesA) { + if(!classesB.contains(className) && !Diff.persistentClasses.has(className)) { + classesA.remove(className) + } + } + + for(let className of classesB) { + if(!classesA.contains(className)) { + classesA.add(className) + } + } + continue }