Skip to content

refactor(measure-text): simplify browser check and improve text measurement logic - #203

Merged
Aarebecca merged 3 commits into
mainfrom
refactor/measure-env
Feb 11, 2026
Merged

Aarebecca merged 3 commits into
mainfrom
refactor/measure-env

Conversation

@Aarebecca

@Aarebecca Aarebecca commented Feb 10, 2026 •

Copy link
Copy Markdown
Contributor

Relative Issue: #199
Update Version: #200

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @Aarebecca, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the text measurement utility to improve its adaptability and accuracy across different execution environments. By simplifying browser detection and enhancing fallback strategies, the changes ensure that text dimensions are reliably calculated, whether using browser-native capabilities or a dedicated measurement library, addressing potential inconsistencies in rendering.

Highlights

  • Simplified Browser Check: The explicit isBrowser utility import and usage have been removed, streamlining the environment detection logic within the text measurement functions.
  • Improved Text Measurement Fallback: The measureText function now includes more robust fallback mechanisms. It checks for document availability before attempting to use browser-specific APIs (canvas and span layout) and falls back to the measury library if browser APIs are unavailable or return invalid dimensions.
  • Enhanced Test Coverage: New unit tests have been added to cover various scenarios, including cases where canvas is available but layout is not, when canvas is unavailable, and when the document object itself is undefined, ensuring the reliability of the updated measurement logic.
Changelog
  • tests/unit/utils/measure-text.test.ts
    • Added new test cases to verify text measurement behavior when canvas is available but layout is not, ensuring correct preference for canvas metrics.
    • Included tests to confirm proper fallback to the measury library when canvas is unavailable and span layout is invalid.
    • Implemented tests to validate fallback to measury when the document object is entirely unavailable, simulating a non-browser environment.
  • src/utils/measure-text.ts
    • Removed the isBrowser utility import, simplifying the dependency tree.
    • Introduced checks for document availability within getCanvasContext and getMeasureSpan to prevent errors in non-browser environments.
    • Added a condition in measureTextInBrowser to return null if getBoundingClientRect reports zero width and height for non-empty content, indicating an invalid measurement.
    • Refactored the main measureText function to remove the explicit isBrowser() check, relying instead on the internal measureTextInBrowser function's environment checks and its null return for fallback.
Activity
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request effectively refactors the text measurement logic by simplifying the browser environment check and improving the fallback mechanism. The changes make the code cleaner and more robust, particularly by handling environments with no layout support (like jsdom) more gracefully. The new tests are thorough and validate the new logic well. I have one suggestion to further optimize the canvas context retrieval to avoid redundant operations in environments where canvas is not supported.

Comment thread src/utils/measure-text.ts Resolved
@codecov-commenter

codecov-commenter commented Feb 10, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 46.18%. Comparing base (d03fde1) to head (c2ad9ad).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #203      +/-   ##
==========================================
+ Coverage   45.95%   46.18%   +0.22%     
==========================================
  Files         338      338              
  Lines       27047    27047              
  Branches     2202     2221      +19     
==========================================
+ Hits        12430    12492      +62     
+ Misses      14606    14544      -62     
  Partials       11       11              
Flag Coverage Δ
infographic 46.18% <100.00%> (+0.22%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/utils/measure-text.ts 83.89% <100.00%> (+52.54%) ⬆️
src/version.ts 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
@Aarebecca
Aarebecca merged commit 5a1a633 into main Feb 11, 2026
3 checks passed
@Aarebecca
Aarebecca deleted the refactor/measure-env branch February 11, 2026 03:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants