Skip to content

Commit c5827d4

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
1 parent 1644566 commit c5827d4

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
@@ -157,6 +157,30 @@ describe('Component Schematic', () => {
157157
).toBeRejectedWithError('Selector "app-1-one" is invalid.');
158158
});
159159

160+
it('should allow dash in selector before a number', async () => {
161+
const options = { ...defaultOptions, name: 'one-1' };
162+
163+
const tree = await schematicRunner.runSchematic('component', options, appTree);
164+
const content = tree.readContent('/projects/bar/src/app/one-1/one-1.component.ts');
165+
expect(content).toMatch(/selector: 'app-one-1'/);
166+
});
167+
168+
it('should allow dash in selector before a number and with a custom prefix', async () => {
169+
const options = { ...defaultOptions, name: 'one-1', prefix: 'pre' };
170+
171+
const tree = await schematicRunner.runSchematic('component', options, appTree);
172+
const content = tree.readContent('/projects/bar/src/app/one-1/one-1.component.ts');
173+
expect(content).toMatch(/selector: 'pre-one-1'/);
174+
});
175+
176+
it('should allow dash in selector before a number and without a prefix', async () => {
177+
const options = { ...defaultOptions, name: 'one-2', selector: 'one-2' };
178+
179+
const tree = await schematicRunner.runSchematic('component', options, appTree);
180+
const content = tree.readContent('/projects/bar/src/app/one-2/one-2.component.ts');
181+
expect(content).toMatch(/selector: 'one-2'/);
182+
});
183+
160184
it('should use the default project prefix if none is passed', async () => {
161185
const options = { ...defaultOptions, prefix: undefined };
162186

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)