8000 Add unit tests to the Keep Markup plugin (#1646) · PrismJS/prism@a944c41 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit a944c41

Browse files
ggrossetiemAAdhaTTah
authored andcommitted
Add unit tests to the Keep Markup plugin (#1646)
Uses JSDOM to simulate the DOM in node.
1 parent 439ea1e commit a944c41

File tree

4 files changed

+97
-4
lines changed

4 files changed

+97
-4
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "prism.js",
66
"style": "themes/prism.css",
77
"scripts": {
8-
"test": "mocha tests/testrunner-tests.js && mocha tests/run.js"
8+
"test": "mocha tests/testrunner-tests.js && mocha tests/run.js && mocha tests/plugins/**/*.js"
99
},
1010
"repository": {
1111
"type": "git",
@@ -29,6 +29,7 @@
2929
"gulp-rename": "^1.2.0",
3030
"gulp-replace": "^1.0.0",
3131
"gulp-uglify": "^3.0.1",
32+
"jsdom": "^13.0.0",
3233
"mocha": "^6.0.0",
3334
"pump": "^3.0.0",
3435
"yargs": "^13.2.2"

plugins/keep-markup/prism-keep-markup.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(function () {
1+
(function (self, document) {
22

33
if (typeof self === 'undefined' || !self.Prism || !self.document || !document.createRange) {
44
return;
@@ -96,4 +96,4 @@
9696
env.highlightedCode = env.element.innerHTML;
9797
}
9898
});
99-
}());
99+
}(self, document));

plugins/keep-markup/prism-keep-markup.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/plugins/keep-markup/test.js

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
const expect = require('chai').expect;
2+
const jsdom = require('jsdom')
3+
const { JSDOM } = jsdom
4+
5+
require('../../../prism')
6+
// fake DOM
7+
global.self = {}
8+
global.self.Prism = Prism
9+
global.document = {}
10+
document.createRange = function () {
11+
}
12+
global.self.document = document
13+
14+
require('../../../plugins/keep-markup/prism-keep-markup')
15+
16+
describe('Prism Keep Markup Plugin', function () {
17+
18+
function execute (code) {
19+
const start = [];
20+
const end = [];
21+
const nodes = [];
22+
document.createRange = function () {
23+
return {
24+
setStart: function (node, offset) {
25+
start.push({ node, offset })
26+
},
27+
setEnd: function (node, offset) {
28+
end.push({ node, offset })
29+
},
30+
extractContents: function () {
31+
return new JSDOM('').window.document.createTextNode('')
32+
},
33+
insertNode: function (node) {
34+
nodes.push(node)
35+
},
36+
detach: function () {
37+
}
38+
}
39+
}
40+
const beforeHighlight = Prism.hooks.all['before-highlight'][0]
41+
const afterHighlight = Prism.hooks.all['after-highlight'][0]
42+
const env = {
43+
element: new JSDOM(code).window.document.getElementsByTagName('code')[0],
44+
language: "javascript"
45+
}
46+
beforeHighlight(env)
47+
afterHighlight(env)
48+
return { start, end, nodes }
49+
}
50+
51+
it('should keep <span> markup', function () {
52+
const result = execute(`<code class="language-javascript">x<span>a</span>y</code>`)
53+
expect(result.start.length).to.equal(1)
54+
expect(result.end.length).to.equal(1)
55+
expect(result.nodes.length).to.equal(1)
56+
expect(result.nodes[0].nodeName).to.equal('SPAN')
57+
})
58+
it('should preserve markup order', function () {
59+
const result = execute(`<code class="language-javascript">x<a></a><b></b>y</code>`)
60+
expect(result.start.length).to.equal(2)
61+
expect(result.start[0].offset).to.equal(0)
62+
expect(result.start[0].node.textContent).to.equal('y')
63+
expect(result.start[1].offset).to.equal(0)
64+
expect(result.start[1].node.textContent).to.equal('y')
65+
expect(result.end.length).to.equal(2)
66+
expect(result.end[0].offset).to.equal(0)
67+
expect(result.end[0].node.textContent).to.equal('y')
68+
expect(result.end[1].offset).to.equal(0)
69+
expect(result.end[1].node.textContent).to.equal('y')
70+
expect(result.nodes.length).to.equal(2)
71+
expect(result.nodes[0].nodeName).to.equal('A')
72+
expect(result.nodes[1].nodeName).to.equal('B')
73+
})
74+
it('should keep last <span> markup', function () {
75+
const result = execute(`<code class="language-javascript">xy<span>a</span></code>`)
76+
expect(result.start.length).to.equal(1)
77+
expect(result.end.length).to.equal(1)
78+
expect(result.nodes.length).to.equal(1)
79+
expect(result.nodes[0].nodeName).to.equal('SPAN')
80+
})
81+
// The markup is removed if it's the last element and the element's name is a single letter: a(nchor), b(old), i(talic)...
82+
// https://github.com/PrismJS/prism/issues/1618
83+
/*
84+
it('should keep last single letter empty markup', function () {
85+
const result = execute(`<code class="language-javascript">xy<a></a></code>`)
86+
expect(result.start.length).to.equal(1)
87+
expect(result.end.length).to.equal(1)
88+
expect(result.nodes.length).to.equal(1)
89+
expect(result.nodes[0].nodeName).to.equal('A')
90+
})
91+
*/
92+
})

0 commit comments

Comments
 (0)
0