@intlify/core 11.4.20 → 11.4.22

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.
package/dist/core.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v11.4.20
2
+ * core v11.4.22
3
3
  * (c) 2026 kazuya kawaguchi
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v11.4.20
2
+ * core v11.4.22
3
3
  * (c) 2026 kazuya kawaguchi
4
4
  * Released under the MIT License.
5
5
  */
@@ -302,6 +302,169 @@ function serializeAttribute(attrName, value, quote, neutralized) {
302
302
  const valueQuote = quote || '';
303
303
  return `${name}=${valueQuote}${sanitizeAttributeValue(attrName, value)}${valueQuote}`;
304
304
  }
305
+ const HTML = 0;
306
+ const SVG = 1;
307
+ const MATHML = 2;
308
+ // Elements that have no end tag, and elements that the HTML parser does not open in the body
309
+ 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)$/;
310
+ // Start tags that end SVG and MathML content (and `<font>` with `color`, `face` or `size`)
311
+ const breakoutElementPattern = /^(?:b|big|blockquote|body|br|center|code|dd|div|dl|dt|em|embed|h[1-6]|head|hr|i|img|li|listing|menu|meta|nobr|ol|p|pre|ruby|s|small|span|strong|strike|sub|sup|table|tt|u|ul|var)$/;
312
+ // Start tags that close an open `<p>`, and the elements that they do not close a `<p>` through
313
+ 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)$/;
314
+ const buttonScopeElementPattern = /^(?:applet|caption|html|table|td|th|marquee|object|template|button)$/;
315
+ const svgIntegrationPattern = /^(?:foreignobject|desc|title)$/;
316
+ // The names of SVG and MathML integration points (except `title`, whose content is text in HTML). Read with the
317
+ // HTML rules, they are unknown HTML elements, and their start tags become text: if the browser closed such an
318
+ // element early (for example, `<button>` of the translation closes the `<button>` that the translation is
319
+ // rendered in), its end tag would close an integration point of the page, and the rest of the page would be read
320
+ // as SVG or MathML
321
+ const integrationNamePattern = /^(?:foreignobject|desc|mi|mo|mn|ms|mtext|annotation-xml)$/;
322
+ const mathmlIntegrationPattern = /^(?:mi|mo|mn|ms|mtext)$/;
323
+ const htmlEncodingPattern = /^(?:text\/html|application\/xhtml\+xml)$/i;
324
+ function asciiLowercase(text) {
325
+ return text.replace(/[A-Z]+/g, upper => upper.toLowerCase());
326
+ }
327
+ // Whether a start tag is read with the rules of SVG and MathML content, rather than with the HTML rules
328
+ function isForeignContext(top, name) {
329
+ return (!!top &&
330
+ top.namespace !== HTML &&
331
+ (!top.integration ||
332
+ (mathmlIntegrationPattern.test(top.name) &&
333
+ (name === 'mglyph' || name === 'malignmark'))) &&
334
+ !(top.name === 'annotation-xml' && name === 'svg'));
335
+ }
336
+ function isBreakout(name, attributes) {
337
+ return (breakoutElementPattern.test(name) ||
338
+ (name === 'font' &&
339
+ (attributes.has('color') ||
340
+ attributes.has('face') ||
341
+ attributes.has('size'))));
342
+ }
343
+ // Pop the elements down to `length`, and return their end tags (innermost first).
344
+ // A `<form>` becomes text instead: in a form of the page, browsers ignore the `<form>`,
345
+ // and `</form>` would close the form of the page
346
+ function closeDownTo(stack, length, parts) {
347
+ let endTags = '';
348
+ for (let index = stack.length - 1; index >= length; index--) {
349
+ const element = stack[index];
350
+ if (element.namespace === HTML && element.name === 'form') {
351
+ parts[element.part] = escapeLessThan(parts[element.part]);
352
+ }
353
+ else {
354
+ endTags += `</${element.name}>`;
355
+ }
356
+ }
357
+ stack.length = length;
358
+ return endTags;
359
+ }
360
+ // Pop SVG and MathML elements down to an HTML element or an integration point, as the tree builder does,
361
+ // and return their end tags
362
+ function closeForeign(stack, parts) {
363
+ let length = stack.length;
364
+ while (length > 0 &&
365
+ stack[length - 1].namespace !== HTML &&
366
+ !stack[length - 1].integration) {
367
+ length--;
368
+ }
369
+ return closeDownTo(stack, length, parts);
370
+ }
371
+ /**
372
+ * Open an element for a start tag, as the tree builder does
373
+ *
374
+ * @returns The end tags to write before the start tag: the SVG and MathML elements that it ends, and a `<p>`
375
+ * of the translation that it closes, with the elements in the `<p>` (so that no formatting element in it is
376
+ * made again after it), or `undefined` when the start tag becomes text
377
+ */
378
+ function openElement(stack, parts, name, attributes, selfClosing) {
379
+ // the start tag is written after the end tags
380
+ const part = parts.length + 1;
381
+ let endTags = '';
382
+ const top = stack[stack.length - 1];
383
+ if (isForeignContext(top, name)) {
384
+ if (!isBreakout(name, attributes)) {
385
+ if (!selfClosing) {
386
+ stack.push({
387
+ name,
388
+ namespace: top.namespace,
389
+ integration: top.namespace === SVG
390
+ ? svgIntegrationPattern.test(name)
391
+ : mathmlIntegrationPattern.test(name) ||
392
+ (name === 'annotation-xml' &&
393
+ htmlEncodingPattern.test((attributes.get('encoding') || '').replace(numericCharacterReferencePattern, decodeNumericCharacterReference))),
394
+ part
395
+ });
396
+ }
397
+ return endTags;
398
+ }
399
+ endTags = closeForeign(stack, parts);
400
+ }
401
+ if (paragraphClosingElementPattern.test(name)) {
402
+ for (let index = stack.length - 1; index >= 0; index--) {
403
+ const element = stack[index];
404
+ if (element.namespace === HTML
405
+ ? buttonScopeElementPattern.test(element.name)
406
+ : element.integration) {
407
+ break;
408
+ }
409
+ if (element.namespace === HTML && element.name === 'p') {
410
+ endTags += closeDownTo(stack, index, parts);
411
+ break;
412
+ }
413
+ }
414
+ }
415
+ if (integrationNamePattern.test(name)) {
416
+ return;
417
+ }
418
+ if (name === 'svg' || name === 'math') {
419
+ if (!selfClosing) {
420
+ stack.push({
421
+ name,
422
+ namespace: name === 'svg' ? SVG : MATHML,
423
+ integration: false,
424
+ part
425
+ });
426
+ }
427
+ }
428
+ else if (!notOpenedElementPattern.test(name)) {
429
+ // HTML elements ignore `/>`
430
+ stack.push({ name, namespace: HTML, integration: false, part });
431
+ }
432
+ return endTags;
433
+ }
434
+ /**
435
+ * Close the element of an end tag: an SVG or MathML element with the same name above the innermost HTML
436
+ * element, or else an HTML element with the same name
437
+ *
438
+ * @returns The end tags to write before the end tag (of the elements in the closed element), or `undefined`
439
+ * when the end tag closes nothing in the translation, and is removed
440
+ */
441
+ function closeElement(stack, parts, name) {
442
+ let endTags = '';
443
+ if (name === 'p' || name === 'br') {
444
+ // they end SVG and MathML content
445
+ endTags = closeForeign(stack, parts);
446
+ }
447
+ let index = stack.length - 1;
448
+ while (index >= 0 &&
449
+ stack[index].namespace !== HTML &&
450
+ stack[index].name !== name) {
451
+ index--;
452
+ }
453
+ if (index >= 0 && stack[index].namespace !== HTML) ;
454
+ else {
455
+ while (index >= 0 &&
456
+ !(stack[index].namespace === HTML && stack[index].name === name)) {
457
+ index--;
458
+ }
459
+ }
460
+ if (index < 0) {
461
+ // browsers read `</br>` as `<br>`, and `</p>` without an open `<p>` as `<p></p>`
462
+ return name === 'br' || name === 'p' ? endTags : undefined;
463
+ }
464
+ endTags += closeDownTo(stack, index + 1, parts);
465
+ stack.length = index;
466
+ return endTags;
467
+ }
305
468
  /**
306
469
  * Read a tag with the rules of the HTML tokenizer.
307
470
  * A start tag is written again with its attributes sanitized, and an end tag is kept as it is
@@ -309,7 +472,8 @@ function serializeAttribute(attrName, value, quote, neutralized) {
309
472
  * @param html - The HTML
310
473
  * @param start - The index of `<`
311
474
  * @param neutralized - What has been neutralized
312
- * @returns The tag, the tag name, and the index after the tag (-1 when the tag is not closed),
475
+ * @returns The tag, the tag name, the index after the tag (-1 when the tag is not closed),
476
+ * whether the start tag is self-closing (`/>`), and the attributes of the start tag,
313
477
  * or `undefined` when `<` does not start a tag
314
478
  */
