-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.mjs
46 lines (34 loc) · 1.3 KB
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import protochain from "protochain";
const NODE_PROTOTYPE_CHAIN = "Node,EventTarget,Object";
const DOCUMENT_PROTOTYPE_CHAIN = `Document,${NODE_PROTOTYPE_CHAIN}`;
const DOCUMENT_TYPE_PROTOTYPE_CHAIN = `DocumentType,${NODE_PROTOTYPE_CHAIN}`;
const getPrototypeNameChain = object => protochain(object).map(({constructor: {name}}) => name).join(",");
// @todo use npmjs.com/dom-predicates
const isDocumentNode = node => getPrototypeNameChain(node).endsWith(DOCUMENT_PROTOTYPE_CHAIN);
// @todo use npmjs.com/dom-predicates
const isDocumentTypeNode = node => getPrototypeNameChain(node).endsWith(DOCUMENT_TYPE_PROTOTYPE_CHAIN);
export default (documentType, document) =>
{
if (!isDocumentTypeNode(documentType) && documentType!==null)
{
throw new TypeError("Parameter 1 must be a DocumentType or null");
}
else if (!isDocumentNode(document))
{
throw new TypeError("Parameter 2 must be a Document");
}
const {doctype: originalDocumentType} = document;
if (documentType!==null && originalDocumentType!==null)
{
originalDocumentType.replaceWith(documentType);
}
else if (documentType!==null && originalDocumentType===null)
{
document.documentElement.before(documentType);
}
else if (documentType===null && originalDocumentType!==null)
{
originalDocumentType.remove();
}
return originalDocumentType;
};