Skip to content

fix(platform-server): generate correct stylings for camel case names #22263

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

Closed
wants to merge 1 commit into from

Conversation

CaerusKaru
Copy link
Member

@CaerusKaru CaerusKaru commented Feb 16, 2018

  • Add correct mapping from camel case to kebab case for CSS style
    names
  • Remove internal CSS methods in favor of native Domino APIs

Fixes #19235

@CaerusKaru CaerusKaru force-pushed the style branch 2 times, most recently from de930c7 to c0c5f48 Compare February 16, 2018 21:13
@CaerusKaru
Copy link
Member Author

@vikerman

"tslib": "^1.7.1",
"domino": "^1.0.29",

Choose a reason for hiding this comment

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

Does Domino use semantic versioning? If so then this PR is also a breaking change

Copy link
Member Author

Choose a reason for hiding this comment

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

Domino uses "semantic versioning". The difference between v1 and v2 is an internal change in how they represent DOM nodes (they switched to a linked list implementation) and they added a fix for namespacing issues.

If we want to consider this a breaking change, I can add that to the PR description, but that just makes the timing of the PR even better with v6 in beta.

@vikerman vikerman added action: merge The PR is ready for merge by the caretaker target: patch This PR is targeted for the next patch release labels Feb 26, 2018
getStyle(element: any, styleName: string): string {
const styleMap = this._readStyleAttribute(element);
return styleMap.hasOwnProperty(styleName) ? (styleMap as any)[styleName] : '';
return element.style[styleName] || element.style.getPropertyValue(styleName) || '';
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

(styleMap as any)[styleName] = styleValue;
this._writeStyleAttribute(element, styleMap);
styleName = styleName.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
element.style[styleName] = styleValue;
Copy link
Contributor

Choose a reason for hiding this comment

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

is it ok to access style[name] directly in domino ?
seems like there is style.properties[name]
What about using setPropertyValue instead ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it's fine. It follows the spec for CSSStyleDeclaration, which allows for string indexing

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

@vicb The indexing is by number, not string as I originally thought. The item operator indicates number aliasing, like with NodeList.

In the case of platform-server, which is using an out-of-date form of CSSStyleDeclaration, Domino does not provide us with enough cover to use setPropertyValue and getPropertyValue. It needs to be updated to include modern CSS style properties. This change gets around that requirement for now by just directly indexing. While not spec compliant, it's a little more palatable than just writing huge style chunks as was previous. Plus if Domino does get updated in the near future, we can just replace the one-liners with their compatible get/setPropertyValue forms.

I agree it's not ideal, but we're a little handicapped by our server DOM implementation, which works well for just about everything else.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok wait, I found it! I knew I wasn't going crazy. In the CSSOM spec, found here:

partial interface CSSStyleDeclaration {
  [CEReactions] attribute [TreatNullAs=EmptyString] CSSOMString _camel_cased_attribute;
};

Which in JavaScript means that for all possible camel-cased attributes, you have string indexing allowed.

@vicb
Copy link
Contributor

vicb commented Feb 26, 2018

Seems like el.stlye is a CSSStyleDeclaration then I wonder if accessing el.style[name] is valid ?

* Add correct mapping from camel case to kebab case for CSS style
names
* Remove internal CSS methods in favor of native Domino APIs

Fixes angular#19235
@CaerusKaru
Copy link
Member Author

@vicb As stated above, using string indexing is totally valid for CSSStyleDeclaration, which Domino implements.

@CaerusKaru
Copy link
Member Author

@vicb Ok I've done a deeper dive into the issue, and here's where I've ended up. If you set a property that isn't specifically enumerated by Domino (which at this point is a little outdated re flex and grid, etc), it gets stored raw on the instance of the CSSStyleDeclaration. That means that if someone applies their own styles outside of the Angular renderer, the renderer may not pick up the styles if it just relies on get/setPropertyValue. Likewise, it means that using setPropertyValue won't allow for string index access for undefined properties (e.g. flex, grid) later on, which leads to undefined behavior.

The solution here essentially bypasses the Domino internal implementation and instead relies on the contract for CSSStyleDeclaration

@alexeagle alexeagle added the area: server Issues related to server-side rendering label Feb 26, 2018
alexeagle pushed a commit that referenced this pull request Feb 27, 2018
…22263)

* Add correct mapping from camel case to kebab case for CSS style
names
* Remove internal CSS methods in favor of native Domino APIs

Fixes #19235

PR Close #22263
@alexeagle alexeagle closed this in 40ba009 Feb 27, 2018
@CaerusKaru CaerusKaru deleted the style branch February 27, 2018 02:45
smdunn pushed a commit to smdunn/angular that referenced this pull request Feb 28, 2018
…ngular#22263)

* Add correct mapping from camel case to kebab case for CSS style
names
* Remove internal CSS methods in favor of native Domino APIs

Fixes angular#19235

PR Close angular#22263
@jimjim2a
Copy link

jimjim2a commented Mar 1, 2018

Since this update, it's no longer possible to apply background-image correctly on server, it is totally dropped.
It used to work properly on 5.2.6 but not on 5.2.7

CaerusKaru added a commit to CaerusKaru/angular that referenced this pull request Mar 1, 2018
* Partially reverts angular#22263 due to lack of total spec complicance
  on the server
* Maintains the camel-case styles fix
@CaerusKaru
Copy link
Member Author

@jimjim2a Thank you for pointing that out. I've put together a partial revert PR in #22527 that fixes that issue and adds a unit test to make sure it doesn't get introduced again.

This was referenced Mar 15, 2018
leo6104 pushed a commit to leo6104/angular that referenced this pull request Mar 25, 2018
…ngular#22263)

* Add correct mapping from camel case to kebab case for CSS style
names
* Remove internal CSS methods in favor of native Domino APIs

Fixes angular#19235

PR Close angular#22263
leo6104 pushed a commit to leo6104/angular that referenced this pull request Mar 25, 2018
* Partially reverts angular#22263 due to lack of total spec compliance
  on the server
* Maintains the camel-case styles fix

PR Close angular#22527
@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 13, 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 area: server Issues related to server-side rendering cla: yes target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug(platform-server): dynamic styles do not get converted from camelCase to kebab-case
10 participants