Skip to content

Processing Instruction: Serialize with "?>" close#12654

Merged
annevk merged 10 commits into
whatwg:mainfrom
sirreal:sirreal/pi-serialization
Jul 9, 2026
Merged

Processing Instruction: Serialize with "?>" close#12654
annevk merged 10 commits into
whatwg:mainfrom
sirreal:sirreal/pi-serialization

Conversation

@sirreal

@sirreal sirreal commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Processing Instructions are currently serialized as "<?" + target + " " + data + ">":

Append "<?" (U+003C LESS-THAN SIGN, U+003F QUESTION MARK), followed by the value of current node's target IDL attribute, followed by a single U+0020 SPACE character, followed by the value of current node's data, followed by a single U+003E GREATER-THAN SIGN character (>).

If serialized in this way, data ending in ? would result in different data on next parse:

<?pi ??><!-- target: "pi"; data: "?" -->
<!-- serializes to: -->
<?pi ?><!-- target: "pi"; data: "" -->

This is resolved be serializing with a final ?> instead of >. This should preserve arbitrary PI data correctly. This idea was already considered: #12118 (comment)

Benefits:

  • It aligns with existing XML processing instructions and expectations.
  • Avoids an edge case with PI data.

Drawbacks:

  • An extra character that's redundant in most cases.

See related tests: web-platform-tests/wpt#57486

(See WHATWG Working Mode: Changes for more details.)


/dynamic-markup-insertion.html ( diff )
/infrastructure.html ( diff )
/parsing.html ( diff )

@sirreal

sirreal commented Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

I've done a quick survey of some major browsers using the following:

const doc = document.createDocumentFragment();
doc.append( document.createElement( 'p' ) );
doc.firstChild.append( document.createProcessingInstruction( 't', 'd' ) );
doc.firstChild.outerHTML
  • Chrome 150: <p><?t d?></p>
  • Firefox 152: <p><?t d></p>
  • Safari 26.5: <p><?t d?></p>

@annevk
annevk requested a review from noamr July 8, 2026 12:57
@annevk

annevk commented Jul 8, 2026

Copy link
Copy Markdown
Member

This seems like a change we should make. We don't want <?test ??????> to slowly lose all the question marks through dances of parsing and serialization.

Comment thread source Outdated
by the value of <var>current node</var>'s <span data-x="concept-cd-data">data</span>,
followed by a single U+003E GREATER-THAN SIGN character (>).</p>
followed by the literal string "<code data-x="">?&gt;</code>" (U+003F QUESTION MARK,
U+003E GREATER-THAN SIGN).</p>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe we can take this opportunity to clean this up a bit. For strings we don't have to state the code points afterwards.

Instead of referencing the IDL attribute we want to reference https://dom.spec.whatwg.org/#concept-pi-target.

We don't have to say U+0020 SPACE character. We can just say U+0020 SPACE.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I updated the DOM reference for PI target. There was an existing example I reused from elsewhere.


The section was inconsistent and needlessly verbose, especially compared with the preceding shadow host rules that exclusively uses quoted strings instead of mentioning Unicode codepoints. I've preferred the string form every except for isolated U+0020 SPACE.

The change is more involved, but I think the result is cleaner, clearer, and more consistent. The resulting DocumentType serialization seems particularly improved:

-Append "<!DOCTYPE" (U+003C LESS-THAN SIGN, U+0021 EXCLAMATION MARK, U+0044 LATIN CAPITAL LETTER D, U+004F LATIN CAPITAL LETTER O, U+0043 LATIN CAPITAL LETTER C, U+0054 LATIN CAPITAL LETTER T, U+0059 LATIN CAPITAL LETTER Y, U+0050 LATIN CAPITAL LETTER P, U+0045 LATIN CAPITAL LETTER E), followed by a space (U+0020 SPACE), followed by the value of current node's name, followed by ">" (U+003E GREATER-THAN SIGN).
+Append "<!DOCTYPE ", followed by the value of current node's name, and "?>".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I notice one related point in the DOM features section. PI interface and PI target are listed separatedly, while other interfaces are on the same bullet point. Shall I combine them?

This could be:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, this made me realize we had the wrong link (also in another place). Fixed.

@noamr

noamr commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Yea the arguments here are good. Consider blink interested.

Comment thread source Outdated

@annevk annevk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@annevk

annevk commented Jul 8, 2026

Copy link
Copy Markdown
Member

@foolip can you update your test PR to match this change?

@foolip

foolip commented Jul 9, 2026

Copy link
Copy Markdown
Member

I've updated web-platform-tests/wpt#57486 to match now.

annevk pushed a commit to web-platform-tests/wpt that referenced this pull request Jul 9, 2026
For whatwg/html#12654.

Co-authored-by: Jon Surrell <sirreal@users.noreply.github.com>
@annevk
annevk merged commit 2e477cf into whatwg:main Jul 9, 2026
2 checks passed
@sirreal
sirreal deleted the sirreal/pi-serialization branch July 9, 2026 11:19
lando-worker Bot pushed a commit to mozilla-firefox/firefox that referenced this pull request Jul 12, 2026
…tions in HTML, a=testonly

Automatic update from web-platform-tests
Test serialization of processing instructions in HTML

For whatwg/html#12654.

Co-authored-by: Jon Surrell <sirreal@users.noreply.github.com>
--

wpt-commits: 982cb52d1f024036bfe8722ee63e9ad869b5db24
wpt-pr: 57486
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

4 participants