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

feat: enable getMetadata to query any document #17

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions src/dom-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export async function loadScript(src, attrs) {
const script = document.createElement('script');
script.src = src;
if (attrs) {
// eslint-disable-next-line no-restricted-syntax, guard-for-in
// eslint-disable-next-line no-restricted-syntax, guard-for-in
for (const attr in attrs) {
script.setAttribute(attr, attrs[attr]);
}
Expand All @@ -59,11 +59,12 @@ export async function loadScript(src, attrs) {
/**
* Retrieves the content of metadata tags.
* @param {string} name The metadata name (or property)
* @param {Document} doc Document object to query for metadata. Defaults to the window's document
* @returns {string} The metadata value(s)
*/
export function getMetadata(name) {
export function getMetadata(name, doc = document) {
const attr = name && name.includes(':') ? 'property' : 'name';
const meta = [...document.head.querySelectorAll(`meta[${attr}="${name}"]`)].map((m) => m.content).join(', ');
const meta = [...doc.head.querySelectorAll(`meta[${attr}="${name}"]`)].map((m) => m.content).join(', ');
return meta || '';
}

Expand Down
26 changes: 25 additions & 1 deletion test/dom-utils/getMetadata.test.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,31 @@
it('get article:tag', () => {
expect(getMetadata('article:tag')).to.equal('tag 1, tag 2, tag 3');
});

// Test a custom document
const testDoc = document.implementation.createHTMLDocument();
const titleMeta = testDoc.createElement('meta');
titleMeta.setAttribute('property', 'og:title');
titleMeta.setAttribute('content', 'Camping in Western Australia');
testDoc.head.appendChild(titleMeta);

const descriptionMeta = testDoc.createElement('meta');
descriptionMeta.setAttribute('property', 'og:description');
descriptionMeta.setAttribute('content', 'The Australian West coast is a camper’s heaven.');
testDoc.head.appendChild(descriptionMeta);

it('get og:title from custom document', () => {
expect(getMetadata('og:title', testDoc)).to.equal('Camping in Western Australia');
});

it('get og:description from custom document', () => {
expect(getMetadata('og:description', testDoc)).to.equal('The Australian West coast is a camper’s heaven.');
});

it('query metadata which does not exist from custom document', () => {
expect(getMetadata('og:not-set-metadata', testDoc)).to.equal('');
});
});
</script>
</body>
</html>
</html>