Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty tag key name for v5 #697

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions src/v5/XmlPartReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

/**
* find paired tag for a stop node
* @param {string} xmlDoc
* @param {string} tagName
* @param {string} xmlDoc
* @param {string} tagName
* @param {number} i : start index
*/
function readStopNode(xmlDoc, tagName, i){
const startIndex = i;
// Starting at 1 since we already have an open tag
let openTagCount = 1;

for (; i < xmlDoc.length; i++) {
if( xmlDoc[i] === "<"){
if( xmlDoc[i] === "<"){
if (xmlDoc[i+1] === "/") {//close tag
const closeIndex = findSubStrIndex(xmlDoc, ">", i, `${tagName} is not closed`);
let closeTagName = xmlDoc.substring(i+2,closeIndex).trim();
Expand All @@ -26,18 +26,18 @@ function readStopNode(xmlDoc, tagName, i){
}
}
i=closeIndex;
} else if(xmlDoc[i+1] === '?') {
} else if(xmlDoc[i+1] === '?') {
const closeIndex = findSubStrIndex(xmlDoc, "?>", i+1, "StopNode is not closed.")
i=closeIndex;
} else if(xmlDoc.substr(i + 1, 3) === '!--') {
} else if(xmlDoc.substr(i + 1, 3) === '!--') {
const closeIndex = findSubStrIndex(xmlDoc, "-->", i+3, "StopNode is not closed.")
i=closeIndex;
} else if(xmlDoc.substr(i + 1, 2) === '![') {
} else if(xmlDoc.substr(i + 1, 2) === '![') {
const closeIndex = findSubStrIndex(xmlDoc, "]]>", i, "StopNode is not closed.") - 2;
i=closeIndex;
} else {
const tagData = readTagExp(xmlDoc, i, '>')

if (tagData) {
const openTagName = tagData && tagData.tagName;
if (openTagName === tagName && tagData.tagExp[tagData.tagExp.length-1] !== "/") {
Expand All @@ -52,7 +52,7 @@ function readStopNode(xmlDoc, tagName, i){

/**
* Read closing tag name
* @param {Source} source
* @param {Source} source
* @returns tag name
*/
function readClosingTagName(source){
Expand All @@ -73,9 +73,9 @@ function readClosingTagName(source){
* This function can be used to read normal tag, pi tag.
* This function can't be used to read comment, CDATA, DOCTYPE.
* Eg <tag attr = ' some"' attr= ">" bool>
* @param {string} xmlDoc
* @param {string} xmlDoc
* @param {number} startIndex starting index
* @returns tag expression includes tag name & attribute string
* @returns tag expression includes tag name & attribute string
*/
function readTagExp(parser) {
let inSingleQuotes = false;
Expand All @@ -100,8 +100,8 @@ function readTagExp(parser) {
if(inSingleQuotes || inDoubleQuotes){
throw new Error("Invalid attribute expression. Quote is not properly closed");
}else if(!EOE) throw new Error("Unexpected closing of source. Waiting for '>'");


const exp = parser.source.readStr(i);
parser.source.updateBufferBoundary(i + 1);
return buildTagExpObj(exp, parser)
Expand Down Expand Up @@ -133,7 +133,7 @@ function readPiExp(parser) {
if(inSingleQuotes || inDoubleQuotes){
throw new Error("Invalid attribute expression. Quote is not properly closed in PI tag expression");
}else if(!EOE) throw new Error("Unexpected closing of source. Waiting for '?>'");

if(!parser.options.attributes.ignore){
//TODO: use regex to verify attributes if not set to ignore
}
Expand All @@ -150,7 +150,11 @@ function buildTagExpObj(exp, parser){
};
let attrsExp = "";

if(exp[exp.length -1] === "/") tagExp.selfClosing = true;
// Check for self-closing tag before setting the name
if(exp[exp.length -1] === "/") {
tagExp.selfClosing = true;
exp = exp.slice(0, -1); // Remove the trailing slash
}

//separate tag name
let i = 0;
Expand Down Expand Up @@ -182,7 +186,7 @@ function parseAttributesExp(attrStr, parser) {
for (let i = 0; i < len; i++) {
let attrName = parser.processAttrName(matches[i][1]);
let attrVal = parser.replaceEntities(matches[i][4] || true);

parser.outputBuilder.addAttribute(attrName, attrVal);
}
}
Expand All @@ -209,4 +213,4 @@ module.exports = {
readClosingTagName: readClosingTagName,
readTagExp: readTagExp,
readPiExp: readPiExp,
}
}
Loading