Skip to content

Boost: Fix is_opened_script() regex to interpolate ignore attribute#47847

Open
kraftbj wants to merge 5 commits intotrunkfrom
boost-versioning-issues
Open

Boost: Fix is_opened_script() regex to interpolate ignore attribute#47847
kraftbj wants to merge 5 commits intotrunkfrom
boost-versioning-issues

Conversation

@kraftbj
Copy link
Copy Markdown
Contributor

@kraftbj kraftbj commented Mar 30, 2026

See #47744

Proposed changes

  • Fix is_opened_script() regex in the Render Blocking JS module — the pattern had literal %s placeholders instead of being interpolated via sprintf(), making the negative lookahead for ignored scripts dead code.

Other information

  • Changelog entry included.

Does this pull request change what data or activity we track or use?

No.

Testing instructions

  • Review the diff — the fix mirrors the existing sprintf() + preg_quote() pattern used in get_script_tags() on line 217 of the same file.
  • Run Boost PHP tests: jp test php plugins/boost — all 102 tests pass.

The regex in is_opened_script() had literal %s placeholders instead of
being interpolated via sprintf(), making the negative lookahead dead
code. This meant ignored scripts were counted as open scripts, which
could cause the output buffer to hold content unnecessarily.
@kraftbj kraftbj added the [Status] Needs Review This PR is ready for review. label Mar 30, 2026
@kraftbj kraftbj self-assigned this Mar 30, 2026
@github-actions github-actions bot added [Boost Feature] Defer JS issues related to the Defer JS feature in Jetpack Boost [Plugin] Boost A feature to speed up the site and improve performance. labels Mar 30, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 30, 2026

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!


Boost plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

@jp-launch-control
Copy link
Copy Markdown

jp-launch-control bot commented Mar 30, 2026

Code Coverage Summary

Coverage changed in 1 file.

File Coverage Δ% Δ Uncovered
projects/plugins/boost/app/modules/optimizations/render-blocking-js/class-render-blocking-js.php 6/82 (7.32%) 4.79% -1 💚

Full summary · PHP report · JS report

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a bug in Jetpack Boost’s Render Blocking JS optimization where is_opened_script() attempted to exclude “ignored” scripts but used a regex with uninterpolated %s placeholders, making the negative lookahead ineffective.

Changes:

  • Interpolate the ignore-attribute/value into the is_opened_script() regex via sprintf() + preg_quote().
  • Add a Boost changelog fragment describing the fix.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
projects/plugins/boost/app/modules/optimizations/render-blocking-js/class-render-blocking-js.php Fixes the ignore-attribute negative lookahead in is_opened_script() by properly interpolating the regex.
projects/plugins/boost/changelog/boost-versioning-issues Adds a changelog entry for the Render Blocking JS fix.

Comment on lines 350 to +354
public function is_opened_script( $buffer ) {
$opening_tags_count = preg_match_all( '~<\s*script(?![^>]*%s="%s")([^>]*)>~', $buffer );
$opening_tags_count = preg_match_all(
sprintf( '~<\s*script(?![^>]*%s="%s")([^>]*)>~', preg_quote( $this->ignore_attribute, '~' ), preg_quote( $this->ignore_value, '~' ) ),
$buffer
);
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

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

There are no PHPUnit tests covering the Render Blocking JS optimization or is_opened_script() behavior (including the ignored-script negative lookahead). Since this regex is easy to regress and directly affects output buffering behavior, add a focused unit test to assert that ignored scripts are not counted as “opened” (and that non-ignored partial scripts are).

Copilot uses AI. Check for mistakes.
kraftbj and others added 2 commits March 30, 2026 12:58
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Replace the Copilot-suggested regex with the established pattern from
get_script_tags() — using named group + backreference for quote
matching, requiring =value to be present. The Copilot version made
=value optional, which would have excluded bare attributes.

Add 11 unit tests for is_opened_script() covering empty buffers,
matched/unmatched tags, ignored scripts with double/single/no quotes,
wrong values, bare attributes, and mixed scenarios.
kraftbj added 2 commits March 30, 2026 14:16
ReflectionProperty::setAccessible() has been a no-op since PHP 8.1
and is deprecated in PHP 8.5. Private properties are accessible via
reflection without it.
setAccessible() is required for private property access before PHP 8.1,
a no-op from 8.1-8.4, and deprecated in 8.5. Guard the calls so they
only run where needed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Boost Feature] Defer JS issues related to the Defer JS feature in Jetpack Boost [Plugin] Boost A feature to speed up the site and improve performance. [Status] Needs Review This PR is ready for review. [Tests] Includes Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants