Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indent and CDATA blocks #17

Open
etdsoft opened this issue Jun 10, 2021 · 0 comments
Open

Indent and CDATA blocks #17

etdsoft opened this issue Jun 10, 2021 · 0 comments

Comments

@etdsoft
Copy link

etdsoft commented Jun 10, 2021

If you use:

xml_builder = Builder::XmlMarkup.new(indent: 2)
xml_builder.card do |card_builder|
  #...
  card_builder.description do
    card_builder.cdata!(description)
  end
end

And then somewhere along the line add CDATA blocks to your tree, you end up with indented CDATA blocks:

    <card>
      ...
      <description>
        <![CDATA[Welcome to....
]]>
      </description>
    </card>
    </

The problem is that upon parsing the indentation (new line and leading whitespace) becomes part of the text() for the <description> node:

irb> xml.at_xpath('card/description').text
=> "\n        Welcome to

This is because XmlMarkup#cdata! calls #_special which in turns calls _indent.

I suspect indent should be skipped on CDATA sections:
https://bugs.openjdk.java.net/browse/JDK-8223291
nashwaan/xml-js#14

In fact Nokogiri does skip indent for CDATA-only nodes:

irb> doc = Nokogiri::XML("<foo><bar><![CDATA[lorem ipsum]]></foo>")
=> #<Nokogiri::XML::Document:0xe09c name="document" children=[#<Nokogiri::XML::Element:0xe088 name="foo" children=[#<Nokogiri::XML::Element:0xe074 name="bar" children=[#<Noko...
irb> puts doc
<?xml version="1.0"?>
<foo>
  <bar><![CDATA[lorem ipsum]]></bar>
</foo>

Is this a bug or a feature? 🤔

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

No branches or pull requests

1 participant