diff --git a/src/tree-view/xml-tree-data-provider.ts b/src/tree-view/xml-tree-data-provider.ts index c21d3a6..afc2ba2 100644 --- a/src/tree-view/xml-tree-data-provider.ts +++ b/src/tree-view/xml-tree-data-provider.ts @@ -29,14 +29,14 @@ export class XmlTreeDataProvider implements TreeDataProvider { return window.activeTextEditor || null; } - getTreeItem(element: any): TreeItem | Thenable { + getTreeItem(element: Node): TreeItem | Thenable { const treeItem = new TreeItem(element.localName); - if (this._getChildAttributeArray(element).length > 0) { + if (this._isElement(element) && this._getChildAttributeArray(element).length > 0) { treeItem.collapsibleState = TreeItemCollapsibleState.Collapsed; } - if (this._getChildElementArray(element).length > 0) { + if (this._isElement(element) && this._getChildElementArray(element).length > 0) { treeItem.collapsibleState = TreeItemCollapsibleState.Collapsed; } @@ -44,7 +44,7 @@ export class XmlTreeDataProvider implements TreeDataProvider { command: constants.nativeCommands.revealLine, title: "", arguments: [{ - lineNumber: element.lineNumber - 1, + lineNumber: (element as any).lineNumber - 1, at: "top" }] }; @@ -54,13 +54,13 @@ export class XmlTreeDataProvider implements TreeDataProvider { return treeItem; } - getChildren(element?: any): any[] | Thenable { + getChildren(element?: Node): any[] | Thenable { if (!this._xmlDocument) { this._refreshTree(); } - if (element) { - return [].concat(this._getChildAttributeArray(element), this._getChildElementArray(element)); + if (this._isElement(element)) { + return [].concat(this._getChildAttributeArray(element), this._getChildElementArray(element)); } else if (this._xmlDocument) { @@ -72,7 +72,7 @@ export class XmlTreeDataProvider implements TreeDataProvider { } } - private _getChildAttributeArray(node: any): any[] { + private _getChildAttributeArray(node: Element): any[] { if (!node.attributes) { return []; } @@ -86,7 +86,7 @@ export class XmlTreeDataProvider implements TreeDataProvider { return array; } - private _getChildElementArray(node: any): any[] { + private _getChildElementArray(node: Element): any[] { if (!node.childNodes) { return []; } @@ -96,7 +96,7 @@ export class XmlTreeDataProvider implements TreeDataProvider { for (let i = 0; i < node.childNodes.length; i++) { const child = node.childNodes[i]; - if (child.tagName) { + if (this._isElement(child)) { array.push(child); } } @@ -104,10 +104,10 @@ export class XmlTreeDataProvider implements TreeDataProvider { return array; } - private _getIcon(element: any): any { + private _getIcon(element: Node): any { let type = "element"; - if (!element.tagName) { + if (!this._isElement(element)) { type = "attribute"; } @@ -119,6 +119,10 @@ export class XmlTreeDataProvider implements TreeDataProvider { return icon; } + private _isElement(node: Node): boolean { + return (!!node && !!(node as Element).tagName); + } + private _refreshTree(): void { if (!this.activeEditor || this.activeEditor.document.languageId !== constants.languageIds.xml) { commands.executeCommand(constants.nativeCommands.setContext, constants.contextKeys.xmlTreeViewEnabled, false); @@ -134,9 +138,15 @@ export class XmlTreeDataProvider implements TreeDataProvider { commands.executeCommand(constants.nativeCommands.setContext, constants.contextKeys.xmlTreeViewEnabled, enableTreeView); const xml = this.activeEditor.document.getText(); - this._xmlDocument = new DOMParser().parseFromString(xml, "text/xml"); - this._onDidChangeTreeData.fire(); + try { + this._xmlDocument = new DOMParser().parseFromString(xml, "text/xml"); + this._onDidChangeTreeData.fire(); + } + + catch { + this._xmlDocument = new DOMParser().parseFromString("", "text/xml"); + } } }