| 17 | } |
| 18 | |
| 19 | export default class Xml2JsParser { |
| 20 | constructor(options) { |
| 21 | this.options = options; |
| 22 | |
| 23 | this.currentTagDetail = null; |
| 24 | this.tagTextData = ""; |
| 25 | this.tagsStack = []; |
| 26 | this.entityParser = new EntitiesParser(options.htmlEntities); |
| 27 | this.stopNodes = []; |
| 28 | for (let i = 0; i < this.options.stopNodes.length; i++) { |
| 29 | this.stopNodes.push(new TagPath(this.options.stopNodes[i])); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | parse(strData) { |
| 34 | this.source = new StringSource(strData); |
| 35 | this.parseXml(); |
| 36 | return this.outputBuilder.getOutput(); |
| 37 | } |
| 38 | parseBytesArr(data) { |
| 39 | this.source = new BufferSource(data ); |
| 40 | this.parseXml(); |
| 41 | return this.outputBuilder.getOutput(); |
| 42 | } |
| 43 | |
| 44 | parseXml() { |
| 45 | //TODO: Separate TagValueParser as separate class. So no scope issue in node builder class |
| 46 | |
| 47 | //OutputBuilder should be set in XML Parser |
| 48 | this.outputBuilder = this.options.OutputBuilder.getInstance(this.options); |
| 49 | this.root = { root: true}; |
| 50 | this.currentTagDetail = this.root; |
| 51 | |
| 52 | while(this.source.canRead()){ |
| 53 | let ch = this.source.readCh(); |
| 54 | if (ch === "") break; |
| 55 | |
| 56 | if(ch === "<"){//tagStart |
| 57 | let nextChar = this.source.readChAt(0); |
| 58 | if (nextChar === "" ) throw new Error("Unexpected end of source"); |
| 59 | |
| 60 | |
| 61 | if(nextChar === "!" || nextChar === "?"){ |
| 62 | this.source.updateBufferBoundary(); |
| 63 | //previously collected text should be added to current node |
| 64 | this.addTextNode(); |
| 65 | |
| 66 | this.readSpecialTag(nextChar);// Read DOCTYPE, comment, CDATA, PI tag |
| 67 | }else if(nextChar === "/"){ |
| 68 | this.source.updateBufferBoundary(); |
| 69 | this.readClosingTag(); |
| 70 | // console.log(this.source.buffer.length, this.source.readable); |
| 71 | // console.log(this.tagsStack.length); |
| 72 | }else{//opening tag |
| 73 | this.readOpeningTag(); |
| 74 | } |
| 75 | }else{ |
| 76 | this.tagTextData += ch; |
nothing calls this directly
no test coverage detected