Skip to content

Conversation

Copy link

Copilot AI commented Dec 16, 2025

Adds comprehensive test coverage for is_javascript_script_tag() and is_json_script_tag() methods introduced in the html-api/auto-escape-javascript-json branch.

Test Coverage

is_javascript_script_tag() (89 test cases)

  • Script tags without type/language attributes (default JavaScript behavior)
  • Empty and boolean attribute values
  • Falsy non-empty strings (e.g., "0") for type and language attributes
  • All 20 JavaScript MIME types per HTML/WHATWG specs (application/ecmascript, application/javascript, text/javascript*, text/jscript, text/livescript, etc.)
  • Case-insensitive MIME type matching
  • Module type scripts
  • Whitespace trimming (ASCII whitespace: space, tab, form feed, CR, LF)
  • Language attribute fallback (text/{language})
  • Non-JavaScript types (JSON, importmap, speculationrules, unknown MIME types)
  • Non-script elements
  • Edge cases (text node positioning, SVG namespace)

is_json_script_tag() (29 test cases)

  • JSON MIME types (application/json, text/json)
  • Special JSON script types (importmap, speculationrules)
  • Case-insensitive matching
  • Whitespace trimming
  • Falsy non-empty strings (e.g., "0") for type attribute
  • Non-JSON types (JavaScript, module, unknown)
  • Non-script elements
  • Edge cases (empty/boolean type attribute, text node positioning, SVG namespace)

Implementation

Test file: tests/phpunit/tests/html-api/wpHtmlTagProcessorScriptTag.php

Uses data providers for maintainability. Follows WordPress test conventions with:

  • Proper PHPDoc annotations
  • Type declarations for test method parameters (string, bool)
  • Text node positioning using next_token() for edge case tests
  • Explicit assertions that processor is positioned on SCRIPT tag in main tests
  • Accurate assertion messages reflecting test conditions
  • All tests associated with ticket 64419
  • Coverage for PHP edge cases (falsy non-empty strings like "0")

Trac ticket: https://core.trac.wordpress.org/ticket/64419


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Original prompt

This section details on the original issue you should resolve

<issue_title>Add unit tests for is_javascript_script_tag function</issue_title>
<issue_description>The html-api/auto-escape-javascript-json branch adds some new methods: is_javascript_script_tag and is_json_script_tag.

They're well documented referencing the HTML standard.

Create a PR against that branch to add unit tests for these two new methods.

The unit tests should be in a new test file and class:

tests/phpunit/tests/html-api/wpHtmlTagProcessorScriptTag.php</issue_description>

<agent_instructions>Be sure to file all test file, class, PHPdoc, and other relevant conventions when creating test suites and cases.

Use data providers where appropriate.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits December 16, 2025 19:48
…thods

