Production build destroys pattern in attribute binding
Bug Report or Feature Request (mark with an x)
- [x] bug report -> please search issues before submitting
- [ ] feature request
Command (mark with an x)
- [ ] new
- [x] build
- [ ] serve
- [ ] test
- [ ] e2e
- [ ] generate
- [ ] add
- [ ] update
- [ ] lint
- [ ] xi18n
- [ ] run
- [ ] config
- [ ] help
- [ ] version
- [ ] doc
Versions
Angular CLI: 6.1.2
Node: 10.6.0
OS: darwin x64
Angular: 6.1.1
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
Package Version
------------------------------------------------------------
@angular-devkit/architect 0.6.8
@angular-devkit/build-angular 0.6.8
@angular-devkit/build-ng-packagr 0.6.8
@angular-devkit/build-optimizer 0.6.8
@angular-devkit/core 0.6.8
@angular-devkit/schematics 0.7.2
@angular/cli 6.1.2
@ngtools/json-schema 1.1.0
@ngtools/webpack 6.0.8
@schematics/angular 0.7.2
@schematics/update 0.7.2
ng-packagr 3.0.6
rxjs 6.2.2
typescript 2.9.2
webpack 4.8.3
Repro steps
ng build --prod removes back slashes of pattern
pattern="^\d{2}\.\d{2}\.\d{4}$"
is converted to
pattern="^d{2}.d{2}.d{4}$".
For reproduction, use the pattern with any input field.
where does pattern appear? is that part of the command line? in a config file?
I am sorry, I thought it was clear enough. I am using the pattern in a component template like this:
@Component({
template: `<input type="text" pattern="^\d{2}\.\d{2}\.\d{4}$">`
})
I suspect this is reproducible only with @angular/compiler-cli (a package in angular/angular) and not caused by the Angular CLI
@CSchulz We had the same issue; moving the pattern to a variable in TS and referencing the variable in HTML solved it for us:
@Component({
template: `<input type="text" [pattern]="pattern">`
})
export class MyComponent {
public pattern = ^\d{2}\.\d{2}\.\d{4}$;
}
I believe this isn't a bug anywhere actually. In the original example:
@Component({
template: `<input type="text" pattern="^\d{2}\.\d{2}\.\d{4}$">`
})
You are creating a string via backticks. Inside a string, the \ character has a special meaning. It escapes the next character, which is how \n becomes a newline in strings.
So when you have \d it means "escaped d" instead of "slash followed by d". Since "escaped d" doesn't have any special meaning, you get just "d".
You can check this in your browser console right now, with no need for any other tooling:
> console.log(`<input type="text" pattern="^\d{2}\.\d{2}\.\d{4}$">`)
<input type="text" pattern="^d{2}.d{2}.d{4}$">
The string you actually want needs to have \\ instead of \. This means "escaped slash" which in a string is \.
> console.log(`<input type="text" pattern="^\\d{2}\\.\\d{2}\\.\\d{4}$">`)
<input type="text" pattern="^\d{2}\.\d{2}\.\d{4}$">
@fbernhard's example also works because it's not using a string but rather a regexp, which gives you the right string when called with .toString():
> /^\d{2}\.\d{2}\.\d{4}$/.toString()
"/^\d{2}\.\d{2}\.\d{4}$/"
@Filipe: the code with patterns in HTML compiled well in NG4. When we upgraded to NG6 we faced that issue.
As the pattern is valid in plain HTML I would expect it to work in Angular too. IMHO this is a bug in the Angular template compiler.
On Wed, Aug 15, 2018, 17:23 Filipe Silva [email protected] wrote:
Closed #11820 https://github.com/angular/angular-cli/issues/11820.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/angular/angular-cli/issues/11820#event-1790272911, or mute the thread https://github.com/notifications/unsubscribe-auth/ACDatJXY_YvcH7A_uVeDVF1wJUvuQFwSks5uRDz_gaJpZM4V0YC7 .
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.