Skip to content

Commit 96ae4bf

Browse files
author
Dmitry Ovsyanko
committed
XMLNode +innerText
1 parent bdab52d commit 96ae4bf

File tree

4 files changed

+186
-414
lines changed

4 files changed

+186
-414
lines changed

__tests__/XMLParser.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ test ('bad', () => {
1111

1212
})
1313

14+
test ('innerText', () => {
15+
16+
const p = new XMLParser ()
17+
18+
const d = p.process ('<root>Before<si>a<t/>n<![CDATA[d]]></si>after</root>')
19+
20+
delete d.children [0].children
21+
22+
expect (d.innerText).toBe ('Beforeandafter')
23+
24+
})
25+
1426
test ('param_types', () => {
1527

1628
const p = new XMLParser ({useEntities: false, useNamespaces: false})

lib/XMLNode.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ const XMLNode = class extends SAXEvent {
152152

153153
}
154154

155+
get innerText () {
156+
157+
const {children} = this; if (!Array.isArray (children) || children.length === 0) return this.text
158+
159+
let s = ''; for (const child of children) s += child.innerText; return s
160+
161+
}
162+
155163
detachChildren (list) {
156164

157165
if (!Array.isArray (list) || list.length === 0) return []

0 commit comments

Comments
 (0)