Skip to content

Support ]]> escaping in dat method #230

Description

@ktmouk

Is your feature request related to a problem? Please describe.
Currently, calling .dat() with a string containing ]]> throws a "The string contains invalid characters." error. This requires developers to implement a custom function that split the CDATA section manually (e.g., before]]>after<![CDATA[before]]><![CDATA[]]]]><![CDATA[>]]><![CDATA[after]]>).

While it's unlikely that a string like ]]> would appear in typical content, defensive escaping is necessary when converting user-generated content into RSS feeds.

Describe the solution you'd like
The .dat() method should automatically split ]]> sequences into separate CDATA sections, rather than throwing an error. Alternatively, a dedicated method like .escapedDat() that handles ]]> escaping would also work.

Describe alternatives you've considered
As a workaround, I'm currently splitting the string at every ]]> occurrence and calling .dat() multiple times to produce separate CDATA sections:

const addEscapedDat = (node, text) => {
  for (const [i, part] of text.split(']]>').entries()) {
    if (i > 0) {
      node.dat(']]')
      node.dat('>')
    }
    node.dat(part)
  }
  return node
}

Additional context
Add any other context about the feature request here.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions