Encode HTML when entered in edit data cells (#1302)

* Encode HTML when entered in edit data cells

* Use VS Code's string encoding function
This commit is contained in:
Matt Irvine
2018-04-30 17:58:39 -07:00
committed by GitHub
parent a0456bf4f7
commit cd140b5527
3 changed files with 49 additions and 20 deletions

View File

@@ -0,0 +1,43 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as sqlops from 'sqlops';
import * as assert from 'assert';
import * as SharedServices from 'sql/parts/grid/services/sharedServices';
const testText = '<div>test text</div>';
suite('Grid shared services tests', () => {
test('textFormatter should encode HTML when formatting a DBCellValue object', () => {
// If I format a DBCellValue object that contains HTML
let cellValue = new SharedServices.DBCellValue();
cellValue.displayValue = testText;
cellValue.isNull = false;
let formattedHtml = SharedServices.textFormatter(undefined, undefined, cellValue, undefined, undefined);
// Then the result is HTML for a span element containing the cell value's display value as plain text
verifyFormattedHtml(formattedHtml, testText);
});
test('textFormatter should encode HTML when formatting a string', () => {
// If I format a string that contains HTML
let formattedHtml = SharedServices.textFormatter(undefined, undefined, testText, undefined, undefined);
// Then the result is HTML for a span element containing the given text as plain text
verifyFormattedHtml(formattedHtml, testText);
});
});
function verifyFormattedHtml(formattedHtml: string, expectedText: string): void {
// Create an element containing the span returned by the format call
let element = document.createElement('div');
element.innerHTML = formattedHtml;
let spanElement = element.children[0];
// Verify that the span element's text, not its innerHTML, matches the expected text
assert.equal(spanElement.textContent, testText);
assert.notEqual(spanElement.innerHTML, testText);
}