Skip to content

Commit

Permalink
supports HTML entities
Browse files Browse the repository at this point in the history
  • Loading branch information
amitguptagwl committed Nov 30, 2021
1 parent 44d7de4 commit c3fb07f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/fxp.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type X2jOptions = {
alwaysCreateTextNode: boolean;
isArray: (tagName: string, jPath: string, isLeafNode: boolean, isAttribute: boolean) => boolean;
processEntities: boolean;
htmlEntities: boolean;
};
type strnumOptions = {
hex: boolean;
Expand Down
6 changes: 4 additions & 2 deletions src/xmlparser/OptionsBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const defaultOptions = {
isArray: () => false,
commentPropName: false,
unpairedTags: [],
processEntities: true
processEntities: true,
htmlEntities: false,
};

const props = [
Expand All @@ -50,7 +51,8 @@ const props = [
'isArray',
'commentPropName',
'unpairedTags',
'processEntities'
'processEntities',
'htmlEntities'
];

const util = require('../util');
Expand Down
21 changes: 21 additions & 0 deletions src/xmlparser/OrderedObjParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ class OrderedObjParser{
"lt" : { regex: /&(lt|#60|#x3C);/g, val : "<"},
"quot" : { regex: /&(quot|#34|#x22);/g, val : "\""},
};
this.htmlEntities = {
"space": { regex: /&(nbsp|#160);/g, val: " " },
// "lt" : { regex: /&(lt|#60);/g, val: "<" },
// "gt" : { regex: /&(gt|#62);/g, val: ">" },
// "amp" : { regex: /&(amp|#38);/g, val: "&" },
// "quot" : { regex: /&(quot|#34);/g, val: "\"" },
// "apos" : { regex: /&(apos|#39);/g, val: "'" },
"cent" : { regex: /&(cent|#162);/g, val: "¢" },
"pound" : { regex: /&(pound|#163);/g, val: "£" },
"yen" : { regex: /&(yen|#165);/g, val: "¥" },
"euro" : { regex: /&(euro|#8364);/g, val: "€" },
"copyright" : { regex: /&(copy|#169);/g, val: "©" },
"reg" : { regex: /&(reg|#174);/g, val: "®" },
"inr" : { regex: /&(inr|#8377);/g, val: "₹" },
};
this.parseXml = parseXml;
this.parseTextData = parseTextData;
this.resolveNameSpace = resolveNameSpace;
Expand Down Expand Up @@ -340,6 +355,12 @@ const replaceEntitiesValue = function(val){
const entity = this.lastEntities[entityName];
val = val.replace( entity.regex, entity.val);
}
if(this.options.htmlEntities){
for(let entityName in this.htmlEntities){
const entity = this.htmlEntities[entityName];
val = val.replace( entity.regex, entity.val);
}
}
}
return val;
}
Expand Down

0 comments on commit c3fb07f

Please sign in to comment.