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(table): column class names should be css friendly #6173

Merged

Conversation

andrewseguin
Copy link
Contributor

@andrewseguin andrewseguin commented Jul 31, 2017

Column classes are given to header and row cells. E.g. a column named column-a will give cells a classname cdk-column-column-a.

If the user has a column with special characters, they should be stripped so that CSS selectors cannot be invalid.

This will provide a css class friendly name where it is only alphanumeric or includes special characters like - and _ (just because, if others should be included let me know)

Fixes #6065

@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Jul 31, 2017
* as part of a CSS class. Allows for the special characters - and _.
*/
getCssClassFriendlyName() {
return this.name.replace(/[^a-z0-9_-]/ig, '');
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you're doing more work than you need to by putting the logic here. I would do:

get name { return this._name; }
set name(value) {
  this._name = value;
  this.cssFriendlyName = (value | '').replace(/[^a-z0-9_-]/ig, '');
}

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 non-normalized name is still used by the CdkTable and should match even with the special characters. This change only should affect the CSS classes

Copy link
Contributor

Choose a reason for hiding this comment

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

Right, I'm not proposing changing that, that's why I was saving it in _name. I just think it makes sense to only update the cssFriendlyName when the name gets set, rather than whenever change detection runs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah whoops, totally missed that you were storing the value into a separate variable. Makes sense

* as part of a CSS class. Allows for the special characters - and _.
*/
getCssClassFriendlyName() {
return this.name.replace(/[^a-z0-9_-]/ig, '');
Copy link
Member

Choose a reason for hiding this comment

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

Need to mention this replacement in the docs.

@@ -97,6 +98,16 @@ describe('CdkTable', () => {
});
});

it('should be able to apply class-friendly css class names for the column cells', () => {
const crazyColumnNameAppFixture = TestBed.createComponent(CrazyColumnNameCdkTableApp);
Copy link
Member

Choose a reason for hiding this comment

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

Add comment here like

// Test component has a column named "crazy-column-NAME-1!@#$%^-_&*()2"

* as part of a CSS class. Allows for the special characters - and _.
*/
getCssClassFriendlyName() {
return this.name.replace(/[^a-z0-9_-]/ig, '');
Copy link
Member

Choose a reason for hiding this comment

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

What's your reasoning for replacing with empty string instead of either _ or -? Should include that in a comment here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No reasoning, not sure of the standard here. Any preference if the invalid characters are replaced with _ or -?

Copy link
Member

Choose a reason for hiding this comment

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

I see dashes more commonly in css

@andrewseguin andrewseguin force-pushed the table-normalize-column-classes branch from 0364eb1 to 644d40d Compare August 2, 2017 16:47
@andrewseguin
Copy link
Contributor Author

Test comment added; string is replaced by dashes; added styles section to cdk-table docs; using variable now instead of function

Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

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

LGTM

@jelbourn jelbourn added the action: merge The PR is ready for merge by the caretaker label Aug 2, 2017
@tinayuangao tinayuangao merged commit 1748397 into angular:master Aug 2, 2017
@mcalmus
Copy link
Contributor

mcalmus commented Sep 1, 2017

Confirmed this is working in beta 10.

@andrewseguin andrewseguin deleted the table-normalize-column-classes branch November 28, 2017 20:36
@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

mdTable class names need to be normalized to exclude invalid characters
6 participants