-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e8f84a6
commit a394a14
Showing
11 changed files
with
723 additions
and
511 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const { assert } = require('chai'); | ||
const PrismLoader = require('./prism-loader'); | ||
|
||
/** | ||
* @typedef {import("./prism-loader").PrismDOM} PrismDOM | ||
* @typedef {import("./prism-loader").PrismWindow} PrismWindow | ||
*/ | ||
|
||
module.exports = { | ||
/** | ||
* @param {PrismWindow} window | ||
*/ | ||
createUtil(window) { | ||
const { Prism, document } = window; | ||
|
||
const util = { | ||
assert: { | ||
highlight({ language = 'none', code, expected }) { | ||
assert.strictEqual(Prism.highlight(code, Prism.languages[language], language), expected); | ||
}, | ||
highlightElement({ language = 'none', code, expected }) { | ||
const element = document.createElement('CODE'); | ||
element.classList.add('language-' + language); | ||
element.textContent = code; | ||
|
||
Prism.highlightElement(element); | ||
|
||
assert.strictEqual(element.innerHTML, expected); | ||
} | ||
}, | ||
}; | ||
|
||
return util; | ||
}, | ||
|
||
/** | ||
* Creates a Prism DOM instance that will be automatically cleaned up after the given test suite finished. | ||
* | ||
* @param {ReturnType<typeof import('mocha')["suite"]>} suite | ||
* @param {Partial<Record<"languages" | "plugins", string | string[]>>} options | ||
*/ | ||
createScopedPrismDom(suite, options = {}) { | ||
const dom = PrismLoader.createPrismDOM(); | ||
|
||
suite.afterAll(function () { | ||
dom.window.close(); | ||
}); | ||
|
||
if (options.languages) { | ||
dom.loadLanguages(options.languages); | ||
} | ||
if (options.plugins) { | ||
dom.loadPlugins(options.plugins); | ||
} | ||
|
||
return dom; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
const { assert } = require('chai'); | ||
const { createScopedPrismDom } = require('../../helper/prism-dom-util'); | ||
|
||
|
||
class DummyClipboard { | ||
constructor() { | ||
this.text = ''; | ||
} | ||
async readText() { | ||
return this.text; | ||
} | ||
/** @param {string} data */ | ||
writeText(data) { | ||
this.text = data; | ||
return Promise.resolve(); | ||
} | ||
} | ||
|
||
describe('Copy to Clipboard', function () { | ||
const { Prism, document, window } = createScopedPrismDom(this, { | ||
languages: 'javascript', | ||
plugins: 'copy-to-clipboard', | ||
}); | ||
|
||
|
||
it('should work', function () { | ||
const clipboard = new DummyClipboard(); | ||
window.navigator.clipboard = clipboard; | ||
|
||
document.body.innerHTML = `<pre class="language-none"><code>foo</code></pre>`; | ||
Prism.highlightAll(); | ||
|
||
const button = document.querySelector('button'); | ||
assert.notStrictEqual(button, null); | ||
|
||
button.click(); | ||
|
||
assert.strictEqual(clipboard.text, 'foo'); | ||
}); | ||
|
||
it('should copy the current text even after the code block changes its text', function () { | ||
const clipboard = new DummyClipboard(); | ||
window.navigator.clipboard = clipboard; | ||
|
||
document.body.innerHTML = `<pre class="language-none"><code>foo</code></pre>`; | ||
Prism.highlightAll(); | ||
|
||
const button = document.querySelector('button'); | ||
assert.notStrictEqual(button, null); | ||
|
||
button.click(); | ||
|
||
assert.strictEqual(clipboard.text, 'foo'); | ||
|
||
// change text | ||
document.querySelector('code').textContent = 'bar'; | ||
// and click | ||
button.click(); | ||
|
||
assert.strictEqual(clipboard.text, 'bar'); | ||
|
||
// change text | ||
document.querySelector('code').textContent = 'baz'; | ||
Prism.highlightAll(); | ||
// and click | ||
button.click(); | ||
|
||
assert.strictEqual(clipboard.text, 'baz'); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const { createUtil, createScopedPrismDom } = require('../../helper/prism-dom-util'); | ||
|
||
|
||
describe('Custom class', function () { | ||
const { Prism, window } = createScopedPrismDom(this, { | ||
languages: 'javascript', | ||
plugins: 'custom-class' | ||
}); | ||
const util = createUtil(window); | ||
|
||
|
||
it('should set prefix', function () { | ||
Prism.plugins.customClass.prefix('prism-'); | ||
|
||
util.assert.highlight({ | ||
language: 'javascript', | ||
code: `var a = true;`, | ||
expected: `<span class="prism-token prism-keyword">var</span> a <span class="prism-token prism-operator">=</span> <span class="prism-token prism-boolean">true</span><span class="prism-token prism-punctuation">;</span>` | ||
}); | ||
}); | ||
|
||
it('should reset prefix', function () { | ||
Prism.plugins.customClass.prefix(''); | ||
|
||
util.assert.highlight({ | ||
language: 'javascript', | ||
code: `var a = true;`, | ||
expected: `<span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>` | ||
}); | ||
}); | ||
|
||
it('should map class names using a function', function () { | ||
Prism.plugins.customClass.map(function (cls, language) { | ||
return `${language}-${cls}`; | ||
}); | ||
|
||
util.assert.highlight({ | ||
language: 'javascript', | ||
code: `var a = true;`, | ||
expected: `<span class="javascript-token javascript-keyword">var</span> a <span class="javascript-token javascript-operator">=</span> <span class="javascript-token javascript-boolean">true</span><span class="javascript-token javascript-punctuation">;</span>` | ||
}); | ||
}); | ||
|
||
it('should map class names using an object', function () { | ||
Prism.plugins.customClass.map({ | ||
boolean: 'b', | ||
keyword: 'kw', | ||
operator: 'op', | ||
punctuation: 'p' | ||
}); | ||
|
||
util.assert.highlight({ | ||
language: 'javascript', | ||
code: `var a = true;`, | ||
expected: `<span class="token kw">var</span> a <span class="token op">=</span> <span class="token b">true</span><span class="token p">;</span>` | ||
}); | ||
}); | ||
|
||
it('should reset map', function () { | ||
Prism.plugins.customClass.map({}); | ||
|
||
util.assert.highlight({ | ||
language: 'javascript', | ||
code: `var a = true;`, | ||
expected: `<span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>` | ||
}); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const { createUtil, createScopedPrismDom } = require('../../helper/prism-dom-util'); | ||
|
||
|
||
describe('Highlight Keywords', function () { | ||
const { window } = createScopedPrismDom(this, { | ||
languages: 'javascript', | ||
plugins: 'highlight-keywords' | ||
}); | ||
const util = createUtil(window); | ||
|
||
|
||
it('should highlight keywords', function () { | ||
util.assert.highlightElement({ | ||
language: 'javascript', | ||
code: `import * from ''; const foo;`, | ||
expected: `<span class="token keyword keyword-import">import</span> <span class="token operator">*</span> <span class="token keyword keyword-from">from</span> <span class="token string">''</span><span class="token punctuation">;</span> <span class="token keyword keyword-const">const</span> foo<span class="token punctuation">;</span>` | ||
}); | ||
}); | ||
|
||
}); |
Oops, something went wrong.