-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(common): add utility functions for xmlns attributes (#173)
- Loading branch information
Showing
6 changed files
with
202 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
const { findNextTextualToken } = require("./find-next-textual-token"); | ||
const { | ||
isXMLNamespaceKey, | ||
getXMLNamespaceKeyPrefix | ||
} = require("./xml-ns-key.js"); | ||
|
||
module.exports = { | ||
findNextTextualToken: findNextTextualToken | ||
findNextTextualToken: findNextTextualToken, | ||
isXMLNamespaceKey: isXMLNamespaceKey, | ||
getXMLNamespaceKeyPrefix: getXMLNamespaceKeyPrefix | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
const { expect } = require("chai"); | ||
const { isXMLNamespaceKey, getXMLNamespaceKeyPrefix } = require("../"); | ||
|
||
describe("The XML-Tools Common Utils", () => { | ||
context("isXMLNamespaceKey", () => { | ||
it("will return true for attribute name starting with xmlns:", () => { | ||
expect(isXMLNamespaceKey({ key: "xmlns:a", includeEmptyPrefix: true })).to | ||
.be.true; | ||
expect(isXMLNamespaceKey({ key: "xmlns:a", includeEmptyPrefix: false })) | ||
.to.be.true; | ||
}); | ||
|
||
it("will return true for attribute name starting with xmlns: that contains dots", () => { | ||
expect( | ||
isXMLNamespaceKey({ key: "xmlns:a.b.c", includeEmptyPrefix: true }) | ||
).to.be.true; | ||
expect( | ||
isXMLNamespaceKey({ key: "xmlns:a.b.c", includeEmptyPrefix: false }) | ||
).to.be.true; | ||
}); | ||
|
||
it("will return true for the default namespace attribute", () => { | ||
expect(isXMLNamespaceKey({ key: "xmlns", includeEmptyPrefix: true })).to | ||
.be.true; | ||
expect(isXMLNamespaceKey({ key: "xmlns", includeEmptyPrefix: false })).to | ||
.be.true; | ||
}); | ||
|
||
it("will return true for xmlns attribute without a name when includeEmptyPrefix is true", () => { | ||
expect(isXMLNamespaceKey({ key: "xmlns:", includeEmptyPrefix: true })).to | ||
.be.true; | ||
}); | ||
|
||
it("will return false for xmlns attribute without a name when includeEmptyPrefix is false", () => { | ||
expect(isXMLNamespaceKey({ key: "xmlns:", includeEmptyPrefix: false })).to | ||
.be.false; | ||
}); | ||
|
||
it("will return false for the non-xmlns attribute", () => { | ||
expect(isXMLNamespaceKey({ key: "abc", includeEmptyPrefix: true })).to.be | ||
.false; | ||
expect(isXMLNamespaceKey({ key: "abc", includeEmptyPrefix: false })).to.be | ||
.false; | ||
}); | ||
|
||
it("will return false for non-xmlns attribute that starts with xmlns", () => { | ||
expect(isXMLNamespaceKey({ key: "xmlnst", includeEmptyPrefix: true })).to | ||
.be.false; | ||
expect(isXMLNamespaceKey({ key: "xmlnst", includeEmptyPrefix: false })).to | ||
.be.false; | ||
}); | ||
|
||
it("will return false for attribute name starting with xmlns: that contains additional colons", () => { | ||
expect( | ||
isXMLNamespaceKey({ key: "xmlns:a.b:c", includeEmptyPrefix: true }) | ||
).to.be.false; | ||
expect( | ||
isXMLNamespaceKey({ key: "xmlns:a.b:c", includeEmptyPrefix: false }) | ||
).to.be.false; | ||
}); | ||
|
||
it("will return false for undefined", () => { | ||
expect(isXMLNamespaceKey({ key: undefined, includeEmptyPrefix: true })).to | ||
.be.false; | ||
expect(isXMLNamespaceKey({ key: undefined, includeEmptyPrefix: false })) | ||
.to.be.false; | ||
}); | ||
|
||
it("will return false for null", () => { | ||
expect(isXMLNamespaceKey({ key: null, includeEmptyPrefix: true })).to.be | ||
.false; | ||
expect(isXMLNamespaceKey({ key: null, includeEmptyPrefix: false })).to.be | ||
.false; | ||
}); | ||
}); | ||
|
||
context("getNamespaceKeyPrefix", () => { | ||
it("will return the name without xmlns prefix for xmlns attribute with name", () => { | ||
expect(getXMLNamespaceKeyPrefix("xmlns:abc")).to.eql("abc"); | ||
}); | ||
|
||
it("will return the name without xmlns prefix for xmlns attribute with name that contains dots", () => { | ||
expect(getXMLNamespaceKeyPrefix("xmlns:abc.efg")).to.eql("abc.efg"); | ||
}); | ||
|
||
it("will return empty string when prefix is empty", () => { | ||
expect(getXMLNamespaceKeyPrefix("xmlns:")).to.be.empty; | ||
}); | ||
|
||
it("will return undefined when key does not start with xmlns", () => { | ||
expect(getXMLNamespaceKeyPrefix("abc")).to.be.undefined; | ||
}); | ||
|
||
it("will return undefined when key starts with xmlns but is not an xmlns key", () => { | ||
expect(getXMLNamespaceKeyPrefix("xmlns*")).to.be.undefined; | ||
}); | ||
|
||
it("will return undefined for undefined", () => { | ||
expect(getXMLNamespaceKeyPrefix(undefined)).to.be.undefined; | ||
}); | ||
|
||
it("will return undefined for null", () => { | ||
expect(getXMLNamespaceKeyPrefix(null)).to.be.undefined; | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters