phylotree 2.4.0 → 2.6.0
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/dist/phylotree.d.ts +118 -0
- package/dist/phylotree.js +1783 -450
- package/dist/phylotree.min.js +1 -1
- package/package.json +4 -4
- package/src/render/draw.js +38 -1
- package/src/render/events.js +19 -1
- package/src/render/nodes.js +16 -5
- package/src/render/options.js +6 -0
- package/src/render/unrooted.js +157 -0
package/dist/phylotree.js
CHANGED
|
@@ -6398,7 +6398,7 @@
|
|
|
6398
6398
|
return matches;
|
|
6399
6399
|
}
|
|
6400
6400
|
|
|
6401
|
-
const isName = function(string) {
|
|
6401
|
+
const isName = function (string) {
|
|
6402
6402
|
const match = regexName.exec(string);
|
|
6403
6403
|
return !(match === null || typeof match === 'undefined');
|
|
6404
6404
|
};
|
|
@@ -6407,8 +6407,23 @@
|
|
|
6407
6407
|
return typeof v !== 'undefined';
|
|
6408
6408
|
}
|
|
6409
6409
|
|
|
6410
|
-
|
|
6411
|
-
|
|
6410
|
+
/**
|
|
6411
|
+
* Dangerous property names that could lead to prototype pollution or security issues
|
|
6412
|
+
*/
|
|
6413
|
+
const DANGEROUS_PROPERTY_NAMES = [
|
|
6414
|
+
// '__proto__',
|
|
6415
|
+
// 'constructor',
|
|
6416
|
+
// 'prototype',
|
|
6417
|
+
'hasOwnProperty',
|
|
6418
|
+
'toString',
|
|
6419
|
+
'valueOf',
|
|
6420
|
+
'__defineGetter__',
|
|
6421
|
+
'__defineSetter__',
|
|
6422
|
+
'__lookupGetter__',
|
|
6423
|
+
'__lookupSetter__'
|
|
6424
|
+
];
|
|
6425
|
+
|
|
6426
|
+
const criticalProperties = ["__proto__", "constructor", "prototype"];
|
|
6412
6427
|
|
|
6413
6428
|
const defaultOptions$1 = {
|
|
6414
6429
|
allowBooleanAttributes: false, //A tag can have attributes without any value
|
|
@@ -6432,19 +6447,19 @@
|
|
|
6432
6447
|
// check for byte order mark (BOM)
|
|
6433
6448
|
xmlData = xmlData.substr(1);
|
|
6434
6449
|
}
|
|
6435
|
-
|
|
6450
|
+
|
|
6436
6451
|
for (let i = 0; i < xmlData.length; i++) {
|
|
6437
6452
|
|
|
6438
|
-
if (xmlData[i] === '<' && xmlData[i+1] === '?') {
|
|
6439
|
-
i+=2;
|
|
6440
|
-
i = readPI(xmlData,i);
|
|
6453
|
+
if (xmlData[i] === '<' && xmlData[i + 1] === '?') {
|
|
6454
|
+
i += 2;
|
|
6455
|
+
i = readPI(xmlData, i);
|
|
6441
6456
|
if (i.err) return i;
|
|
6442
|
-
}else if (xmlData[i] === '<') {
|
|
6457
|
+
} else if (xmlData[i] === '<') {
|
|
6443
6458
|
//starting of tag
|
|
6444
6459
|
//read until you reach to '>' avoiding any '>' in attribute value
|
|
6445
6460
|
let tagStartPos = i;
|
|
6446
6461
|
i++;
|
|
6447
|
-
|
|
6462
|
+
|
|
6448
6463
|
if (xmlData[i] === '!') {
|
|
6449
6464
|
i = readCommentAndCDATA(xmlData, i);
|
|
6450
6465
|
continue;
|
|
@@ -6480,14 +6495,14 @@
|
|
|
6480
6495
|
if (tagName.trim().length === 0) {
|
|
6481
6496
|
msg = "Invalid space after '<'.";
|
|
6482
6497
|
} else {
|
|
6483
|
-
msg = "Tag '"+tagName+"' is an invalid name.";
|
|
6498
|
+
msg = "Tag '" + tagName + "' is an invalid name.";
|
|
6484
6499
|
}
|
|
6485
6500
|
return getErrorObject('InvalidTag', msg, getLineNumberForPosition(xmlData, i));
|
|
6486
6501
|
}
|
|
6487
6502
|
|
|
6488
6503
|
const result = readAttributeStr(xmlData, i);
|
|
6489
6504
|
if (result === false) {
|
|
6490
|
-
return getErrorObject('InvalidAttr', "Attributes for '"+tagName+"' have open quote.", getLineNumberForPosition(xmlData, i));
|
|
6505
|
+
return getErrorObject('InvalidAttr', "Attributes for '" + tagName + "' have open quote.", getLineNumberForPosition(xmlData, i));
|
|
6491
6506
|
}
|
|
6492
6507
|
let attrStr = result.value;
|
|
6493
6508
|
i = result.index;
|
|
@@ -6508,17 +6523,17 @@
|
|
|
6508
6523
|
}
|
|
6509
6524
|
} else if (closingTag) {
|
|
6510
6525
|
if (!result.tagClosed) {
|
|
6511
|
-
return getErrorObject('InvalidTag', "Closing tag '"+tagName+"' doesn't have proper closing.", getLineNumberForPosition(xmlData, i));
|
|
6526
|
+
return getErrorObject('InvalidTag', "Closing tag '" + tagName + "' doesn't have proper closing.", getLineNumberForPosition(xmlData, i));
|
|
6512
6527
|
} else if (attrStr.trim().length > 0) {
|
|
6513
|
-
return getErrorObject('InvalidTag', "Closing tag '"+tagName+"' can't have attributes or invalid starting.", getLineNumberForPosition(xmlData, tagStartPos));
|
|
6528
|
+
return getErrorObject('InvalidTag', "Closing tag '" + tagName + "' can't have attributes or invalid starting.", getLineNumberForPosition(xmlData, tagStartPos));
|
|
6514
6529
|
} else if (tags.length === 0) {
|
|
6515
|
-
return getErrorObject('InvalidTag', "Closing tag '"+tagName+"' has not been opened.", getLineNumberForPosition(xmlData, tagStartPos));
|
|
6530
|
+
return getErrorObject('InvalidTag', "Closing tag '" + tagName + "' has not been opened.", getLineNumberForPosition(xmlData, tagStartPos));
|
|
6516
6531
|
} else {
|
|
6517
6532
|
const otg = tags.pop();
|
|
6518
6533
|
if (tagName !== otg.tagName) {
|
|
6519
6534
|
let openPos = getLineNumberForPosition(xmlData, otg.tagStartPos);
|
|
6520
6535
|
return getErrorObject('InvalidTag',
|
|
6521
|
-
"Expected closing tag '"+otg.tagName+"' (opened in line "+openPos.line+", col "+openPos.col+") instead of closing tag '"+tagName+"'.",
|
|
6536
|
+
"Expected closing tag '" + otg.tagName + "' (opened in line " + openPos.line + ", col " + openPos.col + ") instead of closing tag '" + tagName + "'.",
|
|
6522
6537
|
getLineNumberForPosition(xmlData, tagStartPos));
|
|
6523
6538
|
}
|
|
6524
6539
|
|
|
@@ -6539,8 +6554,8 @@
|
|
|
6539
6554
|
//if the root level has been reached before ...
|
|
6540
6555
|
if (reachedRoot === true) {
|
|
6541
6556
|
return getErrorObject('InvalidXml', 'Multiple possible root nodes found.', getLineNumberForPosition(xmlData, i));
|
|
6542
|
-
} else if(options.unpairedTags.indexOf(tagName) !== -1); else {
|
|
6543
|
-
tags.push({tagName, tagStartPos});
|
|
6557
|
+
} else if (options.unpairedTags.indexOf(tagName) !== -1) ; else {
|
|
6558
|
+
tags.push({ tagName, tagStartPos });
|
|
6544
6559
|
}
|
|
6545
6560
|
tagFound = true;
|
|
6546
6561
|
}
|
|
@@ -6554,7 +6569,7 @@
|
|
|
6554
6569
|
i++;
|
|
6555
6570
|
i = readCommentAndCDATA(xmlData, i);
|
|
6556
6571
|
continue;
|
|
6557
|
-
} else if (xmlData[i+1] === '?') {
|
|
6572
|
+
} else if (xmlData[i + 1] === '?') {
|
|
6558
6573
|
i = readPI(xmlData, ++i);
|
|
6559
6574
|
if (i.err) return i;
|
|
6560
6575
|
} else {
|
|
@@ -6565,7 +6580,7 @@
|
|
|
6565
6580
|
if (afterAmp == -1)
|
|
6566
6581
|
return getErrorObject('InvalidChar', "char '&' is not expected.", getLineNumberForPosition(xmlData, i));
|
|
6567
6582
|
i = afterAmp;
|
|
6568
|
-
}else {
|
|
6583
|
+
} else {
|
|
6569
6584
|
if (reachedRoot === true && !isWhiteSpace(xmlData[i])) {
|
|
6570
6585
|
return getErrorObject('InvalidXml', "Extra text at the end", getLineNumberForPosition(xmlData, i));
|
|
6571
6586
|
}
|
|
@@ -6576,27 +6591,27 @@
|
|
|
6576
6591
|
}
|
|
6577
6592
|
}
|
|
6578
6593
|
} else {
|
|
6579
|
-
if (
|
|
6594
|
+
if (isWhiteSpace(xmlData[i])) {
|
|
6580
6595
|
continue;
|
|
6581
6596
|
}
|
|
6582
|
-
return getErrorObject('InvalidChar', "char '"+xmlData[i]+"' is not expected.", getLineNumberForPosition(xmlData, i));
|
|
6597
|
+
return getErrorObject('InvalidChar', "char '" + xmlData[i] + "' is not expected.", getLineNumberForPosition(xmlData, i));
|
|
6583
6598
|
}
|
|
6584
6599
|
}
|
|
6585
6600
|
|
|
6586
6601
|
if (!tagFound) {
|
|
6587
6602
|
return getErrorObject('InvalidXml', 'Start tag expected.', 1);
|
|
6588
|
-
}else if (tags.length == 1) {
|
|
6589
|
-
|
|
6590
|
-
}else if (tags.length > 0) {
|
|
6591
|
-
|
|
6592
|
-
|
|
6593
|
-
|
|
6603
|
+
} else if (tags.length == 1) {
|
|
6604
|
+
return getErrorObject('InvalidTag', "Unclosed tag '" + tags[0].tagName + "'.", getLineNumberForPosition(xmlData, tags[0].tagStartPos));
|
|
6605
|
+
} else if (tags.length > 0) {
|
|
6606
|
+
return getErrorObject('InvalidXml', "Invalid '" +
|
|
6607
|
+
JSON.stringify(tags.map(t => t.tagName), null, 4).replace(/\r?\n/g, '') +
|
|
6608
|
+
"' found.", { line: 1, col: 1 });
|
|
6594
6609
|
}
|
|
6595
6610
|
|
|
6596
6611
|
return true;
|
|
6597
6612
|
}
|
|
6598
|
-
function isWhiteSpace(char){
|
|
6599
|
-
return char === ' ' || char === '\t' || char === '\n'
|
|
6613
|
+
function isWhiteSpace(char) {
|
|
6614
|
+
return char === ' ' || char === '\t' || char === '\n' || char === '\r';
|
|
6600
6615
|
}
|
|
6601
6616
|
/**
|
|
6602
6617
|
* Read Processing insstructions and skip
|
|
@@ -6730,25 +6745,25 @@
|
|
|
6730
6745
|
for (let i = 0; i < matches.length; i++) {
|
|
6731
6746
|
if (matches[i][1].length === 0) {
|
|
6732
6747
|
//nospace before attribute name: a="sd"b="saf"
|
|
6733
|
-
return getErrorObject('InvalidAttr', "Attribute '"+matches[i][2]+"' has no space in starting.", getPositionFromMatch(matches[i]))
|
|
6748
|
+
return getErrorObject('InvalidAttr', "Attribute '" + matches[i][2] + "' has no space in starting.", getPositionFromMatch(matches[i]))
|
|
6734
6749
|
} else if (matches[i][3] !== undefined && matches[i][4] === undefined) {
|
|
6735
|
-
return getErrorObject('InvalidAttr', "Attribute '"+matches[i][2]+"' is without value.", getPositionFromMatch(matches[i]));
|
|
6750
|
+
return getErrorObject('InvalidAttr', "Attribute '" + matches[i][2] + "' is without value.", getPositionFromMatch(matches[i]));
|
|
6736
6751
|
} else if (matches[i][3] === undefined && !options.allowBooleanAttributes) {
|
|
6737
6752
|
//independent attribute: ab
|
|
6738
|
-
return getErrorObject('InvalidAttr', "boolean attribute '"+matches[i][2]+"' is not allowed.", getPositionFromMatch(matches[i]));
|
|
6753
|
+
return getErrorObject('InvalidAttr', "boolean attribute '" + matches[i][2] + "' is not allowed.", getPositionFromMatch(matches[i]));
|
|
6739
6754
|
}
|
|
6740
6755
|
/* else if(matches[i][6] === undefined){//attribute without value: ab=
|
|
6741
6756
|
return { err: { code:"InvalidAttr",msg:"attribute " + matches[i][2] + " has no value assigned."}};
|
|
6742
6757
|
} */
|
|
6743
6758
|
const attrName = matches[i][2];
|
|
6744
6759
|
if (!validateAttrName(attrName)) {
|
|
6745
|
-
return getErrorObject('InvalidAttr', "Attribute '"+attrName+"' is an invalid name.", getPositionFromMatch(matches[i]));
|
|
6760
|
+
return getErrorObject('InvalidAttr', "Attribute '" + attrName + "' is an invalid name.", getPositionFromMatch(matches[i]));
|
|
6746
6761
|
}
|
|
6747
|
-
if (!
|
|
6762
|
+
if (!Object.prototype.hasOwnProperty.call(attrNames, attrName)) {
|
|
6748
6763
|
//check for duplicate attribute.
|
|
6749
6764
|
attrNames[attrName] = 1;
|
|
6750
6765
|
} else {
|
|
6751
|
-
return getErrorObject('InvalidAttr', "Attribute '"+attrName+"' is repeated.", getPositionFromMatch(matches[i]));
|
|
6766
|
+
return getErrorObject('InvalidAttr', "Attribute '" + attrName + "' is repeated.", getPositionFromMatch(matches[i]));
|
|
6752
6767
|
}
|
|
6753
6768
|
}
|
|
6754
6769
|
|
|
@@ -6827,50 +6842,162 @@
|
|
|
6827
6842
|
return match.startIndex + match[1].length;
|
|
6828
6843
|
}
|
|
6829
6844
|
|
|
6845
|
+
const defaultOnDangerousProperty = (name) => {
|
|
6846
|
+
if (DANGEROUS_PROPERTY_NAMES.includes(name)) {
|
|
6847
|
+
return "__" + name;
|
|
6848
|
+
}
|
|
6849
|
+
return name;
|
|
6850
|
+
};
|
|
6851
|
+
|
|
6852
|
+
|
|
6830
6853
|
const defaultOptions = {
|
|
6831
|
-
|
|
6832
|
-
|
|
6833
|
-
|
|
6834
|
-
|
|
6835
|
-
|
|
6836
|
-
|
|
6837
|
-
|
|
6838
|
-
|
|
6839
|
-
|
|
6840
|
-
|
|
6841
|
-
|
|
6842
|
-
|
|
6843
|
-
|
|
6844
|
-
|
|
6845
|
-
|
|
6846
|
-
|
|
6847
|
-
|
|
6848
|
-
|
|
6849
|
-
|
|
6850
|
-
|
|
6851
|
-
|
|
6852
|
-
|
|
6853
|
-
|
|
6854
|
-
|
|
6855
|
-
|
|
6856
|
-
|
|
6857
|
-
|
|
6858
|
-
|
|
6859
|
-
|
|
6860
|
-
|
|
6861
|
-
|
|
6862
|
-
|
|
6863
|
-
|
|
6864
|
-
|
|
6865
|
-
|
|
6866
|
-
|
|
6867
|
-
|
|
6868
|
-
|
|
6869
|
-
|
|
6854
|
+
preserveOrder: false,
|
|
6855
|
+
attributeNamePrefix: '@_',
|
|
6856
|
+
attributesGroupName: false,
|
|
6857
|
+
textNodeName: '#text',
|
|
6858
|
+
ignoreAttributes: true,
|
|
6859
|
+
removeNSPrefix: false, // remove NS from tag name or attribute name if true
|
|
6860
|
+
allowBooleanAttributes: false, //a tag can have attributes without any value
|
|
6861
|
+
//ignoreRootElement : false,
|
|
6862
|
+
parseTagValue: true,
|
|
6863
|
+
parseAttributeValue: false,
|
|
6864
|
+
trimValues: true, //Trim string values of tag and attributes
|
|
6865
|
+
cdataPropName: false,
|
|
6866
|
+
numberParseOptions: {
|
|
6867
|
+
hex: true,
|
|
6868
|
+
leadingZeros: true,
|
|
6869
|
+
eNotation: true
|
|
6870
|
+
},
|
|
6871
|
+
tagValueProcessor: function (tagName, val) {
|
|
6872
|
+
return val;
|
|
6873
|
+
},
|
|
6874
|
+
attributeValueProcessor: function (attrName, val) {
|
|
6875
|
+
return val;
|
|
6876
|
+
},
|
|
6877
|
+
stopNodes: [], //nested tags will not be parsed even for errors
|
|
6878
|
+
alwaysCreateTextNode: false,
|
|
6879
|
+
isArray: () => false,
|
|
6880
|
+
commentPropName: false,
|
|
6881
|
+
unpairedTags: [],
|
|
6882
|
+
processEntities: true,
|
|
6883
|
+
htmlEntities: false,
|
|
6884
|
+
ignoreDeclaration: false,
|
|
6885
|
+
ignorePiTags: false,
|
|
6886
|
+
transformTagName: false,
|
|
6887
|
+
transformAttributeName: false,
|
|
6888
|
+
updateTag: function (tagName, jPath, attrs) {
|
|
6889
|
+
return tagName
|
|
6890
|
+
},
|
|
6891
|
+
// skipEmptyListItem: false
|
|
6892
|
+
captureMetaData: false,
|
|
6893
|
+
maxNestedTags: 100,
|
|
6894
|
+
strictReservedNames: true,
|
|
6895
|
+
jPath: true, // if true, pass jPath string to callbacks; if false, pass matcher instance
|
|
6896
|
+
onDangerousProperty: defaultOnDangerousProperty
|
|
6870
6897
|
};
|
|
6871
|
-
|
|
6872
|
-
|
|
6873
|
-
|
|
6898
|
+
|
|
6899
|
+
|
|
6900
|
+
/**
|
|
6901
|
+
* Validates that a property name is safe to use
|
|
6902
|
+
* @param {string} propertyName - The property name to validate
|
|
6903
|
+
* @param {string} optionName - The option field name (for error message)
|
|
6904
|
+
* @throws {Error} If property name is dangerous
|
|
6905
|
+
*/
|
|
6906
|
+
function validatePropertyName(propertyName, optionName) {
|
|
6907
|
+
if (typeof propertyName !== 'string') {
|
|
6908
|
+
return; // Only validate string property names
|
|
6909
|
+
}
|
|
6910
|
+
|
|
6911
|
+
const normalized = propertyName.toLowerCase();
|
|
6912
|
+
if (DANGEROUS_PROPERTY_NAMES.some(dangerous => normalized === dangerous.toLowerCase())) {
|
|
6913
|
+
throw new Error(
|
|
6914
|
+
`[SECURITY] Invalid ${optionName}: "${propertyName}" is a reserved JavaScript keyword that could cause prototype pollution`
|
|
6915
|
+
);
|
|
6916
|
+
}
|
|
6917
|
+
|
|
6918
|
+
if (criticalProperties.some(dangerous => normalized === dangerous.toLowerCase())) {
|
|
6919
|
+
throw new Error(
|
|
6920
|
+
`[SECURITY] Invalid ${optionName}: "${propertyName}" is a reserved JavaScript keyword that could cause prototype pollution`
|
|
6921
|
+
);
|
|
6922
|
+
}
|
|
6923
|
+
}
|
|
6924
|
+
|
|
6925
|
+
/**
|
|
6926
|
+
* Normalizes processEntities option for backward compatibility
|
|
6927
|
+
* @param {boolean|object} value
|
|
6928
|
+
* @returns {object} Always returns normalized object
|
|
6929
|
+
*/
|
|
6930
|
+
function normalizeProcessEntities(value) {
|
|
6931
|
+
// Boolean backward compatibility
|
|
6932
|
+
if (typeof value === 'boolean') {
|
|
6933
|
+
return {
|
|
6934
|
+
enabled: value, // true or false
|
|
6935
|
+
maxEntitySize: 10000,
|
|
6936
|
+
maxExpansionDepth: 10,
|
|
6937
|
+
maxTotalExpansions: 1000,
|
|
6938
|
+
maxExpandedLength: 100000,
|
|
6939
|
+
maxEntityCount: 100,
|
|
6940
|
+
allowedTags: null,
|
|
6941
|
+
tagFilter: null
|
|
6942
|
+
};
|
|
6943
|
+
}
|
|
6944
|
+
|
|
6945
|
+
// Object config - merge with defaults
|
|
6946
|
+
if (typeof value === 'object' && value !== null) {
|
|
6947
|
+
return {
|
|
6948
|
+
enabled: value.enabled !== false, // default true if not specified
|
|
6949
|
+
maxEntitySize: value.maxEntitySize ?? 10000,
|
|
6950
|
+
maxExpansionDepth: value.maxExpansionDepth ?? 10,
|
|
6951
|
+
maxTotalExpansions: value.maxTotalExpansions ?? 1000,
|
|
6952
|
+
maxExpandedLength: value.maxExpandedLength ?? 100000,
|
|
6953
|
+
maxEntityCount: value.maxEntityCount ?? 100,
|
|
6954
|
+
allowedTags: value.allowedTags ?? null,
|
|
6955
|
+
tagFilter: value.tagFilter ?? null
|
|
6956
|
+
};
|
|
6957
|
+
}
|
|
6958
|
+
|
|
6959
|
+
// Default to enabled with limits
|
|
6960
|
+
return normalizeProcessEntities(true);
|
|
6961
|
+
}
|
|
6962
|
+
|
|
6963
|
+
const buildOptions = function (options) {
|
|
6964
|
+
const built = Object.assign({}, defaultOptions, options);
|
|
6965
|
+
|
|
6966
|
+
// Validate property names to prevent prototype pollution
|
|
6967
|
+
const propertyNameOptions = [
|
|
6968
|
+
{ value: built.attributeNamePrefix, name: 'attributeNamePrefix' },
|
|
6969
|
+
{ value: built.attributesGroupName, name: 'attributesGroupName' },
|
|
6970
|
+
{ value: built.textNodeName, name: 'textNodeName' },
|
|
6971
|
+
{ value: built.cdataPropName, name: 'cdataPropName' },
|
|
6972
|
+
{ value: built.commentPropName, name: 'commentPropName' }
|
|
6973
|
+
];
|
|
6974
|
+
|
|
6975
|
+
for (const { value, name } of propertyNameOptions) {
|
|
6976
|
+
if (value) {
|
|
6977
|
+
validatePropertyName(value, name);
|
|
6978
|
+
}
|
|
6979
|
+
}
|
|
6980
|
+
|
|
6981
|
+
if (built.onDangerousProperty === null) {
|
|
6982
|
+
built.onDangerousProperty = defaultOnDangerousProperty;
|
|
6983
|
+
}
|
|
6984
|
+
|
|
6985
|
+
// Always normalize processEntities for backward compatibility and validation
|
|
6986
|
+
built.processEntities = normalizeProcessEntities(built.processEntities);
|
|
6987
|
+
|
|
6988
|
+
// Convert old-style stopNodes for backward compatibility
|
|
6989
|
+
if (built.stopNodes && Array.isArray(built.stopNodes)) {
|
|
6990
|
+
built.stopNodes = built.stopNodes.map(node => {
|
|
6991
|
+
if (typeof node === 'string' && node.startsWith('*.')) {
|
|
6992
|
+
// Old syntax: *.tagname meant "tagname anywhere"
|
|
6993
|
+
// Convert to new syntax: ..tagname
|
|
6994
|
+
return '..' + node.substring(2);
|
|
6995
|
+
}
|
|
6996
|
+
return node;
|
|
6997
|
+
});
|
|
6998
|
+
}
|
|
6999
|
+
//console.debug(built.processEntities)
|
|
7000
|
+
return built;
|
|
6874
7001
|
};
|
|
6875
7002
|
|
|
6876
7003
|
let METADATA_SYMBOL$1;
|
|
@@ -6881,23 +7008,23 @@
|
|
|
6881
7008
|
METADATA_SYMBOL$1 = Symbol("XML Node Metadata");
|
|
6882
7009
|
}
|
|
6883
7010
|
|
|
6884
|
-
class XmlNode{
|
|
7011
|
+
class XmlNode {
|
|
6885
7012
|
constructor(tagname) {
|
|
6886
7013
|
this.tagname = tagname;
|
|
6887
7014
|
this.child = []; //nested tags, text, cdata, comments in order
|
|
6888
|
-
this[":@"] =
|
|
7015
|
+
this[":@"] = Object.create(null); //attributes map
|
|
6889
7016
|
}
|
|
6890
|
-
add(key,val){
|
|
7017
|
+
add(key, val) {
|
|
6891
7018
|
// this.child.push( {name : key, val: val, isCdata: isCdata });
|
|
6892
|
-
if(key === "__proto__") key = "#__proto__";
|
|
6893
|
-
this.child.push(
|
|
7019
|
+
if (key === "__proto__") key = "#__proto__";
|
|
7020
|
+
this.child.push({ [key]: val });
|
|
6894
7021
|
}
|
|
6895
7022
|
addChild(node, startIndex) {
|
|
6896
|
-
if(node.tagname === "__proto__") node.tagname = "#__proto__";
|
|
6897
|
-
if(node[":@"] && Object.keys(node[":@"]).length > 0){
|
|
6898
|
-
this.child.push(
|
|
6899
|
-
}else {
|
|
6900
|
-
this.child.push(
|
|
7023
|
+
if (node.tagname === "__proto__") node.tagname = "#__proto__";
|
|
7024
|
+
if (node[":@"] && Object.keys(node[":@"]).length > 0) {
|
|
7025
|
+
this.child.push({ [node.tagname]: node.child, [":@"]: node[":@"] });
|
|
7026
|
+
} else {
|
|
7027
|
+
this.child.push({ [node.tagname]: node.child });
|
|
6901
7028
|
}
|
|
6902
7029
|
// if requested, add the startIndex
|
|
6903
7030
|
if (startIndex !== undefined) {
|
|
@@ -6912,81 +7039,92 @@
|
|
|
6912
7039
|
}
|
|
6913
7040
|
}
|
|
6914
7041
|
|
|
6915
|
-
class DocTypeReader{
|
|
6916
|
-
constructor(
|
|
6917
|
-
this.suppressValidationErr = !
|
|
7042
|
+
class DocTypeReader {
|
|
7043
|
+
constructor(options) {
|
|
7044
|
+
this.suppressValidationErr = !options;
|
|
7045
|
+
this.options = options;
|
|
6918
7046
|
}
|
|
6919
|
-
|
|
6920
|
-
readDocType(xmlData, i){
|
|
6921
|
-
|
|
6922
|
-
|
|
6923
|
-
|
|
7047
|
+
|
|
7048
|
+
readDocType(xmlData, i) {
|
|
7049
|
+
const entities = Object.create(null);
|
|
7050
|
+
let entityCount = 0;
|
|
7051
|
+
|
|
7052
|
+
if (xmlData[i + 3] === 'O' &&
|
|
6924
7053
|
xmlData[i + 4] === 'C' &&
|
|
6925
7054
|
xmlData[i + 5] === 'T' &&
|
|
6926
7055
|
xmlData[i + 6] === 'Y' &&
|
|
6927
7056
|
xmlData[i + 7] === 'P' &&
|
|
6928
|
-
xmlData[i + 8] === 'E')
|
|
6929
|
-
|
|
6930
|
-
i = i+9;
|
|
7057
|
+
xmlData[i + 8] === 'E') {
|
|
7058
|
+
i = i + 9;
|
|
6931
7059
|
let angleBracketsCount = 1;
|
|
6932
7060
|
let hasBody = false, comment = false;
|
|
6933
7061
|
let exp = "";
|
|
6934
|
-
for(;i<xmlData.length;i++){
|
|
7062
|
+
for (; i < xmlData.length; i++) {
|
|
6935
7063
|
if (xmlData[i] === '<' && !comment) { //Determine the tag type
|
|
6936
|
-
if(
|
|
6937
|
-
i += 7;
|
|
7064
|
+
if (hasBody && hasSeq(xmlData, "!ENTITY", i)) {
|
|
7065
|
+
i += 7;
|
|
6938
7066
|
let entityName, val;
|
|
6939
|
-
[entityName, val,i] = this.readEntityExp(xmlData,i+1,this.suppressValidationErr);
|
|
6940
|
-
if(val.indexOf("&") === -1) //Parameter entities are not supported
|
|
6941
|
-
|
|
6942
|
-
|
|
7067
|
+
[entityName, val, i] = this.readEntityExp(xmlData, i + 1, this.suppressValidationErr);
|
|
7068
|
+
if (val.indexOf("&") === -1) { //Parameter entities are not supported
|
|
7069
|
+
if (this.options.enabled !== false &&
|
|
7070
|
+
this.options.maxEntityCount &&
|
|
7071
|
+
entityCount >= this.options.maxEntityCount) {
|
|
7072
|
+
throw new Error(
|
|
7073
|
+
`Entity count (${entityCount + 1}) exceeds maximum allowed (${this.options.maxEntityCount})`
|
|
7074
|
+
);
|
|
7075
|
+
}
|
|
7076
|
+
const escaped = entityName.replace(/[.\-+*:]/g, '\\.');
|
|
7077
|
+
entities[entityName] = {
|
|
7078
|
+
regx: RegExp(`&${escaped};`, "g"),
|
|
6943
7079
|
val: val
|
|
6944
7080
|
};
|
|
7081
|
+
entityCount++;
|
|
7082
|
+
}
|
|
6945
7083
|
}
|
|
6946
|
-
else if(
|
|
7084
|
+
else if (hasBody && hasSeq(xmlData, "!ELEMENT", i)) {
|
|
6947
7085
|
i += 8;//Not supported
|
|
6948
|
-
const {index} = this.readElementExp(xmlData,i+1);
|
|
7086
|
+
const { index } = this.readElementExp(xmlData, i + 1);
|
|
6949
7087
|
i = index;
|
|
6950
|
-
}else if(
|
|
7088
|
+
} else if (hasBody && hasSeq(xmlData, "!ATTLIST", i)) {
|
|
6951
7089
|
i += 8;//Not supported
|
|
6952
7090
|
// const {index} = this.readAttlistExp(xmlData,i+1);
|
|
6953
7091
|
// i = index;
|
|
6954
|
-
}else if(
|
|
7092
|
+
} else if (hasBody && hasSeq(xmlData, "!NOTATION", i)) {
|
|
6955
7093
|
i += 9;//Not supported
|
|
6956
|
-
const {index} = this.readNotationExp(xmlData,i+1,this.suppressValidationErr);
|
|
7094
|
+
const { index } = this.readNotationExp(xmlData, i + 1, this.suppressValidationErr);
|
|
6957
7095
|
i = index;
|
|
6958
|
-
}else if(
|
|
7096
|
+
} else if (hasSeq(xmlData, "!--", i)) comment = true;
|
|
6959
7097
|
else throw new Error(`Invalid DOCTYPE`);
|
|
6960
7098
|
|
|
6961
7099
|
angleBracketsCount++;
|
|
6962
7100
|
exp = "";
|
|
6963
7101
|
} else if (xmlData[i] === '>') { //Read tag content
|
|
6964
|
-
if(comment){
|
|
6965
|
-
if(
|
|
7102
|
+
if (comment) {
|
|
7103
|
+
if (xmlData[i - 1] === "-" && xmlData[i - 2] === "-") {
|
|
6966
7104
|
comment = false;
|
|
6967
7105
|
angleBracketsCount--;
|
|
6968
7106
|
}
|
|
6969
|
-
}else {
|
|
7107
|
+
} else {
|
|
6970
7108
|
angleBracketsCount--;
|
|
6971
7109
|
}
|
|
6972
7110
|
if (angleBracketsCount === 0) {
|
|
6973
|
-
|
|
7111
|
+
break;
|
|
6974
7112
|
}
|
|
6975
|
-
}else if(
|
|
7113
|
+
} else if (xmlData[i] === '[') {
|
|
6976
7114
|
hasBody = true;
|
|
6977
|
-
}else {
|
|
7115
|
+
} else {
|
|
6978
7116
|
exp += xmlData[i];
|
|
6979
7117
|
}
|
|
6980
7118
|
}
|
|
6981
|
-
if(angleBracketsCount !== 0){
|
|
7119
|
+
if (angleBracketsCount !== 0) {
|
|
6982
7120
|
throw new Error(`Unclosed DOCTYPE`);
|
|
6983
7121
|
}
|
|
6984
|
-
}else {
|
|
7122
|
+
} else {
|
|
6985
7123
|
throw new Error(`Invalid Tag instead of DOCTYPE`);
|
|
6986
7124
|
}
|
|
6987
|
-
return {entities, i};
|
|
7125
|
+
return { entities, i };
|
|
6988
7126
|
}
|
|
6989
|
-
readEntityExp(xmlData, i) {
|
|
7127
|
+
readEntityExp(xmlData, i) {
|
|
6990
7128
|
//External entities are not supported
|
|
6991
7129
|
// <!ENTITY ext SYSTEM "http://normal-website.com" >
|
|
6992
7130
|
|
|
@@ -7011,10 +7149,10 @@
|
|
|
7011
7149
|
i = skipWhitespace(xmlData, i);
|
|
7012
7150
|
|
|
7013
7151
|
// Check for unsupported constructs (external entities or parameter entities)
|
|
7014
|
-
if(!this.suppressValidationErr){
|
|
7152
|
+
if (!this.suppressValidationErr) {
|
|
7015
7153
|
if (xmlData.substring(i, i + 6).toUpperCase() === "SYSTEM") {
|
|
7016
7154
|
throw new Error("External entities are not supported");
|
|
7017
|
-
}else if (xmlData[i] === "%") {
|
|
7155
|
+
} else if (xmlData[i] === "%") {
|
|
7018
7156
|
throw new Error("Parameter entities are not supported");
|
|
7019
7157
|
}
|
|
7020
7158
|
}
|
|
@@ -7022,8 +7160,18 @@
|
|
|
7022
7160
|
// Read entity value (internal entity)
|
|
7023
7161
|
let entityValue = "";
|
|
7024
7162
|
[i, entityValue] = this.readIdentifierVal(xmlData, i, "entity");
|
|
7163
|
+
|
|
7164
|
+
// Validate entity size
|
|
7165
|
+
if (this.options.enabled !== false &&
|
|
7166
|
+
this.options.maxEntitySize &&
|
|
7167
|
+
entityValue.length > this.options.maxEntitySize) {
|
|
7168
|
+
throw new Error(
|
|
7169
|
+
`Entity "${entityName}" size (${entityValue.length}) exceeds maximum allowed size (${this.options.maxEntitySize})`
|
|
7170
|
+
);
|
|
7171
|
+
}
|
|
7172
|
+
|
|
7025
7173
|
i--;
|
|
7026
|
-
return [entityName, entityValue, i
|
|
7174
|
+
return [entityName, entityValue, i];
|
|
7027
7175
|
}
|
|
7028
7176
|
|
|
7029
7177
|
readNotationExp(xmlData, i) {
|
|
@@ -7056,25 +7204,25 @@
|
|
|
7056
7204
|
let systemIdentifier = null;
|
|
7057
7205
|
|
|
7058
7206
|
if (identifierType === "PUBLIC") {
|
|
7059
|
-
[i, publicIdentifier
|
|
7207
|
+
[i, publicIdentifier] = this.readIdentifierVal(xmlData, i, "publicIdentifier");
|
|
7060
7208
|
|
|
7061
7209
|
// Skip whitespace after public identifier
|
|
7062
7210
|
i = skipWhitespace(xmlData, i);
|
|
7063
7211
|
|
|
7064
7212
|
// Optionally read system identifier
|
|
7065
7213
|
if (xmlData[i] === '"' || xmlData[i] === "'") {
|
|
7066
|
-
[i, systemIdentifier
|
|
7214
|
+
[i, systemIdentifier] = this.readIdentifierVal(xmlData, i, "systemIdentifier");
|
|
7067
7215
|
}
|
|
7068
7216
|
} else if (identifierType === "SYSTEM") {
|
|
7069
7217
|
// Read system identifier (mandatory for SYSTEM)
|
|
7070
|
-
[i, systemIdentifier
|
|
7218
|
+
[i, systemIdentifier] = this.readIdentifierVal(xmlData, i, "systemIdentifier");
|
|
7071
7219
|
|
|
7072
7220
|
if (!this.suppressValidationErr && !systemIdentifier) {
|
|
7073
7221
|
throw new Error("Missing mandatory system identifier for SYSTEM notation");
|
|
7074
7222
|
}
|
|
7075
7223
|
}
|
|
7076
|
-
|
|
7077
|
-
return {notationName, publicIdentifier, systemIdentifier, index: --i};
|
|
7224
|
+
|
|
7225
|
+
return { notationName, publicIdentifier, systemIdentifier, index: --i };
|
|
7078
7226
|
}
|
|
7079
7227
|
|
|
7080
7228
|
readIdentifierVal(xmlData, i, type) {
|
|
@@ -7103,7 +7251,7 @@
|
|
|
7103
7251
|
// <!ELEMENT title (#PCDATA)>
|
|
7104
7252
|
// <!ELEMENT book (title, author+)>
|
|
7105
7253
|
// <!ELEMENT name (content-model)>
|
|
7106
|
-
|
|
7254
|
+
|
|
7107
7255
|
// Skip leading whitespace after <!ELEMENT
|
|
7108
7256
|
i = skipWhitespace(xmlData, i);
|
|
7109
7257
|
|
|
@@ -7123,8 +7271,8 @@
|
|
|
7123
7271
|
i = skipWhitespace(xmlData, i);
|
|
7124
7272
|
let contentModel = "";
|
|
7125
7273
|
// Expect '(' to start content model
|
|
7126
|
-
if(xmlData[i] === "E" && hasSeq(xmlData, "MPTY",i)) i+=4;
|
|
7127
|
-
else if(xmlData[i] === "A" && hasSeq(xmlData, "NY",i)) i+=2;
|
|
7274
|
+
if (xmlData[i] === "E" && hasSeq(xmlData, "MPTY", i)) i += 4;
|
|
7275
|
+
else if (xmlData[i] === "A" && hasSeq(xmlData, "NY", i)) i += 2;
|
|
7128
7276
|
else if (xmlData[i] === "(") {
|
|
7129
7277
|
i++; // Move past '('
|
|
7130
7278
|
|
|
@@ -7137,10 +7285,10 @@
|
|
|
7137
7285
|
throw new Error("Unterminated content model");
|
|
7138
7286
|
}
|
|
7139
7287
|
|
|
7140
|
-
}else if(!this.suppressValidationErr){
|
|
7288
|
+
} else if (!this.suppressValidationErr) {
|
|
7141
7289
|
throw new Error(`Invalid Element Expression, found "${xmlData[i]}"`);
|
|
7142
7290
|
}
|
|
7143
|
-
|
|
7291
|
+
|
|
7144
7292
|
return {
|
|
7145
7293
|
elementName,
|
|
7146
7294
|
contentModel: contentModel.trim(),
|
|
@@ -7276,16 +7424,16 @@
|
|
|
7276
7424
|
|
|
7277
7425
|
|
|
7278
7426
|
|
|
7279
|
-
function hasSeq(data, seq,i){
|
|
7280
|
-
for(let j=0;j<seq.length;j++){
|
|
7281
|
-
if(seq[j]!==data[i+j+1]) return false;
|
|
7427
|
+
function hasSeq(data, seq, i) {
|
|
7428
|
+
for (let j = 0; j < seq.length; j++) {
|
|
7429
|
+
if (seq[j] !== data[i + j + 1]) return false;
|
|
7282
7430
|
}
|
|
7283
7431
|
return true;
|
|
7284
7432
|
}
|
|
7285
7433
|
|
|
7286
|
-
function validateEntityName(name){
|
|
7434
|
+
function validateEntityName(name) {
|
|
7287
7435
|
if (isName(name))
|
|
7288
|
-
|
|
7436
|
+
return name;
|
|
7289
7437
|
else
|
|
7290
7438
|
throw new Error(`Invalid entity name ${name}`);
|
|
7291
7439
|
}
|
|
@@ -7439,6 +7587,654 @@
|
|
|
7439
7587
|
return () => false
|
|
7440
7588
|
}
|
|
7441
7589
|
|
|
7590
|
+
/**
|
|
7591
|
+
* Expression - Parses and stores a tag pattern expression
|
|
7592
|
+
*
|
|
7593
|
+
* Patterns are parsed once and stored in an optimized structure for fast matching.
|
|
7594
|
+
*
|
|
7595
|
+
* @example
|
|
7596
|
+
* const expr = new Expression("root.users.user");
|
|
7597
|
+
* const expr2 = new Expression("..user[id]:first");
|
|
7598
|
+
* const expr3 = new Expression("root/users/user", { separator: '/' });
|
|
7599
|
+
*/
|
|
7600
|
+
class Expression {
|
|
7601
|
+
/**
|
|
7602
|
+
* Create a new Expression
|
|
7603
|
+
* @param {string} pattern - Pattern string (e.g., "root.users.user", "..user[id]")
|
|
7604
|
+
* @param {Object} options - Configuration options
|
|
7605
|
+
* @param {string} options.separator - Path separator (default: '.')
|
|
7606
|
+
*/
|
|
7607
|
+
constructor(pattern, options = {}) {
|
|
7608
|
+
this.pattern = pattern;
|
|
7609
|
+
this.separator = options.separator || '.';
|
|
7610
|
+
this.segments = this._parse(pattern);
|
|
7611
|
+
|
|
7612
|
+
// Cache expensive checks for performance (O(1) instead of O(n))
|
|
7613
|
+
this._hasDeepWildcard = this.segments.some(seg => seg.type === 'deep-wildcard');
|
|
7614
|
+
this._hasAttributeCondition = this.segments.some(seg => seg.attrName !== undefined);
|
|
7615
|
+
this._hasPositionSelector = this.segments.some(seg => seg.position !== undefined);
|
|
7616
|
+
}
|
|
7617
|
+
|
|
7618
|
+
/**
|
|
7619
|
+
* Parse pattern string into segments
|
|
7620
|
+
* @private
|
|
7621
|
+
* @param {string} pattern - Pattern to parse
|
|
7622
|
+
* @returns {Array} Array of segment objects
|
|
7623
|
+
*/
|
|
7624
|
+
_parse(pattern) {
|
|
7625
|
+
const segments = [];
|
|
7626
|
+
|
|
7627
|
+
// Split by separator but handle ".." specially
|
|
7628
|
+
let i = 0;
|
|
7629
|
+
let currentPart = '';
|
|
7630
|
+
|
|
7631
|
+
while (i < pattern.length) {
|
|
7632
|
+
if (pattern[i] === this.separator) {
|
|
7633
|
+
// Check if next char is also separator (deep wildcard)
|
|
7634
|
+
if (i + 1 < pattern.length && pattern[i + 1] === this.separator) {
|
|
7635
|
+
// Flush current part if any
|
|
7636
|
+
if (currentPart.trim()) {
|
|
7637
|
+
segments.push(this._parseSegment(currentPart.trim()));
|
|
7638
|
+
currentPart = '';
|
|
7639
|
+
}
|
|
7640
|
+
// Add deep wildcard
|
|
7641
|
+
segments.push({ type: 'deep-wildcard' });
|
|
7642
|
+
i += 2; // Skip both separators
|
|
7643
|
+
} else {
|
|
7644
|
+
// Regular separator
|
|
7645
|
+
if (currentPart.trim()) {
|
|
7646
|
+
segments.push(this._parseSegment(currentPart.trim()));
|
|
7647
|
+
}
|
|
7648
|
+
currentPart = '';
|
|
7649
|
+
i++;
|
|
7650
|
+
}
|
|
7651
|
+
} else {
|
|
7652
|
+
currentPart += pattern[i];
|
|
7653
|
+
i++;
|
|
7654
|
+
}
|
|
7655
|
+
}
|
|
7656
|
+
|
|
7657
|
+
// Flush remaining part
|
|
7658
|
+
if (currentPart.trim()) {
|
|
7659
|
+
segments.push(this._parseSegment(currentPart.trim()));
|
|
7660
|
+
}
|
|
7661
|
+
|
|
7662
|
+
return segments;
|
|
7663
|
+
}
|
|
7664
|
+
|
|
7665
|
+
/**
|
|
7666
|
+
* Parse a single segment
|
|
7667
|
+
* @private
|
|
7668
|
+
* @param {string} part - Segment string (e.g., "user", "ns::user", "user[id]", "ns::user:first")
|
|
7669
|
+
* @returns {Object} Segment object
|
|
7670
|
+
*/
|
|
7671
|
+
_parseSegment(part) {
|
|
7672
|
+
const segment = { type: 'tag' };
|
|
7673
|
+
|
|
7674
|
+
// NEW NAMESPACE SYNTAX (v2.0):
|
|
7675
|
+
// ============================
|
|
7676
|
+
// Namespace uses DOUBLE colon (::)
|
|
7677
|
+
// Position uses SINGLE colon (:)
|
|
7678
|
+
//
|
|
7679
|
+
// Examples:
|
|
7680
|
+
// "user" → tag
|
|
7681
|
+
// "user:first" → tag + position
|
|
7682
|
+
// "user[id]" → tag + attribute
|
|
7683
|
+
// "user[id]:first" → tag + attribute + position
|
|
7684
|
+
// "ns::user" → namespace + tag
|
|
7685
|
+
// "ns::user:first" → namespace + tag + position
|
|
7686
|
+
// "ns::user[id]" → namespace + tag + attribute
|
|
7687
|
+
// "ns::user[id]:first" → namespace + tag + attribute + position
|
|
7688
|
+
// "ns::first" → namespace + tag named "first" (NO ambiguity!)
|
|
7689
|
+
//
|
|
7690
|
+
// This eliminates all ambiguity:
|
|
7691
|
+
// :: = namespace separator
|
|
7692
|
+
// : = position selector
|
|
7693
|
+
// [] = attributes
|
|
7694
|
+
|
|
7695
|
+
// Step 1: Extract brackets [attr] or [attr=value]
|
|
7696
|
+
let bracketContent = null;
|
|
7697
|
+
let withoutBrackets = part;
|
|
7698
|
+
|
|
7699
|
+
const bracketMatch = part.match(/^([^\[]+)(\[[^\]]*\])(.*)$/);
|
|
7700
|
+
if (bracketMatch) {
|
|
7701
|
+
withoutBrackets = bracketMatch[1] + bracketMatch[3];
|
|
7702
|
+
if (bracketMatch[2]) {
|
|
7703
|
+
const content = bracketMatch[2].slice(1, -1);
|
|
7704
|
+
if (content) {
|
|
7705
|
+
bracketContent = content;
|
|
7706
|
+
}
|
|
7707
|
+
}
|
|
7708
|
+
}
|
|
7709
|
+
|
|
7710
|
+
// Step 2: Check for namespace (double colon ::)
|
|
7711
|
+
let namespace = undefined;
|
|
7712
|
+
let tagAndPosition = withoutBrackets;
|
|
7713
|
+
|
|
7714
|
+
if (withoutBrackets.includes('::')) {
|
|
7715
|
+
const nsIndex = withoutBrackets.indexOf('::');
|
|
7716
|
+
namespace = withoutBrackets.substring(0, nsIndex).trim();
|
|
7717
|
+
tagAndPosition = withoutBrackets.substring(nsIndex + 2).trim(); // Skip ::
|
|
7718
|
+
|
|
7719
|
+
if (!namespace) {
|
|
7720
|
+
throw new Error(`Invalid namespace in pattern: ${part}`);
|
|
7721
|
+
}
|
|
7722
|
+
}
|
|
7723
|
+
|
|
7724
|
+
// Step 3: Parse tag and position (single colon :)
|
|
7725
|
+
let tag = undefined;
|
|
7726
|
+
let positionMatch = null;
|
|
7727
|
+
|
|
7728
|
+
if (tagAndPosition.includes(':')) {
|
|
7729
|
+
const colonIndex = tagAndPosition.lastIndexOf(':'); // Use last colon for position
|
|
7730
|
+
const tagPart = tagAndPosition.substring(0, colonIndex).trim();
|
|
7731
|
+
const posPart = tagAndPosition.substring(colonIndex + 1).trim();
|
|
7732
|
+
|
|
7733
|
+
// Verify position is a valid keyword
|
|
7734
|
+
const isPositionKeyword = ['first', 'last', 'odd', 'even'].includes(posPart) ||
|
|
7735
|
+
/^nth\(\d+\)$/.test(posPart);
|
|
7736
|
+
|
|
7737
|
+
if (isPositionKeyword) {
|
|
7738
|
+
tag = tagPart;
|
|
7739
|
+
positionMatch = posPart;
|
|
7740
|
+
} else {
|
|
7741
|
+
// Not a valid position keyword, treat whole thing as tag
|
|
7742
|
+
tag = tagAndPosition;
|
|
7743
|
+
}
|
|
7744
|
+
} else {
|
|
7745
|
+
tag = tagAndPosition;
|
|
7746
|
+
}
|
|
7747
|
+
|
|
7748
|
+
if (!tag) {
|
|
7749
|
+
throw new Error(`Invalid segment pattern: ${part}`);
|
|
7750
|
+
}
|
|
7751
|
+
|
|
7752
|
+
segment.tag = tag;
|
|
7753
|
+
if (namespace) {
|
|
7754
|
+
segment.namespace = namespace;
|
|
7755
|
+
}
|
|
7756
|
+
|
|
7757
|
+
// Step 4: Parse attributes
|
|
7758
|
+
if (bracketContent) {
|
|
7759
|
+
if (bracketContent.includes('=')) {
|
|
7760
|
+
const eqIndex = bracketContent.indexOf('=');
|
|
7761
|
+
segment.attrName = bracketContent.substring(0, eqIndex).trim();
|
|
7762
|
+
segment.attrValue = bracketContent.substring(eqIndex + 1).trim();
|
|
7763
|
+
} else {
|
|
7764
|
+
segment.attrName = bracketContent.trim();
|
|
7765
|
+
}
|
|
7766
|
+
}
|
|
7767
|
+
|
|
7768
|
+
// Step 5: Parse position selector
|
|
7769
|
+
if (positionMatch) {
|
|
7770
|
+
const nthMatch = positionMatch.match(/^nth\((\d+)\)$/);
|
|
7771
|
+
if (nthMatch) {
|
|
7772
|
+
segment.position = 'nth';
|
|
7773
|
+
segment.positionValue = parseInt(nthMatch[1], 10);
|
|
7774
|
+
} else {
|
|
7775
|
+
segment.position = positionMatch;
|
|
7776
|
+
}
|
|
7777
|
+
}
|
|
7778
|
+
|
|
7779
|
+
return segment;
|
|
7780
|
+
}
|
|
7781
|
+
|
|
7782
|
+
/**
|
|
7783
|
+
* Get the number of segments
|
|
7784
|
+
* @returns {number}
|
|
7785
|
+
*/
|
|
7786
|
+
get length() {
|
|
7787
|
+
return this.segments.length;
|
|
7788
|
+
}
|
|
7789
|
+
|
|
7790
|
+
/**
|
|
7791
|
+
* Check if expression contains deep wildcard
|
|
7792
|
+
* @returns {boolean}
|
|
7793
|
+
*/
|
|
7794
|
+
hasDeepWildcard() {
|
|
7795
|
+
return this._hasDeepWildcard;
|
|
7796
|
+
}
|
|
7797
|
+
|
|
7798
|
+
/**
|
|
7799
|
+
* Check if expression has attribute conditions
|
|
7800
|
+
* @returns {boolean}
|
|
7801
|
+
*/
|
|
7802
|
+
hasAttributeCondition() {
|
|
7803
|
+
return this._hasAttributeCondition;
|
|
7804
|
+
}
|
|
7805
|
+
|
|
7806
|
+
/**
|
|
7807
|
+
* Check if expression has position selectors
|
|
7808
|
+
* @returns {boolean}
|
|
7809
|
+
*/
|
|
7810
|
+
hasPositionSelector() {
|
|
7811
|
+
return this._hasPositionSelector;
|
|
7812
|
+
}
|
|
7813
|
+
|
|
7814
|
+
/**
|
|
7815
|
+
* Get string representation
|
|
7816
|
+
* @returns {string}
|
|
7817
|
+
*/
|
|
7818
|
+
toString() {
|
|
7819
|
+
return this.pattern;
|
|
7820
|
+
}
|
|
7821
|
+
}
|
|
7822
|
+
|
|
7823
|
+
/**
|
|
7824
|
+
* Matcher - Tracks current path in XML/JSON tree and matches against Expressions
|
|
7825
|
+
*
|
|
7826
|
+
* The matcher maintains a stack of nodes representing the current path from root to
|
|
7827
|
+
* current tag. It only stores attribute values for the current (top) node to minimize
|
|
7828
|
+
* memory usage. Sibling tracking is used to auto-calculate position and counter.
|
|
7829
|
+
*
|
|
7830
|
+
* @example
|
|
7831
|
+
* const matcher = new Matcher();
|
|
7832
|
+
* matcher.push("root", {});
|
|
7833
|
+
* matcher.push("users", {});
|
|
7834
|
+
* matcher.push("user", { id: "123", type: "admin" });
|
|
7835
|
+
*
|
|
7836
|
+
* const expr = new Expression("root.users.user");
|
|
7837
|
+
* matcher.matches(expr); // true
|
|
7838
|
+
*/
|
|
7839
|
+
class Matcher {
|
|
7840
|
+
/**
|
|
7841
|
+
* Create a new Matcher
|
|
7842
|
+
* @param {Object} options - Configuration options
|
|
7843
|
+
* @param {string} options.separator - Default path separator (default: '.')
|
|
7844
|
+
*/
|
|
7845
|
+
constructor(options = {}) {
|
|
7846
|
+
this.separator = options.separator || '.';
|
|
7847
|
+
this.path = [];
|
|
7848
|
+
this.siblingStacks = [];
|
|
7849
|
+
// Each path node: { tag: string, values: object, position: number, counter: number }
|
|
7850
|
+
// values only present for current (last) node
|
|
7851
|
+
// Each siblingStacks entry: Map<tagName, count> tracking occurrences at each level
|
|
7852
|
+
}
|
|
7853
|
+
|
|
7854
|
+
/**
|
|
7855
|
+
* Push a new tag onto the path
|
|
7856
|
+
* @param {string} tagName - Name of the tag
|
|
7857
|
+
* @param {Object} attrValues - Attribute key-value pairs for current node (optional)
|
|
7858
|
+
* @param {string} namespace - Namespace for the tag (optional)
|
|
7859
|
+
*/
|
|
7860
|
+
push(tagName, attrValues = null, namespace = null) {
|
|
7861
|
+
// Remove values from previous current node (now becoming ancestor)
|
|
7862
|
+
if (this.path.length > 0) {
|
|
7863
|
+
const prev = this.path[this.path.length - 1];
|
|
7864
|
+
prev.values = undefined;
|
|
7865
|
+
}
|
|
7866
|
+
|
|
7867
|
+
// Get or create sibling tracking for current level
|
|
7868
|
+
const currentLevel = this.path.length;
|
|
7869
|
+
if (!this.siblingStacks[currentLevel]) {
|
|
7870
|
+
this.siblingStacks[currentLevel] = new Map();
|
|
7871
|
+
}
|
|
7872
|
+
|
|
7873
|
+
const siblings = this.siblingStacks[currentLevel];
|
|
7874
|
+
|
|
7875
|
+
// Create a unique key for sibling tracking that includes namespace
|
|
7876
|
+
const siblingKey = namespace ? `${namespace}:${tagName}` : tagName;
|
|
7877
|
+
|
|
7878
|
+
// Calculate counter (how many times this tag appeared at this level)
|
|
7879
|
+
const counter = siblings.get(siblingKey) || 0;
|
|
7880
|
+
|
|
7881
|
+
// Calculate position (total children at this level so far)
|
|
7882
|
+
let position = 0;
|
|
7883
|
+
for (const count of siblings.values()) {
|
|
7884
|
+
position += count;
|
|
7885
|
+
}
|
|
7886
|
+
|
|
7887
|
+
// Update sibling count for this tag
|
|
7888
|
+
siblings.set(siblingKey, counter + 1);
|
|
7889
|
+
|
|
7890
|
+
// Create new node
|
|
7891
|
+
const node = {
|
|
7892
|
+
tag: tagName,
|
|
7893
|
+
position: position,
|
|
7894
|
+
counter: counter
|
|
7895
|
+
};
|
|
7896
|
+
|
|
7897
|
+
// Store namespace if provided
|
|
7898
|
+
if (namespace !== null && namespace !== undefined) {
|
|
7899
|
+
node.namespace = namespace;
|
|
7900
|
+
}
|
|
7901
|
+
|
|
7902
|
+
// Store values only for current node
|
|
7903
|
+
if (attrValues !== null && attrValues !== undefined) {
|
|
7904
|
+
node.values = attrValues;
|
|
7905
|
+
}
|
|
7906
|
+
|
|
7907
|
+
this.path.push(node);
|
|
7908
|
+
}
|
|
7909
|
+
|
|
7910
|
+
/**
|
|
7911
|
+
* Pop the last tag from the path
|
|
7912
|
+
* @returns {Object|undefined} The popped node
|
|
7913
|
+
*/
|
|
7914
|
+
pop() {
|
|
7915
|
+
if (this.path.length === 0) {
|
|
7916
|
+
return undefined;
|
|
7917
|
+
}
|
|
7918
|
+
|
|
7919
|
+
const node = this.path.pop();
|
|
7920
|
+
|
|
7921
|
+
// Clean up sibling tracking for levels deeper than current
|
|
7922
|
+
// After pop, path.length is the new depth
|
|
7923
|
+
// We need to clean up siblingStacks[path.length + 1] and beyond
|
|
7924
|
+
if (this.siblingStacks.length > this.path.length + 1) {
|
|
7925
|
+
this.siblingStacks.length = this.path.length + 1;
|
|
7926
|
+
}
|
|
7927
|
+
|
|
7928
|
+
return node;
|
|
7929
|
+
}
|
|
7930
|
+
|
|
7931
|
+
/**
|
|
7932
|
+
* Update current node's attribute values
|
|
7933
|
+
* Useful when attributes are parsed after push
|
|
7934
|
+
* @param {Object} attrValues - Attribute values
|
|
7935
|
+
*/
|
|
7936
|
+
updateCurrent(attrValues) {
|
|
7937
|
+
if (this.path.length > 0) {
|
|
7938
|
+
const current = this.path[this.path.length - 1];
|
|
7939
|
+
if (attrValues !== null && attrValues !== undefined) {
|
|
7940
|
+
current.values = attrValues;
|
|
7941
|
+
}
|
|
7942
|
+
}
|
|
7943
|
+
}
|
|
7944
|
+
|
|
7945
|
+
/**
|
|
7946
|
+
* Get current tag name
|
|
7947
|
+
* @returns {string|undefined}
|
|
7948
|
+
*/
|
|
7949
|
+
getCurrentTag() {
|
|
7950
|
+
return this.path.length > 0 ? this.path[this.path.length - 1].tag : undefined;
|
|
7951
|
+
}
|
|
7952
|
+
|
|
7953
|
+
/**
|
|
7954
|
+
* Get current namespace
|
|
7955
|
+
* @returns {string|undefined}
|
|
7956
|
+
*/
|
|
7957
|
+
getCurrentNamespace() {
|
|
7958
|
+
return this.path.length > 0 ? this.path[this.path.length - 1].namespace : undefined;
|
|
7959
|
+
}
|
|
7960
|
+
|
|
7961
|
+
/**
|
|
7962
|
+
* Get current node's attribute value
|
|
7963
|
+
* @param {string} attrName - Attribute name
|
|
7964
|
+
* @returns {*} Attribute value or undefined
|
|
7965
|
+
*/
|
|
7966
|
+
getAttrValue(attrName) {
|
|
7967
|
+
if (this.path.length === 0) return undefined;
|
|
7968
|
+
const current = this.path[this.path.length - 1];
|
|
7969
|
+
return current.values?.[attrName];
|
|
7970
|
+
}
|
|
7971
|
+
|
|
7972
|
+
/**
|
|
7973
|
+
* Check if current node has an attribute
|
|
7974
|
+
* @param {string} attrName - Attribute name
|
|
7975
|
+
* @returns {boolean}
|
|
7976
|
+
*/
|
|
7977
|
+
hasAttr(attrName) {
|
|
7978
|
+
if (this.path.length === 0) return false;
|
|
7979
|
+
const current = this.path[this.path.length - 1];
|
|
7980
|
+
return current.values !== undefined && attrName in current.values;
|
|
7981
|
+
}
|
|
7982
|
+
|
|
7983
|
+
/**
|
|
7984
|
+
* Get current node's sibling position (child index in parent)
|
|
7985
|
+
* @returns {number}
|
|
7986
|
+
*/
|
|
7987
|
+
getPosition() {
|
|
7988
|
+
if (this.path.length === 0) return -1;
|
|
7989
|
+
return this.path[this.path.length - 1].position ?? 0;
|
|
7990
|
+
}
|
|
7991
|
+
|
|
7992
|
+
/**
|
|
7993
|
+
* Get current node's repeat counter (occurrence count of this tag name)
|
|
7994
|
+
* @returns {number}
|
|
7995
|
+
*/
|
|
7996
|
+
getCounter() {
|
|
7997
|
+
if (this.path.length === 0) return -1;
|
|
7998
|
+
return this.path[this.path.length - 1].counter ?? 0;
|
|
7999
|
+
}
|
|
8000
|
+
|
|
8001
|
+
/**
|
|
8002
|
+
* Get current node's sibling index (alias for getPosition for backward compatibility)
|
|
8003
|
+
* @returns {number}
|
|
8004
|
+
* @deprecated Use getPosition() or getCounter() instead
|
|
8005
|
+
*/
|
|
8006
|
+
getIndex() {
|
|
8007
|
+
return this.getPosition();
|
|
8008
|
+
}
|
|
8009
|
+
|
|
8010
|
+
/**
|
|
8011
|
+
* Get current path depth
|
|
8012
|
+
* @returns {number}
|
|
8013
|
+
*/
|
|
8014
|
+
getDepth() {
|
|
8015
|
+
return this.path.length;
|
|
8016
|
+
}
|
|
8017
|
+
|
|
8018
|
+
/**
|
|
8019
|
+
* Get path as string
|
|
8020
|
+
* @param {string} separator - Optional separator (uses default if not provided)
|
|
8021
|
+
* @param {boolean} includeNamespace - Whether to include namespace in output (default: true)
|
|
8022
|
+
* @returns {string}
|
|
8023
|
+
*/
|
|
8024
|
+
toString(separator, includeNamespace = true) {
|
|
8025
|
+
const sep = separator || this.separator;
|
|
8026
|
+
return this.path.map(n => {
|
|
8027
|
+
if (includeNamespace && n.namespace) {
|
|
8028
|
+
return `${n.namespace}:${n.tag}`;
|
|
8029
|
+
}
|
|
8030
|
+
return n.tag;
|
|
8031
|
+
}).join(sep);
|
|
8032
|
+
}
|
|
8033
|
+
|
|
8034
|
+
/**
|
|
8035
|
+
* Get path as array of tag names
|
|
8036
|
+
* @returns {string[]}
|
|
8037
|
+
*/
|
|
8038
|
+
toArray() {
|
|
8039
|
+
return this.path.map(n => n.tag);
|
|
8040
|
+
}
|
|
8041
|
+
|
|
8042
|
+
/**
|
|
8043
|
+
* Reset the path to empty
|
|
8044
|
+
*/
|
|
8045
|
+
reset() {
|
|
8046
|
+
this.path = [];
|
|
8047
|
+
this.siblingStacks = [];
|
|
8048
|
+
}
|
|
8049
|
+
|
|
8050
|
+
/**
|
|
8051
|
+
* Match current path against an Expression
|
|
8052
|
+
* @param {Expression} expression - The expression to match against
|
|
8053
|
+
* @returns {boolean} True if current path matches the expression
|
|
8054
|
+
*/
|
|
8055
|
+
matches(expression) {
|
|
8056
|
+
const segments = expression.segments;
|
|
8057
|
+
|
|
8058
|
+
if (segments.length === 0) {
|
|
8059
|
+
return false;
|
|
8060
|
+
}
|
|
8061
|
+
|
|
8062
|
+
// Handle deep wildcard patterns
|
|
8063
|
+
if (expression.hasDeepWildcard()) {
|
|
8064
|
+
return this._matchWithDeepWildcard(segments);
|
|
8065
|
+
}
|
|
8066
|
+
|
|
8067
|
+
// Simple path matching (no deep wildcards)
|
|
8068
|
+
return this._matchSimple(segments);
|
|
8069
|
+
}
|
|
8070
|
+
|
|
8071
|
+
/**
|
|
8072
|
+
* Match simple path (no deep wildcards)
|
|
8073
|
+
* @private
|
|
8074
|
+
*/
|
|
8075
|
+
_matchSimple(segments) {
|
|
8076
|
+
// Path must be same length as segments
|
|
8077
|
+
if (this.path.length !== segments.length) {
|
|
8078
|
+
return false;
|
|
8079
|
+
}
|
|
8080
|
+
|
|
8081
|
+
// Match each segment bottom-to-top
|
|
8082
|
+
for (let i = 0; i < segments.length; i++) {
|
|
8083
|
+
const segment = segments[i];
|
|
8084
|
+
const node = this.path[i];
|
|
8085
|
+
const isCurrentNode = (i === this.path.length - 1);
|
|
8086
|
+
|
|
8087
|
+
if (!this._matchSegment(segment, node, isCurrentNode)) {
|
|
8088
|
+
return false;
|
|
8089
|
+
}
|
|
8090
|
+
}
|
|
8091
|
+
|
|
8092
|
+
return true;
|
|
8093
|
+
}
|
|
8094
|
+
|
|
8095
|
+
/**
|
|
8096
|
+
* Match path with deep wildcards
|
|
8097
|
+
* @private
|
|
8098
|
+
*/
|
|
8099
|
+
_matchWithDeepWildcard(segments) {
|
|
8100
|
+
let pathIdx = this.path.length - 1; // Start from current node (bottom)
|
|
8101
|
+
let segIdx = segments.length - 1; // Start from last segment
|
|
8102
|
+
|
|
8103
|
+
while (segIdx >= 0 && pathIdx >= 0) {
|
|
8104
|
+
const segment = segments[segIdx];
|
|
8105
|
+
|
|
8106
|
+
if (segment.type === 'deep-wildcard') {
|
|
8107
|
+
// ".." matches zero or more levels
|
|
8108
|
+
segIdx--;
|
|
8109
|
+
|
|
8110
|
+
if (segIdx < 0) {
|
|
8111
|
+
// Pattern ends with "..", always matches
|
|
8112
|
+
return true;
|
|
8113
|
+
}
|
|
8114
|
+
|
|
8115
|
+
// Find where next segment matches in the path
|
|
8116
|
+
const nextSeg = segments[segIdx];
|
|
8117
|
+
let found = false;
|
|
8118
|
+
|
|
8119
|
+
for (let i = pathIdx; i >= 0; i--) {
|
|
8120
|
+
const isCurrentNode = (i === this.path.length - 1);
|
|
8121
|
+
if (this._matchSegment(nextSeg, this.path[i], isCurrentNode)) {
|
|
8122
|
+
pathIdx = i - 1;
|
|
8123
|
+
segIdx--;
|
|
8124
|
+
found = true;
|
|
8125
|
+
break;
|
|
8126
|
+
}
|
|
8127
|
+
}
|
|
8128
|
+
|
|
8129
|
+
if (!found) {
|
|
8130
|
+
return false;
|
|
8131
|
+
}
|
|
8132
|
+
} else {
|
|
8133
|
+
// Regular segment
|
|
8134
|
+
const isCurrentNode = (pathIdx === this.path.length - 1);
|
|
8135
|
+
if (!this._matchSegment(segment, this.path[pathIdx], isCurrentNode)) {
|
|
8136
|
+
return false;
|
|
8137
|
+
}
|
|
8138
|
+
pathIdx--;
|
|
8139
|
+
segIdx--;
|
|
8140
|
+
}
|
|
8141
|
+
}
|
|
8142
|
+
|
|
8143
|
+
// All segments must be consumed
|
|
8144
|
+
return segIdx < 0;
|
|
8145
|
+
}
|
|
8146
|
+
|
|
8147
|
+
/**
|
|
8148
|
+
* Match a single segment against a node
|
|
8149
|
+
* @private
|
|
8150
|
+
* @param {Object} segment - Segment from Expression
|
|
8151
|
+
* @param {Object} node - Node from path
|
|
8152
|
+
* @param {boolean} isCurrentNode - Whether this is the current (last) node
|
|
8153
|
+
* @returns {boolean}
|
|
8154
|
+
*/
|
|
8155
|
+
_matchSegment(segment, node, isCurrentNode) {
|
|
8156
|
+
// Match tag name (* is wildcard)
|
|
8157
|
+
if (segment.tag !== '*' && segment.tag !== node.tag) {
|
|
8158
|
+
return false;
|
|
8159
|
+
}
|
|
8160
|
+
|
|
8161
|
+
// Match namespace if specified in segment
|
|
8162
|
+
if (segment.namespace !== undefined) {
|
|
8163
|
+
// Segment has namespace - node must match it
|
|
8164
|
+
if (segment.namespace !== '*' && segment.namespace !== node.namespace) {
|
|
8165
|
+
return false;
|
|
8166
|
+
}
|
|
8167
|
+
}
|
|
8168
|
+
// If segment has no namespace, it matches nodes with or without namespace
|
|
8169
|
+
|
|
8170
|
+
// Match attribute name (check if node has this attribute)
|
|
8171
|
+
// Can only check for current node since ancestors don't have values
|
|
8172
|
+
if (segment.attrName !== undefined) {
|
|
8173
|
+
if (!isCurrentNode) {
|
|
8174
|
+
// Can't check attributes for ancestor nodes (values not stored)
|
|
8175
|
+
return false;
|
|
8176
|
+
}
|
|
8177
|
+
|
|
8178
|
+
if (!node.values || !(segment.attrName in node.values)) {
|
|
8179
|
+
return false;
|
|
8180
|
+
}
|
|
8181
|
+
|
|
8182
|
+
// Match attribute value (only possible for current node)
|
|
8183
|
+
if (segment.attrValue !== undefined) {
|
|
8184
|
+
const actualValue = node.values[segment.attrName];
|
|
8185
|
+
// Both should be strings
|
|
8186
|
+
if (String(actualValue) !== String(segment.attrValue)) {
|
|
8187
|
+
return false;
|
|
8188
|
+
}
|
|
8189
|
+
}
|
|
8190
|
+
}
|
|
8191
|
+
|
|
8192
|
+
// Match position (only for current node)
|
|
8193
|
+
if (segment.position !== undefined) {
|
|
8194
|
+
if (!isCurrentNode) {
|
|
8195
|
+
// Can't check position for ancestor nodes
|
|
8196
|
+
return false;
|
|
8197
|
+
}
|
|
8198
|
+
|
|
8199
|
+
const counter = node.counter ?? 0;
|
|
8200
|
+
|
|
8201
|
+
if (segment.position === 'first' && counter !== 0) {
|
|
8202
|
+
return false;
|
|
8203
|
+
} else if (segment.position === 'odd' && counter % 2 !== 1) {
|
|
8204
|
+
return false;
|
|
8205
|
+
} else if (segment.position === 'even' && counter % 2 !== 0) {
|
|
8206
|
+
return false;
|
|
8207
|
+
} else if (segment.position === 'nth') {
|
|
8208
|
+
if (counter !== segment.positionValue) {
|
|
8209
|
+
return false;
|
|
8210
|
+
}
|
|
8211
|
+
}
|
|
8212
|
+
}
|
|
8213
|
+
|
|
8214
|
+
return true;
|
|
8215
|
+
}
|
|
8216
|
+
|
|
8217
|
+
/**
|
|
8218
|
+
* Create a snapshot of current state
|
|
8219
|
+
* @returns {Object} State snapshot
|
|
8220
|
+
*/
|
|
8221
|
+
snapshot() {
|
|
8222
|
+
return {
|
|
8223
|
+
path: this.path.map(node => ({ ...node })),
|
|
8224
|
+
siblingStacks: this.siblingStacks.map(map => new Map(map))
|
|
8225
|
+
};
|
|
8226
|
+
}
|
|
8227
|
+
|
|
8228
|
+
/**
|
|
8229
|
+
* Restore state from snapshot
|
|
8230
|
+
* @param {Object} snapshot - State snapshot
|
|
8231
|
+
*/
|
|
8232
|
+
restore(snapshot) {
|
|
8233
|
+
this.path = snapshot.path.map(node => ({ ...node }));
|
|
8234
|
+
this.siblingStacks = snapshot.siblingStacks.map(map => new Map(map));
|
|
8235
|
+
}
|
|
8236
|
+
}
|
|
8237
|
+
|
|
7442
8238
|
// const regx =
|
|
7443
8239
|
// '<((!\\[CDATA\\[([\\s\\S]*?)(]]>))|((NAME:)?(NAME))([^>]*)>|((\\/)(NAME)\\s*>))([^<]*)'
|
|
7444
8240
|
// .replace(/NAME/g, util.nameRegexp);
|
|
@@ -7446,19 +8242,70 @@
|
|
|
7446
8242
|
//const tagsRegx = new RegExp("<(\\/?[\\w:\\-\._]+)([^>]*)>(\\s*"+cdataRegx+")*([^<]+)?","g");
|
|
7447
8243
|
//const tagsRegx = new RegExp("<(\\/?)((\\w*:)?([\\w:\\-\._]+))([^>]*)>([^<]*)("+cdataRegx+"([^<]*))*([^<]+)?","g");
|
|
7448
8244
|
|
|
7449
|
-
|
|
7450
|
-
|
|
8245
|
+
// Helper functions for attribute and namespace handling
|
|
8246
|
+
|
|
8247
|
+
/**
|
|
8248
|
+
* Extract raw attributes (without prefix) from prefixed attribute map
|
|
8249
|
+
* @param {object} prefixedAttrs - Attributes with prefix from buildAttributesMap
|
|
8250
|
+
* @param {object} options - Parser options containing attributeNamePrefix
|
|
8251
|
+
* @returns {object} Raw attributes for matcher
|
|
8252
|
+
*/
|
|
8253
|
+
function extractRawAttributes(prefixedAttrs, options) {
|
|
8254
|
+
if (!prefixedAttrs) return {};
|
|
8255
|
+
|
|
8256
|
+
// Handle attributesGroupName option
|
|
8257
|
+
const attrs = options.attributesGroupName
|
|
8258
|
+
? prefixedAttrs[options.attributesGroupName]
|
|
8259
|
+
: prefixedAttrs;
|
|
8260
|
+
|
|
8261
|
+
if (!attrs) return {};
|
|
8262
|
+
|
|
8263
|
+
const rawAttrs = {};
|
|
8264
|
+
for (const key in attrs) {
|
|
8265
|
+
// Remove the attribute prefix to get raw name
|
|
8266
|
+
if (key.startsWith(options.attributeNamePrefix)) {
|
|
8267
|
+
const rawName = key.substring(options.attributeNamePrefix.length);
|
|
8268
|
+
rawAttrs[rawName] = attrs[key];
|
|
8269
|
+
} else {
|
|
8270
|
+
// Attribute without prefix (shouldn't normally happen, but be safe)
|
|
8271
|
+
rawAttrs[key] = attrs[key];
|
|
8272
|
+
}
|
|
8273
|
+
}
|
|
8274
|
+
return rawAttrs;
|
|
8275
|
+
}
|
|
8276
|
+
|
|
8277
|
+
/**
|
|
8278
|
+
* Extract namespace from raw tag name
|
|
8279
|
+
* @param {string} rawTagName - Tag name possibly with namespace (e.g., "soap:Envelope")
|
|
8280
|
+
* @returns {string|undefined} Namespace or undefined
|
|
8281
|
+
*/
|
|
8282
|
+
function extractNamespace(rawTagName) {
|
|
8283
|
+
if (!rawTagName || typeof rawTagName !== 'string') return undefined;
|
|
8284
|
+
|
|
8285
|
+
const colonIndex = rawTagName.indexOf(':');
|
|
8286
|
+
if (colonIndex !== -1 && colonIndex > 0) {
|
|
8287
|
+
const ns = rawTagName.substring(0, colonIndex);
|
|
8288
|
+
// Don't treat xmlns as a namespace
|
|
8289
|
+
if (ns !== 'xmlns') {
|
|
8290
|
+
return ns;
|
|
8291
|
+
}
|
|
8292
|
+
}
|
|
8293
|
+
return undefined;
|
|
8294
|
+
}
|
|
8295
|
+
|
|
8296
|
+
class OrderedObjParser {
|
|
8297
|
+
constructor(options) {
|
|
7451
8298
|
this.options = options;
|
|
7452
8299
|
this.currentNode = null;
|
|
7453
8300
|
this.tagsNodeStack = [];
|
|
7454
8301
|
this.docTypeEntities = {};
|
|
7455
8302
|
this.lastEntities = {
|
|
7456
|
-
"apos"
|
|
7457
|
-
"gt"
|
|
7458
|
-
"lt"
|
|
7459
|
-
"quot"
|
|
8303
|
+
"apos": { regex: /&(apos|#39|#x27);/g, val: "'" },
|
|
8304
|
+
"gt": { regex: /&(gt|#62|#x3E);/g, val: ">" },
|
|
8305
|
+
"lt": { regex: /&(lt|#60|#x3C);/g, val: "<" },
|
|
8306
|
+
"quot": { regex: /&(quot|#34|#x22);/g, val: "\"" },
|
|
7460
8307
|
};
|
|
7461
|
-
this.ampEntity = { regex: /&(amp|#38|#x26);/g, val
|
|
8308
|
+
this.ampEntity = { regex: /&(amp|#38|#x26);/g, val: "&" };
|
|
7462
8309
|
this.htmlEntities = {
|
|
7463
8310
|
"space": { regex: /&(nbsp|#160);/g, val: " " },
|
|
7464
8311
|
// "lt" : { regex: /&(lt|#60);/g, val: "<" },
|
|
@@ -7466,15 +8313,15 @@
|
|
|
7466
8313
|
// "amp" : { regex: /&(amp|#38);/g, val: "&" },
|
|
7467
8314
|
// "quot" : { regex: /&(quot|#34);/g, val: "\"" },
|
|
7468
8315
|
// "apos" : { regex: /&(apos|#39);/g, val: "'" },
|
|
7469
|
-
"cent"
|
|
7470
|
-
"pound"
|
|
7471
|
-
"yen"
|
|
7472
|
-
"euro"
|
|
7473
|
-
"copyright"
|
|
7474
|
-
"reg"
|
|
7475
|
-
"inr"
|
|
7476
|
-
"num_dec": { regex: /&#([0-9]{1,7});/g, val
|
|
7477
|
-
"num_hex": { regex: /&#x([0-9a-fA-F]{1,6});/g, val
|
|
8316
|
+
"cent": { regex: /&(cent|#162);/g, val: "¢" },
|
|
8317
|
+
"pound": { regex: /&(pound|#163);/g, val: "£" },
|
|
8318
|
+
"yen": { regex: /&(yen|#165);/g, val: "¥" },
|
|
8319
|
+
"euro": { regex: /&(euro|#8364);/g, val: "€" },
|
|
8320
|
+
"copyright": { regex: /&(copy|#169);/g, val: "©" },
|
|
8321
|
+
"reg": { regex: /&(reg|#174);/g, val: "®" },
|
|
8322
|
+
"inr": { regex: /&(inr|#8377);/g, val: "₹" },
|
|
8323
|
+
"num_dec": { regex: /&#([0-9]{1,7});/g, val: (_, str) => fromCodePoint(str, 10, "&#") },
|
|
8324
|
+
"num_hex": { regex: /&#x([0-9a-fA-F]{1,6});/g, val: (_, str) => fromCodePoint(str, 16, "&#x") },
|
|
7478
8325
|
};
|
|
7479
8326
|
this.addExternalEntities = addExternalEntities;
|
|
7480
8327
|
this.parseXml = parseXml;
|
|
@@ -7487,17 +8334,26 @@
|
|
|
7487
8334
|
this.saveTextToParentTag = saveTextToParentTag;
|
|
7488
8335
|
this.addChild = addChild$1;
|
|
7489
8336
|
this.ignoreAttributesFn = getIgnoreAttributesFn(this.options.ignoreAttributes);
|
|
8337
|
+
this.entityExpansionCount = 0;
|
|
8338
|
+
this.currentExpandedLength = 0;
|
|
8339
|
+
|
|
8340
|
+
// Initialize path matcher for path-expression-matcher
|
|
8341
|
+
this.matcher = new Matcher();
|
|
7490
8342
|
|
|
7491
|
-
if
|
|
7492
|
-
|
|
7493
|
-
|
|
7494
|
-
|
|
8343
|
+
// Flag to track if current node is a stop node (optimization)
|
|
8344
|
+
this.isCurrentNodeStopNode = false;
|
|
8345
|
+
|
|
8346
|
+
// Pre-compile stopNodes expressions
|
|
8347
|
+
if (this.options.stopNodes && this.options.stopNodes.length > 0) {
|
|
8348
|
+
this.stopNodeExpressions = [];
|
|
8349
|
+
for (let i = 0; i < this.options.stopNodes.length; i++) {
|
|
7495
8350
|
const stopNodeExp = this.options.stopNodes[i];
|
|
7496
|
-
if(typeof stopNodeExp
|
|
7497
|
-
|
|
7498
|
-
this.
|
|
7499
|
-
}else {
|
|
7500
|
-
|
|
8351
|
+
if (typeof stopNodeExp === 'string') {
|
|
8352
|
+
// Convert string to Expression object
|
|
8353
|
+
this.stopNodeExpressions.push(new Expression(stopNodeExp));
|
|
8354
|
+
} else if (stopNodeExp instanceof Expression) {
|
|
8355
|
+
// Already an Expression object
|
|
8356
|
+
this.stopNodeExpressions.push(stopNodeExp);
|
|
7501
8357
|
}
|
|
7502
8358
|
}
|
|
7503
8359
|
}
|
|
@@ -7505,13 +8361,14 @@
|
|
|
7505
8361
|
|
|
7506
8362
|
}
|
|
7507
8363
|
|
|
7508
|
-
function addExternalEntities(externalEntities){
|
|
8364
|
+
function addExternalEntities(externalEntities) {
|
|
7509
8365
|
const entKeys = Object.keys(externalEntities);
|
|
7510
8366
|
for (let i = 0; i < entKeys.length; i++) {
|
|
7511
8367
|
const ent = entKeys[i];
|
|
8368
|
+
const escaped = ent.replace(/[.\-+*:]/g, '\\.');
|
|
7512
8369
|
this.lastEntities[ent] = {
|
|
7513
|
-
|
|
7514
|
-
|
|
8370
|
+
regex: new RegExp("&" + escaped + ";", "g"),
|
|
8371
|
+
val: externalEntities[ent]
|
|
7515
8372
|
};
|
|
7516
8373
|
}
|
|
7517
8374
|
}
|
|
@@ -7519,7 +8376,7 @@
|
|
|
7519
8376
|
/**
|
|
7520
8377
|
* @param {string} val
|
|
7521
8378
|
* @param {string} tagName
|
|
7522
|
-
* @param {string} jPath
|
|
8379
|
+
* @param {string|Matcher} jPath - jPath string or Matcher instance based on options.jPath
|
|
7523
8380
|
* @param {boolean} dontTrim
|
|
7524
8381
|
* @param {boolean} hasAttributes
|
|
7525
8382
|
* @param {boolean} isLeafNode
|
|
@@ -7530,23 +8387,25 @@
|
|
|
7530
8387
|
if (this.options.trimValues && !dontTrim) {
|
|
7531
8388
|
val = val.trim();
|
|
7532
8389
|
}
|
|
7533
|
-
if(val.length > 0){
|
|
7534
|
-
if(!escapeEntities) val = this.replaceEntitiesValue(val);
|
|
7535
|
-
|
|
7536
|
-
|
|
7537
|
-
|
|
8390
|
+
if (val.length > 0) {
|
|
8391
|
+
if (!escapeEntities) val = this.replaceEntitiesValue(val, tagName, jPath);
|
|
8392
|
+
|
|
8393
|
+
// Pass jPath string or matcher based on options.jPath setting
|
|
8394
|
+
const jPathOrMatcher = this.options.jPath ? jPath.toString() : jPath;
|
|
8395
|
+
const newval = this.options.tagValueProcessor(tagName, val, jPathOrMatcher, hasAttributes, isLeafNode);
|
|
8396
|
+
if (newval === null || newval === undefined) {
|
|
7538
8397
|
//don't parse
|
|
7539
8398
|
return val;
|
|
7540
|
-
}else if(typeof newval !== typeof val || newval !== val){
|
|
8399
|
+
} else if (typeof newval !== typeof val || newval !== val) {
|
|
7541
8400
|
//overwrite
|
|
7542
8401
|
return newval;
|
|
7543
|
-
}else if(this.options.trimValues){
|
|
8402
|
+
} else if (this.options.trimValues) {
|
|
7544
8403
|
return parseValue(val, this.options.parseTagValue, this.options.numberParseOptions);
|
|
7545
|
-
}else {
|
|
8404
|
+
} else {
|
|
7546
8405
|
const trimmedVal = val.trim();
|
|
7547
|
-
if(trimmedVal === val){
|
|
8406
|
+
if (trimmedVal === val) {
|
|
7548
8407
|
return parseValue(val, this.options.parseTagValue, this.options.numberParseOptions);
|
|
7549
|
-
}else {
|
|
8408
|
+
} else {
|
|
7550
8409
|
return val;
|
|
7551
8410
|
}
|
|
7552
8411
|
}
|
|
@@ -7572,7 +8431,7 @@
|
|
|
7572
8431
|
//const attrsRegx = new RegExp("([\\w\\-\\.\\:]+)\\s*=\\s*(['\"])((.|\n)*?)\\2","gm");
|
|
7573
8432
|
const attrsRegx = new RegExp('([^\\s=]+)\\s*(=\\s*([\'"])([\\s\\S]*?)\\3)?', 'gm');
|
|
7574
8433
|
|
|
7575
|
-
function buildAttributesMap(attrStr, jPath) {
|
|
8434
|
+
function buildAttributesMap(attrStr, jPath, tagName) {
|
|
7576
8435
|
if (this.options.ignoreAttributes !== true && typeof attrStr === 'string') {
|
|
7577
8436
|
// attrStr = attrStr.replace(/\r?\n/g, ' ');
|
|
7578
8437
|
//attrStr = attrStr || attrStr.trim();
|
|
@@ -7580,31 +8439,65 @@
|
|
|
7580
8439
|
const matches = getAllMatches(attrStr, attrsRegx);
|
|
7581
8440
|
const len = matches.length; //don't make it inline
|
|
7582
8441
|
const attrs = {};
|
|
8442
|
+
|
|
8443
|
+
// First pass: parse all attributes and update matcher with raw values
|
|
8444
|
+
// This ensures the matcher has all attribute values when processors run
|
|
8445
|
+
const rawAttrsForMatcher = {};
|
|
7583
8446
|
for (let i = 0; i < len; i++) {
|
|
7584
8447
|
const attrName = this.resolveNameSpace(matches[i][1]);
|
|
7585
|
-
|
|
8448
|
+
const oldVal = matches[i][4];
|
|
8449
|
+
|
|
8450
|
+
if (attrName.length && oldVal !== undefined) {
|
|
8451
|
+
let parsedVal = oldVal;
|
|
8452
|
+
if (this.options.trimValues) {
|
|
8453
|
+
parsedVal = parsedVal.trim();
|
|
8454
|
+
}
|
|
8455
|
+
parsedVal = this.replaceEntitiesValue(parsedVal, tagName, jPath);
|
|
8456
|
+
rawAttrsForMatcher[attrName] = parsedVal;
|
|
8457
|
+
}
|
|
8458
|
+
}
|
|
8459
|
+
|
|
8460
|
+
// Update matcher with raw attribute values BEFORE running processors
|
|
8461
|
+
if (Object.keys(rawAttrsForMatcher).length > 0 && typeof jPath === 'object' && jPath.updateCurrent) {
|
|
8462
|
+
jPath.updateCurrent(rawAttrsForMatcher);
|
|
8463
|
+
}
|
|
8464
|
+
|
|
8465
|
+
// Second pass: now process attributes with matcher having full attribute context
|
|
8466
|
+
for (let i = 0; i < len; i++) {
|
|
8467
|
+
const attrName = this.resolveNameSpace(matches[i][1]);
|
|
8468
|
+
|
|
8469
|
+
// Convert jPath to string if needed for ignoreAttributesFn
|
|
8470
|
+
const jPathStr = this.options.jPath ? jPath.toString() : jPath;
|
|
8471
|
+
if (this.ignoreAttributesFn(attrName, jPathStr)) {
|
|
7586
8472
|
continue
|
|
7587
8473
|
}
|
|
8474
|
+
|
|
7588
8475
|
let oldVal = matches[i][4];
|
|
7589
8476
|
let aName = this.options.attributeNamePrefix + attrName;
|
|
8477
|
+
|
|
7590
8478
|
if (attrName.length) {
|
|
7591
8479
|
if (this.options.transformAttributeName) {
|
|
7592
8480
|
aName = this.options.transformAttributeName(aName);
|
|
7593
8481
|
}
|
|
7594
|
-
if(aName === "__proto__") aName
|
|
8482
|
+
//if (aName === "__proto__") aName = "#__proto__";
|
|
8483
|
+
aName = sanitizeName(aName, this.options);
|
|
8484
|
+
|
|
7595
8485
|
if (oldVal !== undefined) {
|
|
7596
8486
|
if (this.options.trimValues) {
|
|
7597
8487
|
oldVal = oldVal.trim();
|
|
7598
8488
|
}
|
|
7599
|
-
oldVal = this.replaceEntitiesValue(oldVal);
|
|
7600
|
-
|
|
7601
|
-
|
|
8489
|
+
oldVal = this.replaceEntitiesValue(oldVal, tagName, jPath);
|
|
8490
|
+
|
|
8491
|
+
// Pass jPath string or matcher based on options.jPath setting
|
|
8492
|
+
const jPathOrMatcher = this.options.jPath ? jPath.toString() : jPath;
|
|
8493
|
+
const newVal = this.options.attributeValueProcessor(attrName, oldVal, jPathOrMatcher);
|
|
8494
|
+
if (newVal === null || newVal === undefined) {
|
|
7602
8495
|
//don't parse
|
|
7603
8496
|
attrs[aName] = oldVal;
|
|
7604
|
-
}else if(typeof newVal !== typeof oldVal || newVal !== oldVal){
|
|
8497
|
+
} else if (typeof newVal !== typeof oldVal || newVal !== oldVal) {
|
|
7605
8498
|
//overwrite
|
|
7606
8499
|
attrs[aName] = newVal;
|
|
7607
|
-
}else {
|
|
8500
|
+
} else {
|
|
7608
8501
|
//parse
|
|
7609
8502
|
attrs[aName] = parseValue(
|
|
7610
8503
|
oldVal,
|
|
@@ -7617,6 +8510,7 @@
|
|
|
7617
8510
|
}
|
|
7618
8511
|
}
|
|
7619
8512
|
}
|
|
8513
|
+
|
|
7620
8514
|
if (!Object.keys(attrs).length) {
|
|
7621
8515
|
return;
|
|
7622
8516
|
}
|
|
@@ -7629,274 +8523,395 @@
|
|
|
7629
8523
|
}
|
|
7630
8524
|
}
|
|
7631
8525
|
|
|
7632
|
-
const parseXml = function(xmlData) {
|
|
8526
|
+
const parseXml = function (xmlData) {
|
|
7633
8527
|
xmlData = xmlData.replace(/\r\n?/g, "\n"); //TODO: remove this line
|
|
7634
8528
|
const xmlObj = new XmlNode('!xml');
|
|
7635
8529
|
let currentNode = xmlObj;
|
|
7636
8530
|
let textData = "";
|
|
7637
|
-
|
|
8531
|
+
|
|
8532
|
+
// Reset matcher for new document
|
|
8533
|
+
this.matcher.reset();
|
|
8534
|
+
|
|
8535
|
+
// Reset entity expansion counters for this document
|
|
8536
|
+
this.entityExpansionCount = 0;
|
|
8537
|
+
this.currentExpandedLength = 0;
|
|
8538
|
+
|
|
7638
8539
|
const docTypeReader = new DocTypeReader(this.options.processEntities);
|
|
7639
|
-
for(let i=0; i< xmlData.length; i++){//for each char in XML data
|
|
8540
|
+
for (let i = 0; i < xmlData.length; i++) {//for each char in XML data
|
|
7640
8541
|
const ch = xmlData[i];
|
|
7641
|
-
if(ch === '<'){
|
|
8542
|
+
if (ch === '<') {
|
|
7642
8543
|
// const nextIndex = i+1;
|
|
7643
8544
|
// const _2ndChar = xmlData[nextIndex];
|
|
7644
|
-
if(
|
|
8545
|
+
if (xmlData[i + 1] === '/') {//Closing Tag
|
|
7645
8546
|
const closeIndex = findClosingIndex(xmlData, ">", i, "Closing Tag is not closed.");
|
|
7646
|
-
let tagName = xmlData.substring(i+2,closeIndex).trim();
|
|
8547
|
+
let tagName = xmlData.substring(i + 2, closeIndex).trim();
|
|
7647
8548
|
|
|
7648
|
-
if(this.options.removeNSPrefix){
|
|
8549
|
+
if (this.options.removeNSPrefix) {
|
|
7649
8550
|
const colonIndex = tagName.indexOf(":");
|
|
7650
|
-
if(colonIndex !== -1){
|
|
7651
|
-
tagName = tagName.substr(colonIndex+1);
|
|
8551
|
+
if (colonIndex !== -1) {
|
|
8552
|
+
tagName = tagName.substr(colonIndex + 1);
|
|
7652
8553
|
}
|
|
7653
8554
|
}
|
|
7654
8555
|
|
|
7655
|
-
|
|
7656
|
-
tagName = this.options.transformTagName(tagName);
|
|
7657
|
-
}
|
|
8556
|
+
tagName = transformTagName(this.options.transformTagName, tagName, "", this.options).tagName;
|
|
7658
8557
|
|
|
7659
|
-
if(currentNode){
|
|
7660
|
-
textData = this.saveTextToParentTag(textData, currentNode,
|
|
8558
|
+
if (currentNode) {
|
|
8559
|
+
textData = this.saveTextToParentTag(textData, currentNode, this.matcher);
|
|
7661
8560
|
}
|
|
7662
8561
|
|
|
7663
8562
|
//check if last tag of nested tag was unpaired tag
|
|
7664
|
-
const lastTagName =
|
|
7665
|
-
if(tagName && this.options.unpairedTags.indexOf(tagName) !== -1
|
|
8563
|
+
const lastTagName = this.matcher.getCurrentTag();
|
|
8564
|
+
if (tagName && this.options.unpairedTags.indexOf(tagName) !== -1) {
|
|
7666
8565
|
throw new Error(`Unpaired tag can not be used as closing tag: </${tagName}>`);
|
|
7667
8566
|
}
|
|
7668
|
-
|
|
7669
|
-
|
|
7670
|
-
|
|
8567
|
+
if (lastTagName && this.options.unpairedTags.indexOf(lastTagName) !== -1) {
|
|
8568
|
+
// Pop the unpaired tag
|
|
8569
|
+
this.matcher.pop();
|
|
7671
8570
|
this.tagsNodeStack.pop();
|
|
7672
|
-
}else {
|
|
7673
|
-
propIndex = jPath.lastIndexOf(".");
|
|
7674
8571
|
}
|
|
7675
|
-
|
|
8572
|
+
// Pop the closing tag
|
|
8573
|
+
this.matcher.pop();
|
|
8574
|
+
this.isCurrentNodeStopNode = false; // Reset flag when closing tag
|
|
7676
8575
|
|
|
7677
8576
|
currentNode = this.tagsNodeStack.pop();//avoid recursion, set the parent tag scope
|
|
7678
8577
|
textData = "";
|
|
7679
8578
|
i = closeIndex;
|
|
7680
|
-
} else if(
|
|
8579
|
+
} else if (xmlData[i + 1] === '?') {
|
|
7681
8580
|
|
|
7682
|
-
let tagData = readTagExp(xmlData,i, false, "?>");
|
|
7683
|
-
if(!tagData) throw new Error("Pi Tag is not closed.");
|
|
8581
|
+
let tagData = readTagExp(xmlData, i, false, "?>");
|
|
8582
|
+
if (!tagData) throw new Error("Pi Tag is not closed.");
|
|
8583
|
+
|
|
8584
|
+
textData = this.saveTextToParentTag(textData, currentNode, this.matcher);
|
|
8585
|
+
if ((this.options.ignoreDeclaration && tagData.tagName === "?xml") || this.options.ignorePiTags) ; else {
|
|
7684
8586
|
|
|
7685
|
-
textData = this.saveTextToParentTag(textData, currentNode, jPath);
|
|
7686
|
-
if( (this.options.ignoreDeclaration && tagData.tagName === "?xml") || this.options.ignorePiTags);else {
|
|
7687
|
-
|
|
7688
8587
|
const childNode = new XmlNode(tagData.tagName);
|
|
7689
8588
|
childNode.add(this.options.textNodeName, "");
|
|
7690
|
-
|
|
7691
|
-
if(tagData.tagName !== tagData.tagExp && tagData.attrExpPresent){
|
|
7692
|
-
childNode[":@"] = this.buildAttributesMap(tagData.tagExp,
|
|
8589
|
+
|
|
8590
|
+
if (tagData.tagName !== tagData.tagExp && tagData.attrExpPresent) {
|
|
8591
|
+
childNode[":@"] = this.buildAttributesMap(tagData.tagExp, this.matcher, tagData.tagName);
|
|
7693
8592
|
}
|
|
7694
|
-
this.addChild(currentNode, childNode,
|
|
8593
|
+
this.addChild(currentNode, childNode, this.matcher, i);
|
|
7695
8594
|
}
|
|
7696
8595
|
|
|
7697
8596
|
|
|
7698
8597
|
i = tagData.closeIndex + 1;
|
|
7699
|
-
} else if(xmlData.substr(i + 1, 3) === '!--') {
|
|
7700
|
-
const endIndex = findClosingIndex(xmlData, "-->", i+4, "Comment is not closed.");
|
|
7701
|
-
if(this.options.commentPropName){
|
|
8598
|
+
} else if (xmlData.substr(i + 1, 3) === '!--') {
|
|
8599
|
+
const endIndex = findClosingIndex(xmlData, "-->", i + 4, "Comment is not closed.");
|
|
8600
|
+
if (this.options.commentPropName) {
|
|
7702
8601
|
const comment = xmlData.substring(i + 4, endIndex - 2);
|
|
7703
8602
|
|
|
7704
|
-
textData = this.saveTextToParentTag(textData, currentNode,
|
|
8603
|
+
textData = this.saveTextToParentTag(textData, currentNode, this.matcher);
|
|
7705
8604
|
|
|
7706
|
-
currentNode.add(this.options.commentPropName, [
|
|
8605
|
+
currentNode.add(this.options.commentPropName, [{ [this.options.textNodeName]: comment }]);
|
|
7707
8606
|
}
|
|
7708
8607
|
i = endIndex;
|
|
7709
|
-
} else if(
|
|
8608
|
+
} else if (xmlData.substr(i + 1, 2) === '!D') {
|
|
7710
8609
|
const result = docTypeReader.readDocType(xmlData, i);
|
|
7711
8610
|
this.docTypeEntities = result.entities;
|
|
7712
8611
|
i = result.i;
|
|
7713
|
-
}else if(xmlData.substr(i + 1, 2) === '![') {
|
|
8612
|
+
} else if (xmlData.substr(i + 1, 2) === '![') {
|
|
7714
8613
|
const closeIndex = findClosingIndex(xmlData, "]]>", i, "CDATA is not closed.") - 2;
|
|
7715
|
-
const tagExp = xmlData.substring(i + 9,closeIndex);
|
|
8614
|
+
const tagExp = xmlData.substring(i + 9, closeIndex);
|
|
7716
8615
|
|
|
7717
|
-
textData = this.saveTextToParentTag(textData, currentNode,
|
|
8616
|
+
textData = this.saveTextToParentTag(textData, currentNode, this.matcher);
|
|
7718
8617
|
|
|
7719
|
-
let val = this.parseTextData(tagExp, currentNode.tagname,
|
|
7720
|
-
if(val == undefined) val = "";
|
|
8618
|
+
let val = this.parseTextData(tagExp, currentNode.tagname, this.matcher, true, false, true, true);
|
|
8619
|
+
if (val == undefined) val = "";
|
|
7721
8620
|
|
|
7722
8621
|
//cdata should be set even if it is 0 length string
|
|
7723
|
-
if(this.options.cdataPropName){
|
|
7724
|
-
currentNode.add(this.options.cdataPropName, [
|
|
7725
|
-
}else {
|
|
8622
|
+
if (this.options.cdataPropName) {
|
|
8623
|
+
currentNode.add(this.options.cdataPropName, [{ [this.options.textNodeName]: tagExp }]);
|
|
8624
|
+
} else {
|
|
7726
8625
|
currentNode.add(this.options.textNodeName, val);
|
|
7727
8626
|
}
|
|
7728
|
-
|
|
8627
|
+
|
|
7729
8628
|
i = closeIndex + 2;
|
|
7730
|
-
}else {//Opening tag
|
|
7731
|
-
let result = readTagExp(xmlData,i, this.options.removeNSPrefix);
|
|
7732
|
-
|
|
8629
|
+
} else {//Opening tag
|
|
8630
|
+
let result = readTagExp(xmlData, i, this.options.removeNSPrefix);
|
|
8631
|
+
|
|
8632
|
+
// Safety check: readTagExp can return undefined
|
|
8633
|
+
if (!result) {
|
|
8634
|
+
// Log context for debugging
|
|
8635
|
+
const context = xmlData.substring(Math.max(0, i - 50), Math.min(xmlData.length, i + 50));
|
|
8636
|
+
throw new Error(`readTagExp returned undefined at position ${i}. Context: "${context}"`);
|
|
8637
|
+
}
|
|
8638
|
+
|
|
8639
|
+
let tagName = result.tagName;
|
|
7733
8640
|
const rawTagName = result.rawTagName;
|
|
7734
8641
|
let tagExp = result.tagExp;
|
|
7735
8642
|
let attrExpPresent = result.attrExpPresent;
|
|
7736
8643
|
let closeIndex = result.closeIndex;
|
|
7737
8644
|
|
|
7738
|
-
|
|
7739
|
-
|
|
7740
|
-
|
|
7741
|
-
|
|
7742
|
-
|
|
7743
|
-
|
|
7744
|
-
|
|
8645
|
+
({ tagName, tagExp } = transformTagName(this.options.transformTagName, tagName, tagExp, this.options));
|
|
8646
|
+
|
|
8647
|
+
if (this.options.strictReservedNames &&
|
|
8648
|
+
(tagName === this.options.commentPropName
|
|
8649
|
+
|| tagName === this.options.cdataPropName
|
|
8650
|
+
)) {
|
|
8651
|
+
throw new Error(`Invalid tag name: ${tagName}`);
|
|
7745
8652
|
}
|
|
7746
|
-
|
|
8653
|
+
|
|
7747
8654
|
//save text as child node
|
|
7748
8655
|
if (currentNode && textData) {
|
|
7749
|
-
if(currentNode.tagname !== '!xml'){
|
|
8656
|
+
if (currentNode.tagname !== '!xml') {
|
|
7750
8657
|
//when nested tag is found
|
|
7751
|
-
textData = this.saveTextToParentTag(textData, currentNode,
|
|
8658
|
+
textData = this.saveTextToParentTag(textData, currentNode, this.matcher, false);
|
|
7752
8659
|
}
|
|
7753
8660
|
}
|
|
7754
8661
|
|
|
7755
8662
|
//check if last tag was unpaired tag
|
|
7756
8663
|
const lastTag = currentNode;
|
|
7757
|
-
if(lastTag && this.options.unpairedTags.indexOf(lastTag.tagname) !== -1
|
|
8664
|
+
if (lastTag && this.options.unpairedTags.indexOf(lastTag.tagname) !== -1) {
|
|
7758
8665
|
currentNode = this.tagsNodeStack.pop();
|
|
7759
|
-
|
|
8666
|
+
this.matcher.pop();
|
|
8667
|
+
}
|
|
8668
|
+
|
|
8669
|
+
// Clean up self-closing syntax BEFORE processing attributes
|
|
8670
|
+
// This is where tagExp gets the trailing / removed
|
|
8671
|
+
let isSelfClosing = false;
|
|
8672
|
+
if (tagExp.length > 0 && tagExp.lastIndexOf("/") === tagExp.length - 1) {
|
|
8673
|
+
isSelfClosing = true;
|
|
8674
|
+
if (tagName[tagName.length - 1] === "/") {
|
|
8675
|
+
tagName = tagName.substr(0, tagName.length - 1);
|
|
8676
|
+
tagExp = tagName;
|
|
8677
|
+
} else {
|
|
8678
|
+
tagExp = tagExp.substr(0, tagExp.length - 1);
|
|
8679
|
+
}
|
|
8680
|
+
|
|
8681
|
+
// Re-check attrExpPresent after cleaning
|
|
8682
|
+
attrExpPresent = (tagName !== tagExp);
|
|
8683
|
+
}
|
|
8684
|
+
|
|
8685
|
+
// Now process attributes with CLEAN tagExp (no trailing /)
|
|
8686
|
+
let prefixedAttrs = null;
|
|
8687
|
+
let namespace = undefined;
|
|
8688
|
+
|
|
8689
|
+
// Extract namespace from rawTagName
|
|
8690
|
+
namespace = extractNamespace(rawTagName);
|
|
8691
|
+
|
|
8692
|
+
// Push tag to matcher FIRST (with empty attrs for now) so callbacks see correct path
|
|
8693
|
+
if (tagName !== xmlObj.tagname) {
|
|
8694
|
+
this.matcher.push(tagName, {}, namespace);
|
|
7760
8695
|
}
|
|
7761
|
-
|
|
7762
|
-
|
|
8696
|
+
|
|
8697
|
+
// Now build attributes - callbacks will see correct matcher state
|
|
8698
|
+
if (tagName !== tagExp && attrExpPresent) {
|
|
8699
|
+
// Build attributes (returns prefixed attributes for the tree)
|
|
8700
|
+
// Note: buildAttributesMap now internally updates the matcher with raw attributes
|
|
8701
|
+
prefixedAttrs = this.buildAttributesMap(tagExp, this.matcher, tagName);
|
|
8702
|
+
|
|
8703
|
+
if (prefixedAttrs) {
|
|
8704
|
+
// Extract raw attributes (without prefix) for our use
|
|
8705
|
+
extractRawAttributes(prefixedAttrs, this.options);
|
|
8706
|
+
}
|
|
8707
|
+
}
|
|
8708
|
+
|
|
8709
|
+
// Now check if this is a stop node (after attributes are set)
|
|
8710
|
+
if (tagName !== xmlObj.tagname) {
|
|
8711
|
+
this.isCurrentNodeStopNode = this.isItStopNode(this.stopNodeExpressions, this.matcher);
|
|
7763
8712
|
}
|
|
8713
|
+
|
|
7764
8714
|
const startIndex = i;
|
|
7765
|
-
if (this.
|
|
8715
|
+
if (this.isCurrentNodeStopNode) {
|
|
7766
8716
|
let tagContent = "";
|
|
7767
|
-
|
|
7768
|
-
|
|
7769
|
-
|
|
7770
|
-
tagName = tagName.substr(0, tagName.length - 1);
|
|
7771
|
-
jPath = jPath.substr(0, jPath.length - 1);
|
|
7772
|
-
tagExp = tagName;
|
|
7773
|
-
}else {
|
|
7774
|
-
tagExp = tagExp.substr(0, tagExp.length - 1);
|
|
7775
|
-
}
|
|
8717
|
+
|
|
8718
|
+
// For self-closing tags, content is empty
|
|
8719
|
+
if (isSelfClosing) {
|
|
7776
8720
|
i = result.closeIndex;
|
|
7777
8721
|
}
|
|
7778
8722
|
//unpaired tag
|
|
7779
|
-
else if(this.options.unpairedTags.indexOf(tagName) !== -1){
|
|
7780
|
-
|
|
8723
|
+
else if (this.options.unpairedTags.indexOf(tagName) !== -1) {
|
|
7781
8724
|
i = result.closeIndex;
|
|
7782
8725
|
}
|
|
7783
8726
|
//normal tag
|
|
7784
8727
|
else {
|
|
7785
8728
|
//read until closing tag is found
|
|
7786
8729
|
const result = this.readStopNodeData(xmlData, rawTagName, closeIndex + 1);
|
|
7787
|
-
if(!result) throw new Error(`Unexpected end of ${rawTagName}`);
|
|
8730
|
+
if (!result) throw new Error(`Unexpected end of ${rawTagName}`);
|
|
7788
8731
|
i = result.i;
|
|
7789
8732
|
tagContent = result.tagContent;
|
|
7790
8733
|
}
|
|
7791
8734
|
|
|
7792
8735
|
const childNode = new XmlNode(tagName);
|
|
7793
8736
|
|
|
7794
|
-
if(
|
|
7795
|
-
childNode[":@"] =
|
|
7796
|
-
);
|
|
8737
|
+
if (prefixedAttrs) {
|
|
8738
|
+
childNode[":@"] = prefixedAttrs;
|
|
7797
8739
|
}
|
|
7798
|
-
|
|
7799
|
-
|
|
7800
|
-
}
|
|
7801
|
-
|
|
7802
|
-
jPath = jPath.substr(0, jPath.lastIndexOf("."));
|
|
8740
|
+
|
|
8741
|
+
// For stop nodes, store raw content as-is without any processing
|
|
7803
8742
|
childNode.add(this.options.textNodeName, tagContent);
|
|
7804
|
-
|
|
7805
|
-
this.addChild(currentNode, childNode, jPath, startIndex);
|
|
7806
|
-
}else {
|
|
7807
|
-
//selfClosing tag
|
|
7808
|
-
if(tagExp.length > 0 && tagExp.lastIndexOf("/") === tagExp.length - 1){
|
|
7809
|
-
if(tagName[tagName.length - 1] === "/"){ //remove trailing '/'
|
|
7810
|
-
tagName = tagName.substr(0, tagName.length - 1);
|
|
7811
|
-
jPath = jPath.substr(0, jPath.length - 1);
|
|
7812
|
-
tagExp = tagName;
|
|
7813
|
-
}else {
|
|
7814
|
-
tagExp = tagExp.substr(0, tagExp.length - 1);
|
|
7815
|
-
}
|
|
7816
|
-
|
|
7817
|
-
if(this.options.transformTagName) {
|
|
7818
|
-
const newTagName = this.options.transformTagName(tagName);
|
|
7819
|
-
if(tagExp === tagName) {
|
|
7820
|
-
tagExp = newTagName;
|
|
7821
|
-
}
|
|
7822
|
-
tagName = newTagName;
|
|
7823
|
-
}
|
|
7824
8743
|
|
|
8744
|
+
this.matcher.pop(); // Pop the stop node tag
|
|
8745
|
+
this.isCurrentNodeStopNode = false; // Reset flag
|
|
8746
|
+
|
|
8747
|
+
this.addChild(currentNode, childNode, this.matcher, startIndex);
|
|
8748
|
+
} else {
|
|
8749
|
+
//selfClosing tag
|
|
8750
|
+
if (isSelfClosing) {
|
|
8751
|
+
({ tagName, tagExp } = transformTagName(this.options.transformTagName, tagName, tagExp, this.options));
|
|
8752
|
+
|
|
8753
|
+
const childNode = new XmlNode(tagName);
|
|
8754
|
+
if (prefixedAttrs) {
|
|
8755
|
+
childNode[":@"] = prefixedAttrs;
|
|
8756
|
+
}
|
|
8757
|
+
this.addChild(currentNode, childNode, this.matcher, startIndex);
|
|
8758
|
+
this.matcher.pop(); // Pop self-closing tag
|
|
8759
|
+
this.isCurrentNodeStopNode = false; // Reset flag
|
|
8760
|
+
}
|
|
8761
|
+
else if (this.options.unpairedTags.indexOf(tagName) !== -1) {//unpaired tag
|
|
7825
8762
|
const childNode = new XmlNode(tagName);
|
|
7826
|
-
if(
|
|
7827
|
-
childNode[":@"] =
|
|
8763
|
+
if (prefixedAttrs) {
|
|
8764
|
+
childNode[":@"] = prefixedAttrs;
|
|
7828
8765
|
}
|
|
7829
|
-
this.addChild(currentNode, childNode,
|
|
7830
|
-
|
|
8766
|
+
this.addChild(currentNode, childNode, this.matcher, startIndex);
|
|
8767
|
+
this.matcher.pop(); // Pop unpaired tag
|
|
8768
|
+
this.isCurrentNodeStopNode = false; // Reset flag
|
|
8769
|
+
i = result.closeIndex;
|
|
8770
|
+
// Continue to next iteration without changing currentNode
|
|
8771
|
+
continue;
|
|
7831
8772
|
}
|
|
7832
|
-
|
|
8773
|
+
//opening tag
|
|
7833
8774
|
else {
|
|
7834
|
-
const childNode = new XmlNode(
|
|
8775
|
+
const childNode = new XmlNode(tagName);
|
|
8776
|
+
if (this.tagsNodeStack.length > this.options.maxNestedTags) {
|
|
8777
|
+
throw new Error("Maximum nested tags exceeded");
|
|
8778
|
+
}
|
|
7835
8779
|
this.tagsNodeStack.push(currentNode);
|
|
7836
|
-
|
|
7837
|
-
if(
|
|
7838
|
-
childNode[":@"] =
|
|
8780
|
+
|
|
8781
|
+
if (prefixedAttrs) {
|
|
8782
|
+
childNode[":@"] = prefixedAttrs;
|
|
7839
8783
|
}
|
|
7840
|
-
this.addChild(currentNode, childNode,
|
|
8784
|
+
this.addChild(currentNode, childNode, this.matcher, startIndex);
|
|
7841
8785
|
currentNode = childNode;
|
|
7842
8786
|
}
|
|
7843
8787
|
textData = "";
|
|
7844
8788
|
i = closeIndex;
|
|
7845
8789
|
}
|
|
7846
8790
|
}
|
|
7847
|
-
}else {
|
|
8791
|
+
} else {
|
|
7848
8792
|
textData += xmlData[i];
|
|
7849
8793
|
}
|
|
7850
8794
|
}
|
|
7851
8795
|
return xmlObj.child;
|
|
7852
8796
|
};
|
|
7853
8797
|
|
|
7854
|
-
function addChild$1(currentNode, childNode,
|
|
8798
|
+
function addChild$1(currentNode, childNode, matcher, startIndex) {
|
|
7855
8799
|
// unset startIndex if not requested
|
|
7856
8800
|
if (!this.options.captureMetaData) startIndex = undefined;
|
|
7857
|
-
|
|
7858
|
-
|
|
8801
|
+
|
|
8802
|
+
// Pass jPath string or matcher based on options.jPath setting
|
|
8803
|
+
const jPathOrMatcher = this.options.jPath ? matcher.toString() : matcher;
|
|
8804
|
+
const result = this.options.updateTag(childNode.tagname, jPathOrMatcher, childNode[":@"]);
|
|
8805
|
+
if (result === false) ; else if (typeof result === "string") {
|
|
7859
8806
|
childNode.tagname = result;
|
|
7860
8807
|
currentNode.addChild(childNode, startIndex);
|
|
7861
|
-
}else {
|
|
8808
|
+
} else {
|
|
7862
8809
|
currentNode.addChild(childNode, startIndex);
|
|
7863
8810
|
}
|
|
7864
8811
|
}
|
|
7865
8812
|
|
|
7866
|
-
|
|
8813
|
+
/**
|
|
8814
|
+
* @param {object} val - Entity object with regex and val properties
|
|
8815
|
+
* @param {string} tagName - Tag name
|
|
8816
|
+
* @param {string|Matcher} jPath - jPath string or Matcher instance based on options.jPath
|
|
8817
|
+
*/
|
|
8818
|
+
function replaceEntitiesValue(val, tagName, jPath) {
|
|
8819
|
+
const entityConfig = this.options.processEntities;
|
|
8820
|
+
|
|
8821
|
+
if (!entityConfig || !entityConfig.enabled) {
|
|
8822
|
+
return val;
|
|
8823
|
+
}
|
|
7867
8824
|
|
|
7868
|
-
if
|
|
7869
|
-
|
|
7870
|
-
|
|
7871
|
-
|
|
8825
|
+
// Check if tag is allowed to contain entities
|
|
8826
|
+
if (entityConfig.allowedTags) {
|
|
8827
|
+
const jPathOrMatcher = this.options.jPath ? jPath.toString() : jPath;
|
|
8828
|
+
const allowed = Array.isArray(entityConfig.allowedTags)
|
|
8829
|
+
? entityConfig.allowedTags.includes(tagName)
|
|
8830
|
+
: entityConfig.allowedTags(tagName, jPathOrMatcher);
|
|
8831
|
+
|
|
8832
|
+
if (!allowed) {
|
|
8833
|
+
return val;
|
|
7872
8834
|
}
|
|
7873
|
-
|
|
7874
|
-
|
|
7875
|
-
|
|
8835
|
+
}
|
|
8836
|
+
|
|
8837
|
+
// Apply custom tag filter if provided
|
|
8838
|
+
if (entityConfig.tagFilter) {
|
|
8839
|
+
const jPathOrMatcher = this.options.jPath ? jPath.toString() : jPath;
|
|
8840
|
+
if (!entityConfig.tagFilter(tagName, jPathOrMatcher)) {
|
|
8841
|
+
return val; // Skip based on custom filter
|
|
7876
8842
|
}
|
|
7877
|
-
|
|
7878
|
-
|
|
7879
|
-
|
|
7880
|
-
|
|
8843
|
+
}
|
|
8844
|
+
|
|
8845
|
+
// Replace DOCTYPE entities
|
|
8846
|
+
for (let entityName in this.docTypeEntities) {
|
|
8847
|
+
const entity = this.docTypeEntities[entityName];
|
|
8848
|
+
const matches = val.match(entity.regx);
|
|
8849
|
+
|
|
8850
|
+
if (matches) {
|
|
8851
|
+
// Track expansions
|
|
8852
|
+
this.entityExpansionCount += matches.length;
|
|
8853
|
+
|
|
8854
|
+
// Check expansion limit
|
|
8855
|
+
if (entityConfig.maxTotalExpansions &&
|
|
8856
|
+
this.entityExpansionCount > entityConfig.maxTotalExpansions) {
|
|
8857
|
+
throw new Error(
|
|
8858
|
+
`Entity expansion limit exceeded: ${this.entityExpansionCount} > ${entityConfig.maxTotalExpansions}`
|
|
8859
|
+
);
|
|
8860
|
+
}
|
|
8861
|
+
|
|
8862
|
+
// Store length before replacement
|
|
8863
|
+
const lengthBefore = val.length;
|
|
8864
|
+
val = val.replace(entity.regx, entity.val);
|
|
8865
|
+
|
|
8866
|
+
// Check expanded length immediately after replacement
|
|
8867
|
+
if (entityConfig.maxExpandedLength) {
|
|
8868
|
+
this.currentExpandedLength += (val.length - lengthBefore);
|
|
8869
|
+
|
|
8870
|
+
if (this.currentExpandedLength > entityConfig.maxExpandedLength) {
|
|
8871
|
+
throw new Error(
|
|
8872
|
+
`Total expanded content size exceeded: ${this.currentExpandedLength} > ${entityConfig.maxExpandedLength}`
|
|
8873
|
+
);
|
|
8874
|
+
}
|
|
7881
8875
|
}
|
|
7882
8876
|
}
|
|
7883
|
-
val = val.replace( this.ampEntity.regex, this.ampEntity.val);
|
|
7884
8877
|
}
|
|
8878
|
+
if (val.indexOf('&') === -1) return val; // Early exit
|
|
8879
|
+
|
|
8880
|
+
// Replace standard entities
|
|
8881
|
+
for (let entityName in this.lastEntities) {
|
|
8882
|
+
const entity = this.lastEntities[entityName];
|
|
8883
|
+
val = val.replace(entity.regex, entity.val);
|
|
8884
|
+
}
|
|
8885
|
+
if (val.indexOf('&') === -1) return val; // Early exit
|
|
8886
|
+
|
|
8887
|
+
// Replace HTML entities if enabled
|
|
8888
|
+
if (this.options.htmlEntities) {
|
|
8889
|
+
for (let entityName in this.htmlEntities) {
|
|
8890
|
+
const entity = this.htmlEntities[entityName];
|
|
8891
|
+
val = val.replace(entity.regex, entity.val);
|
|
8892
|
+
}
|
|
8893
|
+
}
|
|
8894
|
+
|
|
8895
|
+
// Replace ampersand entity last
|
|
8896
|
+
val = val.replace(this.ampEntity.regex, this.ampEntity.val);
|
|
8897
|
+
|
|
7885
8898
|
return val;
|
|
7886
|
-
}
|
|
7887
|
-
|
|
8899
|
+
}
|
|
8900
|
+
|
|
8901
|
+
|
|
8902
|
+
function saveTextToParentTag(textData, parentNode, matcher, isLeafNode) {
|
|
7888
8903
|
if (textData) { //store previously collected data as textNode
|
|
7889
|
-
if(isLeafNode === undefined) isLeafNode =
|
|
7890
|
-
|
|
8904
|
+
if (isLeafNode === undefined) isLeafNode = parentNode.child.length === 0;
|
|
8905
|
+
|
|
7891
8906
|
textData = this.parseTextData(textData,
|
|
7892
|
-
|
|
7893
|
-
|
|
8907
|
+
parentNode.tagname,
|
|
8908
|
+
matcher,
|
|
7894
8909
|
false,
|
|
7895
|
-
|
|
8910
|
+
parentNode[":@"] ? Object.keys(parentNode[":@"]).length !== 0 : false,
|
|
7896
8911
|
isLeafNode);
|
|
7897
8912
|
|
|
7898
8913
|
if (textData !== undefined && textData !== "")
|
|
7899
|
-
|
|
8914
|
+
parentNode.add(this.options.textNodeName, textData);
|
|
7900
8915
|
textData = "";
|
|
7901
8916
|
}
|
|
7902
8917
|
return textData;
|
|
@@ -7904,14 +8919,17 @@
|
|
|
7904
8919
|
|
|
7905
8920
|
//TODO: use jPath to simplify the logic
|
|
7906
8921
|
/**
|
|
7907
|
-
* @param {
|
|
7908
|
-
* @param {
|
|
7909
|
-
* @param {string} jPath
|
|
7910
|
-
* @param {string} currentTagName
|
|
8922
|
+
* @param {Array<Expression>} stopNodeExpressions - Array of compiled Expression objects
|
|
8923
|
+
* @param {Matcher} matcher - Current path matcher
|
|
7911
8924
|
*/
|
|
7912
|
-
function isItStopNode(
|
|
7913
|
-
if(
|
|
7914
|
-
|
|
8925
|
+
function isItStopNode(stopNodeExpressions, matcher) {
|
|
8926
|
+
if (!stopNodeExpressions || stopNodeExpressions.length === 0) return false;
|
|
8927
|
+
|
|
8928
|
+
for (let i = 0; i < stopNodeExpressions.length; i++) {
|
|
8929
|
+
if (matcher.matches(stopNodeExpressions[i])) {
|
|
8930
|
+
return true;
|
|
8931
|
+
}
|
|
8932
|
+
}
|
|
7915
8933
|
return false;
|
|
7916
8934
|
}
|
|
7917
8935
|
|
|
@@ -7921,24 +8939,24 @@
|
|
|
7921
8939
|
* @param {number} i starting index
|
|
7922
8940
|
* @returns
|
|
7923
8941
|
*/
|
|
7924
|
-
function tagExpWithClosingIndex(xmlData, i, closingChar = ">"){
|
|
8942
|
+
function tagExpWithClosingIndex(xmlData, i, closingChar = ">") {
|
|
7925
8943
|
let attrBoundary;
|
|
7926
8944
|
let tagExp = "";
|
|
7927
8945
|
for (let index = i; index < xmlData.length; index++) {
|
|
7928
8946
|
let ch = xmlData[index];
|
|
7929
8947
|
if (attrBoundary) {
|
|
7930
|
-
|
|
8948
|
+
if (ch === attrBoundary) attrBoundary = "";//reset
|
|
7931
8949
|
} else if (ch === '"' || ch === "'") {
|
|
7932
|
-
|
|
8950
|
+
attrBoundary = ch;
|
|
7933
8951
|
} else if (ch === closingChar[0]) {
|
|
7934
|
-
if(closingChar[1]){
|
|
7935
|
-
if(xmlData[index + 1] === closingChar[1]){
|
|
8952
|
+
if (closingChar[1]) {
|
|
8953
|
+
if (xmlData[index + 1] === closingChar[1]) {
|
|
7936
8954
|
return {
|
|
7937
8955
|
data: tagExp,
|
|
7938
8956
|
index: index
|
|
7939
8957
|
}
|
|
7940
8958
|
}
|
|
7941
|
-
}else {
|
|
8959
|
+
} else {
|
|
7942
8960
|
return {
|
|
7943
8961
|
data: tagExp,
|
|
7944
8962
|
index: index
|
|
@@ -7951,33 +8969,33 @@
|
|
|
7951
8969
|
}
|
|
7952
8970
|
}
|
|
7953
8971
|
|
|
7954
|
-
function findClosingIndex(xmlData, str, i, errMsg){
|
|
8972
|
+
function findClosingIndex(xmlData, str, i, errMsg) {
|
|
7955
8973
|
const closingIndex = xmlData.indexOf(str, i);
|
|
7956
|
-
if(closingIndex === -1){
|
|
8974
|
+
if (closingIndex === -1) {
|
|
7957
8975
|
throw new Error(errMsg)
|
|
7958
|
-
}else {
|
|
8976
|
+
} else {
|
|
7959
8977
|
return closingIndex + str.length - 1;
|
|
7960
8978
|
}
|
|
7961
8979
|
}
|
|
7962
8980
|
|
|
7963
|
-
function readTagExp(xmlData,i, removeNSPrefix, closingChar = ">"){
|
|
7964
|
-
const result = tagExpWithClosingIndex(xmlData, i+1, closingChar);
|
|
7965
|
-
if(!result) return;
|
|
8981
|
+
function readTagExp(xmlData, i, removeNSPrefix, closingChar = ">") {
|
|
8982
|
+
const result = tagExpWithClosingIndex(xmlData, i + 1, closingChar);
|
|
8983
|
+
if (!result) return;
|
|
7966
8984
|
let tagExp = result.data;
|
|
7967
8985
|
const closeIndex = result.index;
|
|
7968
8986
|
const separatorIndex = tagExp.search(/\s/);
|
|
7969
8987
|
let tagName = tagExp;
|
|
7970
8988
|
let attrExpPresent = true;
|
|
7971
|
-
if(separatorIndex !== -1){//separate tag name and attributes expression
|
|
8989
|
+
if (separatorIndex !== -1) {//separate tag name and attributes expression
|
|
7972
8990
|
tagName = tagExp.substring(0, separatorIndex);
|
|
7973
8991
|
tagExp = tagExp.substring(separatorIndex + 1).trimStart();
|
|
7974
8992
|
}
|
|
7975
8993
|
|
|
7976
8994
|
const rawTagName = tagName;
|
|
7977
|
-
if(removeNSPrefix){
|
|
8995
|
+
if (removeNSPrefix) {
|
|
7978
8996
|
const colonIndex = tagName.indexOf(":");
|
|
7979
|
-
if(colonIndex !== -1){
|
|
7980
|
-
tagName = tagName.substr(colonIndex+1);
|
|
8997
|
+
if (colonIndex !== -1) {
|
|
8998
|
+
tagName = tagName.substr(colonIndex + 1);
|
|
7981
8999
|
attrExpPresent = tagName !== result.data.substr(colonIndex + 1);
|
|
7982
9000
|
}
|
|
7983
9001
|
}
|
|
@@ -7996,47 +9014,47 @@
|
|
|
7996
9014
|
* @param {string} tagName
|
|
7997
9015
|
* @param {number} i
|
|
7998
9016
|
*/
|
|
7999
|
-
function readStopNodeData(xmlData, tagName, i){
|
|
9017
|
+
function readStopNodeData(xmlData, tagName, i) {
|
|
8000
9018
|
const startIndex = i;
|
|
8001
9019
|
// Starting at 1 since we already have an open tag
|
|
8002
9020
|
let openTagCount = 1;
|
|
8003
9021
|
|
|
8004
9022
|
for (; i < xmlData.length; i++) {
|
|
8005
|
-
if(
|
|
8006
|
-
if (xmlData[i+1] === "/") {//close tag
|
|
8007
|
-
|
|
8008
|
-
|
|
8009
|
-
|
|
8010
|
-
|
|
8011
|
-
|
|
8012
|
-
|
|
8013
|
-
|
|
8014
|
-
|
|
8015
|
-
}
|
|
9023
|
+
if (xmlData[i] === "<") {
|
|
9024
|
+
if (xmlData[i + 1] === "/") {//close tag
|
|
9025
|
+
const closeIndex = findClosingIndex(xmlData, ">", i, `${tagName} is not closed`);
|
|
9026
|
+
let closeTagName = xmlData.substring(i + 2, closeIndex).trim();
|
|
9027
|
+
if (closeTagName === tagName) {
|
|
9028
|
+
openTagCount--;
|
|
9029
|
+
if (openTagCount === 0) {
|
|
9030
|
+
return {
|
|
9031
|
+
tagContent: xmlData.substring(startIndex, i),
|
|
9032
|
+
i: closeIndex
|
|
8016
9033
|
}
|
|
8017
9034
|
}
|
|
8018
|
-
|
|
8019
|
-
|
|
8020
|
-
|
|
8021
|
-
|
|
8022
|
-
|
|
8023
|
-
|
|
8024
|
-
|
|
8025
|
-
|
|
8026
|
-
|
|
8027
|
-
|
|
8028
|
-
|
|
8029
|
-
|
|
9035
|
+
}
|
|
9036
|
+
i = closeIndex;
|
|
9037
|
+
} else if (xmlData[i + 1] === '?') {
|
|
9038
|
+
const closeIndex = findClosingIndex(xmlData, "?>", i + 1, "StopNode is not closed.");
|
|
9039
|
+
i = closeIndex;
|
|
9040
|
+
} else if (xmlData.substr(i + 1, 3) === '!--') {
|
|
9041
|
+
const closeIndex = findClosingIndex(xmlData, "-->", i + 3, "StopNode is not closed.");
|
|
9042
|
+
i = closeIndex;
|
|
9043
|
+
} else if (xmlData.substr(i + 1, 2) === '![') {
|
|
9044
|
+
const closeIndex = findClosingIndex(xmlData, "]]>", i, "StopNode is not closed.") - 2;
|
|
9045
|
+
i = closeIndex;
|
|
9046
|
+
} else {
|
|
9047
|
+
const tagData = readTagExp(xmlData, i, '>');
|
|
8030
9048
|
|
|
8031
|
-
|
|
8032
|
-
|
|
8033
|
-
|
|
8034
|
-
|
|
8035
|
-
}
|
|
8036
|
-
i=tagData.closeIndex;
|
|
9049
|
+
if (tagData) {
|
|
9050
|
+
const openTagName = tagData && tagData.tagName;
|
|
9051
|
+
if (openTagName === tagName && tagData.tagExp[tagData.tagExp.length - 1] !== "/") {
|
|
9052
|
+
openTagCount++;
|
|
8037
9053
|
}
|
|
9054
|
+
i = tagData.closeIndex;
|
|
8038
9055
|
}
|
|
8039
9056
|
}
|
|
9057
|
+
}
|
|
8040
9058
|
}//end for loop
|
|
8041
9059
|
}
|
|
8042
9060
|
|
|
@@ -8044,8 +9062,8 @@
|
|
|
8044
9062
|
if (shouldParse && typeof val === 'string') {
|
|
8045
9063
|
//console.log(options)
|
|
8046
9064
|
const newval = val.trim();
|
|
8047
|
-
if(newval === 'true'
|
|
8048
|
-
else if(newval === 'false'
|
|
9065
|
+
if (newval === 'true') return true;
|
|
9066
|
+
else if (newval === 'false') return false;
|
|
8049
9067
|
else return toNumber(val, options);
|
|
8050
9068
|
} else {
|
|
8051
9069
|
if (isExist(val)) {
|
|
@@ -8056,97 +9074,184 @@
|
|
|
8056
9074
|
}
|
|
8057
9075
|
}
|
|
8058
9076
|
|
|
9077
|
+
function fromCodePoint(str, base, prefix) {
|
|
9078
|
+
const codePoint = Number.parseInt(str, base);
|
|
9079
|
+
|
|
9080
|
+
if (codePoint >= 0 && codePoint <= 0x10FFFF) {
|
|
9081
|
+
return String.fromCodePoint(codePoint);
|
|
9082
|
+
} else {
|
|
9083
|
+
return prefix + str + ";";
|
|
9084
|
+
}
|
|
9085
|
+
}
|
|
9086
|
+
|
|
9087
|
+
function transformTagName(fn, tagName, tagExp, options) {
|
|
9088
|
+
if (fn) {
|
|
9089
|
+
const newTagName = fn(tagName);
|
|
9090
|
+
if (tagExp === tagName) {
|
|
9091
|
+
tagExp = newTagName;
|
|
9092
|
+
}
|
|
9093
|
+
tagName = newTagName;
|
|
9094
|
+
}
|
|
9095
|
+
tagName = sanitizeName(tagName, options);
|
|
9096
|
+
return { tagName, tagExp };
|
|
9097
|
+
}
|
|
9098
|
+
|
|
9099
|
+
|
|
9100
|
+
|
|
9101
|
+
function sanitizeName(name, options) {
|
|
9102
|
+
if (criticalProperties.includes(name)) {
|
|
9103
|
+
throw new Error(`[SECURITY] Invalid name: "${name}" is a reserved JavaScript keyword that could cause prototype pollution`);
|
|
9104
|
+
} else if (DANGEROUS_PROPERTY_NAMES.includes(name)) {
|
|
9105
|
+
return options.onDangerousProperty(name);
|
|
9106
|
+
}
|
|
9107
|
+
return name;
|
|
9108
|
+
}
|
|
9109
|
+
|
|
8059
9110
|
const METADATA_SYMBOL = XmlNode.getMetaDataSymbol();
|
|
8060
9111
|
|
|
9112
|
+
/**
|
|
9113
|
+
* Helper function to strip attribute prefix from attribute map
|
|
9114
|
+
* @param {object} attrs - Attributes with prefix (e.g., {"@_class": "code"})
|
|
9115
|
+
* @param {string} prefix - Attribute prefix to remove (e.g., "@_")
|
|
9116
|
+
* @returns {object} Attributes without prefix (e.g., {"class": "code"})
|
|
9117
|
+
*/
|
|
9118
|
+
function stripAttributePrefix(attrs, prefix) {
|
|
9119
|
+
if (!attrs || typeof attrs !== 'object') return {};
|
|
9120
|
+
if (!prefix) return attrs;
|
|
9121
|
+
|
|
9122
|
+
const rawAttrs = {};
|
|
9123
|
+
for (const key in attrs) {
|
|
9124
|
+
if (key.startsWith(prefix)) {
|
|
9125
|
+
const rawName = key.substring(prefix.length);
|
|
9126
|
+
rawAttrs[rawName] = attrs[key];
|
|
9127
|
+
} else {
|
|
9128
|
+
// Attribute without prefix (shouldn't normally happen, but be safe)
|
|
9129
|
+
rawAttrs[key] = attrs[key];
|
|
9130
|
+
}
|
|
9131
|
+
}
|
|
9132
|
+
return rawAttrs;
|
|
9133
|
+
}
|
|
9134
|
+
|
|
8061
9135
|
/**
|
|
8062
9136
|
*
|
|
8063
9137
|
* @param {array} node
|
|
8064
9138
|
* @param {any} options
|
|
9139
|
+
* @param {Matcher} matcher - Path matcher instance
|
|
8065
9140
|
* @returns
|
|
8066
9141
|
*/
|
|
8067
|
-
function prettify(node, options){
|
|
8068
|
-
return compress(
|
|
9142
|
+
function prettify(node, options, matcher) {
|
|
9143
|
+
return compress(node, options, matcher);
|
|
8069
9144
|
}
|
|
8070
9145
|
|
|
8071
9146
|
/**
|
|
8072
9147
|
*
|
|
8073
9148
|
* @param {array} arr
|
|
8074
9149
|
* @param {object} options
|
|
8075
|
-
* @param {
|
|
9150
|
+
* @param {Matcher} matcher - Path matcher instance
|
|
8076
9151
|
* @returns object
|
|
8077
9152
|
*/
|
|
8078
|
-
function compress(arr, options,
|
|
9153
|
+
function compress(arr, options, matcher) {
|
|
8079
9154
|
let text;
|
|
8080
|
-
const compressedObj = {};
|
|
9155
|
+
const compressedObj = {}; //This is intended to be a plain object
|
|
8081
9156
|
for (let i = 0; i < arr.length; i++) {
|
|
8082
9157
|
const tagObj = arr[i];
|
|
8083
9158
|
const property = propName(tagObj);
|
|
8084
|
-
let newJpath = "";
|
|
8085
|
-
if(jPath === undefined) newJpath = property;
|
|
8086
|
-
else newJpath = jPath + "." + property;
|
|
8087
9159
|
|
|
8088
|
-
|
|
8089
|
-
|
|
9160
|
+
// Push current property to matcher WITH RAW ATTRIBUTES (no prefix)
|
|
9161
|
+
if (property !== undefined && property !== options.textNodeName) {
|
|
9162
|
+
const rawAttrs = stripAttributePrefix(
|
|
9163
|
+
tagObj[":@"] || {},
|
|
9164
|
+
options.attributeNamePrefix
|
|
9165
|
+
);
|
|
9166
|
+
matcher.push(property, rawAttrs);
|
|
9167
|
+
}
|
|
9168
|
+
|
|
9169
|
+
if (property === options.textNodeName) {
|
|
9170
|
+
if (text === undefined) text = tagObj[property];
|
|
8090
9171
|
else text += "" + tagObj[property];
|
|
8091
|
-
}else if(property === undefined){
|
|
9172
|
+
} else if (property === undefined) {
|
|
8092
9173
|
continue;
|
|
8093
|
-
}else if(tagObj[property]){
|
|
8094
|
-
|
|
8095
|
-
let val = compress(tagObj[property], options,
|
|
9174
|
+
} else if (tagObj[property]) {
|
|
9175
|
+
|
|
9176
|
+
let val = compress(tagObj[property], options, matcher);
|
|
8096
9177
|
const isLeaf = isLeafTag(val, options);
|
|
8097
|
-
if (tagObj[METADATA_SYMBOL] !== undefined) {
|
|
8098
|
-
val[METADATA_SYMBOL] = tagObj[METADATA_SYMBOL]; // copy over metadata
|
|
8099
|
-
}
|
|
8100
9178
|
|
|
8101
|
-
if(tagObj[":@"]){
|
|
8102
|
-
assignAttributes$1(
|
|
8103
|
-
}else if(Object.keys(val).length === 1 && val[options.textNodeName] !== undefined && !options.alwaysCreateTextNode){
|
|
9179
|
+
if (tagObj[":@"]) {
|
|
9180
|
+
assignAttributes$1(val, tagObj[":@"], matcher, options);
|
|
9181
|
+
} else if (Object.keys(val).length === 1 && val[options.textNodeName] !== undefined && !options.alwaysCreateTextNode) {
|
|
8104
9182
|
val = val[options.textNodeName];
|
|
8105
|
-
}else if(Object.keys(val).length === 0){
|
|
8106
|
-
if(options.alwaysCreateTextNode) val[options.textNodeName] = "";
|
|
9183
|
+
} else if (Object.keys(val).length === 0) {
|
|
9184
|
+
if (options.alwaysCreateTextNode) val[options.textNodeName] = "";
|
|
8107
9185
|
else val = "";
|
|
8108
9186
|
}
|
|
8109
9187
|
|
|
8110
|
-
if(
|
|
8111
|
-
|
|
8112
|
-
|
|
9188
|
+
if (tagObj[METADATA_SYMBOL] !== undefined && typeof val === "object" && val !== null) {
|
|
9189
|
+
val[METADATA_SYMBOL] = tagObj[METADATA_SYMBOL]; // copy over metadata
|
|
9190
|
+
}
|
|
9191
|
+
|
|
9192
|
+
|
|
9193
|
+
if (compressedObj[property] !== undefined && Object.prototype.hasOwnProperty.call(compressedObj, property)) {
|
|
9194
|
+
if (!Array.isArray(compressedObj[property])) {
|
|
9195
|
+
compressedObj[property] = [compressedObj[property]];
|
|
8113
9196
|
}
|
|
8114
9197
|
compressedObj[property].push(val);
|
|
8115
|
-
}else {
|
|
9198
|
+
} else {
|
|
8116
9199
|
//TODO: if a node is not an array, then check if it should be an array
|
|
8117
9200
|
//also determine if it is a leaf node
|
|
8118
|
-
|
|
9201
|
+
|
|
9202
|
+
// Pass jPath string or matcher based on options.jPath setting
|
|
9203
|
+
const jPathOrMatcher = options.jPath ? matcher.toString() : matcher;
|
|
9204
|
+
if (options.isArray(property, jPathOrMatcher, isLeaf)) {
|
|
8119
9205
|
compressedObj[property] = [val];
|
|
8120
|
-
}else {
|
|
9206
|
+
} else {
|
|
8121
9207
|
compressedObj[property] = val;
|
|
8122
9208
|
}
|
|
8123
9209
|
}
|
|
9210
|
+
|
|
9211
|
+
// Pop property from matcher after processing
|
|
9212
|
+
if (property !== undefined && property !== options.textNodeName) {
|
|
9213
|
+
matcher.pop();
|
|
9214
|
+
}
|
|
8124
9215
|
}
|
|
8125
|
-
|
|
9216
|
+
|
|
8126
9217
|
}
|
|
8127
9218
|
// if(text && text.length > 0) compressedObj[options.textNodeName] = text;
|
|
8128
|
-
if(typeof text === "string"){
|
|
8129
|
-
if(text.length > 0) compressedObj[options.textNodeName] = text;
|
|
8130
|
-
}else if(text !== undefined) compressedObj[options.textNodeName] = text;
|
|
9219
|
+
if (typeof text === "string") {
|
|
9220
|
+
if (text.length > 0) compressedObj[options.textNodeName] = text;
|
|
9221
|
+
} else if (text !== undefined) compressedObj[options.textNodeName] = text;
|
|
9222
|
+
|
|
9223
|
+
|
|
8131
9224
|
return compressedObj;
|
|
8132
9225
|
}
|
|
8133
9226
|
|
|
8134
|
-
function propName(obj){
|
|
9227
|
+
function propName(obj) {
|
|
8135
9228
|
const keys = Object.keys(obj);
|
|
8136
9229
|
for (let i = 0; i < keys.length; i++) {
|
|
8137
9230
|
const key = keys[i];
|
|
8138
|
-
if(key !== ":@") return key;
|
|
9231
|
+
if (key !== ":@") return key;
|
|
8139
9232
|
}
|
|
8140
9233
|
}
|
|
8141
9234
|
|
|
8142
|
-
function assignAttributes$1(obj, attrMap,
|
|
9235
|
+
function assignAttributes$1(obj, attrMap, matcher, options) {
|
|
8143
9236
|
if (attrMap) {
|
|
8144
9237
|
const keys = Object.keys(attrMap);
|
|
8145
9238
|
const len = keys.length; //don't make it inline
|
|
8146
9239
|
for (let i = 0; i < len; i++) {
|
|
8147
|
-
const atrrName = keys[i];
|
|
8148
|
-
|
|
8149
|
-
|
|
9240
|
+
const atrrName = keys[i]; // This is the PREFIXED name (e.g., "@_class")
|
|
9241
|
+
|
|
9242
|
+
// Strip prefix for matcher path (for isArray callback)
|
|
9243
|
+
const rawAttrName = atrrName.startsWith(options.attributeNamePrefix)
|
|
9244
|
+
? atrrName.substring(options.attributeNamePrefix.length)
|
|
9245
|
+
: atrrName;
|
|
9246
|
+
|
|
9247
|
+
// For attributes, we need to create a temporary path
|
|
9248
|
+
// Pass jPath string or matcher based on options.jPath setting
|
|
9249
|
+
const jPathOrMatcher = options.jPath
|
|
9250
|
+
? matcher.toString() + "." + rawAttrName
|
|
9251
|
+
: matcher;
|
|
9252
|
+
|
|
9253
|
+
if (options.isArray(atrrName, jPathOrMatcher, true, true)) {
|
|
9254
|
+
obj[atrrName] = [attrMap[atrrName]];
|
|
8150
9255
|
} else {
|
|
8151
9256
|
obj[atrrName] = attrMap[atrrName];
|
|
8152
9257
|
}
|
|
@@ -8154,10 +9259,10 @@
|
|
|
8154
9259
|
}
|
|
8155
9260
|
}
|
|
8156
9261
|
|
|
8157
|
-
function isLeafTag(obj, options){
|
|
9262
|
+
function isLeafTag(obj, options) {
|
|
8158
9263
|
const { textNodeName } = options;
|
|
8159
9264
|
const propCount = Object.keys(obj).length;
|
|
8160
|
-
|
|
9265
|
+
|
|
8161
9266
|
if (propCount === 0) {
|
|
8162
9267
|
return true;
|
|
8163
9268
|
}
|
|
@@ -8172,38 +9277,38 @@
|
|
|
8172
9277
|
return false;
|
|
8173
9278
|
}
|
|
8174
9279
|
|
|
8175
|
-
class XMLParser{
|
|
8176
|
-
|
|
8177
|
-
constructor(options){
|
|
9280
|
+
class XMLParser {
|
|
9281
|
+
|
|
9282
|
+
constructor(options) {
|
|
8178
9283
|
this.externalEntities = {};
|
|
8179
9284
|
this.options = buildOptions(options);
|
|
8180
|
-
|
|
9285
|
+
|
|
8181
9286
|
}
|
|
8182
9287
|
/**
|
|
8183
9288
|
* Parse XML dats to JS object
|
|
8184
9289
|
* @param {string|Uint8Array} xmlData
|
|
8185
9290
|
* @param {boolean|Object} validationOption
|
|
8186
9291
|
*/
|
|
8187
|
-
parse(xmlData,validationOption){
|
|
8188
|
-
if(typeof xmlData !== "string" && xmlData.toString){
|
|
9292
|
+
parse(xmlData, validationOption) {
|
|
9293
|
+
if (typeof xmlData !== "string" && xmlData.toString) {
|
|
8189
9294
|
xmlData = xmlData.toString();
|
|
8190
|
-
}else if(typeof xmlData !== "string"){
|
|
9295
|
+
} else if (typeof xmlData !== "string") {
|
|
8191
9296
|
throw new Error("XML data is accepted in String or Bytes[] form.")
|
|
8192
9297
|
}
|
|
8193
|
-
|
|
8194
|
-
if(
|
|
8195
|
-
if(validationOption === true) validationOption = {}; //validate with default options
|
|
8196
|
-
|
|
9298
|
+
|
|
9299
|
+
if (validationOption) {
|
|
9300
|
+
if (validationOption === true) validationOption = {}; //validate with default options
|
|
9301
|
+
|
|
8197
9302
|
const result = validate(xmlData, validationOption);
|
|
8198
9303
|
if (result !== true) {
|
|
8199
|
-
|
|
9304
|
+
throw Error(`${result.err.msg}:${result.err.line}:${result.err.col}`)
|
|
8200
9305
|
}
|
|
8201
|
-
|
|
9306
|
+
}
|
|
8202
9307
|
const orderedObjParser = new OrderedObjParser(this.options);
|
|
8203
9308
|
orderedObjParser.addExternalEntities(this.externalEntities);
|
|
8204
9309
|
const orderedResult = orderedObjParser.parseXml(xmlData);
|
|
8205
|
-
if(this.options.preserveOrder || orderedResult === undefined) return orderedResult;
|
|
8206
|
-
else return prettify(orderedResult, this.options);
|
|
9310
|
+
if (this.options.preserveOrder || orderedResult === undefined) return orderedResult;
|
|
9311
|
+
else return prettify(orderedResult, this.options, orderedObjParser.matcher);
|
|
8207
9312
|
}
|
|
8208
9313
|
|
|
8209
9314
|
/**
|
|
@@ -8211,14 +9316,14 @@
|
|
|
8211
9316
|
* @param {string} key
|
|
8212
9317
|
* @param {string} value
|
|
8213
9318
|
*/
|
|
8214
|
-
addEntity(key, value){
|
|
8215
|
-
if(value.indexOf("&") !== -1){
|
|
9319
|
+
addEntity(key, value) {
|
|
9320
|
+
if (value.indexOf("&") !== -1) {
|
|
8216
9321
|
throw new Error("Entity value can't have '&'")
|
|
8217
|
-
}else if(key.indexOf("&") !== -1 || key.indexOf(";") !== -1){
|
|
9322
|
+
} else if (key.indexOf("&") !== -1 || key.indexOf(";") !== -1) {
|
|
8218
9323
|
throw new Error("An entity must be set without '&' and ';'. Eg. use '#xD' for '
'")
|
|
8219
|
-
}else if(value === "&"){
|
|
9324
|
+
} else if (value === "&") {
|
|
8220
9325
|
throw new Error("An entity with value '&' is not permitted");
|
|
8221
|
-
}else {
|
|
9326
|
+
} else {
|
|
8222
9327
|
this.externalEntities[key] = value;
|
|
8223
9328
|
}
|
|
8224
9329
|
}
|
|
@@ -9934,6 +11039,162 @@
|
|
|
9934
11039
|
};
|
|
9935
11040
|
}
|
|
9936
11041
|
|
|
11042
|
+
/**
|
|
11043
|
+
* Count the number of leaf nodes in a subtree.
|
|
11044
|
+
* @param {Object} node - A D3 hierarchy node
|
|
11045
|
+
* @returns {Number} The number of leaves
|
|
11046
|
+
*/
|
|
11047
|
+
function countLeaves(node) {
|
|
11048
|
+
if (!node.children || node.children.length === 0) {
|
|
11049
|
+
return 1;
|
|
11050
|
+
}
|
|
11051
|
+
return node.children.reduce((sum, child) => sum + countLeaves(child), 0);
|
|
11052
|
+
}
|
|
11053
|
+
|
|
11054
|
+
/**
|
|
11055
|
+
* Compute unrooted (equal-angle) layout coordinates for all nodes.
|
|
11056
|
+
*
|
|
11057
|
+
* The algorithm allocates 2π of angular space at the root, then recursively
|
|
11058
|
+
* assigns each child subtree angular space proportional to its leaf count.
|
|
11059
|
+
* Each node is placed at parent_position + branch_length * (cos(angle), sin(angle)).
|
|
11060
|
+
*
|
|
11061
|
+
* After computing raw positions, coordinates are normalized to fit within
|
|
11062
|
+
* the given width × height.
|
|
11063
|
+
*
|
|
11064
|
+
* @param {Object} root - The root node of the D3 hierarchy
|
|
11065
|
+
* @param {Array} scales - [scale_x, scale_y] from placenodes
|
|
11066
|
+
* @param {Number} width - Available width in pixels
|
|
11067
|
+
* @param {Number} height - Available height in pixels
|
|
11068
|
+
* @param {Number} labelPad - Pixel padding to reserve for labels on all sides
|
|
11069
|
+
*/
|
|
11070
|
+
function computeUnrootedLayout(root, scales, width, height, labelPad) {
|
|
11071
|
+
labelPad = labelPad || 0;
|
|
11072
|
+
|
|
11073
|
+
// Assign raw (x, y) using equal-angle algorithm
|
|
11074
|
+
// Root is at the origin
|
|
11075
|
+
root._unrooted_x = 0;
|
|
11076
|
+
root._unrooted_y = 0;
|
|
11077
|
+
|
|
11078
|
+
equalAngle(root, 0, 2 * Math.PI);
|
|
11079
|
+
|
|
11080
|
+
// Find bounding box of all raw positions
|
|
11081
|
+
let minX = Infinity, maxX = -Infinity;
|
|
11082
|
+
let minY = Infinity, maxY = -Infinity;
|
|
11083
|
+
|
|
11084
|
+
root.each(d => {
|
|
11085
|
+
if (d._unrooted_x < minX) minX = d._unrooted_x;
|
|
11086
|
+
if (d._unrooted_x > maxX) maxX = d._unrooted_x;
|
|
11087
|
+
if (d._unrooted_y < minY) minY = d._unrooted_y;
|
|
11088
|
+
if (d._unrooted_y > maxY) maxY = d._unrooted_y;
|
|
11089
|
+
});
|
|
11090
|
+
|
|
11091
|
+
let rawWidth = maxX - minX || 1;
|
|
11092
|
+
let rawHeight = maxY - minY || 1;
|
|
11093
|
+
|
|
11094
|
+
// Reserve pixel-based padding for labels on all sides
|
|
11095
|
+
let drawWidth = width - 2 * labelPad;
|
|
11096
|
+
let drawHeight = height - 2 * labelPad;
|
|
11097
|
+
if (drawWidth < 100) drawWidth = 100;
|
|
11098
|
+
if (drawHeight < 100) drawHeight = 100;
|
|
11099
|
+
|
|
11100
|
+
let scaleX = drawWidth / rawWidth;
|
|
11101
|
+
let scaleY = drawHeight / rawHeight;
|
|
11102
|
+
let scale = Math.min(scaleX, scaleY);
|
|
11103
|
+
|
|
11104
|
+
let offsetX = labelPad + drawWidth / 2 - (minX + maxX) / 2 * scale;
|
|
11105
|
+
let offsetY = labelPad + drawHeight / 2 - (minY + maxY) / 2 * scale;
|
|
11106
|
+
|
|
11107
|
+
// Map to screen coordinates
|
|
11108
|
+
// Note: the coordinate system uses d.y for screen-x and d.x for screen-y
|
|
11109
|
+
// (xCoord returns d.y, yCoord returns d.x)
|
|
11110
|
+
root.each(d => {
|
|
11111
|
+
d.y = d._unrooted_x * scale + offsetX;
|
|
11112
|
+
d.x = d._unrooted_y * scale + offsetY;
|
|
11113
|
+
|
|
11114
|
+
// Compute text angle for labels (angle from parent to this node in degrees)
|
|
11115
|
+
if (d.parent) {
|
|
11116
|
+
let dx = d._unrooted_x - d.parent._unrooted_x;
|
|
11117
|
+
let dy = d._unrooted_y - d.parent._unrooted_y;
|
|
11118
|
+
let angleDeg = Math.atan2(dy, dx) * 180 / Math.PI;
|
|
11119
|
+
|
|
11120
|
+
// Flip text on the left hemisphere so it reads left-to-right
|
|
11121
|
+
let isLeftHalf = angleDeg > 90 || angleDeg < -90;
|
|
11122
|
+
d.text_angle = isLeftHalf ? angleDeg + 180 : angleDeg;
|
|
11123
|
+
d.text_align = isLeftHalf ? "end" : "start";
|
|
11124
|
+
} else {
|
|
11125
|
+
d.text_angle = 0;
|
|
11126
|
+
d.text_align = "start";
|
|
11127
|
+
}
|
|
11128
|
+
|
|
11129
|
+
// Store the angle (in radians) for compatibility with radial code paths
|
|
11130
|
+
d.angle = d.parent
|
|
11131
|
+
? Math.atan2(d._unrooted_y - d.parent._unrooted_y,
|
|
11132
|
+
d._unrooted_x - d.parent._unrooted_x)
|
|
11133
|
+
: 0;
|
|
11134
|
+
d.radius = null;
|
|
11135
|
+
});
|
|
11136
|
+
}
|
|
11137
|
+
|
|
11138
|
+
/**
|
|
11139
|
+
* Recursive equal-angle layout.
|
|
11140
|
+
*
|
|
11141
|
+
* @param {Object} node - Current node
|
|
11142
|
+
* @param {Number} startAngle - Start of the angular arc allocated to this node
|
|
11143
|
+
* @param {Number} arcSize - Total angular arc allocated to this node
|
|
11144
|
+
*/
|
|
11145
|
+
function equalAngle(node, startAngle, arcSize) {
|
|
11146
|
+
let totalLeaves = countLeaves(node);
|
|
11147
|
+
|
|
11148
|
+
if (!node.children || node.children.length === 0) {
|
|
11149
|
+
return;
|
|
11150
|
+
}
|
|
11151
|
+
|
|
11152
|
+
let currentAngle = startAngle;
|
|
11153
|
+
|
|
11154
|
+
node.children.forEach(child => {
|
|
11155
|
+
let childLeaves = countLeaves(child);
|
|
11156
|
+
let childArc = arcSize * (childLeaves / totalLeaves);
|
|
11157
|
+
let midAngle = currentAngle + childArc / 2;
|
|
11158
|
+
|
|
11159
|
+
// Use the node's y value (cumulative branch length / depth) for distance
|
|
11160
|
+
let branchLen = child.y !== undefined ? child.y - (node.y || 0) : 1;
|
|
11161
|
+
if (branchLen <= 0) branchLen = 1;
|
|
11162
|
+
|
|
11163
|
+
child._unrooted_x = node._unrooted_x + branchLen * Math.cos(midAngle);
|
|
11164
|
+
child._unrooted_y = node._unrooted_y + branchLen * Math.sin(midAngle);
|
|
11165
|
+
|
|
11166
|
+
equalAngle(child, currentAngle, childArc);
|
|
11167
|
+
currentAngle += childArc;
|
|
11168
|
+
});
|
|
11169
|
+
}
|
|
11170
|
+
|
|
11171
|
+
/**
|
|
11172
|
+
* Draw a straight line edge between two nodes (for unrooted layout).
|
|
11173
|
+
*
|
|
11174
|
+
* @param {Array} points - Array of [source, target] node objects
|
|
11175
|
+
* @returns {String} SVG path string
|
|
11176
|
+
*/
|
|
11177
|
+
function drawUnrootedEdge(points) {
|
|
11178
|
+
return (
|
|
11179
|
+
"M " + xCoord(points[0]) + "," + yCoord(points[0]) +
|
|
11180
|
+
" L " + xCoord(points[1]) + "," + yCoord(points[1])
|
|
11181
|
+
);
|
|
11182
|
+
}
|
|
11183
|
+
|
|
11184
|
+
/**
|
|
11185
|
+
* Place a label along an unrooted edge via linear interpolation.
|
|
11186
|
+
*
|
|
11187
|
+
* @param {Object} edge - Edge object with source and target
|
|
11188
|
+
* @param {Number} where - Interpolation factor (0 = target, 1 = source)
|
|
11189
|
+
* @returns {Object} {x, y} screen coordinates
|
|
11190
|
+
*/
|
|
11191
|
+
function unrootedSegmentPlacer(edge, where) {
|
|
11192
|
+
return {
|
|
11193
|
+
x: xCoord(edge.target) + (xCoord(edge.source) - xCoord(edge.target)) * where,
|
|
11194
|
+
y: yCoord(edge.target) + (yCoord(edge.source) - yCoord(edge.target)) * where
|
|
11195
|
+
};
|
|
11196
|
+
}
|
|
11197
|
+
|
|
9937
11198
|
function itemTagged(item) {
|
|
9938
11199
|
return item.tag || false;
|
|
9939
11200
|
}
|
|
@@ -9978,6 +11239,12 @@
|
|
|
9978
11239
|
return this;
|
|
9979
11240
|
}
|
|
9980
11241
|
|
|
11242
|
+
function unrooted(attr) {
|
|
11243
|
+
if (!arguments.length) return this.options["is-unrooted"];
|
|
11244
|
+
this.options["is-unrooted"] = attr;
|
|
11245
|
+
return this;
|
|
11246
|
+
}
|
|
11247
|
+
|
|
9981
11248
|
function alignTips(attr) {
|
|
9982
11249
|
if (!arguments.length) return this.options["align-tips"];
|
|
9983
11250
|
this.options["align-tips"] = attr;
|
|
@@ -10088,11 +11355,17 @@
|
|
|
10088
11355
|
predefined_selecters: predefined_selecters,
|
|
10089
11356
|
radial: radial,
|
|
10090
11357
|
selectionCallback: selectionCallback$1,
|
|
10091
|
-
selectionLabel: selectionLabel
|
|
11358
|
+
selectionLabel: selectionLabel,
|
|
11359
|
+
unrooted: unrooted
|
|
10092
11360
|
});
|
|
10093
11361
|
|
|
10094
11362
|
function shiftTip(d) {
|
|
10095
11363
|
|
|
11364
|
+
if (this.unrooted()) {
|
|
11365
|
+
// No tip alignment for unrooted layout
|
|
11366
|
+
return [0, 0];
|
|
11367
|
+
}
|
|
11368
|
+
|
|
10096
11369
|
if (this.radial()) {
|
|
10097
11370
|
return [
|
|
10098
11371
|
(d.text_align == "end" ? -1 : 1) *
|
|
@@ -10147,7 +11420,15 @@
|
|
|
10147
11420
|
return this.ensure_size_is_in_px(this.shown_font_size);
|
|
10148
11421
|
});
|
|
10149
11422
|
|
|
10150
|
-
if (this.
|
|
11423
|
+
if (this.unrooted()) {
|
|
11424
|
+
labels = labels
|
|
11425
|
+
.attr("transform", d => {
|
|
11426
|
+
return this.d3PhylotreeSvgRotate(d.text_angle);
|
|
11427
|
+
})
|
|
11428
|
+
.attr("text-anchor", d => {
|
|
11429
|
+
return d.text_align;
|
|
11430
|
+
});
|
|
11431
|
+
} else if (this.radial()) {
|
|
10151
11432
|
labels = labels
|
|
10152
11433
|
.attr("transform", d => {
|
|
10153
11434
|
return (
|
|
@@ -10240,8 +11521,7 @@
|
|
|
10240
11521
|
if (this.shown_font_size >= 5) {
|
|
10241
11522
|
const isRTL = this.options["layout"] == "right-to-left";
|
|
10242
11523
|
labels = labels.attr("dx", d => {
|
|
10243
|
-
|
|
10244
|
-
const direction = this.radial()
|
|
11524
|
+
const direction = (this.radial() || this.unrooted())
|
|
10245
11525
|
? (d.text_align == "end" ? -1 : 1)
|
|
10246
11526
|
: (isRTL ? -1 : 1);
|
|
10247
11527
|
return direction * ((this.alignTips() ? 0 : shift) + this.shown_font_size * 0.33);
|
|
@@ -10251,8 +11531,7 @@
|
|
|
10251
11531
|
if (this.shown_font_size >= 5) {
|
|
10252
11532
|
const isRTL = this.options["layout"] == "right-to-left";
|
|
10253
11533
|
labels = labels.attr("dx", d => { // eslint-disable-line
|
|
10254
|
-
|
|
10255
|
-
const direction = this.radial()
|
|
11534
|
+
const direction = (this.radial() || this.unrooted())
|
|
10256
11535
|
? (d.text_align == "end" ? -1 : 1)
|
|
10257
11536
|
: (isRTL ? -1 : 1);
|
|
10258
11537
|
return direction * this.shown_font_size * 0.33;
|
|
@@ -10777,7 +12056,25 @@
|
|
|
10777
12056
|
|
|
10778
12057
|
let sizes = this.size;
|
|
10779
12058
|
|
|
10780
|
-
if (this.
|
|
12059
|
+
if (this.unrooted()) {
|
|
12060
|
+
// Unrooted layout: use the computed size (may differ from initial width/height
|
|
12061
|
+
// due to spacing button scaling)
|
|
12062
|
+
this.baseTransform = { x: 0, y: 0 };
|
|
12063
|
+
|
|
12064
|
+
if (svg) {
|
|
12065
|
+
let transform;
|
|
12066
|
+
if (this.currentZoomTransform && this.options["zoom"]) {
|
|
12067
|
+
const zt = this.currentZoomTransform;
|
|
12068
|
+
transform = `translate(${zt.x}, ${zt.y}) scale(${zt.k})`;
|
|
12069
|
+
} else {
|
|
12070
|
+
transform = "translate(0,0)";
|
|
12071
|
+
}
|
|
12072
|
+
|
|
12073
|
+
svg
|
|
12074
|
+
.selectAll("." + css_classes["tree-container"])
|
|
12075
|
+
.attr("transform", transform);
|
|
12076
|
+
}
|
|
12077
|
+
} else if (this.radial()) {
|
|
10781
12078
|
let pad_radius = this.pad_width(),
|
|
10782
12079
|
vertical_offset =
|
|
10783
12080
|
this.options["top-bottom-spacing"] != "fit-to-size"
|
|
@@ -11895,6 +13192,7 @@
|
|
|
11895
13192
|
"bubble-styler": this.radius_pad_for_bubbles,
|
|
11896
13193
|
"binary-selectable": false,
|
|
11897
13194
|
"is-radial": false,
|
|
13195
|
+
"is-unrooted": false,
|
|
11898
13196
|
"attribute-list": [],
|
|
11899
13197
|
"max-radius": 768,
|
|
11900
13198
|
"annular-limit": 0.38196601125010515,
|
|
@@ -12688,7 +13986,42 @@
|
|
|
12688
13986
|
|
|
12689
13987
|
this.shown_font_size = Math.min(this.font_size, this.scales[0]);
|
|
12690
13988
|
|
|
12691
|
-
if (this.
|
|
13989
|
+
if (this.unrooted()) {
|
|
13990
|
+
// Unrooted (equal-angle) layout
|
|
13991
|
+
//
|
|
13992
|
+
// For unrooted, there's no meaningful horizontal vs vertical axis.
|
|
13993
|
+
// Both spacing buttons grow the canvas uniformly. Each click beyond
|
|
13994
|
+
// the default adds pixels in both dimensions.
|
|
13995
|
+
let pixelsPerClick = 40;
|
|
13996
|
+
let deltaV = this.fixed_width[0] - 14; // clicks from default vertical
|
|
13997
|
+
let deltaH = this.fixed_width[1] - 30; // clicks from default horizontal
|
|
13998
|
+
let extraPx = (deltaV + deltaH) * pixelsPerClick;
|
|
13999
|
+
|
|
14000
|
+
let availWidth = this.width + extraPx;
|
|
14001
|
+
let availHeight = this.height + extraPx;
|
|
14002
|
+
if (availWidth < 100) availWidth = 100;
|
|
14003
|
+
if (availHeight < 100) availHeight = 100;
|
|
14004
|
+
|
|
14005
|
+
// Reserve space for labels on all sides
|
|
14006
|
+
this.shown_font_size = this.font_size;
|
|
14007
|
+
this.label_width = this._label_width(this.shown_font_size);
|
|
14008
|
+
let labelPad = this.label_width + this.shown_font_size;
|
|
14009
|
+
|
|
14010
|
+
computeUnrootedLayout(
|
|
14011
|
+
this.phylotree.nodes,
|
|
14012
|
+
this.scales,
|
|
14013
|
+
availWidth,
|
|
14014
|
+
availHeight,
|
|
14015
|
+
labelPad
|
|
14016
|
+
);
|
|
14017
|
+
|
|
14018
|
+
// Set size so resizeSvg sizes the SVG to fit the tree
|
|
14019
|
+
this.size = [availHeight, availWidth];
|
|
14020
|
+
|
|
14021
|
+
this.draw_branch = drawUnrootedEdge;
|
|
14022
|
+
this.edge_placer = unrootedSegmentPlacer;
|
|
14023
|
+
this.draw_scale_bar = null;
|
|
14024
|
+
} else if (this.radial()) {
|
|
12692
14025
|
// map the nodes to polar coordinates
|
|
12693
14026
|
this.draw_branch = ___namespace.partial(drawArc, this.radial_center);
|
|
12694
14027
|
this.edge_placer = arcSegmentPlacer;
|