Skip to content

Commit

Permalink
Add unit tests to the Keep Markup plugin (#1646)
Browse files Browse the repository at this point in the history
Uses JSDOM to simulate the DOM in node.
  • Loading branch information
ggrossetie authored and mAAdhaTTah committed Mar 11, 2019
1 parent 439ea1e commit a944c41
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 4 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "prism.js",
"style": "themes/prism.css",
"scripts": {
"test": "mocha tests/testrunner-tests.js && mocha tests/run.js"
"test": "mocha tests/testrunner-tests.js && mocha tests/run.js && mocha tests/plugins/**/*.js"
},
"repository": {
"type": "git",
Expand All @@ -29,6 +29,7 @@
"gulp-rename": "^1.2.0",
"gulp-replace": "^1.0.0",
"gulp-uglify": "^3.0.1",
"jsdom": "^13.0.0",
"mocha": "^6.0.0",
"pump": "^3.0.0",
"yargs": "^13.2.2"
Expand Down
4 changes: 2 additions & 2 deletions plugins/keep-markup/prism-keep-markup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(function () {
(function (self, document) {

if (typeof self === 'undefined' || !self.Prism || !self.document || !document.createRange) {
return;
Expand Down Expand Up @@ -96,4 +96,4 @@
env.highlightedCode = env.element.innerHTML;
}
});
}());
}(self, document));
2 changes: 1 addition & 1 deletion plugins/keep-markup/prism-keep-markup.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 92 additions & 0 deletions tests/plugins/keep-markup/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const expect = require('chai').expect;
const jsdom = require('jsdom')
const { JSDOM } = jsdom

require('../../../prism')
// fake DOM
global.self = {}
global.self.Prism = Prism
global.document = {}
document.createRange = function () {
}
global.self.document = document

require('../../../plugins/keep-markup/prism-keep-markup')

describe('Prism Keep Markup Plugin', function () {

function execute (code) {
const start = [];
const end = [];
const nodes = [];
document.createRange = function () {
return {
setStart: function (node, offset) {
start.push({ node, offset })
},
setEnd: function (node, offset) {
end.push({ node, offset })
},
extractContents: function () {
return new JSDOM('').window.document.createTextNode('')
},
insertNode: function (node) {
nodes.push(node)
},
detach: function () {
}
}
}
const beforeHighlight = Prism.hooks.all['before-highlight'][0]
const afterHighlight = Prism.hooks.all['after-highlight'][0]
const env = {
element: new JSDOM(code).window.document.getElementsByTagName('code')[0],
language: "javascript"
}
beforeHighlight(env)
afterHighlight(env)
return { start, end, nodes }
}

it('should keep <span> markup', function () {
const result = execute(`<code class="language-javascript">x<span>a</span>y</code>`)
expect(result.start.length).to.equal(1)
expect(result.end.length).to.equal(1)
expect(result.nodes.length).to.equal(1)
expect(result.nodes[0].nodeName).to.equal('SPAN')
})
it('should preserve markup order', function () {
const result = execute(`<code class="language-javascript">x<a></a><b></b>y</code>`)
expect(result.start.length).to.equal(2)
expect(result.start[0].offset).to.equal(0)
expect(result.start[0].node.textContent).to.equal('y')
expect(result.start[1].offset).to.equal(0)
expect(result.start[1].node.textContent).to.equal('y')
expect(result.end.length).to.equal(2)
expect(result.end[0].offset).to.equal(0)
expect(result.end[0].node.textContent).to.equal('y')
expect(result.end[1].offset).to.equal(0)
expect(result.end[1].node.textContent).to.equal('y')
expect(result.nodes.length).to.equal(2)
expect(result.nodes[0].nodeName).to.equal('A')
expect(result.nodes[1].nodeName).to.equal('B')
})
it('should keep last <span> markup', function () {
const result = execute(`<code class="language-javascript">xy<span>a</span></code>`)
expect(result.start.length).to.equal(1)
expect(result.end.length).to.equal(1)
expect(result.nodes.length).to.equal(1)
expect(result.nodes[0].nodeName).to.equal('SPAN')
})
// 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)...
// https://github.com/PrismJS/prism/issues/1618
/*
it('should keep last single letter empty markup', function () {
const result = execute(`<code class="language-javascript">xy<a></a></code>`)
expect(result.start.length).to.equal(1)
expect(result.end.length).to.equal(1)
expect(result.nodes.length).to.equal(1)
expect(result.nodes[0].nodeName).to.equal('A')
})
*/
})

0 comments on commit a944c41

Please sign in to comment.