Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge in remaining header elements (eg: link icons, manifest, meta and title) #752

Merged
merged 3 commits into from
Nov 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/core/drive/page_renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ export class PageRenderer extends Renderer<HTMLBodyElement, PageSnapshot> {
}

async mergeHead() {
const mergedHeadElements = this.mergeProvisionalElements()
const newStylesheetElements = this.copyNewHeadStylesheetElements()
this.copyNewHeadScriptElements()
this.removeCurrentHeadProvisionalElements()
this.copyNewHeadProvisionalElements()
await mergedHeadElements
await newStylesheetElements
}

Expand Down Expand Up @@ -96,6 +96,43 @@ export class PageRenderer extends Renderer<HTMLBodyElement, PageSnapshot> {
}
}

async mergeProvisionalElements() {
const newHeadElements = [...this.newHeadProvisionalElements]

for (const element of this.currentHeadProvisionalElements) {
if (!this.isCurrentElementInElementList(element, newHeadElements)) {
document.head.removeChild(element)
}
}

for (const element of newHeadElements) {
document.head.appendChild(element)
}
}

isCurrentElementInElementList(element: Element, elementList: Element[]) {
for (const [index, newElement] of elementList.entries()) {
// if title element...
if (element.tagName == "TITLE") {
if (newElement.tagName != "TITLE") {
continue
}
if (element.innerHTML == newElement.innerHTML) {
elementList.splice(index, 1)
return true
}
}

// if any other element...
if (newElement.isEqualNode(element)) {
elementList.splice(index, 1)
return true
}
}

return false
}

removeCurrentHeadProvisionalElements() {
for (const element of this.currentHeadProvisionalElements) {
document.head.removeChild(element)
Expand Down