Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(template-compiler): relax attribute name restriction on usage of hyphen and underscore in combination @W-12021741 #3143

Merged
merged 3 commits into from
Nov 9, 2022

Conversation

ravijayaramappa
Copy link
Contributor

Details

Relax the restriction on template compiler to allow usage of an underscore(_) followed by a hyphen(-) in an attribute name.

Also add tests to document additional test case for the current restrictions in template compiler.

Does this pull request introduce a breaking change?

  • ✅ No, it does not introduce a breaking change.

Does this pull request introduce an observable change?

  • ✅ No, it does not introduce an observable change.

GUS work item

W-12021741

@@ -1335,7 +1335,8 @@ function applyAttributes(ctx: ParserCtx, parsedAttr: ParsedAttribute, element: B

// disallow attr name which combines underscore character with special character.
// We normalize camel-cased names with underscores caMel -> ca-mel; thus sanitization.
if (name.match(/_[^a-z0-9]|[^a-z0-9]_/)) {
// Note 1: underscore followed by a hyphen is valid to accomodate property names like thirsty_Camel -> thirsty_-camel
if (name.match(/_[^a-z0-9-]|[^a-z0-9]_/)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only code change in this PR, the rest are all tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a follow-up PR: What is the value of restricting the following pattern /_[^a-z0-9-]|[^a-z0-9]_/? The only remaining test case is under-_hyphen. This name is indeed ambiguous, that said it also blocks other names like foo__bar which is a valid JavaScript identifier.

Copy link
Contributor Author

@ravijayaramappa ravijayaramappa Nov 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the value of restricting the following pattern /[^a-z0-9-]|[^a-z0-9]/?

Yeah, that is a great question. I do not see much value in having this restriction IMO. I added the other test cases(valid-non-alphanumeric/actual.html, valid-hyphen-attribute/actual.html) to establish the current treatment of non alpha numeric characters.

If non-alpha numeric characters are allowed in an attribute name in general, why only restrict is in combination with an underscore(_), at the start and end of attribute name? The follow up work is to explore more about these restrictions and take the next steps as part of W-12021709.

tl;dr
Going back into the history of this restriction, I have come to the conclusion that it was added out of a mis-understanding.

  1. This is the original issue: [compiler] compiler doesn't validate public properties with underscore names #1369. Note that there wasn't an agreement whether allowing underscore in attribute name is a problem.
  2. The PR that added the restriction(fix(compiler): prevent underscore attr name camelcasing #1385) was meant to prevent camel casing attribute names with underscore.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Followup PR #3152

@ravijayaramappa ravijayaramappa marked this pull request as ready for review November 8, 2022 01:42
};
const stc3 = {
props: {
hyphen3pecial: "bar",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AST from L21-44 is to document the existing behavior.

@ravijayaramappa
Copy link
Contributor Author

/nucleus test

@@ -1335,7 +1335,8 @@ function applyAttributes(ctx: ParserCtx, parsedAttr: ParsedAttribute, element: B

// disallow attr name which combines underscore character with special character.
// We normalize camel-cased names with underscores caMel -> ca-mel; thus sanitization.
if (name.match(/_[^a-z0-9]|[^a-z0-9]_/)) {
// Note 1: underscore followed by a hyphen is valid to accomodate property names like thirsty_Camel -> thirsty_-camel
if (name.match(/_[^a-z0-9-]|[^a-z0-9]_/)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a follow-up PR: What is the value of restricting the following pattern /_[^a-z0-9-]|[^a-z0-9]_/? The only remaining test case is under-_hyphen. This name is indeed ambiguous, that said it also blocks other names like foo__bar which is a valid JavaScript identifier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants