Skip to content

Commit

Permalink
Use DomParser for xml parsing (#25018)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheenamalhotra authored Nov 22, 2023
1 parent 805b66d commit d4fcf57
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 34 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "azuredatastudio",
"version": "1.48.0",
"distro": "c7516ffaf758f77c4c9c621f2ec650550714afff",
"distro": "b0465c719e5666cda86a2ae1083752ae50cb491b",
"author": {
"name": "Microsoft Corporation"
},
Expand Down Expand Up @@ -124,7 +124,6 @@
"vscode-oniguruma": "1.7.0",
"vscode-regexpp": "^3.1.0",
"vscode-textmate": "9.0.0",
"xml2js": "^0.6.2",
"xterm": "5.3.0-beta.61",
"xterm-addon-canvas": "0.5.0-beta.22",
"xterm-addon-image": "0.6.0-beta.14",
Expand Down Expand Up @@ -251,7 +250,7 @@
"temp-write": "^3.4.0",
"ts-loader": "^9.4.2",
"ts-node": "^10.9.1",
"tsec": "0.2.7",
"tsec": "0.2.8",
"typemoq": "^0.3.2",
"typescript": "^5.3.0-dev.20230824",
"typescript-formatter": "7.1.0",
Expand Down
41 changes: 23 additions & 18 deletions src/sql/workbench/contrib/query/browser/gridPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ import { IComponentContextService } from 'sql/workbench/services/componentContex
import { GridRange } from 'sql/base/common/gridRange';
import { onUnexpectedError } from 'vs/base/common/errors';
import { defaultTableFilterStyles, defaultTableStyles } from 'sql/platform/theme/browser/defaultStyles';
import { parseString as parseXMLString } from 'xml2js';

const ROW_HEIGHT = 29;
const HEADER_HEIGHT = 26;
Expand Down Expand Up @@ -445,10 +444,10 @@ export abstract class GridTableBase<T> extends Disposable implements IView, IQue
: escape(c.columnName),
field: i.toString(),
formatter: c.isXml || c.isJson ? hyperLinkFormatter : (row: number | undefined, cell: any | undefined, value: ICellValue, columnDef: any | undefined, dataContext: any | undefined): string | { text: string, addClasses: string } => {
if (isXmlCell(value)) {
if (this.isXmlCell(value)) {
this.resultSet.columnInfo[i].isXml = true;
return hyperLinkFormatter(row, cell, value, columnDef, dataContext);
} else if (this.gridConfig.showJsonAsLink && isJsonCell(value)) {
} else if (this.gridConfig.showJsonAsLink && this.isJsonCell(value)) {
this.resultSet.columnInfo[i].isJson = true;
return hyperLinkFormatter(row, cell, value, columnDef, dataContext);
} else {
Expand Down Expand Up @@ -884,7 +883,7 @@ export abstract class GridTableBase<T> extends Disposable implements IView, IQue
if (column) {
const subset = await this.getRowData(event.cell.row, 1);
const value = subset[0][event.cell.cell - 1];
if (column.isXml || (this.gridConfig.showJsonAsLink && isJsonCell(value))) {
if (column.isXml || (this.gridConfig.showJsonAsLink && this.isJsonCell(value))) {
if (column.isXml && this.providerId) {
const result = await this.executionPlanService.isExecutionPlan(this.providerId, value.displayValue);
if (result.isExecutionPlan) {
Expand Down Expand Up @@ -974,6 +973,26 @@ export abstract class GridTableBase<T> extends Disposable implements IView, IQue
];
}

private isJsonCell(value: ICellValue): boolean {
return !!(value && !value.isNull && value.displayValue?.match(IsJsonRegex));
}

private isXmlCell(value: ICellValue): boolean {
let isXML = false;
try {
if (value && !value.isNull && value.displayValue.trim() !== '') {
var parser = new DOMParser();
// Script elements if any are not evaluated during parsing
var doc = parser.parseFromString(value.displayValue, 'text/xml');
isXML = doc.documentElement.tagName !== 'parsererror';
}
} catch (e) {
// Ignore errors when parsing cell content, log and continue
this.logService.debug(`An error occurred when parsing data as XML: ${e}`);
}
return isXML;
}

protected abstract getContextActions(): IAction[];

// The actionsOrientation passed in controls the actionBar orientation
Expand Down Expand Up @@ -1166,17 +1185,3 @@ class GridTable<T> extends GridTableBase<T> {
return [];
}
}

function isJsonCell(value: ICellValue): boolean {
return !!(value && !value.isNull && value.displayValue?.match(IsJsonRegex));
}

function isXmlCell(value: ICellValue): boolean {
let isXML = false;
if (value && !value.isNull && value.displayValue.trim() !== '') {
parseXMLString(value.displayValue, (err, _) => {
isXML = err === null;
});
}
return isXML;
}
4 changes: 3 additions & 1 deletion src/tsec.exemptions.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
],
"ban-domparser-parsefromstring": [
"vs/base/browser/markdownRenderer.ts",
"vs/base/test/browser/markdownRenderer.test.ts"
"vs/base/test/browser/markdownRenderer.test.ts",
// {{SQL CARBON EDIT}} Add our exemption
"sql/workbench/contrib/query/browser/gridPanel.ts"
],
"ban-element-setattribute": [
"**/*.ts"
Expand Down
16 changes: 4 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10125,10 +10125,10 @@ ts-node@^10.9.1:
v8-compile-cache-lib "^3.0.1"
yn "3.1.1"

[email protected].7:
version "0.2.7"
resolved "https://registry.yarnpkg.com/tsec/-/tsec-0.2.7.tgz#be530025907037ed57f37fc7625b6a7e3658fe43"
integrity sha512-Pj9DuBBWLEo8p7QsbrEdXzW/u6QJBcib0ZGOTXkeSDx+PLXFY7hwyZE9Tfhp3TA3LQNpYouyT0WmzXRyUW4otQ==
[email protected].8:
version "0.2.8"
resolved "https://registry.yarnpkg.com/tsec/-/tsec-0.2.8.tgz#a9e7492b144fcff14f1f36327fa84a2d54c4e211"
integrity sha512-d2vdTEtLbPzTs57ygzzPk6QrdW1lA8SBAoHZCVvAyC3R1LTjsQ2eGg/XRmtoCpXOVIflVtMsxtzk7eTHwT+DjQ==
dependencies:
glob "^7.1.1"
minimatch "^3.0.3"
Expand Down Expand Up @@ -10848,14 +10848,6 @@ xml2js@^0.4.19, xml2js@^0.4.23:
sax ">=0.6.0"
xmlbuilder "~11.0.0"

xml2js@^0.6.2:
version "0.6.2"
resolved "https://registry.yarnpkg.com/xml2js/-/xml2js-0.6.2.tgz#dd0b630083aa09c161e25a4d0901e2b2a929b499"
integrity sha512-T4rieHaC1EXcES0Kxxj4JWgaUQHDk+qwHcYOCFHfiwKz7tOVPLq7Hjq9dM1WCMhylqMEfP7hMcOIChvotiZegA==
dependencies:
sax ">=0.6.0"
xmlbuilder "~11.0.0"

xml@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/xml/-/xml-1.0.1.tgz#78ba72020029c5bc87b8a81a3cfcd74b4a2fc1e5"
Expand Down

0 comments on commit d4fcf57

Please sign in to comment.