xmlapi
Version:
a common interface for Node and browser native XML APIs
131 lines (130 loc) • 5.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("./utils");
const XML_1 = utils_1.rawTrimmed `
<root xmlns="http://default.ns" xmlns:ns1="http://prefixed.ns">
<e1>
<ns1:e2>txtx</ns1:e2>
</e1>
<e3 xml:lang="en">lanho</e3>
<e4 lang="uk">lingua</e4>
<e5/>
<e6>
<e7>
<e8><></e8>
</e7>
</e6>
</root>
`;
const XML_NONS = utils_1.rawTrimmed `
<root>
<e1>
<pre:e2>txtx</pre:e2>
</e1>
<e3 xml:lang="en">lanho</e3>
<e4 lang="uk">lingua</e4>
</root>
`;
function describeConcrete(parse) {
describe('Api', () => {
it('throws on invalid parse input', () => {
expect(() => parse('')).toThrow();
expect(() => parse('<e1/><e2/>')).toThrow();
expect(() => parse('<e a="a" a="a"/>')).toThrow();
expect(() => parse('<e1><e2></e1></e2>')).toThrow();
expect(() => parse('<root><unknown:prefix/><root>')).toThrow();
});
it('doc equalls and not', () => {
expect(parse('<e/>').equals(parse('<e/>'))).toBe(false);
let doc = parse('<e/>');
expect(doc.equals(null)).toBe(false);
expect(doc.equals(undefined)).toBe(false);
expect(doc.equals(doc)).toBe(true);
expect(doc.root().document().equals(doc)).toBe(true);
expect(doc.equals(doc.root().document())).toBe(true);
});
it('reserializes to same string', () => {
let doc = parse(XML_1);
let backStr = doc.serialize();
expect(backStr.length).toBeGreaterThan(200);
expect(backStr).toBe(parse(backStr).serialize());
});
let doc = parse(XML_1);
it('is sane', () => {
expect(doc.root().isSame(undefined)).toBe(false);
expect(doc.root().isSame(null)).toBe(false);
expect(doc.root().isSame(doc.root().elementChildren()[2])).toBe(false);
expect(doc.root().isSame(doc.root())).toBe(true);
expect(doc.root().isSame(doc.root().firstChild().parent())).toBe(true);
expect(doc.root().firstChild().isSame(doc.root().child(0))).toBe(true);
expect(doc.root().firstChild().nextSibling().isElement()).toBe(true);
});
it('namespaces', () => {
expect(doc.root().name()).toBe('{http://default.ns}root');
expect(doc.root().namespaceUri()).toBe('http://default.ns');
expect(doc.root().prefixedName()).toBe('root');
expect(doc.root().localName()).toBe('root');
expect(doc.root().namespacePrefix()).toBe(null);
});
it('is walkable', () => {
// expect(grabElements(doc.root)).toEqual([/*'root',*/ 'e1', 'e2', 'e3', 'e4', 'e5', 'e6', 'e7', 'e8']);
});
it('throws on wrong conversions', () => {
expect(doc.root().firstChild().asElement).toThrow();
});
// it('walks if nodes are deleted', () => {
// let i = 0;
// for (let node of doc.root.walkDocument(x => x.isElement())) {
// if (i++ % 2) {
// expect(node.remove()).not.toBeNull;
// }
// }
// expect(grabElements(doc.root)).toEqual(['e1']);
// });
});
describe('Namespaces', () => {
const XML = utils_1.rawTrimmed `
<root xmlns="http://example.com/1" xmlns:two="http://example.com/2">
<e1>1 </e1>
<two:e2 xmlns:three="http://example.com/3">
<e3 xmlns="http://example.com/4"></e3>
</two:e2>
</root>
`;
let doc = parse(XML);
// it('correctly builds namespace map', () => {
// let e3 = doc.root.next(x => x.isElement() && x.asElement().localName === 'e3') as AbstractElement;
// expect(e3.buildNsMap()).toEqual({
// '': 'http://example.com/4',
// three: 'http://example.com/3',
// two: 'http://example.com/2'
// })
// });
});
describe('lang()', () => {
it('returns null where belongs', () => {
expect(parse('<a></a>').root().lang()).toBe(null);
expect(parse('<a xml:lang=""></a>').root().lang()).toBe(null);
expect(parse('<a lang="fake-lang"></a>').root().lang()).toBe(null);
expect(parse('<a lang="fake-lang">texto</a>').root().firstChild().lang()).toBe(null);
});
it('resets language with an empty attribute', () => {
let doc = parse('<a xml:lang="cl"><b xml:lang=""><c/></b></a>');
expect(doc.root().firstChild().lang()).toBe(null);
expect(doc.root().firstElementChild().firstChild().lang()).toBe(null);
});
});
describe('XPath', () => {
it('resolves xml: prefix with empty nsMap', () => {
let doc = parse('<a><b xml:lang="uk">Голос твоєї</b></a>');
expect(doc.root().evaluateBoolean('//@xml:lang="uk"')).toBe(true);
expect(doc.root().evaluateElement('//*[@xml:lang="uk"]').localName()).toBe('b');
expect(doc.root().evaluateBoolean('//@lang="uk"')).toBe(false);
});
});
}
exports.describeConcrete = describeConcrete;
// function grabElements(root: AbstractElement) {
// return [...root.firstChild().walkDocumentDown(x => x.isElement())]
// .map(x => (x as AbstractElement).localName);
// }