export default function collectMetaElements () {
const selector = 'head meta'
const realMatchesFn = window.Element.prototype.matches
const metas: Element[] = []
const _findAllElements = (nodes: NodeListOf<Element>) => {
for (let i = 0, el; el = nodes[i]; ++i) {
if (!selector || realMatchesFn.call(el, selector)) {
metas.push(el)
}
if (el.shadowRoot) {
_findAllElements(el.shadowRoot.querySelectorAll('*'))
}
}
}
_findAllElements(document.querySelectorAll('*'))
return metas.map(meta => {
const getAttribute = (name: string) => {
const attr = meta.attributes.getNamedItem(name)
if (!attr) {
return
}
return attr.value
}
return {
name: meta.name.toLowerCase(),
content: meta.content,
property: getAttribute('property'),
httpEquiv: meta.httpEquiv ? meta.httpEquiv.toLowerCase() : undefined,
charset: getAttribute('charset'),
}
})
}