AAPT2: Fix quoted text in res/xml assets
AAPT2 aggressively processed xml nodes and regressed from the behavior
of AAPT. This change restores AAPT's processing of xml nodes with
quotations.
Bug: 71805084
Test: Created tests in XmlFlattener_test.cpp and ran the test in the
Android Studio project given in the bug
Change-Id: Idedb4f1002e9fd705ceb5feae35289235b754b02
diff --git a/tools/aapt2/format/binary/XmlFlattener.cpp b/tools/aapt2/format/binary/XmlFlattener.cpp
index d897941..2fe2424 100644
--- a/tools/aapt2/format/binary/XmlFlattener.cpp
+++ b/tools/aapt2/format/binary/XmlFlattener.cpp
@@ -79,23 +79,31 @@
}
void Visit(const xml::Text* node) override {
- if (util::TrimWhitespace(node->text).empty()) {
- // Skip whitespace only text nodes.
+ std::string text = util::TrimWhitespace(node->text).to_string();
+
+ // Skip whitespace only text nodes.
+ if (text.empty()) {
return;
}
+ // Compact leading and trailing whitespace into a single space
+ if (isspace(node->text[0])) {
+ text = ' ' + text;
+ }
+ if (isspace(node->text[node->text.length() - 1])) {
+ text = text + ' ';
+ }
+
ChunkWriter writer(buffer_);
ResXMLTree_node* flat_node = writer.StartChunk<ResXMLTree_node>(RES_XML_CDATA_TYPE);
flat_node->lineNumber = util::HostToDevice32(node->line_number);
flat_node->comment.index = util::HostToDevice32(-1);
- ResXMLTree_cdataExt* flat_text = writer.NextBlock<ResXMLTree_cdataExt>();
-
// Process plain strings to make sure they get properly escaped.
- StringBuilder builder;
- builder.AppendText(node->text);
- AddString(builder.to_string(), kLowPriority, &flat_text->data);
+ text = StringBuilder(true /*preserve_spaces*/).AppendText(text).to_string();
+ ResXMLTree_cdataExt* flat_text = writer.NextBlock<ResXMLTree_cdataExt>();
+ AddString(text, kLowPriority, &flat_text->data);
writer.Finish();
}