Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds unit tests to the Keep Markup plugin #1646

Merged
merged 1 commit into from
Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know, this change is mandatory otherwise we can't fake self and document in the unit tests.
In a browser environment both self and document are global and will be resolved.

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')
})
*/
})