backend v4 half
This commit is contained in:
Generated
Vendored
+28
-23
@@ -1,10 +1,11 @@
|
||||
'use strict';
|
||||
///@ts-check
|
||||
|
||||
const util = require('../util');
|
||||
const xmlNode = require('./xmlNode');
|
||||
const readDocType = require("./DocTypeReader");
|
||||
const toNumber = require("strnum");
|
||||
import {getAllMatches, isExist} from '../util.js';
|
||||
import xmlNode from './xmlNode.js';
|
||||
import readDocType from './DocTypeReader.js';
|
||||
import toNumber from "strnum";
|
||||
import getIgnoreAttributesFn from "../ignoreAttributes.js";
|
||||
|
||||
// const regx =
|
||||
// '<((!\\[CDATA\\[([\\s\\S]*?)(]]>))|((NAME:)?(NAME))([^>]*)>|((\\/)(NAME)\\s*>))([^<]*)'
|
||||
@@ -13,7 +14,7 @@ const toNumber = require("strnum");
|
||||
//const tagsRegx = new RegExp("<(\\/?[\\w:\\-\._]+)([^>]*)>(\\s*"+cdataRegx+")*([^<]+)?","g");
|
||||
//const tagsRegx = new RegExp("<(\\/?)((\\w*:)?([\\w:\\-\._]+))([^>]*)>([^<]*)("+cdataRegx+"([^<]*))*([^<]+)?","g");
|
||||
|
||||
class OrderedObjParser{
|
||||
export default class OrderedObjParser{
|
||||
constructor(options){
|
||||
this.options = options;
|
||||
this.currentNode = null;
|
||||
@@ -40,8 +41,8 @@ class OrderedObjParser{
|
||||
"copyright" : { regex: /&(copy|#169);/g, val: "©" },
|
||||
"reg" : { regex: /&(reg|#174);/g, val: "®" },
|
||||
"inr" : { regex: /&(inr|#8377);/g, val: "₹" },
|
||||
"num_dec": { regex: /&#([0-9]{1,7});/g, val : (_, str) => String.fromCharCode(Number.parseInt(str, 10)) },
|
||||
"num_hex": { regex: /&#x([0-9a-fA-F]{1,6});/g, val : (_, str) => String.fromCharCode(Number.parseInt(str, 16)) },
|
||||
"num_dec": { regex: /&#([0-9]{1,7});/g, val : (_, str) => String.fromCodePoint(Number.parseInt(str, 10)) },
|
||||
"num_hex": { regex: /&#x([0-9a-fA-F]{1,6});/g, val : (_, str) => String.fromCodePoint(Number.parseInt(str, 16)) },
|
||||
};
|
||||
this.addExternalEntities = addExternalEntities;
|
||||
this.parseXml = parseXml;
|
||||
@@ -53,6 +54,7 @@ class OrderedObjParser{
|
||||
this.readStopNodeData = readStopNodeData;
|
||||
this.saveTextToParentTag = saveTextToParentTag;
|
||||
this.addChild = addChild;
|
||||
this.ignoreAttributesFn = getIgnoreAttributesFn(this.options.ignoreAttributes)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -125,15 +127,18 @@ function resolveNameSpace(tagname) {
|
||||
const attrsRegx = new RegExp('([^\\s=]+)\\s*(=\\s*([\'"])([\\s\\S]*?)\\3)?', 'gm');
|
||||
|
||||
function buildAttributesMap(attrStr, jPath, tagName) {
|
||||
if (!this.options.ignoreAttributes && typeof attrStr === 'string') {
|
||||
if (this.options.ignoreAttributes !== true && typeof attrStr === 'string') {
|
||||
// attrStr = attrStr.replace(/\r?\n/g, ' ');
|
||||
//attrStr = attrStr || attrStr.trim();
|
||||
|
||||
const matches = util.getAllMatches(attrStr, attrsRegx);
|
||||
const matches = getAllMatches(attrStr, attrsRegx);
|
||||
const len = matches.length; //don't make it inline
|
||||
const attrs = {};
|
||||
for (let i = 0; i < len; i++) {
|
||||
const attrName = this.resolveNameSpace(matches[i][1]);
|
||||
if (this.ignoreAttributesFn(attrName, jPath)) {
|
||||
continue
|
||||
}
|
||||
let oldVal = matches[i][4];
|
||||
let aName = this.options.attributeNamePrefix + attrName;
|
||||
if (attrName.length) {
|
||||
@@ -241,8 +246,7 @@ const parseXml = function(xmlData) {
|
||||
if(tagData.tagName !== tagData.tagExp && tagData.attrExpPresent){
|
||||
childNode[":@"] = this.buildAttributesMap(tagData.tagExp, jPath, tagData.tagName);
|
||||
}
|
||||
this.addChild(currentNode, childNode, jPath)
|
||||
|
||||
this.addChild(currentNode, childNode, jPath, i);
|
||||
}
|
||||
|
||||
|
||||
@@ -307,6 +311,7 @@ const parseXml = function(xmlData) {
|
||||
if(tagName !== xmlObj.tagname){
|
||||
jPath += jPath ? "." + tagName : tagName;
|
||||
}
|
||||
const startIndex = i;
|
||||
if (this.isItStopNode(this.options.stopNodes, jPath, tagName)) {
|
||||
let tagContent = "";
|
||||
//self-closing tag
|
||||
@@ -335,6 +340,7 @@ const parseXml = function(xmlData) {
|
||||
}
|
||||
|
||||
const childNode = new xmlNode(tagName);
|
||||
|
||||
if(tagName !== tagExp && attrExpPresent){
|
||||
childNode[":@"] = this.buildAttributesMap(tagExp, jPath, tagName);
|
||||
}
|
||||
@@ -345,7 +351,7 @@ const parseXml = function(xmlData) {
|
||||
jPath = jPath.substr(0, jPath.lastIndexOf("."));
|
||||
childNode.add(this.options.textNodeName, tagContent);
|
||||
|
||||
this.addChild(currentNode, childNode, jPath)
|
||||
this.addChild(currentNode, childNode, jPath, startIndex);
|
||||
}else{
|
||||
//selfClosing tag
|
||||
if(tagExp.length > 0 && tagExp.lastIndexOf("/") === tagExp.length - 1){
|
||||
@@ -365,7 +371,7 @@ const parseXml = function(xmlData) {
|
||||
if(tagName !== tagExp && attrExpPresent){
|
||||
childNode[":@"] = this.buildAttributesMap(tagExp, jPath, tagName);
|
||||
}
|
||||
this.addChild(currentNode, childNode, jPath)
|
||||
this.addChild(currentNode, childNode, jPath, startIndex);
|
||||
jPath = jPath.substr(0, jPath.lastIndexOf("."));
|
||||
}
|
||||
//opening tag
|
||||
@@ -376,7 +382,7 @@ const parseXml = function(xmlData) {
|
||||
if(tagName !== tagExp && attrExpPresent){
|
||||
childNode[":@"] = this.buildAttributesMap(tagExp, jPath, tagName);
|
||||
}
|
||||
this.addChild(currentNode, childNode, jPath)
|
||||
this.addChild(currentNode, childNode, jPath, startIndex);
|
||||
currentNode = childNode;
|
||||
}
|
||||
textData = "";
|
||||
@@ -390,14 +396,16 @@ const parseXml = function(xmlData) {
|
||||
return xmlObj.child;
|
||||
}
|
||||
|
||||
function addChild(currentNode, childNode, jPath){
|
||||
function addChild(currentNode, childNode, jPath, startIndex){
|
||||
// unset startIndex if not requested
|
||||
if (!this.options.captureMetaData) startIndex = undefined;
|
||||
const result = this.options.updateTag(childNode.tagname, jPath, childNode[":@"])
|
||||
if(result === false){
|
||||
}else if(typeof result === "string"){
|
||||
} else if(typeof result === "string"){
|
||||
childNode.tagname = result
|
||||
currentNode.addChild(childNode);
|
||||
currentNode.addChild(childNode, startIndex);
|
||||
}else{
|
||||
currentNode.addChild(childNode);
|
||||
currentNode.addChild(childNode, startIndex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -424,7 +432,7 @@ const replaceEntitiesValue = function(val){
|
||||
}
|
||||
function saveTextToParentTag(textData, currentNode, jPath, isLeafNode) {
|
||||
if (textData) { //store previously collected data as textNode
|
||||
if(isLeafNode === undefined) isLeafNode = Object.keys(currentNode.child).length === 0
|
||||
if(isLeafNode === undefined) isLeafNode = currentNode.child.length === 0
|
||||
|
||||
textData = this.parseTextData(textData,
|
||||
currentNode.tagname,
|
||||
@@ -589,13 +597,10 @@ function parseValue(val, shouldParse, options) {
|
||||
else if(newval === 'false' ) return false;
|
||||
else return toNumber(val, options);
|
||||
} else {
|
||||
if (util.isExist(val)) {
|
||||
if (isExist(val)) {
|
||||
return val;
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
module.exports = OrderedObjParser;
|
||||
|
||||
Reference in New Issue
Block a user