From c3fb07f330093b3196130d1d292a968e075ea2d3 Mon Sep 17 00:00:00 2001 From: Amit Gupta Date: Sun, 21 Nov 2021 08:39:52 +0530 Subject: [PATCH] supports HTML entities --- src/fxp.d.ts | 1 + src/xmlparser/OptionsBuilder.js | 6 ++++-- src/xmlparser/OrderedObjParser.js | 21 +++++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/fxp.d.ts b/src/fxp.d.ts index 219e19ba..fef5a7f8 100644 --- a/src/fxp.d.ts +++ b/src/fxp.d.ts @@ -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; diff --git a/src/xmlparser/OptionsBuilder.js b/src/xmlparser/OptionsBuilder.js index 52312c52..58fad67a 100644 --- a/src/xmlparser/OptionsBuilder.js +++ b/src/xmlparser/OptionsBuilder.js @@ -27,7 +27,8 @@ const defaultOptions = { isArray: () => false, commentPropName: false, unpairedTags: [], - processEntities: true + processEntities: true, + htmlEntities: false, }; const props = [ @@ -50,7 +51,8 @@ const props = [ 'isArray', 'commentPropName', 'unpairedTags', - 'processEntities' + 'processEntities', + 'htmlEntities' ]; const util = require('../util'); diff --git a/src/xmlparser/OrderedObjParser.js b/src/xmlparser/OrderedObjParser.js index b927357b..00dcf5f9 100644 --- a/src/xmlparser/OrderedObjParser.js +++ b/src/xmlparser/OrderedObjParser.js @@ -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; @@ -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; }