Hash :
f46938e9
Author :
Date :
2021-04-17T22:35:25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
'use strict';
function testFunction(name, object, tester) {
const func = object[name];
object[name] = function () {
tester.apply(this, arguments);
return func.apply(this, arguments);
};
}
/**
* @param {readonly T[]} a1
* @param {readonly T[]} a2
* @returns {boolean}
* @template T
*/
function arrayEqual(a1, a2) {
if (a1.length !== a2.length) {
return false;
}
for (let i = 0, l = a1.length; i < l; i++) {
if (a1[i] !== a2[i]) {
return false;
}
}
return true;
}
/**
* Returns a slice of the first array that doesn't contain the leading and trailing elements the both arrays have in
* common.
*
* Examples:
*
* trimCommon([1,2,3,4], [1,3,2,4]) => [2,3]
* trimCommon([1,2,3,4], [1,2,3,4]) => []
* trimCommon([1,2,0,0,3,4], [1,2,3,4]) => [0,0]
* trimCommon([1,2,3,4], [1,2,0,0,3,4]) => []
*
* @param {readonly T[]} a1
* @param {readonly T[]} a2
* @returns {T[]}
* @template T
*/
function trimCommon(a1, a2) {
let commonBefore = 0;
for (let i = 0; i < a1.length; i++) {
if (a1[i] === a2[i]) {
commonBefore++;
} else {
break;
}
}
a1 = a1.slice(commonBefore);
let commonAfter = 0;
for (let i = 0; i < a1.length; i++) {
if (a1[a1.length - 1 - i] === a2[a2.length - 1 - i]) {
commonAfter++;
} else {
break;
}
}
return a1.slice(0, a1.length - commonAfter);
}
/**
* @param {string[]} a
*/
function joinEnglishList(a) {
if (a.length === 0) {
return '(no elements)';
} else if (a.length === 1) {
return a[0];
} else if (a.length === 2) {
return `${a[0]} and ${a[1]}`;
} else {
return a.slice(0, a.length - 1).join(', ') + ', and ' + a[a.length - 1];
}
}
module.exports = (Prism) => {
// The test for Prism.languages.extend has to be able to tell whether an object is a clone, so we mark it with a
// special property
const oldClone = Prism.util.clone;
Prism.util.clone = obj => {
const clone = oldClone(obj);
if (clone && typeof clone === 'object') {
Object.defineProperty(clone, '__cloned', { value: true });
}
return clone;
};
function extendTest(id, redef) {
let redefStr;
if (Prism.util.type(redef) === 'Object') {
redefStr = '{\n';
for (const key in redef) {
const element = redef[key];
let elementStr;
if (Array.isArray(element)) {
elementStr = `[ ... ${element.length} element(s) ]`;
} else if (Prism.util.type(element) === 'RegExp') {
elementStr = 'RegExp';
} else if (Prism.util.type(element) === 'Object') {
elementStr = '{ ... }';
} else {
elementStr = String(element);
}
redefStr += `\t'${key}': ${elementStr},\n`;
}
redefStr += '}';
} else {
redefStr = String(redef);
}
const details = `\n\nActual method call (nonconforming):` +
`\n\n\tPrism.languages.extend('${id}', ${redefStr.replace(/\n/g, '\n\t')});` +
'\n\nFor more information see: https://prismjs.com/docs/Prism.languages.html#.extend';
// type checks
if (typeof id !== 'string') {
throw new TypeError(`The id argument has to be a 'string'.` + details);
}
if (typeof redef !== 'object') {
throw new TypeError(`The redef argument has to be an 'object'.` + details);
}
// language has to be loaded
if (!(id in Prism.languages)) {
throw new Error(`Cannot extend '${id}' because it is not defined in Prism.languages.`);
}
// rest property check
if ('rest' in redef) {
throw new Error(`The redef object is not allowed to contain a "rest" property.` + details);
}
// redef cannot be a direct reference to a language
let isReference = false;
Prism.languages.DFS(Prism.languages, (key, value) => {
if (value === redef) {
isReference = true;
}
});
if (isReference) {
throw new Error(`The redef object cannot be a reference to an existing language.` +
` Use Prism.util.clone(object) to create a deep clone.` + details);
}
// check the order of properties in redef
if (!redef.__cloned) {
const languageKeys = Object.keys(Prism.languages[id]);
const redefKeys = Object.keys(redef);
const overwriteKeys = redefKeys.filter(k => languageKeys.indexOf(k) !== -1);
const appendKeys = redefKeys.filter(k => languageKeys.indexOf(k) === -1);
if (!arrayEqual(redefKeys, [...overwriteKeys, ...appendKeys])) {
const lastOverwrite = overwriteKeys[overwriteKeys.length - 1];
const lastOverwriteIndex = redefKeys.indexOf(lastOverwrite);
const offenders = appendKeys.filter(k => redefKeys.indexOf(k) < lastOverwriteIndex);
const offendersList = joinEnglishList(offenders.map(k => `'${k}'`));
throw new Error(
`All tokens in the redef object that do not overwrite tokens of the extended language have to be placed after the last token that does overwrite an existing token.` +
` Move the token(s) ${offendersList} after the '${lastOverwrite}' token.` + details);
}
const sortedOverwriteKeys = [...overwriteKeys].sort((a, b) => {
return languageKeys.indexOf(a) - languageKeys.indexOf(b);
});
if (!arrayEqual(overwriteKeys, sortedOverwriteKeys)) {
const trimmedUnsorted = trimCommon(overwriteKeys, sortedOverwriteKeys).map(k => `'${k}'`).join(', ');
const trimmedSorted = trimCommon(sortedOverwriteKeys, overwriteKeys).map(k => `'${k}'`).join(', ');
throw new Error(
'The tokens in the redef object that overwrite existing tokens of the extended language should be in the order of the existing tokens. ' +
`The tokens ${trimmedUnsorted} should be in the order ${trimmedSorted}.` + details);
}
}
}
function insertBeforeTest(inside, before, insert, root) {
const details = `\ninsertBefore("${inside}", "${before}", ${insert}, ${root})`;
// type checks
if (typeof inside !== 'string') {
throw new TypeError(`The inside argument has to be a 'string'.` + details);
}
if (typeof before !== 'string') {
throw new TypeError(`The before argument has to be a 'string'.` + details);
}
if (typeof insert !== 'object') {
throw new TypeError(`The insert argument has to be an 'object'.` + details);
}
if (root && typeof root !== 'object') {
throw new TypeError(`The root argument has to be an 'object' if defined.` + details);
}
root = root || Prism.languages;
let grammar = root[inside];
if (typeof grammar !== 'object') {
throw new Error(`The grammar "${inside}" has to be an 'object' not '${typeof grammar}'.`);
}
if (!(before in grammar)) {
throw new Error(`"${before}" has to be a key of the grammar "${inside}".`);
}
}
testFunction('extend', Prism.languages, extendTest);
testFunction('insertBefore', Prism.languages, insertBeforeTest);
};