Skip to content

HTML API: Correct the output comment in the class_list() example. - #15686

Open
sirreal wants to merge 2 commits into
WordPress:trunkfrom
sirreal:html-api/class-list-example-output
Open

sirreal wants to merge 2 commits into
WordPress:trunkfrom
sirreal:html-api/class-list-example-output

Conversation

@sirreal

@sirreal sirreal commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

The class_list() example prints each class name followed by a space. Its output comment wrapped that output in quotes, and a // Outputs: comment states the rest of its line as the output, so the Code Reference shows quotes the code never prints. The example now prints one class name per line, and the multiline output comment states that output as plain text, with a final empty // line for the trailing newline.

Output comment Stated output Printed output
// Outputs: "free <egg> lang-en " (trunk) '"free <egg> lang-en "' 'free <egg> lang-en '
// Outputs:, then // free, // <egg>, // lang-en, // "free\n<egg>\nlang-en\n" "free\n<egg>\nlang-en\n"

Stated outputs are what phpdoc-parser at WordPress/phpdoc-parser@a1ba4bd extracts; printed outputs are from running each example.

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

Use of AI Tools

AI assistance: Yes
Tools: Claude Code
Models: Claude Opus 5.5
Used for: Finding the incorrect comment, the change, its verification, and drafting this description.


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.

The example prints each class name followed by a space, so its output has no quotes and ends with a space. The output comment wrapped that output in quotes, so the Code Reference showed output the code never prints. The comment now states the output as a JSON-encoded string, which shows the trailing space.
@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

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

Props jonsurrell, motylanogha.

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


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@sirreal

sirreal commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

I will probably tweak the expected output, I'm not excited about the JSON encoded version just to protect the trailing space. Open to suggestions.

@sirreal
sirreal marked this pull request as draft November 6, 2026 12:42
A JSON-encoded output comment is harder to read than the output it states. Printing each class name on its own line lets the multiline output comment state the output as plain text, with a final empty line for the trailing newline.
@sirreal
sirreal marked this pull request as ready for review November 6, 2026 18:02
@wppoland

wppoland commented Nov 8, 2026

Copy link
Copy Markdown

One clean alternative that avoids both the trailing space issue and JSON encoding is converting to an array or joining the yielded classes:

$classes = iterator_to_array( $p->class_list() );
// [ 'free', '<egg>', 'lang-en' ]

Or if joining them with a comma/delimiter:

echo implode( ', ', iterator_to_array( $p->class_list() ) );
// Outputs: free, <egg>, lang-en

Alternatively, if keeping the foreach loop in the example is preferred (to demonstrate that class_list() is a generator/iterable), collecting and outputting them comma-separated avoids trailing space and doesn't require an empty // line:

$names = array();
foreach ( $p->class_list() as $class_name ) {
    $names[] = $class_name;
}
echo implode( ', ', $names );
// Outputs: free, <egg>, lang-en

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.

2 participants