Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should add a spec loader', async () => {
let called = 0;
let loaded = 0;
Mode.addSpecLoader(async spec => {
called++;
if (spec.mode !== 'bar') {
return false;
}
loaded++;
return true;
}, 42);
CodeMirror.modeInfo.push(fakeMode('bar'));
let spec = await Mode.ensure('bar');
expect(called).to.equal(1);
expect(loaded).to.equal(1);
expect(spec!.name).to.equal('BAR');
spec = await Mode.ensure('python');
expect(called).to.equal(1);
expect(loaded).to.equal(1);
try {
spec = await Mode.ensure('APL');
} catch (err) {
// apparently one cannot use webpack `require` in jest
}
expect(called).to.equal(2);
expect(loaded).to.equal(1);
});
called++;
if (spec.mode !== 'bar') {
return false;
}
loaded++;
return true;
}, 42);
CodeMirror.modeInfo.push(fakeMode('bar'));
let spec = await Mode.ensure('bar');
expect(called).to.equal(1);
expect(loaded).to.equal(1);
expect(spec!.name).to.equal('BAR');
spec = await Mode.ensure('python');
expect(called).to.equal(1);
expect(loaded).to.equal(1);
try {
spec = await Mode.ensure('APL');
} catch (err) {
// apparently one cannot use webpack `require` in jest
}
expect(called).to.equal(2);
expect(loaded).to.equal(1);
});
highlight: (code, lang, callback) => {
let cb = (err: Error | null, code: string) => {
if (callback) {
callback(err, code);
}
return code;
};
if (!lang) {
// no language, no highlight
return cb(null, code);
}
Mode.ensure(lang)
.then(spec => {
let el = document.createElement('div');
if (!spec) {
console.log(`No CodeMirror mode: ${lang}`);
return cb(null, code);
}
try {
Mode.run(code, spec.mime, el);
return cb(null, el.innerHTML);
} catch (err) {
console.log(`Failed to highlight ${lang} code`, err);
return cb(err, code);
}
})
.catch(err => {
console.log(`No CodeMirror mode: ${lang}`);
highlight: (code, lang, callback) => {
if (!lang) {
// no language, no highlight
if (callback) {
callback(null, code);
return;
} else {
return code;
}
}
Mode.ensure(lang).then(spec => {
let el = document.createElement('div');
if (!spec) {
console.log(`No CodeMirror mode: ${lang}`);
callback(null, code);
return;
}
try {
Mode.run(code, spec.mime, el);
callback(null, el.innerHTML);
} catch (err) {
console.log(`Failed to highlight ${lang} code`, err);
callback(err, code);
}
}).catch(err => {
console.log(`No CodeMirror mode: ${lang}`);
console.log(`Require CodeMirror mode error: ${err}`);
highlight: (code, lang, callback) => {
if (!lang) {
// no language, no highlight
if (callback) {
callback(null, code);
return;
} else {
return code;
}
}
Mode.ensure(lang).then(spec => {
let el = document.createElement('div');
if (!spec) {
console.log(`No CodeMirror mode: ${lang}`);
callback(null, code);
return;
}
try {
Mode.run(code, spec.mime, el);
callback(null, el.innerHTML);
} catch (err) {
console.log(`Failed to highlight ${lang} code`, err);
callback(err, code);
}
}).catch(err => {
console.log(`No CodeMirror mode: ${lang}`);
console.log(`Require CodeMirror mode error: ${err}`);
highlight: (code, lang, callback) => {
let cb = (err: Error | null, code: string) => {
if (callback) {
callback(err, code);
}
return code;
};
if (!lang) {
// no language, no highlight
return cb(null, code);
}
Mode.ensure(lang)
.then(spec => {
let el = document.createElement('div');
if (!spec) {
console.log(`No CodeMirror mode: ${lang}`);
return cb(null, code);
}
try {
Mode.run(code, spec.mime, el);
return cb(null, el.innerHTML);
} catch (err) {
console.log(`Failed to highlight ${lang} code`, err);
return cb(err, code);
}
})
.catch(err => {
console.log(`No CodeMirror mode: ${lang}`);
it('should load a defined spec', async () => {
CodeMirror.modeInfo.push(fakeMode('foo'));
CodeMirror.defineMode('foo', () => {
return {};
});
let spec = (await Mode.ensure('text/foo'))!;
expect(spec.name).to.equal('FOO');
});
it('should load a bundled spec', async () => {
let spec = (await Mode.ensure('application/json'))!;
expect(spec.name).to.equal('JSON');
});
Widget.attach(outputArea, theDiv);
const mode = $element.data("language") || "python";
const required = {
value: source,
mode: mode,
extraKeys: {
"Shift-Enter": execute,
},
};
let codeMirrorConfig = Object.assign(
mergedOptions.codeMirrorconfig || {},
required
);
let cm = new CodeMirror($cm_element[0], codeMirrorConfig);
Mode.ensure(mode).then(modeSpec => {
cm.setOption("mode", mode);
});
return $cell;
}