-
Notifications
You must be signed in to change notification settings - Fork 18
/
xml-ns-key.js
54 lines (49 loc) · 1.33 KB
/
xml-ns-key.js
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
47
48
49
50
51
52
53
54
// The xml parser takes care of validating the attribute name.
// If the user started the attribute name with "xmlns:" we can assume that
// they meant for it to be an xml namespace attribute.
// xmlns attributes explicitly can't contain ":" after the "xmlns:" part.
const namespaceRegex = /^xmlns(?<prefixWithColon>:(?<prefix>[^:]*))?$/;
/**
* See comment in api.d.ts.
*
* @param {string} key
* @param {boolean} includeEmptyPrefix
* @returns {boolean}
*/
function isXMLNamespaceKey({ key, includeEmptyPrefix }) {
if (typeof key !== "string") {
return false;
}
const matchArr = key.match(namespaceRegex);
// No match - this is not an xmlns key
if (matchArr === null) {
return false;
}
return !!(
includeEmptyPrefix === true ||
// "xmlns" case
!matchArr.groups.prefixWithColon ||
// "xmlns:<prefix>" case
matchArr.groups.prefix
);
}
/**
* See comment in api.d.ts.
*
* @param {string} key
* @returns {string|undefined}
*/
function getXMLNamespaceKeyPrefix(key) {
if (typeof key !== "string") {
return undefined;
}
const matchArr = key.match(namespaceRegex);
if (matchArr === null) {
return undefined;
}
return (matchArr.groups && matchArr.groups.prefix) || "";
}
module.exports = {
isXMLNamespaceKey: isXMLNamespaceKey,
getXMLNamespaceKeyPrefix: getXMLNamespaceKeyPrefix,
};