Skip to content

fix: set start and end indices on the text inside CDATA nodes - #2043

Open
bardrop wants to merge 2 commits into
fb55:masterfrom
bardrop-oss:fix-cdata-text-indices
Open

bardrop wants to merge 2 commits into
fb55:masterfrom
bardrop-oss:fix-cdata-text-indices

Conversation

@bardrop

@bardrop bardrop commented Nov 16, 2026 •

Copy link
Copy Markdown

With withStartIndices, the text node inside a CDATA node always has startIndex: null.

import { parseDocument } from "htmlparser2";

const doc = parseDocument("<a><![CDATA[x]]></a>", {
    xmlMode: true,
    withStartIndices: true,
    withEndIndices: true,
});
const cdata = doc.children[0].children[0];
console.log(cdata.startIndex, cdata.endIndex); // 3 15
const text = cdata.children[0];
console.log(text.startIndex, text.endIndex); // null 15 (expected 3 15)

oncdatastart creates the text child itself and never passes it through addNode, so the start index is never set. The end index only works because ontext updates endIndex on the last text node. Same with recognizeCDATA in HTML mode.

Fix: copy both indices from the CDATA node to its text child. That matches how a comment's text gets the comment's range.

Test: new fixture 28-cdata-indices.json (runs both char-by-char and in one go). It fails on main (expected null to be 3) and passes with the fix. npm test (vitest + lint) is green.

Found while fuzzing node indices from htmlparser2.

View guided diff

Summary by CodeRabbit

  • Bug Fixes
    • Text inside CDATA sections now reports the same start and end positions as its enclosing CDATA node when index tracking is enabled, in both XML and HTML parsing.
  • Tests
    • Added coverage for CDATA and text-node positions, along with the enclosing element’s positions, in XML and HTML modes.

The text child of a CDATA node is not added via addNode, so with
withStartIndices its startIndex stayed null (endIndex was only set
because ontext updates it). Copy both indices from the CDATA node.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-16T21:55:34.739617Z 3da20ba New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The CDATA text child now receives the start and end indices of its parent CDATA node. XML-mode and HTML-mode fixtures check the indices on the CDATA node, its text child, and the enclosing tag.

Changes: CDATA Text Indices

Layer / File(s) Summary
Copy and verify CDATA indices
src/index.ts, src/__fixtures__/28-cdata-indices.json, src/__fixtures__/29-cdata-indices-html.json
oncdatastart copies the CDATA node’s start and end indices to its text child. The XML and HTML fixtures check that both nodes have indices 3–15 and that the enclosing a tag has indices 0–19.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix

Merge Risk: Minimal · up to 3da20

CDATA text children now carry start and end indices when index tracking is enabled. No merge-blocking risk is apparent.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: the text node inside CDATA nodes now gets start and end indices. The raw summary confirms the src/index.ts change copies the CDATA node's startIndex and endIndex to it…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: the text node inside CDATA nodes now gets start and end indices. The raw summary confirms the src/index.ts change copies the CDATA node's startIndex and endIndex to its text child in oncdatastart, and the fixtures cover this behavior. The title is specific and matches the changeset.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checks the CDATA line,
The child gets indices, start to end.
XML and HTML fixtures agree,
The enclosing tag keeps its span.
I hop past 3 to 15,
And leave the indices in good order.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 7dd1add6-7d47-4458-b0ac-b8f9deda538b
Commits

Reviewing files that changed from the base of the PR and between 4481e04 and 3da20ba.

Files selected for processing: 1

  • src/__fixtures__/29-cdata-indices-html.json

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 0 remain after this review.

@greptile-apps

greptile-apps Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Low impact] The PR appears safe to merge; no actionable issues were found.

Summary

The PR gives CDATA text children the same starting indices as their parent nodes and copies their ending indices.

  • Text inside a CDATA node now shares its source range.

Reviews (2) · Last reviewed commit: "test: cover CDATA indices in HTML mode w..." · Reviewed by Greptile

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 2 files

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread src/__fixtures__/28-cdata-indices.json Resolved

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.

1 participant