@intlify/core 12.0.0-alpha.11 → 12.0.0-alpha.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @intlify/core v12.0.0-alpha.11
2
+ * @intlify/core v12.0.0-alpha.13
3
3
  * (c) 2016-present kazuya kawaguchi and contributors
4
4
  * @license MIT
5
5
  **/
@@ -223,6 +223,106 @@ var IntlifyCore = (function(exports) {
223
223
  const valueQuote = quote || "";
224
224
  return `${name}=${valueQuote}${sanitizeAttributeValue(attrName, value)}${valueQuote}`;
225
225
  }
226
+ const HTML = 0;
227
+ const SVG = 1;
228
+ const MATHML = 2;
229
+ const notOpenedElementPattern = /^(?:area|base|basefont|bgsound|br|col|embed|frame|hr|image|img|input|keygen|link|meta|param|source|track|wbr|html|head|body|frameset)$/;
230
+ const breakoutElementPattern = /^(?:[bipsu]|big|blockquote|body|br|center|code|dd|div|dl|dt|em|embed|h[1-6]|head|hr|img|li|listing|menu|meta|nobr|ol|pre|ruby|small|span|strong|strike|sub|sup|table|tt|ul|var)$/;
231
+ const paragraphClosingElementPattern = /^(?:address|article|aside|blockquote|center|details|dialog|dir|div|dl|fieldset|figcaption|figure|footer|header|hgroup|main|menu|nav|ol|p|search|section|summary|ul|h[1-6]|pre|listing|form|li|dd|dt|plaintext|table|hr|xmp)$/;
232
+ const buttonScopeElementPattern = /^(?:applet|caption|html|table|td|th|marquee|object|template|button)$/;
233
+ const svgIntegrationPattern = /^(?:foreignobject|desc|title)$/;
234
+ const integrationNamePattern = /^(?:foreignobject|desc|mi|mo|mn|ms|mtext|annotation-xml)$/;
235
+ const mathmlIntegrationPattern = /^(?:mi|mo|mn|ms|mtext)$/;
236
+ const htmlEncodingPattern = /^(?:text\/html|application\/xhtml\+xml)$/i;
237
+ function asciiLowercase(text) {
238
+ return text.replace(/[A-Z]+/g, (upper) => upper.toLowerCase());
239
+ }
240
+ function isForeignContext(top, name) {
241
+ return !!top && top.namespace !== HTML && (!top.integration || mathmlIntegrationPattern.test(top.name) && (name === "mglyph" || name === "malignmark")) && !(top.name === "annotation-xml" && name === "svg");
242
+ }
243
+ function isBreakout(name, attributes) {
244
+ return breakoutElementPattern.test(name) || name === "font" && (attributes.has("color") || attributes.has("face") || attributes.has("size"));
245
+ }
246
+ function closeDownTo(stack, length, parts) {
247
+ let endTags = "";
248
+ for (let index = stack.length - 1; index >= length; index--) {
249
+ const element = stack[index];
250
+ if (element.namespace === HTML && element.name === "form") parts[element.part] = escapeLessThan(parts[element.part]);
251
+ else endTags += `</${element.name}>`;
252
+ }
253
+ stack.length = length;
254
+ return endTags;
255
+ }
256
+ function closeForeign(stack, parts) {
257
+ let length = stack.length;
258
+ while (length > 0 && stack[length - 1].namespace !== HTML && !stack[length - 1].integration) length--;
259
+ return closeDownTo(stack, length, parts);
260
+ }
261
+ /**
262
+ * Open an element for a start tag, as the tree builder does
263
+ *
264
+ * @returns The end tags to write before the start tag: the SVG and MathML elements that it ends, and a `<p>`
265
+ * of the translation that it closes, with the elements in the `<p>` (so that no formatting element in it is
266
+ * made again after it), or `undefined` when the start tag becomes text
267
+ */
268
+ function openElement(stack, parts, name, attributes, selfClosing) {
269
+ const part = parts.length + 1;
270
+ let endTags = "";
271
+ const top = stack[stack.length - 1];
272
+ if (isForeignContext(top, name)) {
273
+ if (!isBreakout(name, attributes)) {
274
+ if (!selfClosing) stack.push({
275
+ name,
276
+ namespace: top.namespace,
277
+ integration: top.namespace === SVG ? svgIntegrationPattern.test(name) : mathmlIntegrationPattern.test(name) || name === "annotation-xml" && htmlEncodingPattern.test((attributes.get("encoding") || "").replace(numericCharacterReferencePattern, decodeNumericCharacterReference)),
278
+ part
279
+ });
280
+ return endTags;
281
+ }
282
+ endTags = closeForeign(stack, parts);
283
+ }
284
+ if (paragraphClosingElementPattern.test(name)) for (let index = stack.length - 1; index >= 0; index--) {
285
+ const element = stack[index];
286
+ if (element.namespace === HTML ? buttonScopeElementPattern.test(element.name) : element.integration) break;
287
+ if (element.namespace === HTML && element.name === "p") {
288
+ endTags += closeDownTo(stack, index, parts);
289
+ break;
290
+ }
291
+ }
292
+ if (integrationNamePattern.test(name)) return;
293
+ if (name === "svg" || name === "math") {
294
+ if (!selfClosing) stack.push({
295
+ name,
296
+ namespace: name === "svg" ? SVG : MATHML,
297
+ integration: false,
298
+ part
299
+ });
300
+ } else if (!notOpenedElementPattern.test(name)) stack.push({
301
+ name,
302
+ namespace: HTML,
303
+ integration: false,
304
+ part
305
+ });
306
+ return endTags;
307
+ }
308
+ /**
309
+ * Close the element of an end tag: an SVG or MathML element with the same name above the innermost HTML
310
+ * element, or else an HTML element with the same name
311
+ *
312
+ * @returns The end tags to write before the end tag (of the elements in the closed element), or `undefined`
313
+ * when the end tag closes nothing in the translation, and is removed
314
+ */
315
+ function closeElement(stack, parts, name) {
316
+ let endTags = "";
317
+ if (name === "p" || name === "br") endTags = closeForeign(stack, parts);
318
+ let index = stack.length - 1;
319
+ while (index >= 0 && stack[index].namespace !== HTML && stack[index].name !== name) index--;
320
+ if (index >= 0 && stack[index].namespace !== HTML) {} else while (index >= 0 && !(stack[index].namespace === HTML && stack[index].name === name)) index--;
321
+ if (index < 0) return name === "br" || name === "p" ? endTags : void 0;
322
+ endTags += closeDownTo(stack, index + 1, parts);
323
+ stack.length = index;
324
+ return endTags;
325
+ }
226
326
  /**
227
327
  * Read a tag with the rules of the HTML tokenizer.
228
328
  * A start tag is written again with its attributes sanitized, and an end tag is kept as it is
@@ -230,7 +330,8 @@ var IntlifyCore = (function(exports) {
230
330
  * @param html - The HTML
231
331
  * @param start - The index of `<`
232
332
  * @param neutralized - What has been neutralized
233
- * @returns The tag, the tag name, and the index after the tag (-1 when the tag is not closed),
333
+ * @returns The tag, the tag name, the index after the tag (-1 when the tag is not closed),
334
+ * whether the start tag is self-closing (`/>`), and the attributes of the start tag,
234
335
  * or `undefined` when `<` does not start a tag
235
336
  */
236
337
  function readTag(html, start, neutralized) {
@@ -239,6 +340,7 @@ var IntlifyCore = (function(exports) {
239
340
  if (!tagStart) return;
240
341
  const [, slash, tagName] = tagStart;
241
342
  let tag = tagStart[0];
343
+ const attributes = /* @__PURE__ */ new Map();
242
344
  let step;
243
345
  tagStepPattern.lastIndex = tagStartPattern.lastIndex;
244
346
  while (step = tagStepPattern.exec(html)) {
@@ -248,16 +350,25 @@ var IntlifyCore = (function(exports) {
248
350
  return [
249
351
  slash ? html.slice(start, end) : tag + raw,
250
352
  tagName,
251
- end
353
+ end,
354
+ !slash && separator.endsWith("/"),
355
+ attributes
252
356
  ];
253
357
  }
254
358
  if (quote && !closingQuote) break;
255
- if (!slash) tag += separator + serializeAttribute(attrName, quote ? quotedValue : unquotedValue, quote, neutralized);
359
+ if (!slash) {
360
+ const value = quote ? quotedValue : unquotedValue;
361
+ const name = asciiLowercase(attrName);
362
+ if (!attributes.has(name)) attributes.set(name, value || "");
363
+ tag += separator + serializeAttribute(attrName, value, quote, neutralized);
364
+ }
256
365
  }
257
366
  return [
258
367
  tag,
259
368
  tagName,
260
- -1
369
+ -1,
370
+ false,
371
+ attributes
261
372
  ];
262
373
  }
263
374
  /**
@@ -281,60 +392,87 @@ var IntlifyCore = (function(exports) {
281
392
  * where each tag and each attribute starts and ends, and every attribute of every start tag is checked.
282
393
  * What is still open at the end of the string (a tag, a comment, or an element whose content is text,
283
394
  * like `<textarea>`) becomes text: on the server (SSR), the rest of the page follows the translation,
284
- * and it would be read as a part of what is open
395
+ * and it would be read as a part of what is open.
396
+ * For the same reason, the elements are followed as the tree builder of browsers opens and closes them,
397
+ * including SVG and MathML: an end tag that closes no element of the translation is removed, the elements
398
+ * in the element that an end tag closes are closed before it, and the elements still open at the end are
399
+ * closed. A `<form>` that the translation does not close becomes text, because browsers ignore it in a form
400
+ * of the page, and `</form>` would close that form
285
401
  *
286
402
  * @param html - The translated HTML
287
403
  * @returns The sanitized HTML
288
404
  */
289
405
  function sanitizeTranslatedHtml(html) {
290
406
  const neutralized = {};
291
- let result = "";
407
+ const parts = [];
408
+ const stack = [];
292
409
  let index = 0;
293
410
  let lessThan;
294
411
  while ((lessThan = html.indexOf("<", index)) !== -1) {
295
- result += html.slice(index, lessThan);
412
+ parts.push(html.slice(index, lessThan));
296
413
  index = lessThan + 1;
297
414
  const read = readTag(html, lessThan, neutralized);
298
415
  if (read) {
299
- const [tag, tagName, end] = read;
416
+ const [tag, tagName, end, selfClosing, attributes] = read;
300
417
  if (end === -1) {
301
418
  neutralized.unclosed = true;
302
- result += escapeLessThan(html.slice(lessThan));
419
+ parts.push(escapeLessThan(html.slice(lessThan)));
303
420
  index = html.length;
304
421
  break;
305
422
  }
306
423
  index = end;
307
- if (tag[1] === "/") result += tag;
308
- else if (dangerousElementPattern.test(tagName)) {
424
+ if (tag[1] === "/") {
425
+ const endTags = closeElement(stack, parts, asciiLowercase(tagName));
426
+ if (endTags === void 0) neutralized.closed = true;
427
+ else {
428
+ if (endTags) neutralized.closed = true;
429
+ parts.push(endTags + tag);
430
+ }
431
+ } else if (dangerousElementPattern.test(tagName)) {
309
432
  neutralized.element = true;
310
- result += escapeLessThan(tag);
311
- } else if (textContentElementPattern.test(tagName)) {
312
- const contentEnd = findTextContentEnd(html, end, tagName);
313
- if (contentEnd === -1) {
314
- neutralized.unclosed = true;
315
- result += escapeLessThan(tag);
316
- } else {
317
- result += tag + escapeLessThan(html.slice(end, contentEnd));
433
+ parts.push(escapeLessThan(tag));
434
+ } else {
435
+ let content = "";
436
+ if (textContentElementPattern.test(tagName)) {
437
+ const contentEnd = findTextContentEnd(html, end, tagName);
438
+ if (contentEnd === -1) {
439
+ neutralized.unclosed = true;
440
+ parts.push(escapeLessThan(tag));
441
+ continue;
442
+ }
443
+ content = escapeLessThan(html.slice(end, contentEnd));
318
444
  index = contentEnd;
319
445
  }
320
- } else result += tag;
446
+ const endTags = openElement(stack, parts, asciiLowercase(tagName), attributes, selfClosing);
447
+ if (endTags === void 0) {
448
+ neutralized.closed = true;
449
+ parts.push(escapeLessThan(tag) + content);
450
+ } else parts.push(endTags, tag + content);
451
+ }
321
452
  continue;
322
453
  }
323
454
  commentPattern.lastIndex = lessThan;
324
455
  if (commentPattern.test(html)) {
325
- result += html.slice(lessThan, commentPattern.lastIndex);
456
+ parts.push(html.slice(lessThan, commentPattern.lastIndex));
326
457
  index = commentPattern.lastIndex;
327
458
  continue;
328
459
  }
329
460
  if (html.startsWith("<!--", lessThan)) neutralized.unclosed = true;
330
- result += "&lt;";
461
+ parts.push("&lt;");
462
+ }
463
+ parts.push(html.slice(index));
464
+ for (let open = stack.length - 1; open >= 0; open--) {
465
+ const element = stack[open];
466
+ neutralized.closed = true;
467
+ if (element.namespace === HTML && element.name === "form") parts[element.part] = escapeLessThan(parts[element.part]);
468
+ else parts.push(`</${element.name}>`);
331
469
  }
332
- result += html.slice(index);
333
470
  if (neutralized.eventHandler) warn("Potentially dangerous event handlers detected in translation. Consider removing onclick, onerror, etc. from your translation messages.");
334
471
  if (neutralized.srcdoc) warn("Potentially dangerous srcdoc attribute detected in translation. Consider removing srcdoc from your translation messages.");
335
472
  if (neutralized.element) warn("Potentially dangerous element (<base>, <animate>, <set>, <script> or <plaintext>) detected in translation. Consider removing it from your translation messages.");
336
473
  if (neutralized.unclosed) warn("Unclosed tag, comment or element (such as <textarea>) detected in translation. Consider closing it in your translation messages.");
337
- return result;
474
+ if (neutralized.closed) warn("Unclosed or misnested elements detected in translation. Consider closing every element in the right order in your translation messages.");
475
+ return parts.join("");
338
476
  }
339
477
  // packages/shared/src/messages.ts
340
478
  const isNotObjectOrIsArray = (val) => !isObject(val) || isArray(val);
@@ -941,7 +1079,7 @@ var IntlifyCore = (function(exports) {
941
1079
  * Intlify core-base version
942
1080
  * @internal
943
1081
  */
944
- const VERSION = "12.0.0-alpha.11";
1082
+ const VERSION = "12.0.0-alpha.13";
945
1083
  const NOT_RESOLVED = -1;
946
1084
  const DEFAULT_LOCALE = "en-US";
947
1085
  const MISSING_RESOLVE_VALUE = "";
@@ -1534,7 +1672,7 @@ var IntlifyCore = (function(exports) {
1534
1672
  };
1535
1673
  const msg = !isMessageFunction(format) ? compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, onError) : format;
1536
1674
  if (occurred) return format;
1537
- const msgContext = createMessageContext(getMessageContextOptions(context, targetLocale, message, options));
1675
+ const msgContext = createMessageContext(getMessageContextOptions(context, targetLocale, message, options, escapeParameter));
1538
1676
  const timeline = startTimelineMeasure(context, "evaluation");
1539
1677
  const messaged = msg(msgContext);
1540
1678
  if (timeline) timeline.emitter.emit("message-evaluation", {
@@ -1559,6 +1697,13 @@ var IntlifyCore = (function(exports) {
1559
1697
  options.named = named;
1560
1698
  }
1561
1699
  }
1700
+ function interpolateEscaped(value) {
1701
+ return isString(value) ? value : escapeHtml(toDisplayString(value));
1702
+ }
1703
+ function resolveProcessor(processor, escapeParameter) {
1704
+ if (!escapeParameter || isFunction(processor?.interpolate)) return processor ?? void 0;
1705
+ return assign({}, processor, { interpolate: interpolateEscaped });
1706
+ }
1562
1707
  function resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn, missingWarn) {
1563
1708
  const { messages, onWarn, messageResolver: resolveValue, localeFallbacker } = context;
1564
1709
  const locales = localeFallbacker(context, fallbackLocale, locale);
@@ -1703,7 +1848,7 @@ var IntlifyCore = (function(exports) {
1703
1848
  if (isString(source)) return source;
1704
1849
  else if (source.loc?.source) return source.loc.source;
1705
1850
  }
1706
- function getMessageContextOptions(context, locale, message, options) {
1851
+ function getMessageContextOptions(context, locale, message, options, escapeParameter) {
1707
1852
  const { modifiers, pluralRules, messageResolver: resolveValue, fallbackLocale, fallbackWarn, missingWarn, fallbackContext } = context;
1708
1853
  const resolveMessage = (key, useLinked) => {
1709
1854
  let val = resolveValue(message, key);
@@ -1726,7 +1871,7 @@ var IntlifyCore = (function(exports) {
1726
1871
  modifiers,
1727
1872
  pluralRules,
1728
1873
  messages: resolveMessage,
1729
- processor: context.processor ?? void 0,
1874
+ processor: resolveProcessor(context.processor, escapeParameter),
1730
1875
  list: options.list ?? void 0,
1731
1876
  named: options.named ?? void 0,
1732
1877
  pluralIndex: isNumber(options.plural) ? options.plural : void 0