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

Prevented error thrown when ckeditor5-utils/src/env.ts is used in an environment without window global object #16370

Merged
merged 3 commits into from
May 16, 2024

Conversation

scofalik
Copy link
Contributor

@scofalik scofalik commented May 16, 2024

Suggested merge commit message (convention)

Fix (utils): Prevented error thrown when editor files are imported in an environment without `window` global object. Closes #16368.

… is used in an environment without `window` global object.
Copy link
Contributor

coderabbitai bot commented May 16, 2024

Warning

Rate Limit Exceeded

@scofalik has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 10 minutes and 10 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.
Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.
Please see our FAQ for further information.

Commits Files that changed from the base of the PR and between 7f27cc0 and 64cd07c.

Walkthrough

The recent updates focus on refining the CKEditor 5 utilities package by enhancing the env.ts and env.js files. These changes bring improvements such as introducing an import for globalVar, modifying the getUserAgent function, converting isMediaForcedColors and isMotionReduced into getter methods, adding isMotionReduced to exports, updating tests, and expanding test coverage.

Changes

Files Change Summary
packages/ckeditor5-utils/src/env.ts
packages/ckeditor5-utils/tests/env.js
Introduced import for globalVar, modified getUserAgent function, converted isMediaForcedColors and isMotionReduced to getter methods. Added isMotionReduced to exports, updated isMediaForcedColors tests, and included new test cases for both functions.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Comment on lines +114 to +116
get isMediaForcedColors() {
return isMediaForcedColors();
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I first wanted to get rid of these getters and make it so that the property is resolved immediately (just like all other is* properties above). However, the existing tests were failing and it appeared that we test whether the util reacts to the change of related settings.

Indeed, this setting can change (tested that), so I decided to keep these functions as getters.

Copy link
Member

Choose a reason for hiding this comment

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

Doh... so maybe this should be a function? To indicate that it's a dynamic value?

Copy link
Member

Choose a reason for hiding this comment

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

It's an API change, but from what I understand this is a freshly introduced prop and that'd be fine (not a breaking change, IMO).

Copy link
Member

Choose a reason for hiding this comment

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

All the other properties of the env object are static (should not change with time) except the two recently introduced. So it'd make sense to think twice before we commit in any direction.

*/
export function isMotionReduced(): boolean {
return window.matchMedia( '(prefers-reduced-motion)' ).matches;
return globalVar.window.matchMedia ? globalVar.window.matchMedia( '(prefers-reduced-motion)' ).matches : false;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because it was a getter, this function didn't crash in described scenario because the getter was never called. However, the getter would still crash if called in an environment without window. To make the solution a bit more future-proof, I decided to add a check for globalVar.window.

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe we should mock all methods and fields in globalVar.window that we utilize in our codebase. Centralizing them would eliminate the need to repeat this logic in multiple places. However, perhaps this task could be deferred until later.

} );

it( 'returns false if window object is not available', () => {
// `global.window` is an empty object if `window` was not available in global space.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you have a better idea how to mock, please suggest.

Copy link
Member

Choose a reason for hiding this comment

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

The only thing I can think of is the use of before/after() so it's properly cleaned up on a fail, but it's a simple test so I don't see this as a must-have.

@@ -9,11 +9,13 @@
* @module utils/env
*/

import globalVar from './dom/global.js';
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
import globalVar from './dom/global.js';
import global from './dom/global.js';

I think we always use this convention.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

*/
export function isMotionReduced(): boolean {
return window.matchMedia( '(prefers-reduced-motion)' ).matches;
return globalVar.window.matchMedia ? globalVar.window.matchMedia( '(prefers-reduced-motion)' ).matches : false;
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe we should mock all methods and fields in globalVar.window that we utilize in our codebase. Centralizing them would eliminate the need to repeat this logic in multiple places. However, perhaps this task could be deferred until later.

@scofalik
Copy link
Contributor Author

scofalik commented May 16, 2024

I believe we should mock all methods and fields in globalVar.window that we utilize in our codebase. Centralizing them would eliminate the need to repeat this logic in multiple places. However, perhaps this task could be deferred until later.

That would be a bit overkill. We are not able to get rid of DOM dependencies. You are basically asking about re-attaching whole UI and whole conversion from the DOM. That's an enormous, multi-level task (that could be titled "allow CKE5 to be run in Node"). Because, even if you mock, what next? The code must execute, you need to make alternative execution flows and scenarios, etc.

I think it is enough to safeguard these functions that could be mistakenly used when they shouldn't.

@DawidKossowski
Copy link
Contributor

@scofalik, it seems like we didn't quite understand each other in that comment. Let's try again.

I don't want to replace the entire DOM dependencies, but only provide those mocks when window doesn't exist. So, I only meant that we have this logic:

try {
	globalVar = { window, document };
} catch ( e ) {
	globalVar = { window: {} as any, document: {} as any };
}

Instead of returning an empty object in that catch block, we could return something like this:

{
    window: {
         matchMedia: () => false,
         ...
    }
}

This would reduce the complexity of the logic you wrote to check if that function exists.

@scofalik scofalik merged commit 0b70608 into release May 16, 2024
7 checks passed
@scofalik scofalik deleted the ck/16368 branch May 16, 2024 18:15
@scofalik
Copy link
Contributor Author

scofalik commented May 16, 2024

I understood but went too far with conclusion. Well, that could have worked for simple methods and properties, but we have a lot of window. calls to many methods and properties. I guess some of them return instances of other DOM classes, and we go deeper and deeper. That's what I meant in my conclusion.

@Reinmar
Copy link
Member

Reinmar commented May 16, 2024

Doh, GitHub lagged to refresh the thread for me and I started commenting in a thread:

https://github.com/ckeditor/ckeditor5/pull/16370/files#r1603635378

I can see 3 4 options:

  • Keep those props as they are now if you don't agree with me that they should be functions.
  • Document them as private for now to change them in the upcoming version, without the need to declare this as a breaking change.
  • Change this ASAP (for v41.4.2) and treat this as a non-breaking-change since they were recently introduced.
  • Document these getters as potentially returning different values over the time.

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.

4 participants