@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.
package/dist/core.js CHANGED
@@ -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
  **/
package/dist/core.node.js CHANGED
@@ -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
  **/
@@ -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
  **/
@@ -221,6 +221,106 @@ function serializeAttribute(attrName, value, quote, neutralized) {
221
221
  const valueQuote = quote || "";
222
222
  return `${name}=${valueQuote}${sanitizeAttributeValue(attrName, value)}${valueQuote}`;
223
223
  }
224
+ const HTML = 0;
225
+ const SVG = 1;
226
+ const MATHML = 2;
227
+ 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)$/;
228
+ 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)$/;
229
+ 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)$/;
230
+ const buttonScopeElementPattern = /^(?:applet|caption|html|table|td|th|marquee|object|template|button)$/;
231
+ const svgIntegrationPattern = /^(?:foreignobject|desc|title)$/;
232
+ const integrationNamePattern = /^(?:foreignobject|desc|mi|mo|mn|ms|mtext|annotation-xml)$/;
233
+ const mathmlIntegrationPattern = /^(?:mi|mo|mn|ms|mtext)$/;
234
+ const htmlEncodingPattern = /^(?:text\/html|application\/xhtml\+xml)$/i;
235
+ function asciiLowercase(text) {
236
+ return text.replace(/[A-Z]+/g, (upper) => upper.toLowerCase());
237
+ }
238
+ function isForeignContext(top, name) {
239
+ return !!top && top.namespace !== HTML && (!top.integration || mathmlIntegrationPattern.test(top.name) && (name === "mglyph" || name === "malignmark")) && !(top.name === "annotation-xml" && name === "svg");
240
+ }
241
+ function isBreakout(name, attributes) {
242
+ return breakoutElementPattern.test(name) || name === "font" && (attributes.has("color") || attributes.has("face") || attributes.has("size"));
243
+ }
244
+ function closeDownTo(stack, length, parts) {
245
+ let endTags = "";
246
+ for (let index = stack.length - 1; index >= length; index--) {
247
+ const element = stack[index];
248
+ if (element.namespace === HTML && element.name === "form") parts[element.part] = escapeLessThan(parts[element.part]);
249
+ else endTags += `</${element.name}>`;
250
+ }
251
+ stack.length = length;
252
+ return endTags;
253
+ }
254
+ function closeForeign(stack, parts) {
255
+ let length = stack.length;
256
+ while (length > 0 && stack[length - 1].namespace !== HTML && !stack[length - 1].integration) length--;
257
+ return closeDownTo(stack, length, parts);
258
+ }
259
+ /**
260
+ * Open an element for a start tag, as the tree builder does
261
+ *
262
+ * @returns The end tags to write before the start tag: the SVG and MathML elements that it ends, and a `<p>`
263
+ * of the translation that it closes, with the elements in the `<p>` (so that no formatting element in it is
264
+ * made again after it), or `undefined` when the start tag becomes text
265
+ */
266
+ function openElement(stack, parts, name, attributes, selfClosing) {
267
+ const part = parts.length + 1;
268
+ let endTags = "";
269
+ const top = stack[stack.length - 1];
270
+ if (isForeignContext(top, name)) {
271
+ if (!isBreakout(name, attributes)) {
272
+ if (!selfClosing) stack.push({
273
+ name,
274
+ namespace: top.namespace,
275
+ integration: top.namespace === SVG ? svgIntegrationPattern.test(name) : mathmlIntegrationPattern.test(name) || name === "annotation-xml" && htmlEncodingPattern.test((attributes.get("encoding") || "").replace(numericCharacterReferencePattern, decodeNumericCharacterReference)),
276
+ part
277
+ });
278
+ return endTags;
279
+ }
280
+ endTags = closeForeign(stack, parts);
281
+ }
282
+ if (paragraphClosingElementPattern.test(name)) for (let index = stack.length - 1; index >= 0; index--) {
283
+ const element = stack[index];
284
+ if (element.namespace === HTML ? buttonScopeElementPattern.test(element.name) : element.integration) break;
285
+ if (element.namespace === HTML && element.name === "p") {
286
+ endTags += closeDownTo(stack, index, parts);
287
+ break;
288
+ }
289
+ }
290
+ if (integrationNamePattern.test(name)) return;
291
+ if (name === "svg" || name === "math") {
292
+ if (!selfClosing) stack.push({
293
+ name,
294
+ namespace: name === "svg" ? SVG : MATHML,
295
+ integration: false,
296
+ part
297
+ });
298
+ } else if (!notOpenedElementPattern.test(name)) stack.push({
299
+ name,
300
+ namespace: HTML,
301
+ integration: false,
302
+ part
303
+ });
304
+ return endTags;
305
+ }
306
+ /**
307
+ * Close the element of an end tag: an SVG or MathML element with the same name above the innermost HTML
308
+ * element, or else an HTML element with the same name
309
+ *
310
+ * @returns The end tags to write before the end tag (of the elements in the closed element), or `undefined`
311
+ * when the end tag closes nothing in the translation, and is removed
312
+ */
313
+ function closeElement(stack, parts, name) {
314
+ let endTags = "";
315
+ if (name === "p" || name === "br") endTags = closeForeign(stack, parts);
316
+ let index = stack.length - 1;
317
+ while (index >= 0 && stack[index].namespace !== HTML && stack[index].name !== name) index--;
318
+ if (index >= 0 && stack[index].namespace !== HTML) {} else while (index >= 0 && !(stack[index].namespace === HTML && stack[index].name === name)) index--;
319
+ if (index < 0) return name === "br" || name === "p" ? endTags : void 0;
320
+ endTags += closeDownTo(stack, index + 1, parts);
321
+ stack.length = index;
322
+ return endTags;
323
+ }
224
324
  /**
225
325
  * Read a tag with the rules of the HTML tokenizer.
226
326
  * A start tag is written again with its attributes sanitized, and an end tag is kept as it is
@@ -228,7 +328,8 @@ function serializeAttribute(attrName, value, quote, neutralized) {
228
328
  * @param html - The HTML
229
329
  * @param start - The index of `<`
230
330
  * @param neutralized - What has been neutralized
231
- * @returns The tag, the tag name, and the index after the tag (-1 when the tag is not closed),
331
+ * @returns The tag, the tag name, the index after the tag (-1 when the tag is not closed),
332
+ * whether the start tag is self-closing (`/>`), and the attributes of the start tag,
232
333
  * or `undefined` when `<` does not start a tag
233
334
  */
234
335
  function readTag(html, start, neutralized) {
@@ -237,6 +338,7 @@ function readTag(html, start, neutralized) {
237
338
  if (!tagStart) return;
238
339
  const [, slash, tagName] = tagStart;
239
340
  let tag = tagStart[0];
341
+ const attributes = /* @__PURE__ */ new Map();
240
342
  let step;
241
343
  tagStepPattern.lastIndex = tagStartPattern.lastIndex;
242
344
  while (step = tagStepPattern.exec(html)) {
@@ -246,16 +348,25 @@ function readTag(html, start, neutralized) {
246
348
  return [
247
349
  slash ? html.slice(start, end) : tag + raw,
248
350
  tagName,
249
- end
351
+ end,
352
+ !slash && separator.endsWith("/"),
353
+ attributes
250
354
  ];
251
355
  }
252
356
  if (quote && !closingQuote) break;
253
- if (!slash) tag += separator + serializeAttribute(attrName, quote ? quotedValue : unquotedValue, quote, neutralized);
357
+ if (!slash) {
358
+ const value = quote ? quotedValue : unquotedValue;
359
+ const name = asciiLowercase(attrName);
360
+ if (!attributes.has(name)) attributes.set(name, value || "");
361
+ tag += separator + serializeAttribute(attrName, value, quote, neutralized);
362
+ }
254
363
  }
255
364
  return [
256
365
  tag,
257
366
  tagName,
258
- -1
367
+ -1,
368
+ false,
369
+ attributes
259
370
  ];
260
371
  }
261
372
  /**
@@ -279,60 +390,87 @@ function findTextContentEnd(html, from, tagName) {
279
390
  * where each tag and each attribute starts and ends, and every attribute of every start tag is checked.
280
391
  * What is still open at the end of the string (a tag, a comment, or an element whose content is text,
281
392
  * like `<textarea>`) becomes text: on the server (SSR), the rest of the page follows the translation,
282
- * and it would be read as a part of what is open
393
+ * and it would be read as a part of what is open.
394
+ * For the same reason, the elements are followed as the tree builder of browsers opens and closes them,
395
+ * including SVG and MathML: an end tag that closes no element of the translation is removed, the elements
396
+ * in the element that an end tag closes are closed before it, and the elements still open at the end are
397
+ * closed. A `<form>` that the translation does not close becomes text, because browsers ignore it in a form
398
+ * of the page, and `</form>` would close that form
283
399
  *
284
400
  * @param html - The translated HTML
285
401
  * @returns The sanitized HTML
286
402
  */
287
403
  function sanitizeTranslatedHtml(html) {
288
404
  const neutralized = {};
289
- let result = "";
405
+ const parts = [];
406
+ const stack = [];
290
407
  let index = 0;
291
408
  let lessThan;
292
409
  while ((lessThan = html.indexOf("<", index)) !== -1) {
293
- result += html.slice(index, lessThan);
410
+ parts.push(html.slice(index, lessThan));
294
411
  index = lessThan + 1;
295
412
  const read = readTag(html, lessThan, neutralized);
296
413
  if (read) {
297
- const [tag, tagName, end] = read;
414
+ const [tag, tagName, end, selfClosing, attributes] = read;
298
415
  if (end === -1) {
299
416
  neutralized.unclosed = true;
300
- result += escapeLessThan(html.slice(lessThan));
417
+ parts.push(escapeLessThan(html.slice(lessThan)));
301
418
  index = html.length;
302
419
  break;
303
420
  }
304
421
  index = end;
305
- if (tag[1] === "/") result += tag;
306
- else if (dangerousElementPattern.test(tagName)) {
422
+ if (tag[1] === "/") {
423
+ const endTags = closeElement(stack, parts, asciiLowercase(tagName));
424
+ if (endTags === void 0) neutralized.closed = true;
425
+ else {
426
+ if (endTags) neutralized.closed = true;
427
+ parts.push(endTags + tag);
428
+ }
429
+ } else if (dangerousElementPattern.test(tagName)) {
307
430
  neutralized.element = true;
308
- result += escapeLessThan(tag);
309
- } else if (textContentElementPattern.test(tagName)) {
310
- const contentEnd = findTextContentEnd(html, end, tagName);
311
- if (contentEnd === -1) {
312
- neutralized.unclosed = true;
313
- result += escapeLessThan(tag);
314
- } else {
315
- result += tag + escapeLessThan(html.slice(end, contentEnd));
431
+ parts.push(escapeLessThan(tag));
432
+ } else {
433
+ let content = "";
434
+ if (textContentElementPattern.test(tagName)) {
435
+ const contentEnd = findTextContentEnd(html, end, tagName);
436
+ if (contentEnd === -1) {
437
+ neutralized.unclosed = true;
438
+ parts.push(escapeLessThan(tag));
439
+ continue;
440
+ }
441
+ content = escapeLessThan(html.slice(end, contentEnd));
316
442
  index = contentEnd;
317
443
  }
318
- } else result += tag;
444
+ const endTags = openElement(stack, parts, asciiLowercase(tagName), attributes, selfClosing);
445
+ if (endTags === void 0) {
446
+ neutralized.closed = true;
447
+ parts.push(escapeLessThan(tag) + content);
448
+ } else parts.push(endTags, tag + content);
449
+ }
319
450
  continue;
320
451
  }
321
452
  commentPattern.lastIndex = lessThan;
322
453
  if (commentPattern.test(html)) {
323
- result += html.slice(lessThan, commentPattern.lastIndex);
454
+ parts.push(html.slice(lessThan, commentPattern.lastIndex));
324
455
  index = commentPattern.lastIndex;
325
456
  continue;
326
457
  }
327
458
  if (html.startsWith("<!--", lessThan)) neutralized.unclosed = true;
328
- result += "&lt;";
459
+ parts.push("&lt;");
460
+ }
461
+ parts.push(html.slice(index));
462
+ for (let open = stack.length - 1; open >= 0; open--) {
463
+ const element = stack[open];
464
+ neutralized.closed = true;
465
+ if (element.namespace === HTML && element.name === "form") parts[element.part] = escapeLessThan(parts[element.part]);
466
+ else parts.push(`</${element.name}>`);
329
467
  }
330
- result += html.slice(index);
331
468
  if (neutralized.eventHandler) warn("Potentially dangerous event handlers detected in translation. Consider removing onclick, onerror, etc. from your translation messages.");
332
469
  if (neutralized.srcdoc) warn("Potentially dangerous srcdoc attribute detected in translation. Consider removing srcdoc from your translation messages.");
333
470
  if (neutralized.element) warn("Potentially dangerous element (<base>, <animate>, <set>, <script> or <plaintext>) detected in translation. Consider removing it from your translation messages.");
334
471
  if (neutralized.unclosed) warn("Unclosed tag, comment or element (such as <textarea>) detected in translation. Consider closing it in your translation messages.");
335
- return result;
472
+ if (neutralized.closed) warn("Unclosed or misnested elements detected in translation. Consider closing every element in the right order in your translation messages.");
473
+ return parts.join("");
336
474
  }
337
475
  // packages/shared/src/messages.ts
338
476
  const isNotObjectOrIsArray = (val) => !isObject(val) || isArray(val);
@@ -939,7 +1077,7 @@ function getWarnMessage(code, ...args) {
939
1077
  * Intlify core-base version
940
1078
  * @internal
941
1079
  */
942
- const VERSION = "12.0.0-alpha.11";
1080
+ const VERSION = "12.0.0-alpha.13";
943
1081
  const NOT_RESOLVED = -1;
944
1082
  const DEFAULT_LOCALE = "en-US";
945
1083
  const MISSING_RESOLVE_VALUE = "";
@@ -1532,7 +1670,7 @@ function translate(context, ...args) {
1532
1670
  };
1533
1671
  const msg = !isMessageFunction(format) ? compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, onError) : format;
1534
1672
  if (occurred) return format;
1535
- const msgContext = createMessageContext(getMessageContextOptions(context, targetLocale, message, options));
1673
+ const msgContext = createMessageContext(getMessageContextOptions(context, targetLocale, message, options, escapeParameter));
1536
1674
  const timeline = startTimelineMeasure(context, "evaluation");
1537
1675
  const messaged = msg(msgContext);
1538
1676
  if (timeline) timeline.emitter.emit("message-evaluation", {
@@ -1557,6 +1695,13 @@ function escapeParams(options) {
1557
1695
  options.named = named;
1558
1696
  }
1559
1697
  }
1698
+ function interpolateEscaped(value) {
1699
+ return isString(value) ? value : escapeHtml(toDisplayString(value));
1700
+ }
1701
+ function resolveProcessor(processor, escapeParameter) {
1702
+ if (!escapeParameter || isFunction(processor?.interpolate)) return processor ?? void 0;
1703
+ return assign({}, processor, { interpolate: interpolateEscaped });
1704
+ }
1560
1705
  function resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn, missingWarn) {
1561
1706
  const { messages, onWarn, messageResolver: resolveValue, localeFallbacker } = context;
1562
1707
  const locales = localeFallbacker(context, fallbackLocale, locale);
@@ -1701,7 +1846,7 @@ function getSourceForCodeFrame(source) {
1701
1846
  if (isString(source)) return source;
1702
1847
  else if (source.loc?.source) return source.loc.source;
1703
1848
  }
1704
- function getMessageContextOptions(context, locale, message, options) {
1849
+ function getMessageContextOptions(context, locale, message, options, escapeParameter) {
1705
1850
  const { modifiers, pluralRules, messageResolver: resolveValue, fallbackLocale, fallbackWarn, missingWarn, fallbackContext } = context;
1706
1851
  const resolveMessage = (key, useLinked) => {
1707
1852
  let val = resolveValue(message, key);
@@ -1724,7 +1869,7 @@ function getMessageContextOptions(context, locale, message, options) {
1724
1869
  modifiers,
1725
1870
  pluralRules,
1726
1871
  messages: resolveMessage,
1727
- processor: context.processor ?? void 0,
1872
+ processor: resolveProcessor(context.processor, escapeParameter),
1728
1873
  list: options.list ?? void 0,
1729
1874
  named: options.named ?? void 0,
1730
1875
  pluralIndex: isNumber(options.plural) ? options.plural : void 0