react-i18next 17.0.10 → 17.0.11

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/react-i18next.js CHANGED
@@ -2245,127 +2245,318 @@
2245
2245
  instance.loadNamespaces;
2246
2246
  instance.loadLanguages;
2247
2247
 
2248
- function getDefaultExportFromCjs (x) {
2249
- return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
2248
+ const voidElements = {
2249
+ area: true,
2250
+ base: true,
2251
+ br: true,
2252
+ col: true,
2253
+ embed: true,
2254
+ hr: true,
2255
+ img: true,
2256
+ input: true,
2257
+ link: true,
2258
+ meta: true,
2259
+ param: true,
2260
+ source: true,
2261
+ track: true,
2262
+ wbr: true,
2263
+ '!doctype': true,
2264
+ '!DOCTYPE': true
2265
+ };
2266
+ const attrRE = /\s([^'"/\s><]+?)[\s/>]|([^\s=]+)=\s?("[^"]*"|'[^']*')/g;
2267
+ function parseTag(tag) {
2268
+ const res = {
2269
+ type: 'tag',
2270
+ name: '',
2271
+ voidElement: false,
2272
+ attrs: {},
2273
+ children: []
2274
+ };
2275
+ const tagMatch = tag.match(/<\/?([^\s]+?)[/\s>]/);
2276
+ if (tagMatch) {
2277
+ res.name = tagMatch[1];
2278
+ if (voidElements[tagMatch[1]] || tag.charAt(tag.length - 2) === '/') {
2279
+ res.voidElement = true;
2280
+ }
2281
+ if (res.name.startsWith('!--')) {
2282
+ const endIndex = tag.indexOf('-->');
2283
+ return {
2284
+ type: 'comment',
2285
+ comment: endIndex !== -1 ? tag.slice(4, endIndex) : ''
2286
+ };
2287
+ }
2288
+ }
2289
+ const reg = new RegExp(attrRE);
2290
+ let result = null;
2291
+ for (;;) {
2292
+ result = reg.exec(tag);
2293
+ if (result === null) {
2294
+ break;
2295
+ }
2296
+ if (!result[0].trim()) {
2297
+ continue;
2298
+ }
2299
+ if (result[1]) {
2300
+ const attr = result[1].trim();
2301
+ let arr = [attr, null];
2302
+ const eq = attr.indexOf('=');
2303
+ if (eq > -1) {
2304
+ arr = [attr.slice(0, eq), attr.slice(eq + 1)];
2305
+ }
2306
+ res.attrs[arr[0]] = arr[1];
2307
+ reg.lastIndex--;
2308
+ } else if (result[2]) {
2309
+ res.attrs[result[2]] = result[3].trim().substring(1, result[3].length - 1);
2310
+ }
2311
+ }
2312
+ return res;
2250
2313
  }
2251
-
2252
- var voidElements;
2253
- var hasRequiredVoidElements;
2254
-
2255
- function requireVoidElements () {
2256
- if (hasRequiredVoidElements) return voidElements;
2257
- hasRequiredVoidElements = 1;
2258
- voidElements = {
2259
- "area": true,
2260
- "base": true,
2261
- "br": true,
2262
- "col": true,
2263
- "embed": true,
2264
- "hr": true,
2265
- "img": true,
2266
- "input": true,
2267
- "link": true,
2268
- "meta": true,
2269
- "param": true,
2270
- "source": true,
2271
- "track": true,
2272
- "wbr": true
2273
- };
2274
- return voidElements;
2314
+ const tagRE = /<!--[\s\S]*?-->|<[a-zA-Z0-9\-!/](?:"[^"]*"|'[^']*'|[^'">])*>/g;
2315
+ const tagNameRE = /<\/?([^\s]+?)[/\s>]/;
2316
+ const whitespaceRE = /^\s*$/;
2317
+ const rawTextRE = /^(script|style)$/i;
2318
+ const sentinel = '\u0000';
2319
+ const empty = Object.create(null);
2320
+ function restoreSentinels(nodes) {
2321
+ nodes.forEach(function (node) {
2322
+ if (node.type === 'text') {
2323
+ node.content = node.content.split(sentinel).join('<');
2324
+ return;
2325
+ }
2326
+ if (node.type === 'comment') {
2327
+ node.comment = node.comment.split(sentinel).join('<');
2328
+ return;
2329
+ }
2330
+ for (const key in node.attrs) {
2331
+ const value = node.attrs[key];
2332
+ if (typeof value === 'string' && value.indexOf(sentinel) > -1) {
2333
+ node.attrs[key] = value.split(sentinel).join('<');
2334
+ }
2335
+ }
2336
+ if (node.children.length) {
2337
+ restoreSentinels(node.children);
2338
+ }
2339
+ });
2275
2340
  }
2276
-
2277
- var voidElementsExports = requireVoidElements();
2278
- var e = /*@__PURE__*/getDefaultExportFromCjs(voidElementsExports);
2279
-
2280
- var t = /\s([^'"/\s><]+?)[\s/>]|([^\s=]+)=\s?(".*?"|'.*?')/g;
2281
- function n(n) {
2282
- var r = {
2283
- type: "tag",
2284
- name: "",
2285
- voidElement: false,
2286
- attrs: {},
2287
- children: []
2288
- },
2289
- i = n.match(/<\/?([^\s]+?)[/\s>]/);
2290
- if (i && (r.name = i[1], (e[i[1]] || "/" === n.charAt(n.length - 2)) && (r.voidElement = true), r.name.startsWith("!--"))) {
2291
- var s = n.indexOf("--\x3e");
2292
- return {
2293
- type: "comment",
2294
- comment: -1 !== s ? n.slice(4, s) : ""
2341
+ function parse(html, options) {
2342
+ const components = options && options.components || empty;
2343
+ const allowedTags = options && options.allowedTags;
2344
+ let restoreNeeded = false;
2345
+ if (allowedTags) {
2346
+ const isAllowed = typeof allowedTags === 'function' ? allowedTags : function (name) {
2347
+ return allowedTags.indexOf(name) > -1;
2295
2348
  };
2349
+ let out = '';
2350
+ let pos = 0;
2351
+ tagRE.lastIndex = 0;
2352
+ let am;
2353
+ while (am = tagRE.exec(html)) {
2354
+ const tag = am[0];
2355
+ out += html.slice(pos, am.index);
2356
+ const nameMatch = tag.match(tagNameRE);
2357
+ if (tag.startsWith('<!--') || nameMatch && isAllowed(nameMatch[1])) {
2358
+ out += tag;
2359
+ pos = am.index + tag.length;
2360
+ } else {
2361
+ restoreNeeded = true;
2362
+ out += sentinel;
2363
+ pos = am.index + 1;
2364
+ tagRE.lastIndex = pos;
2365
+ }
2366
+ }
2367
+ html = out + html.slice(pos);
2296
2368
  }
2297
- for (var a = new RegExp(t), c = null; null !== (c = a.exec(n));) if (c[0].trim()) if (c[1]) {
2298
- var o = c[1].trim(),
2299
- l = [o, ""];
2300
- o.indexOf("=") > -1 && (l = o.split("=")), r.attrs[l[0]] = l[1], a.lastIndex--;
2301
- } else c[2] && (r.attrs[c[2]] = c[3].trim().substring(1, c[3].length - 1));
2302
- return r;
2303
- }
2304
- var r = /<[a-zA-Z0-9\-\!\/](?:"[^"]*"|'[^']*'|[^'">])*>/g,
2305
- i = /^\s*$/,
2306
- s = Object.create(null);
2307
- function a(e, t) {
2308
- switch (t.type) {
2309
- case "text":
2310
- return e + t.content;
2311
- case "tag":
2312
- return e += "<" + t.name + (t.attrs ? function (e) {
2313
- var t = [];
2314
- for (var n in e) t.push(n + '="' + e[n] + '"');
2315
- return t.length ? " " + t.join(" ") : "";
2316
- }(t.attrs) : "") + (t.voidElement ? "/>" : ">"), t.voidElement ? e : e + t.children.reduce(a, "") + "</" + t.name + ">";
2317
- case "comment":
2318
- return e + "\x3c!--" + t.comment + "--\x3e";
2369
+ const result = [];
2370
+ const arr = [];
2371
+ let current;
2372
+ let level = -1;
2373
+ let inComponent = false;
2374
+ let rawUntil = 0;
2375
+ let htmlLower;
2376
+ if (html.indexOf('<') !== 0) {
2377
+ const end = html.indexOf('<');
2378
+ result.push({
2379
+ type: 'text',
2380
+ content: end === -1 ? html : html.substring(0, end)
2381
+ });
2319
2382
  }
2320
- }
2321
- var c = {
2322
- parse: function (e, t) {
2323
- t || (t = {}), t.components || (t.components = s);
2324
- var a,
2325
- c = [],
2326
- o = [],
2327
- l = -1,
2328
- m = false;
2329
- if (0 !== e.indexOf("<")) {
2330
- var u = e.indexOf("<");
2331
- c.push({
2332
- type: "text",
2333
- content: -1 === u ? e : e.substring(0, u)
2334
- });
2383
+ const matches = [];
2384
+ let m;
2385
+ while (m = tagRE.exec(html)) {
2386
+ matches.push(m);
2387
+ }
2388
+ matches.forEach(function (match, i) {
2389
+ const tag = match[0];
2390
+ if (!tag) return;
2391
+ if (tag.startsWith('<!--')) return;
2392
+ let lts = 0;
2393
+ let gts = 0;
2394
+ let secondLt = -1;
2395
+ let quote = null;
2396
+ for (let j = 0; j < tag.length; j++) {
2397
+ const c = tag.charAt(j);
2398
+ if (quote) {
2399
+ if (c === quote) quote = null;
2400
+ } else if (c === '"' || c === "'") {
2401
+ quote = c;
2402
+ } else if (c === '<') {
2403
+ lts++;
2404
+ if (lts === 2) secondLt = j;
2405
+ } else if (c === '>') {
2406
+ gts++;
2407
+ }
2408
+ }
2409
+ const validSplit = secondLt > -1 && /[a-zA-Z0-9\-!/]/.test(tag.charAt(secondLt + 1));
2410
+ if (lts > gts && validSplit) {
2411
+ const firstPart = tag.substring(0, secondLt);
2412
+ const secondPart = tag.substring(firstPart.length);
2413
+ matches[i][0] = secondPart;
2414
+ matches[i].index += firstPart.length;
2335
2415
  }
2336
- return e.replace(r, function (r, s) {
2337
- if (m) {
2338
- if (r !== "</" + a.name + ">") return;
2339
- m = false;
2340
- }
2341
- var u,
2342
- f = "/" !== r.charAt(1),
2343
- h = r.startsWith("\x3c!--"),
2344
- p = s + r.length,
2345
- d = e.charAt(p);
2346
- if (h) {
2347
- var v = n(r);
2348
- return l < 0 ? (c.push(v), c) : ((u = o[l]).children.push(v), c);
2349
- }
2350
- if (f && (l++, "tag" === (a = n(r)).type && t.components[a.name] && (a.type = "component", m = true), a.voidElement || m || !d || "<" === d || a.children.push({
2351
- type: "text",
2352
- content: e.slice(p, e.indexOf("<", p))
2353
- }), 0 === l && c.push(a), (u = o[l - 1]) && u.children.push(a), o[l] = a), (!f || a.voidElement) && (l > -1 && (a.voidElement || a.name === r.slice(2, -1)) && (l--, a = -1 === l ? c : o[l]), !m && "<" !== d && d)) {
2354
- u = -1 === l ? c : o[l].children;
2355
- var x = e.indexOf("<", p),
2356
- g = e.slice(p, -1 === x ? void 0 : x);
2357
- i.test(g) && (g = " "), (x > -1 && l + u.length >= 0 || " " !== g) && u.push({
2358
- type: "text",
2359
- content: g
2416
+ });
2417
+ matches.forEach(function (match, i) {
2418
+ const tag = match[0];
2419
+ if (!tag) return;
2420
+ const index = match.index;
2421
+ if (index < rawUntil) return;
2422
+ if (inComponent) {
2423
+ if (tag !== '</' + current.name + '>') {
2424
+ return;
2425
+ } else {
2426
+ inComponent = false;
2427
+ }
2428
+ }
2429
+ const isOpen = tag.charAt(1) !== '/';
2430
+ const isComment = tag.startsWith('<!--');
2431
+ const start = index + tag.length;
2432
+ const nextChar = html.charAt(start);
2433
+ const nextMatch = matches[i + 1];
2434
+ let isText;
2435
+ if (nextChar === '<' && nextMatch) {
2436
+ const nextTag = html.substring(start, nextMatch.index);
2437
+ isText = nextTag.split('<').length > nextTag.split('>').length;
2438
+ }
2439
+ let parent;
2440
+ if (isComment) {
2441
+ const comment = parseTag(tag);
2442
+ if (level < 0) {
2443
+ result.push(comment);
2444
+ return result;
2445
+ }
2446
+ parent = arr[level];
2447
+ parent.children.push(comment);
2448
+ const text = html.slice(start, nextMatch ? nextMatch.index : undefined);
2449
+ if (text.length > 0) {
2450
+ parent.children.push({
2451
+ type: 'text',
2452
+ content: text
2360
2453
  });
2361
2454
  }
2362
- }), c;
2363
- },
2364
- stringify: function (e) {
2365
- return e.reduce(function (e, t) {
2366
- return e + a("", t);
2367
- }, "");
2455
+ return result;
2456
+ }
2457
+ if (isOpen) {
2458
+ level++;
2459
+ current = parseTag(tag);
2460
+ if (current.type === 'tag' && components[current.name]) {
2461
+ current.type = 'component';
2462
+ inComponent = true;
2463
+ }
2464
+ let isRawText = false;
2465
+ if (!inComponent && !current.voidElement && rawTextRE.test(current.name)) {
2466
+ isRawText = true;
2467
+ htmlLower || (htmlLower = html.toLowerCase());
2468
+ const closeIndex = htmlLower.indexOf('</' + current.name.toLowerCase() + '>', start);
2469
+ const contentEnd = closeIndex === -1 ? html.length : closeIndex;
2470
+ const content = html.slice(start, contentEnd);
2471
+ if (content) {
2472
+ current.children.push({
2473
+ type: 'text',
2474
+ content
2475
+ });
2476
+ }
2477
+ rawUntil = contentEnd;
2478
+ }
2479
+ if (!current.voidElement && !inComponent && !isRawText && nextChar && nextChar !== '<') {
2480
+ current.children.push({
2481
+ type: 'text',
2482
+ content: html.slice(start, nextMatch ? nextMatch.index : undefined)
2483
+ });
2484
+ }
2485
+ if (level === 0) {
2486
+ result.push(current);
2487
+ }
2488
+ parent = arr[level - 1];
2489
+ if (parent) {
2490
+ parent.children.push(current);
2491
+ }
2492
+ arr[level] = current;
2493
+ }
2494
+ if (!isOpen || current.voidElement) {
2495
+ if (level > -1 && (current.voidElement || current.name === tag.slice(2, -1))) {
2496
+ level--;
2497
+ current = level === -1 ? result : arr[level];
2498
+ }
2499
+ if (!inComponent && (nextChar !== '<' || isText) && nextChar) {
2500
+ parent = level === -1 ? result : arr[level].children;
2501
+ const end = nextMatch ? nextMatch.index : -1;
2502
+ let content = html.slice(start, end === -1 ? undefined : end);
2503
+ if (whitespaceRE.test(content)) {
2504
+ content = ' ';
2505
+ }
2506
+ if (end > -1 && level + parent.length >= 0 || content !== ' ') {
2507
+ parent.push({
2508
+ type: 'text',
2509
+ content
2510
+ });
2511
+ }
2512
+ }
2513
+ }
2514
+ });
2515
+ if (restoreNeeded) {
2516
+ restoreSentinels(result);
2517
+ }
2518
+ return result;
2519
+ }
2520
+ function attrString(attrs) {
2521
+ const buff = [];
2522
+ for (const key in attrs) {
2523
+ if (attrs[key] === null) {
2524
+ buff.push(key);
2525
+ } else {
2526
+ buff.push(key + '="' + String(attrs[key]).replace(/"/g, '&quot;') + '"');
2527
+ }
2528
+ }
2529
+ if (!buff.length) {
2530
+ return '';
2368
2531
  }
2532
+ return ' ' + buff.join(' ');
2533
+ }
2534
+ function stringifyNode(buff, doc) {
2535
+ switch (doc.type) {
2536
+ case 'text':
2537
+ return buff + doc.content;
2538
+ case 'tag':
2539
+ {
2540
+ const tagEnd = doc.voidElement && doc.name.toLowerCase() !== '!doctype' ? '/>' : '>';
2541
+ buff += '<' + doc.name + (doc.attrs ? attrString(doc.attrs) : '') + tagEnd;
2542
+ if (doc.voidElement) {
2543
+ return buff;
2544
+ }
2545
+ return buff + doc.children.reduce(stringifyNode, '') + '</' + doc.name + '>';
2546
+ }
2547
+ case 'comment':
2548
+ buff += '<!--' + doc.comment + '-->';
2549
+ return buff;
2550
+ }
2551
+ }
2552
+ function stringify(doc) {
2553
+ return doc.reduce(function (token, rootEl) {
2554
+ return token + stringifyNode('', rootEl);
2555
+ }, '');
2556
+ }
2557
+ var index = {
2558
+ parse,
2559
+ stringify
2369
2560
  };
2370
2561
 
2371
2562
  const warn = (i18n, code, msg, rest) => {
@@ -2583,46 +2774,6 @@
2583
2774
  });
2584
2775
  return stringNode;
2585
2776
  };
2586
- const escapeLiteralLessThan = (str, keepArray = [], knownComponentsMap = {}) => {
2587
- if (!str) return str;
2588
- const knownNames = Object.keys(knownComponentsMap);
2589
- const allValidNames = [...keepArray, ...knownNames];
2590
- let result = '';
2591
- let i = 0;
2592
- while (i < str.length) {
2593
- if (str[i] === '<') {
2594
- let isValidTag = false;
2595
- const closingMatch = str.slice(i).match(/^<\/(\d+|[a-zA-Z][a-zA-Z0-9_-]*)>/);
2596
- if (closingMatch) {
2597
- const tagName = closingMatch[1];
2598
- if (/^\d+$/.test(tagName) || allValidNames.includes(tagName)) {
2599
- isValidTag = true;
2600
- result += closingMatch[0];
2601
- i += closingMatch[0].length;
2602
- }
2603
- }
2604
- if (!isValidTag) {
2605
- const openingMatch = str.slice(i).match(/^<(\d+|[a-zA-Z][a-zA-Z0-9_-]*)(\s+[\w-]+(?:=(?:"[^"]*"|'[^']*'|[^\s>]+))?)*\s*(\/)?>/);
2606
- if (openingMatch) {
2607
- const tagName = openingMatch[1];
2608
- if (/^\d+$/.test(tagName) || allValidNames.includes(tagName)) {
2609
- isValidTag = true;
2610
- result += openingMatch[0];
2611
- i += openingMatch[0].length;
2612
- }
2613
- }
2614
- }
2615
- if (!isValidTag) {
2616
- result += '&lt;';
2617
- i += 1;
2618
- }
2619
- } else {
2620
- result += str[i];
2621
- i += 1;
2622
- }
2623
- }
2624
- return result;
2625
- };
2626
2777
  const renderNodes = (children, knownComponentsMap, targetString, i18n, i18nOptions, combinedTOpts, shouldUnescape) => {
2627
2778
  if (targetString === '') return [];
2628
2779
  const keepArray = i18nOptions.transKeepBasicHtmlNodesFor || [];
@@ -2637,8 +2788,11 @@
2637
2788
  });
2638
2789
  };
2639
2790
  getData(children);
2640
- const escapedString = escapeLiteralLessThan(targetString, keepArray, data);
2641
- const ast = c.parse(`<0>${escapedString}</0>`);
2791
+ const knownNames = Object.keys(data);
2792
+ const allowedTags = name => /^\d+$/.test(name) || keepArray.indexOf(name) > -1 || knownNames.indexOf(name) > -1;
2793
+ const ast = index.parse(`<0>${targetString}</0>`, {
2794
+ allowedTags
2795
+ });
2642
2796
  const opts = {
2643
2797
  ...data,
2644
2798
  ...combinedTOpts