forked from xmppjs/ltx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
81d2b62
commit 0ee7a59
Showing
29 changed files
with
6,458 additions
and
2,056 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
extends: "../.eslintrc.cjs", | ||
env: { | ||
jest: true, | ||
}, | ||
}; |
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,17 @@ | ||
import assert from "assert"; | ||
import JSONify from "../src/JSONify.js"; | ||
import Element from "../src/Element.js"; | ||
|
||
describe("JSONify", () => { | ||
it("serialize to json", () => { | ||
const e = new Element("e", { foo: 23, bar: 0, nil: null }) | ||
.c("f") | ||
.t(1000) | ||
.up(); | ||
assert.deepStrictEqual(JSONify(e), { | ||
name: "e", | ||
attrs: { foo: 23, bar: 0, nil: null }, | ||
children: [{ name: "f", attrs: {}, children: [1000] }], | ||
}); | ||
}); | ||
}); |
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,80 @@ | ||
import assert from "assert"; | ||
import _parse from "../src/parse.js"; | ||
import LtxParser from "../src/parsers/ltx.js"; | ||
import h from "../src/createElement.js"; | ||
import Parser from "../src/Parser.js"; | ||
|
||
function parse(s) { | ||
return _parse(s, { Parser: LtxParser }); | ||
} | ||
|
||
function parseChunks(chunks) { | ||
const p = new Parser(); | ||
|
||
let result = null; | ||
let error = null; | ||
|
||
p.on("tree", (tree) => { | ||
result = tree; | ||
}); | ||
p.on("error", (e) => { | ||
error = e; | ||
}); | ||
|
||
for (const chunk of chunks) { | ||
p.write(chunk); | ||
} | ||
p.end(); | ||
|
||
if (error) { | ||
throw error; | ||
} else { | ||
return result; | ||
} | ||
} | ||
|
||
describe("sax_ltx", () => { | ||
describe("CDATA parsing", () => { | ||
it("issue-19: parse CDATA content as text", () => { | ||
const el = parse("<root><![CDATA[Content]]></root>"); | ||
assert.strictEqual(el.name, "root"); | ||
assert.strictEqual(el.getText(), "Content"); | ||
}); | ||
it("do not unescape CDATA content", () => { | ||
const el = parse( | ||
'<root><![CDATA[Content & "more content"]]></root>' | ||
); | ||
assert.strictEqual(el.name, "root"); | ||
assert.strictEqual(el.getText(), 'Content & "more content"'); | ||
}); | ||
it("issue-132", () => { | ||
const el = parse( | ||
"<a><b><![CDATA[]]></b><b><![CDATA[--><c>&d;]]></b></a>" | ||
); | ||
assert.deepStrictEqual( | ||
el, | ||
h("a", null, h("b", null, ""), h("b", null, "--><c>&d;")) | ||
); | ||
}); | ||
it("split CDATA between chunks", () => { | ||
const el1 = parseChunks(["<root><![CDA", "TA[Content]]></root>"]); | ||
assert.strictEqual(el1.getText(), "Content"); | ||
const el2 = parseChunks(["<root><![CDATA[Con", "tent]]></root>"]); | ||
assert.strictEqual(el2.getText(), "Content"); | ||
const el3 = parseChunks(["<root><![CDATA[Content]]", "></root>"]); | ||
assert.strictEqual(el3.getText(), "Content"); | ||
|
||
const str = "<root><![CDATA[Content]]></root>"; | ||
for (let i = 0; i < str.length; i++) { | ||
const chunks = [ | ||
str.slice(0, Math.max(0, i)) || "", | ||
str.slice(Math.max(0, i)) || "", | ||
]; | ||
assert.strictEqual( | ||
parseChunks(chunks).toString(), | ||
"<root>Content</root>" | ||
); | ||
} | ||
}); | ||
}); | ||
}); |
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,62 @@ | ||
import assert from "assert"; | ||
import _clone from "../src/clone.js"; | ||
import Element from "../src/Element.js"; | ||
|
||
describe("clone", () => { | ||
it("clones", () => { | ||
const orig = new Element("msg", { type: "get" }) | ||
.c("content") | ||
.t("foo") | ||
.root(); | ||
const clone = _clone(orig); | ||
assert.strictEqual(clone.name, orig.name); | ||
assert.strictEqual(clone.attrs.type, orig.attrs.type); | ||
assert.strictEqual(clone.attrs.to, orig.attrs.to); | ||
assert.strictEqual(clone.children.length, orig.children.length); | ||
assert.strictEqual( | ||
clone.getChildText("content"), | ||
orig.getChildText("content") | ||
); | ||
|
||
assert.strictEqual(orig.getChild("content").up(), orig); | ||
assert.strictEqual(clone.getChild("content").up(), clone); | ||
}); | ||
it("mod attr", () => { | ||
const orig = new Element("msg", { type: "get" }); | ||
const clone = _clone(orig); | ||
clone.attrs.type += "-result"; | ||
|
||
assert.strictEqual(orig.attrs.type, "get"); | ||
assert.strictEqual(clone.attrs.type, "get-result"); | ||
}); | ||
it("rm attr", () => { | ||
const orig = new Element("msg", { from: "me" }); | ||
const clone = _clone(orig); | ||
delete clone.attrs.from; | ||
clone.attrs.to = "you"; | ||
|
||
assert.strictEqual(orig.attrs.from, "me"); | ||
assert.strictEqual(orig.attrs.to, undefined); | ||
assert.strictEqual(clone.attrs.from, undefined); | ||
assert.strictEqual(clone.attrs.to, "you"); | ||
}); | ||
it("mod child", () => { | ||
const orig = new Element("msg", { type: "get" }) | ||
.c("content") | ||
.t("foo") | ||
.root(); | ||
const clone = _clone(orig); | ||
clone.getChild("content").t("bar").name = "description"; | ||
|
||
assert.strictEqual(orig.children[0].name, "content"); | ||
assert.strictEqual(orig.getChildText("content"), "foo"); | ||
assert.strictEqual(clone.children[0].name, "description"); | ||
assert.strictEqual(clone.getChildText("description"), "foobar"); | ||
}); | ||
it("use original constructor for the clone", () => { | ||
class Foo extends Element {} | ||
const foo = new Foo(); | ||
assert(_clone(foo) instanceof Element); | ||
assert(_clone(foo) instanceof Foo); | ||
}); | ||
}); |
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,69 @@ | ||
import assert from "assert"; | ||
import Element from "../src/Element.js"; | ||
import createElement from "../src/createElement.js"; | ||
|
||
describe("createElement", () => { | ||
it("create a new element and set children", () => { | ||
const c = new Element("bar"); | ||
const e = createElement("foo", { foo: "bar" }, "foo", c); | ||
assert(e instanceof Element); | ||
assert(e.is("foo")); | ||
assert.strictEqual(e.attrs.foo, "bar"); | ||
assert.strictEqual(e.children.length, 2); | ||
assert.strictEqual(e.children[0], "foo"); | ||
assert.strictEqual(e.children[1], c); | ||
}); | ||
it("null and undefined children are discarded", () => { | ||
const e = createElement( | ||
"foo", | ||
null, | ||
undefined, | ||
"bar", | ||
null, | ||
createElement("test"), | ||
"baz" | ||
); | ||
const b = new Element("foo").t("bar").c("test").up().t("baz"); | ||
assert.strictEqual(e.root().toString(), b.root().toString()); | ||
}); | ||
it("boolean children are discarded", () => { | ||
assert.deepEqual( | ||
createElement("foo", null, true, false), | ||
new Element("foo") | ||
); | ||
}); | ||
it("falsy numbers", () => { | ||
assert.deepEqual( | ||
createElement("foo", null, -1, 0, 1).toString(), | ||
"<foo>-101</foo>" | ||
); | ||
}); | ||
it("__source and __self attributes are discarded", () => { | ||
const e = createElement("foo", { | ||
__self: "foo", | ||
__source: "bar", | ||
foo: "bar", | ||
}); | ||
assert.equal(e.attrs.__self, undefined); | ||
assert.equal(e.attrs.__source, undefined); | ||
assert.equal(e.attrs.foo, "bar"); | ||
}); | ||
it("children array", () => { | ||
assert.deepEqual( | ||
createElement("bar", null, "foo", [ | ||
"foo", | ||
["foo", ["foo"]], | ||
"foo", | ||
]).toString(), | ||
"<bar>foofoofoofoofoo</bar>" | ||
); | ||
}); | ||
it("null and undefined attributes are discarded", () => { | ||
const e = createElement("foo", { foo: null, bar: undefined }); | ||
assert.deepEqual(e.attrs, {}); | ||
}); | ||
it("number attribute are converted to string", () => { | ||
const e = createElement("foo", { foo: 1 }); | ||
assert.deepEqual(e.attrs, { foo: "1" }); | ||
}); | ||
}); |
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,50 @@ | ||
import assert from "assert"; | ||
import parsers from "../src/parsers.js"; | ||
import DOMElement from "../src/DOMElement.js"; | ||
import _parse from "../src/parse.js"; | ||
|
||
for (const Parser of parsers) { | ||
// eslint-disable-next-line no-inner-declarations | ||
function parse(s) { | ||
return _parse(s, { Parser: Parser, Element: DOMElement }); | ||
} | ||
describe("Parsing returns DOMElement's", () => { | ||
it("Returns DOMElement on parse", () => { | ||
const stanza = | ||
'<message><body xmlns="http://www.w3.org/1999/xhtml">' + | ||
"<p>DOM</p></body></message>"; | ||
const el = parse(stanza); | ||
|
||
assert(el.getChild("body") instanceof DOMElement); | ||
assert.strictEqual(el.getChild("body").constructor.name, "DOMElement"); | ||
const body = el.getChild("body"); | ||
expect(body.localName).toBeDefined(); | ||
assert.strictEqual(body.localName, "body"); | ||
|
||
expect(body.namespaceURI).toBeDefined(); | ||
assert.strictEqual(body.namespaceURI, "http://www.w3.org/1999/xhtml"); | ||
|
||
expect(body.parentNode).toBeDefined(); | ||
assert.strictEqual(body.parentNode.getName(), "message"); | ||
|
||
expect(body.childNodes).toBeDefined(); | ||
expect(body.childNodes).toBeInstanceOf(Array); | ||
assert.strictEqual(body.childNodes.length, 1); | ||
|
||
expect(body.textContent).toBeDefined(); | ||
assert.strictEqual(body.textContent, ""); | ||
|
||
assert.strictEqual(body.getChild("p").textContent, "DOM"); | ||
}); | ||
it("createElement: create a new element and set children", () => { | ||
const c = new DOMElement("bar"); | ||
const e = DOMElement.createElement("foo", { foo: "bar" }, "foo", c); | ||
assert(e instanceof DOMElement); | ||
assert.strictEqual(e.localName, "foo"); | ||
assert.strictEqual(e.getAttribute("foo"), "bar"); | ||
assert.strictEqual(e.childNodes.length, 2); | ||
assert.strictEqual(e.childNodes[0], "foo"); | ||
assert.strictEqual(e.childNodes[1], c); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.