Implement lineHeight px format support
Co-authored-by: Tyriar <2193314+Tyriar@users.noreply.github.com>
This commit is contained in:
co-authored by
Tyriar
parent
0dffbedf76
commit
b3503f4916
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* Copyright (c) 2023 The xterm.js authors. All rights reserved.
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
import { assert } from 'chai';
|
||||
import { OptionsService } from 'common/services/OptionsService';
|
||||
|
||||
describe('OptionsService lineHeight', () => {
|
||||
let optionsService: OptionsService;
|
||||
|
||||
beforeEach(() => {
|
||||
optionsService = new OptionsService({});
|
||||
});
|
||||
|
||||
describe('numeric lineHeight', () => {
|
||||
it('should accept numeric values', () => {
|
||||
optionsService.options.lineHeight = 1.5;
|
||||
assert.equal(optionsService.rawOptions.lineHeight, 1.5);
|
||||
});
|
||||
|
||||
it('should reject values less than 1', () => {
|
||||
assert.throws(() => optionsService.options.lineHeight = 0.5);
|
||||
});
|
||||
|
||||
it('should accept 1 as minimum value', () => {
|
||||
optionsService.options.lineHeight = 1;
|
||||
assert.equal(optionsService.rawOptions.lineHeight, 1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('string lineHeight with px format', () => {
|
||||
it('should accept px values', () => {
|
||||
optionsService.options.lineHeight = '24px';
|
||||
assert.equal(optionsService.rawOptions.lineHeight, '24px');
|
||||
});
|
||||
|
||||
it('should accept decimal px values', () => {
|
||||
optionsService.options.lineHeight = '23.5px';
|
||||
assert.equal(optionsService.rawOptions.lineHeight, '23.5px');
|
||||
});
|
||||
|
||||
it('should reject px values less than 1', () => {
|
||||
assert.throws(() => optionsService.options.lineHeight = '0.5px');
|
||||
});
|
||||
|
||||
it('should accept 1px as minimum value', () => {
|
||||
optionsService.options.lineHeight = '1px';
|
||||
assert.equal(optionsService.rawOptions.lineHeight, '1px');
|
||||
});
|
||||
|
||||
it('should reject non-px string values', () => {
|
||||
assert.throws(() => optionsService.options.lineHeight = '24' as any);
|
||||
assert.throws(() => optionsService.options.lineHeight = '24em' as any);
|
||||
assert.throws(() => optionsService.options.lineHeight = 'normal' as any);
|
||||
});
|
||||
|
||||
it('should reject invalid px values', () => {
|
||||
assert.throws(() => optionsService.options.lineHeight = 'invalidpx' as any);
|
||||
assert.throws(() => optionsService.options.lineHeight = 'px' as any);
|
||||
});
|
||||
});
|
||||
|
||||
describe('invalid lineHeight types', () => {
|
||||
it('should reject null', () => {
|
||||
assert.throws(() => optionsService.options.lineHeight = null as any);
|
||||
});
|
||||
|
||||
it('should reject undefined when explicitly set', () => {
|
||||
assert.throws(() => optionsService.options.lineHeight = undefined as any);
|
||||
});
|
||||
|
||||
it('should reject boolean values', () => {
|
||||
assert.throws(() => optionsService.options.lineHeight = true as any);
|
||||
assert.throws(() => optionsService.options.lineHeight = false as any);
|
||||
});
|
||||
|
||||
it('should reject arrays', () => {
|
||||
assert.throws(() => optionsService.options.lineHeight = [] as any);
|
||||
});
|
||||
|
||||
it('should reject objects', () => {
|
||||
assert.throws(() => optionsService.options.lineHeight = {} as any);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -172,12 +172,31 @@ export class OptionsService extends Disposable implements IOptionsService {
|
||||
case 'cursorWidth':
|
||||
value = Math.floor(value);
|
||||
// Fall through for bounds check
|
||||
case 'lineHeight':
|
||||
case 'tabStopWidth':
|
||||
if (value < 1) {
|
||||
throw new Error(`${key} cannot be less than 1, value: ${value}`);
|
||||
}
|
||||
break;
|
||||
case 'lineHeight':
|
||||
if (typeof value === 'string') {
|
||||
// Handle 'px' format
|
||||
if (value.endsWith('px')) {
|
||||
const pxValue = parseFloat(value.slice(0, -2));
|
||||
if (isNaN(pxValue) || pxValue < 1) {
|
||||
throw new Error(`${key} with 'px' format must be a valid number >= 1, value: ${value}`);
|
||||
}
|
||||
// Keep the string value as-is for downstream processing
|
||||
} else {
|
||||
throw new Error(`${key} string value must end with 'px', value: ${value}`);
|
||||
}
|
||||
} else if (typeof value === 'number') {
|
||||
if (value < 1) {
|
||||
throw new Error(`${key} cannot be less than 1, value: ${value}`);
|
||||
}
|
||||
} else {
|
||||
throw new Error(`${key} must be a number or string with 'px' suffix, value: ${value}`);
|
||||
}
|
||||
break;
|
||||
case 'minimumContrastRatio':
|
||||
value = Math.max(1, Math.min(21, Math.round(value * 10) / 10));
|
||||
break;
|
||||
|
||||
@@ -231,7 +231,7 @@ export interface ITerminalOptions {
|
||||
fontWeightBold?: FontWeight;
|
||||
ignoreBracketedPasteMode?: boolean;
|
||||
letterSpacing?: number;
|
||||
lineHeight?: number;
|
||||
lineHeight?: number | string;
|
||||
linkHandler?: ILinkHandler | null;
|
||||
logLevel?: LogLevel;
|
||||
logger?: ILogger | null;
|
||||
|
||||
Reference in New Issue
Block a user