UNPKG

libxmljs

Version:

libxml bindings for v8 javascript engine

179 lines 7.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var libxml = require("../index"); var index_1 = require("../index"); module.exports.create = function (assert) { var doc = libxml.Document(); var elem = doc.node("name1"); var ns = elem.defineNamespace("http://my-namespace.com"); assert.ok(ns); assert.equal(null, elem.namespace()); assert.equal(null, ns.prefix()); assert.equal("http://my-namespace.com", ns.href()); assert.done(); }; module.exports.set = function (assert) { var _a, _b; var doc = libxml.Document(); var elem = doc.node("name1"); var ns = elem.namespace("http://my-namespace.com"); assert.ok(ns); assert.equal(ns, elem.namespace()); assert.equal(null, (_a = elem.namespace()) === null || _a === void 0 ? void 0 : _a.prefix()); assert.equal("http://my-namespace.com", (_b = elem.namespace()) === null || _b === void 0 ? void 0 : _b.href()); assert.done(); }; module.exports.with_prefix = function (assert) { var _a, _b; var doc = libxml.Document(); var elem = doc.node("name1"); var ns = elem.defineNamespace("pref", "http://my-namespace.com"); assert.equal(null, elem.namespace()); assert.equal("pref", ns.prefix()); assert.equal("http://my-namespace.com", ns.href()); var ns2 = elem.namespace("pref", "http://my-namespace.com"); assert.ok(ns2); assert.equal(ns, ns2); assert.equal(ns, elem.namespace()); assert.equal("pref", (_a = elem.namespace()) === null || _a === void 0 ? void 0 : _a.prefix()); assert.equal("http://my-namespace.com", (_b = elem.namespace()) === null || _b === void 0 ? void 0 : _b.href()); assert.done(); }; module.exports.from_parsing = function (assert) { var _a, _b, _c, _d; var doc = libxml.parseXml('<?xml version="1.0" encoding="UTF-8"?>' + '<name1 xmlns="http://my-namespace.com"/>'); var elem = doc.root(); assert.ok(elem.namespace()); assert.equal(null, (_a = elem.namespace()) === null || _a === void 0 ? void 0 : _a.prefix()); assert.equal("http://my-namespace.com", (_b = elem.namespace()) === null || _b === void 0 ? void 0 : _b.href()); var doc = libxml.parseXml('<?xml version="1.0" encoding="UTF-8"?>' + '<name1 xmlns:pref="http://my-namespace.com"/>'); var elem = doc.root(); assert.ok(!elem.namespace()); var doc = libxml.parseXml('<?xml version="1.0" encoding="UTF-8"?>' + '<pref:name1 xmlns:pref="http://my-namespace.com"/>'); var elem = doc.root(); assert.ok(elem.namespace()); assert.equal("pref", (_c = elem.namespace()) === null || _c === void 0 ? void 0 : _c.prefix()); assert.equal("http://my-namespace.com", (_d = elem.namespace()) === null || _d === void 0 ? void 0 : _d.href()); assert.done(); }; module.exports.existing = function (assert) { var doc = libxml.Document(); var elem = doc.node("name1"); var ns = elem.defineNamespace("http://my-namespace.com"); elem.namespace("http://my-namespace.com"); assert.ok(ns); assert.equal(ns, elem.namespace()); var doc = libxml.Document(); var elem = doc.node("name1"); var ns = elem.defineNamespace("pref", "http://my-namespace.com"); elem.namespace("pref", "http://my-namespace.com"); assert.ok(ns); assert.equal(ns, elem.namespace()); assert.done(); }; module.exports.remove = function (assert) { var doc = libxml.Document(); var elem = doc.node("name1"); var ns = elem.namespace("http://my-namespace.com"); assert.ok(ns); assert.ok(ns == elem.namespace()); elem.namespace(null); assert.ok(!elem.namespace()); assert.done(); }; module.exports.all = function (assert) { var document = libxml.Document(); var root = document.node("root"); var list = []; list.push(root.namespace("com", "http://example.com")); list.push(root.namespace("net", "http://example.net")); list.push(root.namespace("http://example.org")); assert.ok(root.namespaces().every(function (ns, index) { return ns.href() === list[index].href() && ns.prefix() === list[index].prefix(); })); assert.equal(root.namespaces().length, list.length); assert.done(); }; module.exports.empty = function (assert) { var document = libxml.Document(); var root = document.node("root"); assert.equal(root.namespaces().length, 0); assert.done(); }; module.exports.nested = function (assert) { var document = libxml.Document(); var root = document.node("root"); root.namespace("com", "http://example.com"); assert.equal(root.namespaces().length, 1); var child = root.node("child"); child.namespace("net", "http://example.net"); assert.equal(child.namespaces().length, 2); root.namespace("http://example.org"); assert.equal(child.namespaces().length, 3); assert.done(); }; module.exports.xmlns = function (assert) { var _a, _b; var str = '<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><div>BACON</div><div>ROCKS</div><p>WUT?</p></body></html>'; var doc = libxml.parseXml(str); var divs = doc.find("//xmlns:div", "http://www.w3.org/1999/xhtml"); assert.equal(divs.length, 2); var div = doc.get("//xmlns:div", "http://www.w3.org/1999/xhtml"); assert.ok(div instanceof index_1.XMLElement); assert.ok(div != null); var exp = (_b = (_a = doc.root()) === null || _a === void 0 ? void 0 : _a.child(1)) === null || _b === void 0 ? void 0 : _b.child(0); assert.ok(exp != null); assert.equal(div.toString(), exp.toString()); assert.done(); }; module.exports.custom_ns = function (assert) { var _a, _b; var str = '<html xmlns:bacon="http://www.example.com/fake/uri"><head></head><body><bacon:div>BACON</bacon:div><bacon:div>ROCKS</bacon:div><p>WUT?</p></body></html>'; var doc = libxml.parseXml(str); var divs = doc.find("//bacon:div", { bacon: "http://www.example.com/fake/uri" }); assert.equal(divs.length, 2); var div = doc.get("//bacon:div", { bacon: "http://www.example.com/fake/uri" }); assert.ok(div instanceof index_1.XMLElement); assert.ok(div != null); var exp = (_b = (_a = doc.root()) === null || _a === void 0 ? void 0 : _a.child(1)) === null || _b === void 0 ? void 0 : _b.child(0); assert.ok(exp != null); assert.equal(div.toString(), exp.toString()); assert.done(); }; module.exports.local_namespaces = function (assert) { var _a; var str = '<html xmlns="urn:example" xmlns:ex1="urn:example:1"><body xmlns:ex2="urn:example:2"/></html>'; var doc = libxml.parseXml(str); assert.ok(doc); var root = doc.root(); assert.ok(root); var decls = root.namespaces(true); assert.ok(decls); assert.equal(2, decls.length); decls.forEach(function (n) { if (n.prefix() == null) { assert.equal("urn:example", n.href()); } else if (n.prefix() == "ex1") { assert.equal("urn:example:1", n.href()); } else { assert.ok(false); } }); var body = root.get("ex:body", { ex: "urn:example" }); assert.ok(body); decls = body.namespaces(true); assert.equal(1, decls.length); assert.equal("urn:example:2", (_a = decls[0]) === null || _a === void 0 ? void 0 : _a.href()); decls = body.namespaces(); assert.equal(3, decls.length); decls = body.namespaces(false); assert.equal(3, decls.length); decls = body.namespaces(0); assert.equal(3, decls.length); decls = body.namespaces(1); assert.equal(3, decls.length); assert.done(); }; //# sourceMappingURL=namespace.js.map