Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

All notable changes for each version of this project will be documented in this file.

## 21.2.29

### Bug Fixes

- `IgxMaskDirective`
- A character that equals a literal of the mask is kept in the value. Previously, with the mask `+1 (000) 000-0000`, typing `5551234567` gave the value `555234567`, and the input lost the same digits when it got the focus again (#17816).
- A value set from code keeps a character that equals a literal of the mask. Previously the value `5551234567` was shown as `+1 (555) 234-567_`.
- **Behavioral Change** - A value set from code is read as a text with the mask only when it has the length of the mask and each literal in its place, such as `12/34` for the mask `00/00`. Any other value is read as the raw value, so a literal in it takes an editable position: `12/3` is shown as `12/_3`. Previously it was shown as `12/3_`.
- `IgxMaskDirective`, `IgxDateTimeEditorDirective`
- **Behavioral Change** - `promptChar` rejects a mask rule character (`0`, `9`, `#`, `L`, `?`, `A`, `a`, `&`, `C`), an emoji or another character that takes two UTF-16 code units, and an empty value. The previous prompt character, `_` by default, stays in effect. Previously, with the mask `00/00` and the prompt `0`, a typed `0` was taken for an empty position and was dropped from the value (#17817).
- `promptChar` returns the one character in use. Previously it returned the whole text that was set, such as `* @#`, while the mask used the first character.
- `IgxDateTimeEditorDirective`
- A `promptChar` that is special in a regular expression is read as that character. Previously `[`, `(`, `*` or `+` threw an error when the typed date was read, and with `.` the typed digits were dropped.

## 21.2.28

### Bug Fixes
Expand All @@ -19,11 +33,6 @@ All notable changes for each version of this project will be documented in this
- The US time zone abbreviations `UT`, `EDT`, `CST`, `CDT`, `MDT`, `PST` and `PDT`, in any letter case, are formatted with fixed offsets, as with the Angular date pipe. This applies to date formatting such as `IColumnPipeArgs.timezone` and the date pipe. A format that shows the zone shows the offset, such as `GMT-8` for `PST`. Previously `UT`, `EDT`, `CDT`, `MDT` and `PDT` produced the unformatted date text, and `CST` and `PST` were shifted by daylight saving time. `EST` and `MST` are formatted as before (#18031).
- `IgxMaskDirective`
- No mask position accepts an emoji or another character that takes two UTF-16 code units. Previously such a character was written into one position, which made the masked text longer than the mask and left the caret in the middle of the character (#17818).
- `IgxMaskDirective`, `IgxDateTimeEditorDirective`
- **Behavioral Change** - `promptChar` rejects a mask rule character (`0`, `9`, `#`, `L`, `?`, `A`, `a`, `&`, `C`), an emoji or another character that takes two UTF-16 code units, and an empty value. The previous prompt character, `_` by default, stays in effect. Previously, with the mask `00/00` and the prompt `0`, a typed `0` was taken for an empty position and was dropped from the value (#17817).
- `promptChar` returns the one character in use. Previously it returned the whole text that was set, such as `* @#`, while the mask used the first character.
- `IgxDateTimeEditorDirective`
- A `promptChar` that is special in a regular expression is read as that character. Previously `[`, `(`, `*` or `+` threw an error when the typed date was read, and with `.` the typed digits were dropped.

## 21.2.27

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ The following built-in mask rules should be supported:
&: any keyboard character (excluding space).
C: any keyboard character.

Static symbols (literals) in the mask pattern are also supported.
Static symbols (literals) in the mask pattern are also supported. A literal is told by its position in the mask, so the same character can be typed in an editable position: with the mask `+1 (000) 000-0000`, the typed `5551234567` is shown as `+1 (555) 123-4567` and the raw value is `5551234567`.

A value set from code is read as a text with the mask when it has the length of the mask and each literal in its place, such as `+1 (555) 123-4567`. Any other value is read as the raw value and fills the editable positions in order.

Each mask position holds one UTF-16 code unit. No position accepts a character that takes two code units, such as most emoji.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ describe('MaskParsingService', () => {
expect(parser.applyMask('😀ab', options('CCC'))).toEqual('_ab');
expect(parser.applyMask('a😀b', options('(CC) C'))).toEqual('(a_) b');
expect(parser.applyMask('😀😀😀', options('CC-C'))).toEqual('__-_');
// a raw value with the length of the mask still takes one position for the astral character
expect(parser.applyMask('😀a', options('CCC'))).toEqual('_a_');
});

it('should still accept characters of one code unit', () => {
Expand Down Expand Up @@ -75,4 +77,30 @@ describe('MaskParsingService', () => {
expect(normalizePrompt(undefined, '*')).toEqual('*');
});
});

describe('literals by position', () => {
const phone = options('+1 (000) 000-0000');
const text = options('CCC-CCC');

it('should keep a character that equals a literal in the parsed value', () => {
expect(parser.parseValueFromMask('+1 (555) 123-4567', phone)).toEqual('5551234567');
expect(parser.parseValueFromMask('a-b-___', text)).toEqual('a-b');
});

it('should keep a character that equals a literal when the mask is applied to a raw value', () => {
expect(parser.applyMask('5551234567', phone)).toEqual('+1 (555) 123-4567');
expect(parser.applyMask('a-b', text)).toEqual('a-b-___');
});

it('should keep the positions of a text that already has the mask', () => {
expect(parser.applyMask('+1 (555) 123-4567', phone)).toEqual('+1 (555) 123-4567');
expect(parser.applyMask('+1 (_1_) 1__-___1', phone)).toEqual('+1 (_1_) 1__-___1');
});

it('should read a value that does not have the whole mask as a raw value', () => {
// too short for the mask `00/00`, so the `/` is a character of the value and not a literal
expect(parser.applyMask('12/3', options('00/00'))).toEqual('12/_3');
expect(parser.parseValueFromMask('12/3', options('00/00'))).toEqual('12/3');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,31 @@ export function normalizePrompt(value: string | null | undefined, current: strin
return FLAGS.has(char) || isSurrogate(char) ? current : char;
}

/**
* Returns whether `value` is a text with the mask: it has the length of the mask and each literal
* is in its place. Any other value is a raw value, with no literals in it.
*/
function isMaskedText(value: string, { literals, mask }: ParsedMask): boolean {
if (value.length !== mask.length) {
return false;
}

for (let position = 0; position < value.length; position++) {
const char = value[position];
const literal = literals.get(position);

if (literal === undefined) {
// The mask never writes half of an astral character, so a value that has one is a raw value.
if (isSurrogate(char)) {
return false;
}
} else if (char !== literal) {
return false;
}
}

return true;
}

export function parseMask(format: string): ParsedMask {
const literals = new Map<number, string>();
Expand Down Expand Up @@ -79,10 +104,10 @@ export class MaskParsingService {
public applyMask(inputVal: string, maskOptions: MaskOptions, pos = 0): string {
let outputVal = '';
let value = '';
const { literals, mask } = parseMask(maskOptions.format);
const parsedMask = parseMask(maskOptions.format);
const { literals, mask } = parsedMask;
const literalKeys: number[] = Array.from(literals.keys());
const nonLiteralIndices: number[] = this.getNonLiteralIndices(mask, literalKeys);
const literalValues: string[] = Array.from(literals.values());

if (inputVal != null) {
value = inputVal.toString();
Expand All @@ -100,7 +125,10 @@ export class MaskParsingService {
return outputVal;
}

const nonLiteralValues: string[] = this.getNonLiteralValues(value, literalValues);
// A literal is told by its position and not by its value, so that the same character can be typed.
const nonLiteralValues: string[] = isMaskedText(value, parsedMask)
? value.split('').filter((_, position) => !literals.has(position))
: Array.from(value);

for (let i = 0; i < nonLiteralValues.length; i++) {
const char = nonLiteralValues[i];
Expand All @@ -125,13 +153,16 @@ export class MaskParsingService {

public parseValueFromMask(maskedValue: string, maskOptions: MaskOptions): string {
let outputVal = '';
const literalValues: string[] = Array.from(parseMask(maskOptions.format).literals.values());
const parsedMask = parseMask(maskOptions.format);
// A text without the mask, such as the result of a display pipe, has no literals to skip.
const masked = isMaskedText(maskedValue, parsedMask);

for (const val of maskedValue) {
if (literalValues.indexOf(val) === -1) {
if (val !== maskOptions.promptChar) {
outputVal += val;
}
for (let i = 0; i < maskedValue.length; i++) {
const val = maskedValue[i];
const isLiteral = masked && parsedMask.literals.has(i);

if (!isLiteral && val !== maskOptions.promptChar) {
outputVal += val;
}
}

Expand Down Expand Up @@ -199,17 +230,6 @@ export class MaskParsingService {

return nonLiteralsIndices;
}
private getNonLiteralValues(value: string, literalValues: string[]): string[] {
const nonLiteralValues: string[] = [];

for (const val of value) {
if (literalValues.indexOf(val) === -1) {
nonLiteralValues.push(val);
}
}

return nonLiteralValues;
}

private replaceIMENumbers(value: string): string {
return value.replace(/[0123456789]/g, (num) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,31 @@ describe('igxMask', () => {
expect(inputElement.nativeElement.value).toEqual('**/**');
});

it('should keep a typed character that equals a literal of the mask', fakeAsync(() => {
const fixture = TestBed.createComponent(DefMaskComponent);
fixture.componentInstance.mask = '+1 (000) 000-0000';
fixture.detectChanges();
tick(); // NgModel updateValue Promise
const inputElement = fixture.debugElement.query(By.css('input'));
const input = inputElement.nativeElement as HTMLInputElement;

inputElement.triggerEventHandler('focus');
fixture.detectChanges();

UIInteractions.simulateTyping('5551234567', inputElement, 4, 4);
fixture.detectChanges();

expect(input.value).toEqual('+1 (555) 123-4567');
expect(fixture.componentInstance.value).toEqual('5551234567');

// the focus applies the mask to the text of the input again
inputElement.triggerEventHandler('blur', { target: input });
inputElement.triggerEventHandler('focus');
fixture.detectChanges();

expect(input.value).toEqual('+1 (555) 123-4567');
}));

it('should handle deleteContentBackward input event after compositionend (line 209 branch)', fakeAsync(() => {
// This test covers the branch at line 209 of mask.directive.ts:
// After compositionend, Chromium fires an input event with inputType='deleteContentBackward'.
Expand Down
Loading