when adding custom head html, ensure there are no duplicate meta tags (#7510)

* fix: remove old element to prevent duplication

remove old element before append new meta tag to prevent duplication

* fix: unexpected

* Update

* format

* unexpected

* unexpected

* add changeset

* improve

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
paragonnov 2024-02-24 02:04:04 +09:00 committed by GitHub
parent daf40b5ce3
commit 08c2d491ec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 39 additions and 7 deletions

View File

@ -0,0 +1,6 @@
---
"@gradio/app": patch
"gradio": patch
---
fix:when adding custom head html, ensure there are no duplicate meta tags

View File

@ -19,5 +19,8 @@
"eslint.options": {
"overrideConfigFile": "./.config/eslint.config.js"
},
"typescript.tsdk": "node_modules/typescript/lib"
"typescript.tsdk": "node_modules/typescript/lib",
"i18n-ally.localesPaths": [
"js/app/src/lang"
]
}

View File

@ -166,12 +166,35 @@
if (parsed_head_html) {
for (let head_element of parsed_head_html) {
let newElement = document.createElement(head_element.tagName);
Array.from(head_element.attributes).forEach((attr) => {
newElement.setAttribute(attr.name, attr.value);
});
newElement.textContent = head_element.textContent;
document.head.appendChild(newElement);
for (let head_element of parsed_head_html) {
let newElement = document.createElement(head_element.tagName);
Array.from(head_element.attributes).forEach((attr) => {
newElement.setAttribute(attr.name, attr.value);
});
newElement.textContent = head_element.textContent;
if (
newElement.tagName == "META" &&
newElement.getAttribute("property")
) {
const domMetaList = Array.from(
document.head.getElementsByTagName("meta") ?? []
);
const matched = domMetaList.find((el) => {
return (
el.getAttribute("property") ==
newElement.getAttribute("property") &&
!el.isEqualNode(newElement)
);
});
if (matched) {
document.head.replaceChild(newElement, matched);
continue;
}
}
document.head.appendChild(newElement);
}
}
}
}