* Creates a new instance of the XmlParser class. * @param onEvent The callback to execute when a parser event occurs. The 'event' parameter contains information about the event. * @param onError The callback to execute when a parser error occurs. The 'error' parameter contains the error. * @pa
(onEvent: (event: ParserEvent) => void, onError?: (error: Error, position: Position) => void, processNamespaces?: boolean)
| 479 | * @param processNamespaces Specifies whether namespaces should be processed. |
| 480 | */ |
| 481 | constructor(onEvent: (event: ParserEvent) => void, onError?: (error: Error, position: Position) => void, processNamespaces?: boolean) { |
| 482 | this._processNamespaces = processNamespaces; |
| 483 | this._parser = new EasySAXParser(); |
| 484 | |
| 485 | const that = this; |
| 486 | this._parser.on('startNode', function (elem, attr, uq, tagend, str, pos) { |
| 487 | let attributes = attr(); |
| 488 | |
| 489 | if (attributes === true) { |
| 490 | //HACK: For some reason easysax returns the true literal when an element has no attributes. |
| 491 | attributes = undefined; |
| 492 | } |
| 493 | |
| 494 | if (attributes) { |
| 495 | for (const key in attributes) { |
| 496 | if (attributes.hasOwnProperty(key)) { |
| 497 | // Convert entities such as > to > |
| 498 | attributes[key] = XmlParser._dereferenceEntities(attributes[key]); |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | let prefix = undefined; |
| 504 | let namespace = undefined; |
| 505 | let name = elem; |
| 506 | |
| 507 | if (that._processNamespaces) { |
| 508 | const stackEntry = XmlParser._getNamespacesStackEntry(attributes); |
| 509 | that._namespaceStack.push(stackEntry); |
| 510 | |
| 511 | const resolved = that._resolveNamespace(name); |
| 512 | prefix = resolved.prefix; |
| 513 | namespace = resolved.namespace; |
| 514 | name = resolved.name; |
| 515 | } |
| 516 | |
| 517 | onEvent(new ParserEvent(ParserEventType.StartElement, pos(), prefix, namespace, name, attributes, undefined)); |
| 518 | }); |
| 519 | |
| 520 | this._parser.on('textNode', function (text, uq, pos) { |
| 521 | const data = uq(XmlParser._dereferenceEntities(text)); // Decode entity references such as < and > |
| 522 | onEvent(new ParserEvent(ParserEventType.Text, pos(), undefined, undefined, undefined, undefined, data)); |
| 523 | }); |
| 524 | |
| 525 | this._parser.on('endNode', function (elem, uq, tagstart, str, pos) { |
| 526 | let prefix = undefined; |
| 527 | let namespace = undefined; |
| 528 | let name = elem; |
| 529 | |
| 530 | if (that._processNamespaces) { |
| 531 | const resolved = that._resolveNamespace(name); |
| 532 | prefix = resolved.prefix; |
| 533 | namespace = resolved.namespace; |
| 534 | name = resolved.name; |
| 535 | } |
| 536 | |
| 537 | onEvent(new ParserEvent(ParserEventType.EndElement, pos(), prefix, namespace, name, undefined, undefined)); |
| 538 |
nothing calls this directly
no test coverage detected