Cleanup - fix various tests.

This commit is contained in:
Per Bothner
2023-05-17 18:37:27 -07:00
parent 2f7fe1189c
commit 67e968926c
12 changed files with 136 additions and 54 deletions
@@ -10,7 +10,7 @@ import * as UC from './UnicodeProperties';
export class UnicodeGraphemeProvider implements IUnicodeVersionProvider {
public readonly version = '15-graphemes';
public ambiguousCharsAreWide: boolean = false;
constructor() {
}
@@ -19,9 +19,8 @@ export class UnicodeGraphemeProvider implements IUnicodeVersionProvider {
let w = UC.infoToWidthInfo(charInfo);
let shouldJoin = false;
if (w >= 2) {
const preferWide = false; //this.ambiguousCharsAreWide(context);
// Treat emoji_presentation_selector as WIDE.
w = w == 3 || preferWide || codepoint === 0xfe0f ? 2 : 1;
w = w == 3 || this.ambiguousCharsAreWide || codepoint === 0xfe0f ? 2 : 1;
} else
w = 1;
if (preceding !== 0) {
@@ -31,14 +30,23 @@ export class UnicodeGraphemeProvider implements IUnicodeVersionProvider {
if (shouldJoin) {
if (oldWidth > w)
w = oldWidth;
else if (charInfo === 32) // FIXME UC.GRAPHEME_BREAK_SAW_Regional_Pair)
else if (charInfo === 32) // FIXME UC.GRAPHEME_BREAK_SAW_Regional_Pair)
w = 2;
}
}
return UnicodeService.createPropertyValue(charInfo, w, shouldJoin);
}
public wcwidth(num: number): UnicodeCharWidth {
return UC.infoToWidth(UC.getInfo(num));
public wcwidth(codepoint: number): UnicodeCharWidth {
let charInfo = UC.getInfo(codepoint);
let w = UC.infoToWidthInfo(charInfo);
let kind = (charInfo & UC.GRAPHEME_BREAK_MASK) >> UC.GRAPHEME_BREAK_SHIFT;
if (kind === UC.GRAPHEME_BREAK_Extend
|| kind === UC.GRAPHEME_BREAK_Prepend)
return 0;
else if (w >= 2)
return w == 3 || this.ambiguousCharsAreWide? 2 : 1;
else
return 1;
}
}
@@ -0,0 +1,46 @@
/**
* Copyright (c) 2019 The xterm.js authors. All rights reserved.
* @license MIT
*/
import { assert } from 'chai';
import { openTerminal, launchBrowser } from '../../../out-test/api/TestUtils';
import { Browser, Page } from 'playwright';
const APP = 'http://127.0.0.1:3001/test';
let browser: Browser;
let page: Page;
const width = 800;
const height = 600;
describe('UnicodeGraphemesAddon', () => {
before(async function(): Promise<any> {
browser = await launchBrowser();
page = await (await browser.newContext()).newPage();
await page.setViewportSize({ width, height });
});
after(async () => {
await browser.close();
});
beforeEach(async function(): Promise<any> {
await page.goto(APP);
await openTerminal(page);
});
const ourVersion = '15-graphemes';
it('wcwidth V15 emoji test', async () => {
await page.evaluate(`
window.unicode = new UnicodeGraphemesAddon();
window.term.loadAddon(window.unicode);
`);
// should have loaded '15-graphemes'
assert.deepEqual(await page.evaluate(`window.term.unicode.versions`), ['6', ourVersion]);
// switch should not throw
await page.evaluate(`window.term.unicode.activeVersion = '${ourVersion}';`);
assert.deepEqual(await page.evaluate(`window.term.unicode.activeVersion`), ourVersion);
// v6: 10, V15: 20
assert.deepEqual(await page.evaluate(`window.term._core.unicodeService.getStringCellWidth('🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣')`), 20);
});
});
@@ -0,0 +1,35 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es2015",
"lib": [
"dom",
"es2015"
],
"rootDir": ".",
"outDir": "../out-test",
"sourceMap": true,
"removeComments": true,
"strict": true,
"baseUrl": ".",
"paths": {
"common/*": [
"../../../src/common/*"
]
},
"types": [
"../../../node_modules/@types/mocha",
"../../../node_modules/@types/node",
"../../../out-test/api/TestUtils"
]
},
"include": [
"./**/*",
"../../../typings/xterm.d.ts"
],
"references": [
{
"path": "../../../src/common"
}
]
}
@@ -1,11 +1,11 @@
/**
* Copyright (c) 2017 The xterm.js authors. All rights reserved.
* Copyright (c) 2023 The xterm.js authors. All rights reserved.
* @license MIT
*/
import { Terminal, ITerminalAddon } from 'xterm';
declare module 'xterm-addon-unicode11' {
declare module 'xterm-addon-unicode-graphemes' {
export class Unicode11Addon implements ITerminalAddon {
constructor();
public activate(terminal: Terminal): void;
@@ -219,14 +219,12 @@ export class UnicodeV11 implements IUnicodeVersionProvider {
return 1;
}
charProperties(codepoint: number, preceding: UnicodeCharProperties): UnicodeCharProperties {
public charProperties(codepoint: number, preceding: UnicodeCharProperties): UnicodeCharProperties {
let width = this.wcwidth(codepoint);
let shouldJoin = width === 0;
let shouldJoin = width === 0 && preceding !== 0;
if (shouldJoin) {
let oldWidth = preceding === 0 ? 0
: UnicodeService.extractWidth(preceding);
const oldWidth = UnicodeService.extractWidth(preceding);
if (oldWidth === 0) {
width = 1;
shouldJoin = false;
} else if (oldWidth > width) {
width = oldWidth;