Skip to content

Commit 75b7491

Browse files
clydinalan-agius4
authored andcommitted
fix(@schematics/angular): allow dash in selector before a number
This commit updates the validator regexp to allow a dash before a number. Closes #25164 (cherry picked from commit c5827d4)
1 parent 8f16486 commit 75b7491

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

packages/schematics/angular/component/index_spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,30 @@ describe('Component Schematic', () => {
208208
).toBeRejectedWithError('Selector "app-1-one" is invalid.');
209209
});
210210

211+
it('should allow dash in selector before a number', async () => {
212+
const options = { ...defaultOptions, name: 'one-1' };
213+
214+
const tree = await schematicRunner.runSchematic('component', options, appTree);
215+
const content = tree.readContent('/projects/bar/src/app/one-1/one-1.component.ts');
216+
expect(content).toMatch(/selector: 'app-one-1'/);
217+
});
218+
219+
it('should allow dash in selector before a number and with a custom prefix', async () => {
220+
const options = { ...defaultOptions, name: 'one-1', prefix: 'pre' };
221+
222+
const tree = await schematicRunner.runSchematic('component', options, appTree);
223+
const content = tree.readContent('/projects/bar/src/app/one-1/one-1.component.ts');
224+
expect(content).toMatch(/selector: 'pre-one-1'/);
225+
});
226+
227+
it('should allow dash in selector before a number and without a prefix', async () => {
228+
const options = { ...defaultOptions, name: 'one-2', selector: 'one-2' };
229+
230+
const tree = await schematicRunner.runSchematic('component', options, appTree);
231+
const content = tree.readContent('/projects/bar/src/app/one-2/one-2.component.ts');
232+
expect(content).toMatch(/selector: 'one-2'/);
233+
});
234+
211235
it('should use the default project prefix if none is passed', async () => {
212236
const options = { ...defaultOptions, prefix: undefined };
213237

packages/schematics/angular/utility/validation.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import { SchematicsException } from '@angular-devkit/schematics';
1010

1111
// Must start with a letter, and must contain only alphanumeric characters or dashes.
1212
// When adding a dash the segment after the dash must also start with a letter.
13-
export const htmlSelectorRe = /^[a-zA-Z][.0-9a-zA-Z]*(:?-[a-zA-Z][.0-9a-zA-Z]*)*$/;
13+
export const htmlSelectorRe =
14+
/^[a-zA-Z][.0-9a-zA-Z]*((:?-[0-9]+)*|(:?-[a-zA-Z][.0-9a-zA-Z]*(:?-[0-9]+)*)*)$/;
1415

1516
// See: https://github.com/tc39/proposal-regexp-unicode-property-escapes/blob/fe6d07fad74cd0192d154966baa1e95e7cda78a1/README.md#other-examples
1617
const ecmaIdentifierNameRegExp = /^(?:[$_\p{ID_Start}])(?:[$_\u200C\u200D\p{ID_Continue}])*$/u;

0 commit comments

Comments
 (0)