Source code
Revision control
Copy as Markdown
Other Tools
Test Info:
/* Any copyright is dedicated to the Public Domain.
"use strict";
const { findCandidates } = ChromeUtils.importESModule(
"moz-src:///browser/components/tabnotes/CanonicalURL.sys.mjs"
);
/**
* @param {string[]} scripts
* @returns {Document}
*/
function getDocument(scripts) {
const scriptTags = scripts
.map(content => `<script type="application/ld+json">${content}</script>`)
.join("\n");
const html = `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
${scriptTags}
</body>
</html>
`;
return Document.parseHTMLUnsafe(html);
}
add_task(async function test_json_ld_missing() {
const doc = getDocument([]);
const candidates = findCandidates(doc);
Assert.equal(
candidates.jsonLd,
undefined,
`JSON-LD data should not be found`
);
});
add_task(async function test_json_ld_basic() {
const doc = getDocument([
JSON.stringify({
"@type": "Thing",
}),
]);
const candidates = findCandidates(doc);
Assert.equal(
candidates.jsonLd,
`JSON-LD data should be found`
);
});
add_task(async function test_json_ld_selects_first() {
const doc = getDocument([
JSON.stringify({
"@type": "Thing",
}),
JSON.stringify({
"@type": "CreativeWork",
}),
JSON.stringify({
"@type": "WebPage",
}),
]);
const candidates = findCandidates(doc);
Assert.equal(
candidates.jsonLd,
`the first JSON-LD data should be preferred`
);
});
add_task(async function test_json_ld_robust_to_url_array() {
const doc = getDocument([
JSON.stringify({
"@type": "SiteMap",
url: [
],
}),
]);
const candidates = findCandidates(doc);
Assert.equal(
candidates.jsonLd,
undefined,
`when url is an array, the JSON-LD data should not be used`
);
});