(xmlData, options)
| 9 | |
| 10 | //const tagsPattern = new RegExp("<\\/?([\\w:\\-_\.]+)\\s*\/?>","g"); |
| 11 | export function validate(xmlData, options) { |
| 12 | options = Object.assign({}, defaultOptions, options); |
| 13 | |
| 14 | //xmlData = xmlData.replace(/(\r\n|\n|\r)/gm,"");//make it single line |
| 15 | //xmlData = xmlData.replace(/(^\s*<\?xml.*?\?>)/g,"");//Remove XML starting tag |
| 16 | //xmlData = xmlData.replace(/(<!DOCTYPE[\s\w\"\.\/\-\:]+(\[.*\])*\s*>)/g,"");//Remove DOCTYPE |
| 17 | const tags = []; |
| 18 | let tagFound = false; |
| 19 | |
| 20 | //indicates that the root tag has been closed (aka. depth 0 has been reached) |
| 21 | let reachedRoot = false; |
| 22 | |
| 23 | if (xmlData[0] === '\ufeff') { |
| 24 | // check for byte order mark (BOM) |
| 25 | xmlData = xmlData.substr(1); |
| 26 | } |
| 27 | |
| 28 | for (let i = 0; i < xmlData.length; i++) { |
| 29 | |
| 30 | if (xmlData[i] === '<' && xmlData[i + 1] === '?') { |
| 31 | i += 2; |
| 32 | i = readPI(xmlData, i); |
| 33 | if (i.err) return i; |
| 34 | } else if (xmlData[i] === '<') { |
| 35 | //starting of tag |
| 36 | //read until you reach to '>' avoiding any '>' in attribute value |
| 37 | let tagStartPos = i; |
| 38 | i++; |
| 39 | |
| 40 | if (xmlData[i] === '!') { |
| 41 | i = readCommentAndCDATA(xmlData, i); |
| 42 | continue; |
| 43 | } else { |
| 44 | let closingTag = false; |
| 45 | if (xmlData[i] === '/') { |
| 46 | //closing tag |
| 47 | closingTag = true; |
| 48 | i++; |
| 49 | } |
| 50 | //read tagname |
| 51 | let tagName = ''; |
| 52 | for (; i < xmlData.length && |
| 53 | xmlData[i] !== '>' && |
| 54 | xmlData[i] !== ' ' && |
| 55 | xmlData[i] !== '\t' && |
| 56 | xmlData[i] !== '\n' && |
| 57 | xmlData[i] !== '\r'; i++ |
| 58 | ) { |
| 59 | tagName += xmlData[i]; |
| 60 | } |
| 61 | tagName = tagName.trim(); |
| 62 | //console.log(tagName); |
| 63 | |
| 64 | if (tagName[tagName.length - 1] === '/') { |
| 65 | //self closing tag without attributes |
| 66 | tagName = tagName.substring(0, tagName.length - 1); |
| 67 | //continue; |
| 68 | i--; |
no test coverage detected