Path: blob/main/extensions/emmet/src/test/updateImageSize.test.ts
4774 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import 'mocha';6import * as assert from 'assert';7import { Selection } from 'vscode';8import { withRandomFileEditor, closeAllEditors } from './testUtils';9import { updateImageSize } from '../updateImageSize';1011suite('Tests for Emmet actions on html tags', () => {12teardown(closeAllEditors);1314const imageUrl = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAHYcAAB2HAY/l8WUAAAATSURBVBhXY/jPwADGDP////8PAB/uBfuDMzhuAAAAAElFTkSuQmCC';15const imageWidth = 2;16const imageHeight = 2;1718test('update image css with multiple cursors in css file', () => {19const cssContents = `20.one {21margin: 10px;22padding: 10px;23background-image: url('${imageUrl}');24}25.two {26background-image: url('${imageUrl}');27height: 42px;28}29.three {30background-image: url('${imageUrl}');31width: 42px;32}33`;34const expectedContents = `35.one {36margin: 10px;37padding: 10px;38background-image: url('${imageUrl}');39width: ${imageWidth}px;40height: ${imageHeight}px;41}42.two {43background-image: url('${imageUrl}');44width: ${imageWidth}px;45height: ${imageHeight}px;46}47.three {48background-image: url('${imageUrl}');49height: ${imageHeight}px;50width: ${imageWidth}px;51}52`;53return withRandomFileEditor(cssContents, 'css', (editor, doc) => {54editor.selections = [55new Selection(4, 50, 4, 50),56new Selection(7, 50, 7, 50),57new Selection(11, 50, 11, 50)58];5960return updateImageSize()!.then(() => {61assert.strictEqual(doc.getText(), expectedContents);62return Promise.resolve();63});64});65});6667test('update image size in css in html file with multiple cursors', () => {68const htmlWithCssContents = `69<html>70<style>71.one {72margin: 10px;73padding: 10px;74background-image: url('${imageUrl}');75}76.two {77background-image: url('${imageUrl}');78height: 42px;79}80.three {81background-image: url('${imageUrl}');82width: 42px;83}84</style>85</html>86`;87const expectedContents = `88<html>89<style>90.one {91margin: 10px;92padding: 10px;93background-image: url('${imageUrl}');94width: ${imageWidth}px;95height: ${imageHeight}px;96}97.two {98background-image: url('${imageUrl}');99width: ${imageWidth}px;100height: ${imageHeight}px;101}102.three {103background-image: url('${imageUrl}');104height: ${imageHeight}px;105width: ${imageWidth}px;106}107</style>108</html>109`;110return withRandomFileEditor(htmlWithCssContents, 'html', (editor, doc) => {111editor.selections = [112new Selection(6, 50, 6, 50),113new Selection(9, 50, 9, 50),114new Selection(13, 50, 13, 50)115];116117return updateImageSize()!.then(() => {118assert.strictEqual(doc.getText(), expectedContents);119return Promise.resolve();120});121});122});123124test('update image size in img tag in html file with multiple cursors', () => {125const htmlwithimgtag = `126<html>127<img id="one" src="${imageUrl}" />128<img id="two" src="${imageUrl}" width="56" />129<img id="three" src="${imageUrl}" height="56" />130</html>131`;132const expectedContents = `133<html>134<img id="one" src="${imageUrl}" width="${imageWidth}" height="${imageHeight}" />135<img id="two" src="${imageUrl}" width="${imageWidth}" height="${imageHeight}" />136<img id="three" src="${imageUrl}" height="${imageHeight}" width="${imageWidth}" />137</html>138`;139return withRandomFileEditor(htmlwithimgtag, 'html', (editor, doc) => {140editor.selections = [141new Selection(2, 50, 2, 50),142new Selection(3, 50, 3, 50),143new Selection(4, 50, 4, 50)144];145146return updateImageSize()!.then(() => {147assert.strictEqual(doc.getText(), expectedContents);148return Promise.resolve();149});150});151});152});153154155