Co-authored-by: sirreal <841763+sirreal@users.noreply.github.com>
Copilot AI changed the title [WIP] Add unit tests for is_javascript_script_tag function Add unit tests for is_javascript_script_tag and is_json_script_tag methods Dec 16, 2025
Copilot AI requested a review from sirreal December 16, 2025 19:59
@sirreal sirreal changed the base branch from trunk to html-api/auto-escape-javascript-json December 16, 2025 20:15
* @param string $html HTML containing a script tag.
* @param bool $expected_result Whether the script tag should be identified as JSON.
*/
public function test_is_json_script_tag( $html, $expected_result ) {
Copy link
Owner

Choose a reason for hiding this comment

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

@copilot Add types to the test parameters

Copy link
Author

Choose a reason for hiding this comment

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

Added type declarations to test method parameters in a86cf86.

Comment on lines 119 to 124
$processor = new WP_HTML_Tag_Processor( '<script type="text/javascript"></script>' );

$this->assertFalse(
$processor->is_javascript_script_tag(),
'Should return false when no tag has been selected'
);
Copy link
Owner

Choose a reason for hiding this comment

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

@copilot This is a good test, but just make it a text node.

Copy link
Author

Choose a reason for hiding this comment

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

Changed to use text node in 697881c. Also updated the similar test for is_json_script_tag for consistency.

Co-authored-by: sirreal <841763+sirreal@users.noreply.github.com>
Co-authored-by: sirreal <841763+sirreal@users.noreply.github.com>
* @covers WP_HTML_Tag_Processor::is_json_script_tag
*/
public function test_is_json_script_tag_returns_false_before_finding_tags() {
$processor = new WP_HTML_Tag_Processor( 'Just some text' );
Copy link
Owner

Choose a reason for hiding this comment

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

@copilot this needs to call next_token to reach the text node

Copy link
Author

Choose a reason for hiding this comment

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

Added next_token() call in 727c090. Also updated the similar test for is_javascript_script_tag for consistency.

* @covers WP_HTML_Tag_Processor::is_javascript_script_tag
*/
public function test_is_javascript_script_tag_returns_false_before_finding_tags() {
$processor = new WP_HTML_Tag_Processor( 'Just some text' );
Copy link
Owner

Choose a reason for hiding this comment

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

@copilot this needs to call next_token to reach the text node

Copy link
Author

Choose a reason for hiding this comment

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

Already addressed in 727c090 - next_token() is present.


$this->assertFalse(
$processor->is_javascript_script_tag(),
'Should return false when no tag has been selected'
Copy link
Owner

Choose a reason for hiding this comment

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

@copilot "when not stopped on script tag" - same in the JSON test.

Co-authored-by: sirreal <841763+sirreal@users.noreply.github.com>
$processor = new WP_HTML_Tag_Processor( $html );
$processor->next_tag();

$this->assertSame(
Copy link
Owner

Choose a reason for hiding this comment

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

@copilot let's add an assertion here and in the JSON test that the processor is on a SCRIPT tag.

Copy link
Author

Choose a reason for hiding this comment

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

Added SCRIPT tag assertions in e5476e2.

Copilot AI requested a review from sirreal December 17, 2025 10:12
class Tests_HtmlApi_WpHtmlTagProcessorScriptTag extends WP_UnitTestCase {

/**
* @ticket TBD
Copy link
Owner

Choose a reason for hiding this comment

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

@copilot associate all the new tests with ticket 64419

Suggested change
* @ticket TBD
* @ticket 64419

Copy link
Author

Choose a reason for hiding this comment

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

Updated all ticket references to 64419 in 638925a.

Co-authored-by: sirreal <841763+sirreal@users.noreply.github.com>
* @coversDefaultClass WP_HTML_Tag_Processor
*/
class Tests_HtmlApi_WpHtmlTagProcessorScriptTag extends WP_UnitTestCase {

Copy link
Owner

Choose a reason for hiding this comment

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

@copilot remove the empty newline

Suggested change

Copy link
Author

Choose a reason for hiding this comment

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

Removed empty line in 638925a.

Co-authored-by: sirreal <841763+sirreal@users.noreply.github.com>
@sirreal
Copy link
Owner

sirreal commented Dec 19, 2025

@copilot add some test cases where the relevant attribute values are non-empty strings that are treated by PHP as falsy.

Co-authored-by: sirreal <841763+sirreal@users.noreply.github.com>
Copy link
Author

Copilot AI commented Dec 19, 2025

@copilot add some test cases where the relevant attribute values are non-empty strings that are treated by PHP as falsy.

Added test cases for the string "0" (PHP's falsy non-empty string) in 6d34c8a. Added cases for both type="0" and language="0" attributes in is_javascript_script_tag tests, and type="0" in is_json_script_tag tests.

@sirreal sirreal marked this pull request as ready for review December 19, 2025 12:15
@github-actions
Copy link

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @copilot.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

Core Committers: Use this line as a base for the props when committing in SVN:

Props jonsurrell, dmsnell.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@sirreal sirreal merged commit 1362451 into html-api/auto-escape-javascript-json Dec 19, 2025
8 checks passed
@sirreal sirreal deleted the copilot/add-unit-tests-for-javascript-tag branch December 19, 2025 12:35
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.

Add unit tests for is_javascript_script_tag function

3 participants