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(compiler): preserve @page rules in encapsulated styles #41915

Closed
wants to merge 3 commits into from

Conversation

crisbeto
Copy link
Member

@crisbeto crisbeto commented May 2, 2021

Currently the compiler treats @page rules in the same way as @media, however that is incorrect and it results in invalid CSS, because @page allows style declarations at the root (e.g. @page { margin: 50%; }) and it only allows a limited set of at-rules to be nested into it. Given these restrictions, we can't really encapsulate the styles since they apply at the document level when the user tries to print.

These changes make it so that @page rules are preserved so that we don't break the user's CSS.

More information: https://www.w3.org/TR/css-page-3

Fixes #26269.

@google-cla google-cla bot added the cla: yes label May 2, 2021
class SomeClass3 {}
class SomeClass4 {}
class SomeClass5 {}
@Directive()
Copy link
Member Author

Choose a reason for hiding this comment

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

These changes aren't necessary for the fix, but they prevent focused tests in shadow_css_spec.ts from failing when run with the Ivy compiler.

@crisbeto crisbeto marked this pull request as ready for review May 2, 2021 12:20
@pullapprove pullapprove bot requested a review from AndrewKushnir May 2, 2021 12:20
@crisbeto crisbeto added action: review The PR is still awaiting reviews from at least one requested reviewer area: compiler Issues related to `ngc`, Angular's template compiler target: rc This PR is targeted for the next release-candidate labels May 2, 2021
@ngbot ngbot bot modified the milestone: Backlog May 2, 2021
Copy link
Member

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

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

I think one could argue that @page has similar requirements to @font-face. As such it should be processed in the same way. See da6ed15

@crisbeto
Copy link
Member Author

crisbeto commented May 2, 2021

I don't mind changing it, although since the spec doesn't allow for @page to be prefixed we'd technically be trying to correct invalid syntax. I'd put @font-face and @page in the same category as @keyframes where we should just leave them as is, because they can't be encapsulated. Quote from MDN:

@font-face cannot be declared within a CSS selector. For example, the following will not work:

.className {
  @font-face {
    font-family: MyHelvetica;
    src: local("Helvetica Neue Bold"),
         local("HelveticaNeue-Bold"),
         url(MgOpenModernaBold.ttf);
    font-weight: bold;
  }
}

@petebacondarwin
Copy link
Member

Yeah exactly. They should not be encapsulated. But there is an issue if you happen to import a file with such rules from within a rule that contains :host. See #41751.
So if we are going to do that workaround for @font-face we should do it for @page too.

Currently the compiler treats `@page` rules in the same way as `@media`, however that is incorrect and it results in invalid CSS, because `@page` allows style declarations at the root (e.g. `@page (margin: 50%) {}`) and it only allows a limited set of at-rules to be nested into it. Given these restrictions, we can't really encapsulate the styles since they apply at the document level when the user tries to print.

These changes make it so that `@page` rules are preserved so that we don't break the user's CSS.

More information: https://www.w3.org/TR/css-page-3

Fixes angular#26269.
@crisbeto
Copy link
Member Author

crisbeto commented May 5, 2021

Sorry for the delay @petebacondarwin, I've addressed the feedback. I also managed to remove a bit of logic from the code that handles @font-family.

Copy link
Member

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

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

LGTM

@crisbeto crisbeto added action: merge The PR is ready for merge by the caretaker action: presubmit The PR is in need of a google3 presubmit and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels May 5, 2021
@AndrewKushnir
Copy link
Contributor

Presubmit.

Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

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

Looks great, thanks @crisbeto 👍 Just left a comment on adding more docs (based on the info from PR description) into the code.

packages/compiler/src/shadow_css.ts Show resolved Hide resolved
@AndrewKushnir AndrewKushnir added the action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews label May 5, 2021
@crisbeto crisbeto removed the action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews label May 5, 2021
@AndrewKushnir AndrewKushnir removed the action: presubmit The PR is in need of a google3 presubmit label May 5, 2021
@alxhub alxhub closed this in abcd4bb May 6, 2021
alxhub pushed a commit that referenced this pull request May 6, 2021
Currently the compiler treats `@page` rules in the same way as `@media`, however that is incorrect and it results in invalid CSS, because `@page` allows style declarations at the root (e.g. `@page (margin: 50%) {}`) and it only allows a limited set of at-rules to be nested into it. Given these restrictions, we can't really encapsulate the styles since they apply at the document level when the user tries to print.

These changes make it so that `@page` rules are preserved so that we don't break the user's CSS.

More information: https://www.w3.org/TR/css-page-3

Fixes #26269.

PR Close #41915
@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 Jun 6, 2021
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: compiler Issues related to `ngc`, Angular's template compiler cla: yes target: rc This PR is targeted for the next release-candidate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@page css rule in component style is ignored
3 participants