315
479
  function readTag(html, start, neutralized) {
@@ -320,6 +484,7 @@ function readTag(html, start, neutralized) {
320
484
  }
321
485
  const [, slash, tagName] = tagStart;
322
486
  let tag = tagStart[0];
487
+ const attributes = new Map();
323
488
  let step;
324
489
  tagStepPattern.lastIndex = tagStartPattern.lastIndex;
325
490
  while ((step = tagStepPattern.exec(html))) {
@@ -327,17 +492,27 @@ function readTag(html, start, neutralized) {
327
492
  if (close) {
328
493
  const end = tagStepPattern.lastIndex;
329
494
  // browsers ignore the attributes of end tags
330
- return [slash ? html.slice(start, end) : tag + raw, tagName, end];
495
+ return [
496
+ slash ? html.slice(start, end) : tag + raw,
497
+ tagName,
498
+ end,
499
+ !slash && separator.endsWith('/'),
500
+ attributes
501
+ ];
331
502
  }
332
503
  if (quote && !closingQuote) {
333
504
  break;
334
505
  }
335
506
  if (!slash) {
336
507
  const value = quote ? quotedValue : unquotedValue;
508
+ const name = asciiLowercase(attrName);
509
+ if (!attributes.has(name)) {
510
+ attributes.set(name, value || '');
511
+ }
337
512
  tag += separator + serializeAttribute(attrName, value, quote, neutralized);
338
513
  }
339
514
  }
340
- return [tag, tagName, -1];
515
+ return [tag, tagName, -1, false, attributes];
341
516
  }
342
517
  /**
343
518
  * Find the end tag of an element whose content is text, as the HTML tokenizer does:
@@ -362,57 +537,79 @@ function findTextContentEnd(html, from, tagName) {
362
537
  * where each tag and each attribute starts and ends, and every attribute of every start tag is checked.
363
538
  * What is still open at the end of the string (a tag, a comment, or an element whose content is text,
364
539
  * like `<textarea>`) becomes text: on the server (SSR), the rest of the page follows the translation,
365
- * and it would be read as a part of what is open
540
+ * and it would be read as a part of what is open.
541
+ * For the same reason, the elements are followed as the tree builder of browsers opens and closes them,
542
+ * including SVG and MathML: an end tag that closes no element of the translation is removed, the elements
543
+ * in the element that an end tag closes are closed before it, and the elements still open at the end are
544
+ * closed. A `<form>` that the translation does not close becomes text, because browsers ignore it in a form
545
+ * of the page, and `</form>` would close that form
366
546
  *
367
547
  * @param html - The translated HTML
368
548
  * @returns The sanitized HTML
369
549
  */
370
550
  function sanitizeTranslatedHtml(html) {
371
551
  const neutralized = {};
372
- let result = '';
552
+ const parts = [];
553
+ const stack = [];
373
554
  let index = 0;
374
555
  let lessThan;
375
556
  while ((lessThan = html.indexOf('<', index)) !== -1) {
376
- result += html.slice(index, lessThan);
557
+ parts.push(html.slice(index, lessThan));
377
558
  index = lessThan + 1;
378
559
  const read = readTag(html, lessThan, neutralized);
379
560
  if (read) {
380
- const [tag, tagName, end] = read;
561
+ const [tag, tagName, end, selfClosing, attributes] = read;
381
562
  if (end === -1) {
382
563
  neutralized.unclosed = true;
383
- result += escapeLessThan(html.slice(lessThan));
564
+ parts.push(escapeLessThan(html.slice(lessThan)));
384
565
  index = html.length;
385
566
  break;
386
567
  }
387
568
  index = end;
388
569
  if (tag[1] === '/') {
389
- result += tag;
570
+ const endTags = closeElement(stack, parts, asciiLowercase(tagName));
571
+ if (endTags === undefined) {
572
+ neutralized.closed = true;
573
+ }
574
+ else {
575
+ if (endTags) {
576
+ neutralized.closed = true;
577
+ }
578
+ parts.push(endTags + tag);
579
+ }
390
580
  }
391
581
  else if (dangerousElementPattern.test(tagName)) {
392
582
  // the start tag becomes text
393
583
  neutralized.element = true;
394
- result += escapeLessThan(tag);
395
- }
396
- else if (textContentElementPattern.test(tagName)) {
397
- const contentEnd = findTextContentEnd(html, end, tagName);
398
- if (contentEnd === -1) {
399
- // the start tag becomes text, and the content is read as HTML, as browsers do then
400
- neutralized.unclosed = true;
401
- result += escapeLessThan(tag);
584
+ parts.push(escapeLessThan(tag));
585
+ }
586
+ else {
587
+ let content = '';
588
+ if (textContentElementPattern.test(tagName)) {
589
+ const contentEnd = findTextContentEnd(html, end, tagName);
590
+ if (contentEnd === -1) {
591
+ // the start tag becomes text, and the content is read as HTML, as browsers do then
592
+ neutralized.unclosed = true;
593
+ parts.push(escapeLessThan(tag));
594
+ continue;
595
+ }
596
+ content = escapeLessThan(html.slice(end, contentEnd));
597
+ index = contentEnd;
598
+ }
599
+ const endTags = openElement(stack, parts, asciiLowercase(tagName), attributes, selfClosing);
600
+ if (endTags === undefined) {
601
+ neutralized.closed = true;
602
+ parts.push(escapeLessThan(tag) + content);
402
603
  }
403
604
  else {
404
- result += tag + escapeLessThan(html.slice(end, contentEnd));
405
- index = contentEnd;
605
+ parts.push(endTags, tag + content);
406
606
  }
407
607
  }
408
- else {
409
- result += tag;
410
- }
411
608
  continue;
412
609
  }
413
610
  commentPattern.lastIndex = lessThan;
414
611
  if (commentPattern.test(html)) {
415
- result += html.slice(lessThan, commentPattern.lastIndex);
612
+ parts.push(html.slice(lessThan, commentPattern.lastIndex));
416
613
  index = commentPattern.lastIndex;
417
614
  continue;
418
615
  }
@@ -420,9 +617,21 @@ function sanitizeTranslatedHtml(html) {
420
617
  neutralized.unclosed = true;
421
618
  }
422
619
  // `<!DOCTYPE`, `<![CDATA[`, `<?`, `</` without a tag name, `<` at the end and any other `<` become text
423
- result += '&lt;';
620
+ parts.push('&lt;');
621
+ }
622
+ parts.push(html.slice(index));
623
+ // close the elements that are still open
624
+ for (let open = stack.length - 1; open >= 0; open--) {
625
+ const element = stack[open];
626
+ neutralized.closed = true;
627
+ if (element.namespace === HTML && element.name === 'form') {
628
+ // the browser ignores `<form>` in a form of the page, and `</form>` would close that form
629
+ parts[element.part] = escapeLessThan(parts[element.part]);
630
+ }
631
+ else {
632
+ parts.push(`</${element.name}>`);
633
+ }
424
634
  }
425
- result += html.slice(index);
426
635
  {
427
636
  if (neutralized.eventHandler) {
428
637
  warn('Potentially dangerous event handlers detected in translation. ' +
@@ -440,8 +649,12 @@ function sanitizeTranslatedHtml(html) {
440
649
  warn('Unclosed tag, comment or element (such as <textarea>) detected in translation. ' +
441
650
  'Consider closing it in your translation messages.');
442
651
  }
652
+ if (neutralized.closed) {
653
+ warn('Unclosed or misnested elements detected in translation. ' +
654
+ 'Consider closing every element in the right order in your translation messages.');
655
+ }
443
656
  }
444
- return result;
657
+ return parts.join('');
445
658
  }
446
659
 
447
660
  function createPosition(line, column, offset) {
@@ -2743,7 +2956,7 @@ function getWarnMessage(code, ...args) {
2743
2956
  * Intlify core-base version
2744
2957
  * @internal
2745
2958
  */
2746
- const VERSION = '11.4.20';
2959
+ const VERSION = '11.4.22';
2747
2960
  const NOT_REOSLVED = -1;
2748
2961
  const DEFAULT_LOCALE = 'en-US';
2749
2962
  const MISSING_RESOLVE_VALUE = '';
@@ -3519,7 +3732,7 @@ function translate(context, ...args) {
3519
3732
  return format;
3520
3733
  }
3521
3734
  // evaluate message with context
3522
- const ctxOptions = getMessageContextOptions(context, targetLocale, message, options);
3735
+ const ctxOptions = getMessageContextOptions(context, targetLocale, message, options, escapeParameter);
3523
3736
  const msgContext = createMessageContext(ctxOptions);
3524
3737
  const messaged = evaluateMessage(context, msg, msgContext);
3525
3738
  // if use post translation option, proceed it with handler
@@ -3555,6 +3768,8 @@ function translate(context, ...args) {
3555
3768
  }
3556
3769
  return ret;
3557
3770
  }
3771
+ // values that are not strings, such as arrays and objects, are kept here,
3772
+ // and their text is escaped when they are interpolated (see `resolveProcessor()`)
3558
3773
  function escapeParams(options) {
3559
3774
  // a call can pass both list and named values, so escape each of them
3560
3775
  if (isArray(options.list)) {
@@ -3571,6 +3786,20 @@ function escapeParams(options) {
3571
3786
  options.named = named;
3572
3787
  }
3573
3788
  }
3789
+ // strings have been escaped by `escapeParams()`. other values are turned into text here,
3790
+ // with `JSON.stringify()` for arrays and plain objects and `String()` for the rest, so escape that text
3791
+ function interpolateEscaped(value) {
3792
+ return isString(value) ? value : escapeHtml(toDisplayString(value));
3793
+ }
3794
+ function resolveProcessor(processor, escapeParameter) {
3795
+ // keep a processor that interpolates values by itself, such as the one of `<i18n-t>`, which renders VNodes
3796
+ if (!escapeParameter || isFunction(processor?.interpolate)) {
3797
+ return processor;
3798
+ }
3799
+ return assign({}, processor, {
3800
+ interpolate: interpolateEscaped
3801
+ });
3802
+ }
3574
3803
  function resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn, missingWarn) {
3575
3804
  const { messages, onWarn, messageResolver: resolveValue, localeFallbacker } = context;
3576
3805
  const locales = localeFallbacker(context, fallbackLocale, locale); // eslint-disable-line @typescript-eslint/no-explicit-any
@@ -3810,7 +4039,7 @@ function getSourceForCodeFrame(source) {
3810
4039
  }
3811
4040
  }
3812
4041
  }
3813
- function getMessageContextOptions(context, locale, message, options) {
4042
+ function getMessageContextOptions(context, locale, message, options, escapeParameter) {
3814
4043
  const { modifiers, pluralRules, messageResolver: resolveValue, fallbackLocale, fallbackWarn, missingWarn, fallbackContext } = context;
3815
4044
  const resolveMessage = (key, useLinked) => {
3816
4045
  let val = resolveValue(message, key);
@@ -3844,8 +4073,9 @@ function getMessageContextOptions(context, locale, message, options) {
3844
4073
  pluralRules,
3845
4074
  messages: resolveMessage
3846
4075
  };
3847
- if (context.processor) {
3848
- ctxOptions.processor = context.processor;
4076
+ const processor = resolveProcessor(context.processor, escapeParameter);
4077
+ if (processor) {
4078
+ ctxOptions.processor = processor;
3849
4079
  }
3850
4080
  if (options.list) {
3851
4081
  ctxOptions.list = options.list;