162 lines
4.6 KiB
TypeScript
Raw Normal View History

2018-06-03 02:11:19 +00:00
import MutationQueue from "./MutationQueue"
2018-03-14 02:08:50 +00:00
2018-06-03 02:37:32 +00:00
// Diff provides diffing utilities to morph existing DOM elements
// into the target HTML string.
//
// Example:
// Diff.innerHTML(body, "<div>This is my new content</div>")
//
// Whatever contents will be in the body, they will be re-used and morphed
// into the new DOM defined by a simple HTML string. This is useful for
2018-06-03 02:39:18 +00:00
// Single Page Applications that use server rendered content. The server
2018-06-03 02:37:32 +00:00
// responds with the pre-rendered HTML and we can simply morph our current
// contents into the next page.
2018-04-02 05:34:16 +00:00
export default class Diff {
2017-07-19 02:18:56 +00:00
static persistentClasses = new Set<string>()
2017-07-19 07:09:55 +00:00
static persistentAttributes = new Set<string>()
2018-03-22 14:52:52 +00:00
static mutations: MutationQueue = new MutationQueue()
2017-07-05 19:06:38 +00:00
// innerHTML will diff the element with the given HTML string and apply DOM mutations.
2021-11-23 12:57:43 +00:00
static innerHTML(aRoot: HTMLElement, html: string) {
2019-11-17 09:25:14 +00:00
const container = document.createElement("main")
2018-04-07 10:34:41 +00:00
container.innerHTML = html
2017-11-10 07:41:45 +00:00
2019-04-22 09:06:50 +00:00
return new Promise((resolve, _) => {
2018-04-07 10:34:41 +00:00
Diff.childNodes(aRoot, container)
2021-11-23 12:57:43 +00:00
this.mutations.wait(() => {
resolve(null)
})
2017-11-10 07:41:45 +00:00
})
2017-07-05 19:06:38 +00:00
}
2017-07-19 03:23:06 +00:00
// root will diff the document root element with the given HTML string and apply DOM mutations.
static root(aRoot: HTMLElement, html: string) {
2019-04-22 09:06:50 +00:00
return new Promise((resolve, _) => {
2019-11-17 09:25:14 +00:00
const rootContainer = document.createElement("html")
2018-04-07 10:34:41 +00:00
rootContainer.innerHTML = html.replace("<!DOCTYPE html>", "")
2017-11-10 07:41:45 +00:00
2018-04-07 10:34:41 +00:00
Diff.childNodes(aRoot.getElementsByTagName("body")[0], rootContainer.getElementsByTagName("body")[0])
2021-11-23 12:57:43 +00:00
this.mutations.wait(() => {
resolve(null)
})
2017-11-10 07:41:45 +00:00
})
2017-07-19 03:23:06 +00:00
}
2017-07-05 19:06:38 +00:00
// childNodes diffs the child nodes of 2 given elements and applies DOM mutations.
2017-06-26 01:57:29 +00:00
static childNodes(aRoot: Node, bRoot: Node) {
2019-11-17 09:25:14 +00:00
const aChild = [...aRoot.childNodes]
const bChild = [...bRoot.childNodes]
const numNodes = Math.max(aChild.length, bChild.length)
2017-11-10 07:41:45 +00:00
2017-06-21 16:44:20 +00:00
for(let i = 0; i < numNodes; i++) {
2019-11-17 09:25:14 +00:00
const a = aChild[i]
2017-06-21 16:44:20 +00:00
2017-07-19 02:04:19 +00:00
// Remove nodes at the end of a that do not exist in b
2017-06-21 16:44:20 +00:00
if(i >= bChild.length) {
2018-03-14 02:08:50 +00:00
this.mutations.queue(() => aRoot.removeChild(a))
2017-06-21 16:44:20 +00:00
continue
}
2019-11-17 09:25:14 +00:00
const b = bChild[i]
2017-06-21 16:44:20 +00:00
2017-07-19 02:04:19 +00:00
// If a doesn't have that many nodes, simply append at the end of a
2017-06-21 16:44:20 +00:00
if(i >= aChild.length) {
2018-03-14 02:08:50 +00:00
this.mutations.queue(() => aRoot.appendChild(b))
2017-06-21 16:44:20 +00:00
continue
}
2017-07-19 02:04:19 +00:00
// If it's a completely different HTML tag or node type, replace it
2017-06-21 16:44:20 +00:00
if(a.nodeName !== b.nodeName || a.nodeType !== b.nodeType) {
2018-03-14 02:08:50 +00:00
this.mutations.queue(() => aRoot.replaceChild(b, a))
2017-06-21 16:44:20 +00:00
continue
}
2017-07-19 02:04:19 +00:00
// Text node:
// We don't need to check for b to be a text node as well because
// we eliminated different node types in the previous condition.
2017-06-26 01:57:29 +00:00
if(a.nodeType === Node.TEXT_NODE) {
2018-03-14 02:08:50 +00:00
this.mutations.queue(() => a.textContent = b.textContent)
2017-06-26 01:57:29 +00:00
continue
}
2017-07-19 02:04:19 +00:00
// HTML element:
2017-06-21 16:44:20 +00:00
if(a.nodeType === Node.ELEMENT_NODE) {
2019-11-17 09:25:14 +00:00
const elemA = a as HTMLElement
const elemB = b as HTMLElement
2017-06-26 01:57:29 +00:00
2019-11-17 09:25:14 +00:00
const removeAttributes: Attr[] = []
2017-11-10 07:41:45 +00:00
2021-11-23 12:57:43 +00:00
for(const attrib of elemA.attributes) {
2017-06-21 16:44:20 +00:00
if(attrib.specified) {
2017-07-19 07:09:55 +00:00
if(!elemB.hasAttribute(attrib.name) && !Diff.persistentAttributes.has(attrib.name)) {
2017-06-21 16:44:20 +00:00
removeAttributes.push(attrib)
}
}
}
2018-03-14 02:08:50 +00:00
this.mutations.queue(() => {
2019-11-17 09:25:14 +00:00
for(const attr of removeAttributes) {
2018-03-14 02:08:50 +00:00
elemA.removeAttributeNode(attr)
}
})
2017-06-21 16:44:20 +00:00
2021-11-23 12:57:43 +00:00
for(const attrib of elemB.attributes) {
2017-07-19 02:47:32 +00:00
if(!attrib.specified) {
continue
}
2017-10-16 09:53:47 +00:00
// If the attribute value is exactly the same, skip this attribute.
if(elemA.getAttribute(attrib.name) === attrib.value) {
continue
}
2017-07-19 02:18:56 +00:00
2017-10-16 09:53:47 +00:00
if(attrib.name === "class") {
2019-11-17 09:25:14 +00:00
const classesA = elemA.classList
const classesB = elemB.classList
const removeClasses: string[] = []
2017-07-19 02:47:32 +00:00
2019-11-17 09:25:14 +00:00
for(const className of classesA) {
2017-07-19 02:47:32 +00:00
if(!classesB.contains(className) && !Diff.persistentClasses.has(className)) {
2017-10-08 07:03:55 +00:00
removeClasses.push(className)
2017-07-19 02:18:56 +00:00
}
2017-07-19 02:47:32 +00:00
}
2017-07-19 02:18:56 +00:00
2018-03-14 02:08:50 +00:00
this.mutations.queue(() => {
2019-11-17 09:25:14 +00:00
for(const className of removeClasses) {
2018-03-14 02:08:50 +00:00
classesA.remove(className)
}
2017-10-08 07:03:55 +00:00
2019-11-17 09:25:14 +00:00
for(const className of classesB) {
2018-03-14 02:08:50 +00:00
if(!classesA.contains(className)) {
classesA.add(className)
}
2017-07-19 02:47:32 +00:00
}
2018-03-14 02:08:50 +00:00
})
2017-06-29 13:55:04 +00:00
2017-07-19 02:47:32 +00:00
continue
2017-06-21 16:44:20 +00:00
}
2017-11-10 07:41:45 +00:00
2018-03-14 02:08:50 +00:00
this.mutations.queue(() => elemA.setAttribute(attrib.name, attrib.value))
2017-06-21 16:44:20 +00:00
}
2017-06-24 00:10:04 +00:00
// Special case: Apply state of input elements
2017-06-26 01:57:29 +00:00
if(elemA !== document.activeElement && elemA instanceof HTMLInputElement && elemB instanceof HTMLInputElement) {
2018-03-14 02:08:50 +00:00
this.mutations.queue(() => {
(elemA as HTMLInputElement).value = (elemB as HTMLInputElement).value
})
2017-06-24 00:10:04 +00:00
}
2017-06-21 16:44:20 +00:00
}
2019-08-30 07:04:28 +00:00
// Never diff the contents of web components
if(a.nodeName.includes("-")) {
continue
}
// Child nodes
2017-06-21 16:44:20 +00:00
Diff.childNodes(a, b)
}
}
2019-11-17 09:44:30 +00:00
}