8d2f7f8f1396c4fb61ad1d9521c16a29ba04969b3861eb9d6557063b6e0036d78a88036ac695ec28cb3f64118f344f10711c733ff9769b0594ff6627bd236b 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*!
  2. * shared v9.14.5
  3. * (c) 2025 kazuya kawaguchi
  4. * Released under the MIT License.
  5. */
  6. 'use strict';
  7. function warn(msg, err) {
  8. if (typeof console !== 'undefined') {
  9. console.warn(`[intlify] ` + msg);
  10. /* istanbul ignore if */
  11. if (err) {
  12. console.warn(err.stack);
  13. }
  14. }
  15. }
  16. const hasWarned = {};
  17. function warnOnce(msg) {
  18. if (!hasWarned[msg]) {
  19. hasWarned[msg] = true;
  20. warn(msg);
  21. }
  22. }
  23. /**
  24. * Original Utilities
  25. * written by kazuya kawaguchi
  26. */
  27. const inBrowser = typeof window !== 'undefined';
  28. exports.mark = void 0;
  29. exports.measure = void 0;
  30. {
  31. const perf = inBrowser && window.performance;
  32. if (perf &&
  33. perf.mark &&
  34. perf.measure &&
  35. perf.clearMarks &&
  36. // @ts-ignore browser compat
  37. perf.clearMeasures) {
  38. exports.mark = (tag) => {
  39. perf.mark(tag);
  40. };
  41. exports.measure = (name, startTag, endTag) => {
  42. perf.measure(name, startTag, endTag);
  43. perf.clearMarks(startTag);
  44. perf.clearMarks(endTag);
  45. };
  46. }
  47. }
  48. const RE_ARGS = /\{([0-9a-zA-Z]+)\}/g;
  49. /* eslint-disable */
  50. function format(message, ...args) {
  51. if (args.length === 1 && isObject(args[0])) {
  52. args = args[0];
  53. }
  54. if (!args || !args.hasOwnProperty) {
  55. args = {};
  56. }
  57. return message.replace(RE_ARGS, (match, identifier) => {
  58. return args.hasOwnProperty(identifier) ? args[identifier] : '';
  59. });
  60. }
  61. const makeSymbol = (name, shareable = false) => !shareable ? Symbol(name) : Symbol.for(name);
  62. const generateFormatCacheKey = (locale, key, source) => friendlyJSONstringify({ l: locale, k: key, s: source });
  63. const friendlyJSONstringify = (json) => JSON.stringify(json)
  64. .replace(/\u2028/g, '\\u2028')
  65. .replace(/\u2029/g, '\\u2029')
  66. .replace(/\u0027/g, '\\u0027');
  67. const isNumber = (val) => typeof val === 'number' && isFinite(val);
  68. const isDate = (val) => toTypeString(val) === '[object Date]';
  69. const isRegExp = (val) => toTypeString(val) === '[object RegExp]';
  70. const isEmptyObject = (val) => isPlainObject(val) && Object.keys(val).length === 0;
  71. const assign = Object.assign;
  72. const _create = Object.create;
  73. const create = (obj = null) => _create(obj);
  74. let _globalThis;
  75. const getGlobalThis = () => {
  76. // prettier-ignore
  77. return (_globalThis ||
  78. (_globalThis =
  79. typeof globalThis !== 'undefined'
  80. ? globalThis
  81. : typeof self !== 'undefined'
  82. ? self
  83. : typeof window !== 'undefined'
  84. ? window
  85. : typeof global !== 'undefined'
  86. ? global
  87. : create()));
  88. };
  89. function escapeHtml(rawText) {
  90. return rawText
  91. .replace(/&/g, '&') // escape `&` first to avoid double escaping
  92. .replace(/</g, '&lt;')
  93. .replace(/>/g, '&gt;')
  94. .replace(/"/g, '&quot;')
  95. .replace(/'/g, '&apos;')
  96. .replace(/\//g, '&#x2F;') // escape `/` to prevent closing tags or JavaScript URLs
  97. .replace(/=/g, '&#x3D;'); // escape `=` to prevent attribute injection
  98. }
  99. function escapeAttributeValue(value) {
  100. return value
  101. .replace(/&(?![a-zA-Z0-9#]{2,6};)/g, '&amp;') // escape unescaped `&`
  102. .replace(/"/g, '&quot;')
  103. .replace(/'/g, '&apos;')
  104. .replace(/</g, '&lt;')
  105. .replace(/>/g, '&gt;');
  106. }
  107. function sanitizeTranslatedHtml(html) {
  108. // Escape dangerous characters in attribute values
  109. // Process attributes with double quotes
  110. html = html.replace(/(\w+)\s*=\s*"([^"]*)"/g, (_, attrName, attrValue) => `${attrName}="${escapeAttributeValue(attrValue)}"`);
  111. // Process attributes with single quotes
  112. html = html.replace(/(\w+)\s*=\s*'([^']*)'/g, (_, attrName, attrValue) => `${attrName}='${escapeAttributeValue(attrValue)}'`);
  113. // Detect and neutralize event handler attributes
  114. const eventHandlerPattern = /\s*on\w+\s*=\s*["']?[^"'>]+["']?/gi;
  115. if (eventHandlerPattern.test(html)) {
  116. {
  117. warn('Potentially dangerous event handlers detected in translation. ' +
  118. 'Consider removing onclick, onerror, etc. from your translation messages.');
  119. }
  120. // Neutralize event handler attributes by escaping 'on'
  121. html = html.replace(/(\s+)(on)(\w+\s*=)/gi, '$1&#111;n$3');
  122. }
  123. // Disable javascript: URLs in various contexts
  124. const javascriptUrlPattern = [
  125. // In href, src, action, formaction attributes
  126. /(\s+(?:href|src|action|formaction)\s*=\s*["']?)\s*javascript:/gi,
  127. // In style attributes within url()
  128. /(style\s*=\s*["'][^"']*url\s*\(\s*)javascript:/gi
  129. ];
  130. javascriptUrlPattern.forEach(pattern => {
  131. html = html.replace(pattern, '$1javascript&#58;');
  132. });
  133. return html;
  134. }
  135. const hasOwnProperty = Object.prototype.hasOwnProperty;
  136. function hasOwn(obj, key) {
  137. return hasOwnProperty.call(obj, key);
  138. }
  139. /* eslint-enable */
  140. /**
  141. * Useful Utilities By Evan you
  142. * Modified by kazuya kawaguchi
  143. * MIT License
  144. * https://github.com/vuejs/vue-next/blob/master/packages/shared/src/index.ts
  145. * https://github.com/vuejs/vue-next/blob/master/packages/shared/src/codeframe.ts
  146. */
  147. const isArray = Array.isArray;
  148. const isFunction = (val) => typeof val === 'function';
  149. const isString = (val) => typeof val === 'string';
  150. const isBoolean = (val) => typeof val === 'boolean';
  151. const isSymbol = (val) => typeof val === 'symbol';
  152. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  153. const isObject = (val) => val !== null && typeof val === 'object';
  154. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  155. const isPromise = (val) => {
  156. return isObject(val) && isFunction(val.then) && isFunction(val.catch);
  157. };
  158. const objectToString = Object.prototype.toString;
  159. const toTypeString = (value) => objectToString.call(value);
  160. const isPlainObject = (val) => {
  161. if (!isObject(val))
  162. return false;
  163. const proto = Object.getPrototypeOf(val);
  164. return proto === null || proto.constructor === Object;
  165. };
  166. // for converting list and named values to displayed strings.
  167. const toDisplayString = (val) => {
  168. return val == null
  169. ? ''
  170. : isArray(val) || (isPlainObject(val) && val.toString === objectToString)
  171. ? JSON.stringify(val, null, 2)
  172. : String(val);
  173. };
  174. function join(items, separator = '') {
  175. return items.reduce((str, item, index) => (index === 0 ? str + item : str + separator + item), '');
  176. }
  177. const RANGE = 2;
  178. function generateCodeFrame(source, start = 0, end = source.length) {
  179. const lines = source.split(/\r?\n/);
  180. let count = 0;
  181. const res = [];
  182. for (let i = 0; i < lines.length; i++) {
  183. count += lines[i].length + 1;
  184. if (count >= start) {
  185. for (let j = i - RANGE; j <= i + RANGE || end > count; j++) {
  186. if (j < 0 || j >= lines.length)
  187. continue;
  188. const line = j + 1;
  189. res.push(`${line}${' '.repeat(3 - String(line).length)}| ${lines[j]}`);
  190. const lineLength = lines[j].length;
  191. if (j === i) {
  192. // push underline
  193. const pad = start - (count - lineLength) + 1;
  194. const length = Math.max(1, end > count ? lineLength - pad : end - start);
  195. res.push(` | ` + ' '.repeat(pad) + '^'.repeat(length));
  196. }
  197. else if (j > i) {
  198. if (end > count) {
  199. const length = Math.max(Math.min(end - count, lineLength), 1);
  200. res.push(` | ` + '^'.repeat(length));
  201. }
  202. count += lineLength + 1;
  203. }
  204. }
  205. break;
  206. }
  207. }
  208. return res.join('\n');
  209. }
  210. function incrementer(code) {
  211. let current = code;
  212. return () => ++current;
  213. }
  214. /**
  215. * Event emitter, forked from the below:
  216. * - original repository url: https://github.com/developit/mitt
  217. * - code url: https://github.com/developit/mitt/blob/master/src/index.ts
  218. * - author: Jason Miller (https://github.com/developit)
  219. * - license: MIT
  220. */
  221. /**
  222. * Create a event emitter
  223. *
  224. * @returns An event emitter
  225. */
  226. function createEmitter() {
  227. const events = new Map();
  228. const emitter = {
  229. events,
  230. on(event, handler) {
  231. const handlers = events.get(event);
  232. const added = handlers && handlers.push(handler);
  233. if (!added) {
  234. events.set(event, [handler]);
  235. }
  236. },
  237. off(event, handler) {
  238. const handlers = events.get(event);
  239. if (handlers) {
  240. handlers.splice(handlers.indexOf(handler) >>> 0, 1);
  241. }
  242. },
  243. emit(event, payload) {
  244. (events.get(event) || [])
  245. .slice()
  246. .map(handler => handler(payload));
  247. (events.get('*') || [])
  248. .slice()
  249. .map(handler => handler(event, payload));
  250. }
  251. };
  252. return emitter;
  253. }
  254. const isNotObjectOrIsArray = (val) => !isObject(val) || isArray(val);
  255. // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
  256. function deepCopy(src, des) {
  257. // src and des should both be objects, and none of them can be a array
  258. if (isNotObjectOrIsArray(src) || isNotObjectOrIsArray(des)) {
  259. throw new Error('Invalid value');
  260. }
  261. const stack = [{ src, des }];
  262. while (stack.length) {
  263. const { src, des } = stack.pop();
  264. // using `Object.keys` which skips prototype properties
  265. Object.keys(src).forEach(key => {
  266. if (key === '__proto__') {
  267. return;
  268. }
  269. // if src[key] is an object/array, set des[key]
  270. // to empty object/array to prevent setting by reference
  271. if (isObject(src[key]) && !isObject(des[key])) {
  272. des[key] = Array.isArray(src[key]) ? [] : create();
  273. }
  274. if (isNotObjectOrIsArray(des[key]) || isNotObjectOrIsArray(src[key])) {
  275. // replace with src[key] when:
  276. // src[key] or des[key] is not an object, or
  277. // src[key] or des[key] is an array
  278. des[key] = src[key];
  279. }
  280. else {
  281. // src[key] and des[key] are both objects, merge them
  282. stack.push({ src: src[key], des: des[key] });
  283. }
  284. });
  285. }
  286. }
  287. exports.assign = assign;
  288. exports.create = create;
  289. exports.createEmitter = createEmitter;
  290. exports.deepCopy = deepCopy;
  291. exports.escapeHtml = escapeHtml;
  292. exports.format = format;
  293. exports.friendlyJSONstringify = friendlyJSONstringify;
  294. exports.generateCodeFrame = generateCodeFrame;
  295. exports.generateFormatCacheKey = generateFormatCacheKey;
  296. exports.getGlobalThis = getGlobalThis;
  297. exports.hasOwn = hasOwn;
  298. exports.inBrowser = inBrowser;
  299. exports.incrementer = incrementer;
  300. exports.isArray = isArray;
  301. exports.isBoolean = isBoolean;
  302. exports.isDate = isDate;
  303. exports.isEmptyObject = isEmptyObject;
  304. exports.isFunction = isFunction;
  305. exports.isNumber = isNumber;
  306. exports.isObject = isObject;
  307. exports.isPlainObject = isPlainObject;
  308. exports.isPromise = isPromise;
  309. exports.isRegExp = isRegExp;
  310. exports.isString = isString;
  311. exports.isSymbol = isSymbol;
  312. exports.join = join;
  313. exports.makeSymbol = makeSymbol;
  314. exports.objectToString = objectToString;
  315. exports.sanitizeTranslatedHtml = sanitizeTranslatedHtml;
  316. exports.toDisplayString = toDisplayString;
  317. exports.toTypeString = toTypeString;
  318. exports.warn = warn;
  319. exports.warnOnce = warnOnce;