Skip to content

Commit

Permalink
Merge in remaining header elements (eg: link icons, manifest, meta an…
Browse files Browse the repository at this point in the history
…d title) (#752)

* Update page_renderer.ts

* Spacing change to re-trigger CI

* Lint updates
  • Loading branch information
scuml authored Nov 27, 2022
1 parent 00b287a commit 0f3632a
Showing 1 changed file with 39 additions and 2 deletions.
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

0 comments on commit 0f3632a

Please sign in to comment.