Skip to content

Editing an element and generating XML files using the {attribute_quote: :quote} wrongly escapes quotes in attributes #92

@edouard

Description

@edouard
require 'rexml'

xml = <<~XML
<?xml version="1.0" encoding="UTF-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 http://docs.oasis-open.org/xliff/v1.2/os/xliff-core-1.2-strict.xsd">
  <file original="test.plist" source-language="en" datatype="plaintext" target-language="en">
    <header>
      <tool tool-id="com.apple.dt.xcode" tool-name="Xcode" tool-version="8.3.3" build-num="8E3004b" />
    </header>
    <body>
      <trans-unit id="test">
        <source>test</source>
        <target></target>
      </trans-unit>
      <trans-unit id="We're happy to see you">
        <source>We're happy to see you</source>
        <target></target>
      </trans-unit>
    </body>
  </file>
</xliff>
XML

@doc = REXML::Document.new(xml)
REXML::XPath.first(@doc, '//trans-unit').attributes['id'] = "I'm here"
puts @doc.to_s
<?xml version='1.0' encoding='UTF-8'?>
<xliff xsi:schemaLocation='urn:oasis:names:tc:xliff:document:1.2 http://docs.oasis-open.org/xliff/v1.2/os/xliff-core-1.2-strict.xsd' version='1.2' xmlns='urn:oasis:names:tc:xliff:document:1.2' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
  <file datatype='plaintext' original='test.plist' source-language='en' target-language='en'>
    <header>
      <tool build-num='8E3004b' tool-id='com.apple.dt.xcode' tool-name='Xcode' tool-version='8.3.3'/>
    </header>
    <body>
      <trans-unit id='I&apos;m here'>
        <source>test</source>
        <target/>
      </trans-unit>
      <trans-unit id='We&apos;re happy to see you'>
        <source>We're happy to see you</source>
        <target/>
      </trans-unit>
    </body>
  </file>
</xliff>

All good! ✅

When using {attribute_quote: :quote} to generate files with double quoted attributes, not editing anything there:

@doc = REXML::Document.new(xml, {attribute_quote: :quote})
puts @doc.to_s
<?xml version='1.0' encoding='UTF-8'?>
<xliff xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 http://docs.oasis-open.org/xliff/v1.2/os/xliff-core-1.2-strict.xsd" version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <file datatype="plaintext" original="test.plist" source-language="en" target-language="en">
    <header>
      <tool build-num="8E3004b" tool-id="com.apple.dt.xcode" tool-name="Xcode" tool-version="8.3.3"/>
    </header>
    <body>
      <trans-unit id="test">
        <source>test</source>
        <target/>
      </trans-unit>
      <trans-unit id="We're happy to see you">
        <source>We're happy to see you</source>
        <target/>
      </trans-unit>
    </body>
  </file>
</xliff>

All good too! ✅

But if I edit a trans-unit:

@doc = REXML::Document.new(xml, {attribute_quote: :quote})
REXML::XPath.first(@doc, '//trans-unit').attributes['id'] = "I'm here"
puts @doc.to_s
<?xml version='1.0' encoding='UTF-8'?>
<xliff xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 http://docs.oasis-open.org/xliff/v1.2/os/xliff-core-1.2-strict.xsd" version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <file datatype="plaintext" original="test.plist" source-language="en" target-language="en">
    <header>
      <tool build-num="8E3004b" tool-id="com.apple.dt.xcode" tool-name="Xcode" tool-version="8.3.3"/>
    </header>
    <body>
      <trans-unit id="I&apos;m here">
        <source>test</source>
        <target/>
      </trans-unit>
      <trans-unit id="We're happy to see you">
        <source>We're happy to see you</source>
        <target/>
      </trans-unit>
    </body>
  </file>
</xliff>

(Note the <trans-unit id="I&apos;m here">). It should be <trans-unit id="I'm here">. The element we edited has its quote incorrectly HTML-escaped, while the one we didn't edit is correctly unescaped.

It looks like editing an element makes it loose its context[:attribute_quote:].

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions