-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105589 from annkamsk/render-codicons-elements
Make renderCodicons function return HTMLElement instead of string
- Loading branch information
Showing
11 changed files
with
109 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as dom from 'vs/base/browser/dom'; | ||
import { renderCodiconsRegex } from 'vs/base/common/codicons'; | ||
|
||
export function renderCodiconsAsElement(text: string): Array<HTMLSpanElement | string> { | ||
const elements = new Array<HTMLSpanElement | string>(); | ||
let match: RegExpMatchArray | null; | ||
|
||
let textStart = 0, textStop = 0; | ||
while ((match = renderCodiconsRegex.exec(text)) !== null) { | ||
textStop = match.index || 0; | ||
elements.push(text.substring(textStart, textStop)); | ||
textStart = (match.index || 0) + match[0].length; | ||
|
||
const [, escaped, codicon, name, animation] = match; | ||
elements.push(escaped ? `$(${codicon})` : dom.$(`span.codicon.codicon-${name}${animation ? `.codicon-animation-${animation}` : ''}`)); | ||
} | ||
|
||
if (textStart < text.length) { | ||
elements.push(text.substring(textStart)); | ||
} | ||
return elements; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { renderCodiconsAsElement } from 'vs/base/browser/codicons'; | ||
import * as assert from 'assert'; | ||
|
||
suite('renderCodicons', () => { | ||
|
||
test('no codicons', () => { | ||
const result = renderCodiconsAsElement(' hello World .'); | ||
|
||
assert.equal(elementsToString(result), ' hello World .'); | ||
}); | ||
|
||
test('codicon only', () => { | ||
const result = renderCodiconsAsElement('$(alert)'); | ||
|
||
assert.equal(elementsToString(result), '<span class="codicon codicon-alert"></span>'); | ||
}); | ||
|
||
test('codicon and non-codicon strings', () => { | ||
const result = renderCodiconsAsElement(` $(alert) Unresponsive`); | ||
|
||
assert.equal(elementsToString(result), ' <span class="codicon codicon-alert"></span> Unresponsive'); | ||
}); | ||
|
||
test('multiple codicons', () => { | ||
const result = renderCodiconsAsElement('$(check)$(error)'); | ||
|
||
assert.equal(elementsToString(result), '<span class="codicon codicon-check"></span><span class="codicon codicon-error"></span>'); | ||
}); | ||
|
||
test('escaped codicon', () => { | ||
const result = renderCodiconsAsElement('\\$(escaped)'); | ||
|
||
assert.equal(elementsToString(result), '$(escaped)'); | ||
}); | ||
|
||
test('codicon with animation', () => { | ||
const result = renderCodiconsAsElement('$(zip~anim)'); | ||
|
||
assert.equal(elementsToString(result), '<span class="codicon codicon-zip codicon-animation-anim"></span>'); | ||
}); | ||
|
||
const elementsToString = (elements: Array<HTMLElement | string>): string => { | ||
return elements | ||
.map(elem => elem instanceof HTMLElement ? elem.outerHTML : elem) | ||
.reduce((a, b) => a + b, ''); | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters