MCPcopy Create free account
hub / github.com/NaturalIntelligence/fast-xml-parser / readStopNodeData

Function readStopNodeData

src/xmlparser/OrderedObjParser.js:800–864  ·  view source on GitHub ↗

* find paired tag for a stop node * @param {string} xmlData * @param {string} tagName * @param {number} i

(xmlData, tagName, i)

Source from the content-addressed store, hash-verified

798 * @param {number} i
799 */
800function readStopNodeData(xmlData, tagName, i) {
801 const startIndex = i;
802 // Starting at 1 since we already have an open tag
803 let openTagCount = 1;
804 let lenient = false; // set once a quote-aware tag scan fails
805 let nextGt = -1; // cached index of the next '>'
806
807 const xmllen = xmlData.length;
808 for (; i < xmllen; i++) {
809 if (xmlData[i] === "<") {
810 const c1 = xmlData.charCodeAt(i + 1);
811 if (c1 === 47) {//close tag '/'
812 const closeIndex = findClosingChar(xmlData, ">", i, `${tagName} is not closed`);
813 let closeTagName = xmlData.substring(i + 2, closeIndex).trim();
814 if (closeTagName === tagName) {
815 openTagCount--;
816 if (openTagCount === 0) {
817 return {
818 tagContent: xmlData.substring(startIndex, i),
819 i: closeIndex
820 }
821 }
822 }
823 i = closeIndex;
824 } else if (c1 === 63) { //?
825 const closeIndex = findClosingIndex(xmlData, "?>", i + 1, "StopNode is not closed.")
826 i = closeIndex;
827 } else if (c1 === 33
828 && xmlData.charCodeAt(i + 2) === 45
829 && xmlData.charCodeAt(i + 3) === 45) { // '!--'
830 const closeIndex = findClosingIndex(xmlData, "-->", i + 3, "StopNode is not closed.")
831 i = closeIndex;
832 } else if (c1 === 33
833 && xmlData.charCodeAt(i + 2) === 91) { // '!['
834 const closeIndex = findClosingIndex(xmlData, "]]>", i, "StopNode is not closed.") - 2;
835 i = closeIndex;
836 } else {
837 const tagData = lenient ? undefined : readTagExp(xmlData, i, false);
838
839 if (tagData) {
840 const openTagName = tagData.tagName;
841 if (openTagName === tagName && tagData.tagExp[tagData.tagExp.length - 1] !== "/") {
842 openTagCount++;
843 }
844 i = tagData.closeIndex;
845 } else {
846 // no closing '>'. use lenient approach to find '<' instead of readTagExp, to avoid repeated rescans.
847 lenient = true;
848 if (nextGt < i) {
849 nextGt = xmlData.indexOf(">", i);
850 if (nextGt === -1) return; // no '>' left, so no closing tag can follow
851 }
852 const nextLt = xmlData.indexOf("<", i + 1);
853 if (nextLt === -1 || nextLt > nextGt) { // looks like a real tag
854 const exp = xmlData.substring(i + 1, nextGt);
855 const sep = exp.search(/\s/);
856 const name = sep === -1 ? exp : exp.substring(0, sep);
857 if (name === tagName && exp[exp.length - 1] !== "/") openTagCount++;

Callers

nothing calls this directly

Calls 3

findClosingCharFunction · 0.85
findClosingIndexFunction · 0.85
readTagExpFunction · 0.70

Tested by

no test coverage detected