Skip to content

fix(php) end one-line comments at the closing ?> tag - #4572

Open
clark719664 wants to merge 1 commit into
highlightjs:mainfrom
clark719664:fix/php-line-comment-closing-tag
Open

clark719664 wants to merge 1 commit into
highlightjs:mainfrom
clark719664:fix/php-line-comment-closing-tag

Conversation

@clark719664

Copy link
Copy Markdown

Resolves #4569

Changes

In PHP a // or # comment ends at the end of the line or at the closing ?> tag, whichever comes first. php.js used the generic C_LINE_COMMENT_MODE / HASH_COMMENT_MODE (and one COMMENT('//', '$')), which run to the end of the line. In php-template the closing tag is the end lexeme of the PHP block, so it is handed to the php sub-language together with the comment and swallowed by it: ?> lost its meta scope, and anything after it on the same line was highlighted as PHP instead of markup. This is common in WordPress templates (// phpcs:ignore ?>).

  • php.js now defines its own line-comment modes whose end is /$|(?=\?>)/ and uses them in all five places the generic modes were used (top level, the attribute and SUBST contents). Block comments are untouched, and #[...] attributes still win over # comments as before.
  • New markup test test/markup/php-template/comment-closing-tag covering // and # comments before ?>, a quoted string inside the comment, and a comment with no closing tag on its line.
  • CHANGES.md entry.

Verification (Node 24, Windows):

  • npm run build && mocha test: the new should markup comment-closing-tag test fails on main (comment span contains ?>, no hljs-meta for the tag) and passes with this change; the full suite is 1651 passing, 3 pending.
  • The reproduction from the issue now produces <span class="hljs-comment">// note </span><span class="hljs-meta">?&gt;</span> and highlighting returns to markup right after the tag.
  • eslint src/languages/php.js reports the same 2 errors / 4 warnings as on main (pre-existing object-property-newline on the PREPROCESSOR variants); no new findings.

Assisted-by: Claude Fable 5.1 (high). This change was developed with an AI coding assistant (Claude Code) and reviewed by Clark.

Checklist

  • Added markup tests, or they don't apply here because...
  • I have read and followed our AI-assisted contributions policy (human review, no slop)

🤖 Generated with Claude Code

A `//` or `#` comment in PHP ends at the end of the line or at `?>`,
whichever comes first. The grammar used the generic line-comment modes,
which run to the end of the line, so in `php-template` a comment right
before the closing tag swallowed `?>` and the tag lost its `meta` scope
while the markup after it was still treated as PHP.

Use comment modes whose end is either end of line or a lookahead for `?>`
everywhere php.js used the generic ones, so the tag is highlighted and
the template switches back to markup as before.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Assisted-by: Claude Fable 5.1 (high)

This branch has not been deployed

No deployments
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.

(PHP Template) A ?> after a // comment is highlighted as part of the comment

1 participant