Creates callbacks that collect events into an array.
()
| 47 | |
| 48 | /** Creates callbacks that collect events into an array. */ |
| 49 | function createEventCollector(): { |
| 50 | events: XmlEvent[]; |
| 51 | callbacks: XmlEventCallbacks; |
| 52 | } { |
| 53 | const events: XmlEvent[] = []; |
| 54 | const callbacks: XmlEventCallbacks = { |
| 55 | onDeclaration(version, encoding, standalone, line, column, offset) { |
| 56 | events.push({ |
| 57 | type: "declaration", |
| 58 | version, |
| 59 | ...(encoding !== undefined ? { encoding } : {}), |
| 60 | ...(standalone !== undefined ? { standalone } : {}), |
| 61 | line, |
| 62 | column, |
| 63 | offset, |
| 64 | } as XmlEvent); |
| 65 | }, |
| 66 | onStartElement( |
| 67 | name, |
| 68 | colonIndex, |
| 69 | uri, |
| 70 | attributes: XmlAttributeIterator, |
| 71 | selfClosing, |
| 72 | line, |
| 73 | column, |
| 74 | offset, |
| 75 | ) { |
| 76 | const attrs: Array<{ name: XmlName; value: string }> = []; |
| 77 | for (let i = 0; i < attributes.count; i++) { |
| 78 | const attrName = attributes.getName(i); |
| 79 | const attrColonIndex = attributes.getColonIndex(i); |
| 80 | const attrUri = attributes.getUri(i); |
| 81 | attrs.push({ |
| 82 | name: parseName(attrName, attrColonIndex, attrUri), |
| 83 | value: attributes.getValue(i), |
| 84 | }); |
| 85 | } |
| 86 | events.push({ |
| 87 | type: "start_element", |
| 88 | name: parseName(name, colonIndex, uri), |
| 89 | attributes: attrs, |
| 90 | selfClosing, |
| 91 | line, |
| 92 | column, |
| 93 | offset, |
| 94 | }); |
| 95 | }, |
| 96 | onEndElement(name, colonIndex, uri, line, column, offset) { |
| 97 | events.push({ |
| 98 | type: "end_element", |
| 99 | name: parseName(name, colonIndex, uri), |
| 100 | line, |
| 101 | column, |
| 102 | offset, |
| 103 | }); |
| 104 | }, |
| 105 | onText(text, line, column, offset) { |
| 106 | events.push({ type: "text", text, line, column, offset }); |