Skip to content

Add processing instruction callbacks - #10

Merged
xoofx merged 1 commit into
xoofx:mainfrom
mamoreau-devolutions:add-processing-instruction-callbacks
Jul 13, 2026
Merged

xoofx merged 1 commit into
xoofx:mainfrom
mamoreau-devolutions:add-processing-instruction-callbacks

Conversation

@mamoreau-devolutions

@mamoreau-devolutions mamoreau-devolutions commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Summary

TurboXml currently recognizes generic processing instructions (PIs) for well-formedness, but silently discards them. This prevents handler-based consumers from preserving unmodeled XML extension content when they need to capture and later reproduce it.

This PR adds a backward-compatible IXmlReadHandler.OnProcessingInstruction callback so consumers can opt in to handling generic PIs.

API and behavior

void OnProcessingInstruction(
    ReadOnlySpan<char> target,
    ReadOnlySpan<char> data,
    int line,
    int column)
  • target is the PI target.
  • data is the exact source text after target and before ?>, including any separator whitespace, tabs, and line endings.
  • A handler can reconstruct the original PI as <?{target}{data}?>.
  • The callback has a default no-op implementation, preserving the existing behavior for handlers that do not need PIs.
  • XML declarations remain exclusively routed to OnXmlDeclaration; they are not reported as generic PIs.
  • The parser continues to reject malformed PIs and reserved xml-like targets according to its existing validation rules.

The callback follows the parser's existing ReadOnlySpan<char> event model, avoiding string allocation in the parser API.

Why this matters

Consumers that retain unknown or extension XML for later serialization can currently lose a PI during parsing even when they preserve the surrounding elements and attributes. Reporting the PI makes lossless capture and round-tripping possible without changing the default behavior for existing consumers.

Tests

Added regression coverage for:

  • exact reconstruction, including whitespace, tabs, and CRLF data;
  • PIs with no data;
  • PIs inside element content;
  • malformed PI rejection; and
  • XML declaration routing remaining unchanged.

Validation

cd src
dotnet build -c Release
dotnet test -c Release

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@xoofx
xoofx merged commit 9ee9ef5 into xoofx:main Jul 13, 2026
1 check passed
@xoofx

xoofx commented Jul 13, 2026

Copy link
Copy Markdown
Owner

Thanks!

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