forked from libxmljs/libxmljs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext.ts
More file actions
136 lines (105 loc) · 3.79 KB
/
text.ts
File metadata and controls
136 lines (105 loc) · 3.79 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import * as libxml from "../index";
module.exports.invalid_new = function (assert: any) {
var doc = libxml.Document();
assert.throws(function () {
//@ts-ignore
libxml.Text();
});
assert.throws(function () {
libxml.Text(doc);
});
assert.done();
};
module.exports.new = function (assert: any) {
var doc = libxml.Document();
var elem = libxml.Text(doc, "node content");
doc.root(elem);
assert.equal("node content", elem.text());
assert.done();
};
module.exports.setters = function (assert: any) {
var doc = libxml.Document();
var elem = libxml.Text(doc, "node content");
// change content
assert.equal("node content", elem.text());
elem.text("content && more content <>");
assert.equal("content && more content <>", elem.text());
assert.done();
};
// removed for interoperability between XMLText and XMLElement
//
// module.exports.getters = function (assert: any) {
// var doc = libxml.Document();
// var elem = libxml.Text(doc, "getters");
// assert.throws(function () {
// elem.name();
// }, "text nodes should NOT expose a name");
// assert.equal("text", elem.type());
// assert.done();
// };
module.exports.remove = function (assert: any) {
var doc = libxml.Document();
var elem = libxml.Text(doc, "node content");
doc.root(elem);
assert.equal('<?xml version="1.0" encoding="UTF-8"?>\nnode content\n', doc.toString());
elem.remove();
assert.equal('<?xml version="1.0" encoding="UTF-8"?>\n', doc.toString());
assert.done();
};
module.exports.toString = function (assert: any) {
var doc = libxml.Document();
var elem = libxml.Text(doc, "node content");
doc.root(elem);
assert.equal("node content", elem.toString());
assert.done();
};
module.exports.addChild = function (assert: any) {
var doc = libxml.Document();
var newTextNode = libxml.Text(doc, "my text");
var newElement = libxml.Element(doc, "div");
newElement.addChild(newTextNode);
doc.root(newElement);
assert.equal("<div>my text</div>", newElement.toString());
assert.done();
};
module.exports.addSiblings = function (assert: any) {
var doc = libxml.Document();
var parentNode = libxml.Element(doc, "div");
var child = parentNode.node("child", "i'm a child");
var prevTextNode = libxml.Text(doc, "before text");
var nextTextNode = libxml.Text(doc, "after text");
child.addPrevSibling(prevTextNode);
child.addNextSibling(nextTextNode);
assert.equal("<div>before text<child>i'm a child</child>after text</div>", parentNode.toString());
assert.done();
};
module.exports.add_prev_sibling_merge_text = function (assert: any) {
var str = "<foo>bar<baz/></foo>";
var doc = libxml.parseXml(str);
var bar = doc.root()?.childNodes()[0];
var qux = libxml.Text(doc, "qux");
bar?.addPrevSibling(qux);
// added text is merged into existing child node
var children = doc.root()?.childNodes();
assert.strictEqual(children?.length, 2);
assert.strictEqual("quxbar", children?.[0]?.text());
assert.ok(children?.[0] != qux);
// passed node is not changed
assert.strictEqual(doc, qux.parent());
assert.strictEqual("qux", qux.text());
assert.done();
};
module.exports.add_next_sibling_merge_text = function (assert: any) {
var str = "<foo>bar<baz/></foo>";
var doc = libxml.parseXml(str);
var bar = doc.root()?.childNodes()[0];
var qux = libxml.Text(doc, "qux");
bar?.addNextSibling(qux);
var children = doc.root()?.childNodes();
assert.strictEqual(children?.length, 2);
assert.strictEqual("barqux", children?.[0]?.text());
assert.ok(children?.[0] != qux);
assert.strictEqual(doc, qux.parent());
assert.strictEqual("qux", qux.text());
assert.done();
};