Debian Codegraph @

test/turndown-test.js

raw symbols SCIP
1 var Attendant = require('turndown-attendant')
2 var TurndownService = require('../lib/turndown.cjs')
3
4 var attendant = new Attendant({
5 file: __dirname + '/index.html',
6 TurndownService: TurndownService
7 })
8 var test = attendant.test
9
10 attendant.run()
11
12 test('malformed documents', function (t) {
13 t.plan(0)
14 var turndownService = new TurndownService()
15 turndownService.turndown('<HTML><head></head><BODY><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><body onload=alert(document.cookie);></body></html>')
16 t.end()
17 })
18
19 test('null input', function (t) {
20 t.plan(1)
21 var turndownService = new TurndownService()
22 t.throws(
23 function () { turndownService.turndown(null) }, /null is not a string/
24 )
25 })
26
27 test('undefined input', function (t) {
28 t.plan(1)
29 var turndownService = new TurndownService()
30 t.throws(
31 function () { turndownService.turndown(void (0)) },
32 /undefined is not a string/
33 )
34 })
35
36 test('#addRule returns the instance', function (t) {
37 t.plan(1)
38 var turndownService = new TurndownService()
39 var rule = {
40 filter: ['del', 's', 'strike'],
41 replacement: function (content) {
42 return '~~' + content + '~~'
43 }
44 }
45 t.equal(turndownService.addRule('strikethrough', rule), turndownService)
46 })
47
48 test('#addRule adds the rule', function (t) {
49 t.plan(2)
50 var turndownService = new TurndownService()
51 var rule = {
52 filter: ['del', 's', 'strike'],
53 replacement: function (content) {
54 return '~~' + content + '~~'
55 }
56 }
57 // Assert rules#add is called
58 turndownService.rules.add = function (key, r) {
59 t.equal(key, 'strikethrough')
60 t.equal(rule, r)
61 }
62 turndownService.addRule('strikethrough', rule)
63 })
64
65 test('#use returns the instance for chaining', function (t) {
66 t.plan(1)
67 var turndownService = new TurndownService()
68 t.equal(turndownService.use(function plugin () {}), turndownService)
69 })
70
71 test('#use with a single plugin calls the fn with instance', function (t) {
72 t.plan(1)
73 var turndownService = new TurndownService()
74 function plugin (service) {
75 t.equal(service, turndownService)
76 }
77 turndownService.use(plugin)
78 })
79
80 test('#use with multiple plugins calls each fn with instance', function (t) {
81 t.plan(2)
82 var turndownService = new TurndownService()
83 function plugin1 (service) {
84 t.equal(service, turndownService)
85 }
86 function plugin2 (service) {
87 t.equal(service, turndownService)
88 }
89 turndownService.use([plugin1, plugin2])
90 })
91
92 test('#keep keeps elements as HTML', function (t) {
93 t.plan(2)
94 var turndownService = new TurndownService()
95 var input = '<p>Hello <del>world</del><ins>World</ins></p>'
96
97 // Without `.keep(['del', 'ins'])`
98 t.equal(turndownService.turndown(input), 'Hello worldWorld')
99
100 // With `.keep(['del', 'ins'])`
101 turndownService.keep(['del', 'ins'])
102 t.equal(
103 turndownService.turndown('<p>Hello <del>world</del><ins>World</ins></p>'),
104 'Hello <del>world</del><ins>World</ins>'
105 )
106 })
107
108 test('#keep returns the TurndownService instance for chaining', function (t) {
109 t.plan(1)
110 var turndownService = new TurndownService()
111 t.equal(turndownService.keep(['del', 'ins']), turndownService)
112 })
113
114 test('keep rules are overridden by the standard rules', function (t) {
115 t.plan(1)
116 var turndownService = new TurndownService()
117 turndownService.keep('p')
118 t.equal(turndownService.turndown('<p>Hello world</p>'), 'Hello world')
119 })
120
121 test('keeping elements that have a blank textContent but contain significant elements', function (t) {
122 t.plan(1)
123 var turndownService = new TurndownService()
124 turndownService.keep('figure')
125 t.equal(
126 turndownService.turndown('<figure><iframe src="http://example.com"></iframe></figure>'),
127 '<figure><iframe src="http://example.com"></iframe></figure>'
128 )
129 })
130
131 test('keepReplacement can be customised', function (t) {
132 t.plan(1)
133 var turndownService = new TurndownService({
134 keepReplacement: function (content, node) {
135 return '\n\n' + node.outerHTML + '\n\n'
136 }
137 })
138 turndownService.keep(['del', 'ins'])
139 t.equal(turndownService.turndown(
140 '<p>Hello <del>world</del><ins>World</ins></p>'),
141 'Hello \n\n<del>world</del>\n\n<ins>World</ins>'
142 )
143 })
144
145 test('#remove removes elements', function (t) {
146 t.plan(2)
147 var turndownService = new TurndownService()
148 var input = '<del>Please redact me</del>'
149
150 // Without `.remove('del')`
151 t.equal(turndownService.turndown(input), 'Please redact me')
152
153 // With `.remove('del')`
154 turndownService.remove('del')
155 t.equal(turndownService.turndown(input), '')
156 })
157
158 test('#remove returns the TurndownService instance for chaining', function (t) {
159 t.plan(1)
160 var turndownService = new TurndownService()
161 t.equal(turndownService.remove(['del', 'ins']), turndownService)
162 })
163
164 test('remove elements are overridden by rules', function (t) {
165 t.plan(1)
166 var turndownService = new TurndownService()
167 turndownService.remove('p')
168 t.equal(turndownService.turndown('<p>Hello world</p>'), 'Hello world')
169 })
170
171 test('remove elements are overridden by keep', function (t) {
172 t.plan(1)
173 var turndownService = new TurndownService()
174 turndownService.keep(['del', 'ins'])
175 turndownService.remove(['del', 'ins'])
176 t.equal(turndownService.turndown(
177 '<p>Hello <del>world</del><ins>World</ins></p>'),
178 'Hello <del>world</del><ins>World</ins>'
179 )
180 })