fast-xml-parser 5.3.3 → 5.3.5

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.
@@ -37,12 +37,13 @@ export default class EntitiesParser{
37
37
  }
38
38
  addExternalEntity(key,val){
39
39
  validateEntityName(key);
40
+ const escaped = key.replace(/[.\-+*:]/g, '\\.');
40
41
  if(val.indexOf("&") !== -1) {
41
42
  reportWarning(`Entity ${key} is not added as '&' is found in value;`)
42
43
  return;
43
44
  }else{
44
- this.lastEntities[ent] = {
45
- regex: new RegExp("&"+key+";","g"),
45
+ this.lastEntities[key] = {
46
+ regex: new RegExp("&"+escaped+";","g"),
46
47
  val : val
47
48
  }
48
49
  }
@@ -52,8 +53,9 @@ export default class EntitiesParser{
52
53
  const entKeys = Object.keys(entities);
53
54
  for (let i = 0; i < entKeys.length; i++) {
54
55
  const ent = entKeys[i];
56
+ const escaped = ent.replace(/[.\-+*:]/g, '\\.');
55
57
  this.docTypeEntities[ent] = {
56
- regex: new RegExp("&"+ent+";","g"),
58
+ regex: new RegExp("&"+escaped+";","g"),
57
59
  val : entities[ent]
58
60
  }
59
61
  }
@@ -89,11 +91,11 @@ export default class EntitiesParser{
89
91
  }
90
92
  return val;
91
93
  }
92
- };
94
+ }
93
95
 
94
96
  //an entity name should not contains special characters that may be used in regex
95
97
  //Eg !?\\\/[]$%{}^&*()<>
96
- const specialChar = "!?\\\/[]$%{}^&*()<>|+";
98
+ const specialChar = "!?\\/[]$%{}^&*()<>|+";
97
99
 
98
100
  function validateEntityName(name){
99
101
  for (let i = 0; i < specialChar.length; i++) {
@@ -25,11 +25,13 @@ export default class DocTypeReader{
25
25
  i += 7;
26
26
  let entityName, val;
27
27
  [entityName, val,i] = this.readEntityExp(xmlData,i+1,this.suppressValidationErr);
28
- if(val.indexOf("&") === -1) //Parameter entities are not supported
28
+ if(val.indexOf("&") === -1){ //Parameter entities are not supported
29
+ const escaped = entityName.replace(/[.\-+*:]/g, '\\.');
29
30
  entities[ entityName ] = {
30
- regx : RegExp( `&${entityName};`,"g"),
31
+ regx : RegExp( `&${escaped};`,"g"),
31
32
  val: val
32
33
  };
34
+ }
33
35
  }
34
36
  else if( hasBody && hasSeq(xmlData, "!ELEMENT",i)) {
35
37
  i += 8;//Not supported
@@ -41,8 +41,8 @@ export default class OrderedObjParser{
41
41
  "copyright" : { regex: /&(copy|#169);/g, val: "©" },
42
42
  "reg" : { regex: /&(reg|#174);/g, val: "®" },
43
43
  "inr" : { regex: /&(inr|#8377);/g, val: "₹" },
44
- "num_dec": { regex: /&#([0-9]{1,7});/g, val : (_, str) => String.fromCodePoint(Number.parseInt(str, 10)) },
45
- "num_hex": { regex: /&#x([0-9a-fA-F]{1,6});/g, val : (_, str) => String.fromCodePoint(Number.parseInt(str, 16)) },
44
+ "num_dec": { regex: /&#([0-9]{1,7});/g, val : (_, str) => fromCodePoint(str, 10, "&#") },
45
+ "num_hex": { regex: /&#x([0-9a-fA-F]{1,6});/g, val : (_, str) => fromCodePoint(str, 16, "&#x") },
46
46
  };
47
47
  this.addExternalEntities = addExternalEntities;
48
48
  this.parseXml = parseXml;
@@ -77,8 +77,9 @@ function addExternalEntities(externalEntities){
77
77
  const entKeys = Object.keys(externalEntities);
78
78
  for (let i = 0; i < entKeys.length; i++) {
79
79
  const ent = entKeys[i];
80
+ const escaped = ent.replace(/[.\-+*:]/g, '\\.');
80
81
  this.lastEntities[ent] = {
81
- regex: new RegExp("&"+ent+";","g"),
82
+ regex: new RegExp("&"+escaped+";","g"),
82
83
  val : externalEntities[ent]
83
84
  }
84
85
  }
@@ -627,3 +628,13 @@ function parseValue(val, shouldParse, options) {
627
628
  }
628
629
  }
629
630
  }
631
+
632
+ function fromCodePoint(str, base, prefix){
633
+ const codePoint = Number.parseInt(str, base);
634
+
635
+ if (codePoint >= 0 && codePoint <= 0x10FFFF) {
636
+ return String.fromCodePoint(codePoint);
637
+ } else {
638
+ return prefix +str + ";";
639
+ }
640
+ }