From 3a7984c4189fde60dba20d395da486d2ee6ede37 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Fri, 4 Dec 2026 21:49:57 +0800 Subject: [PATCH] refactor(android): share decoded string child traversal (#16743) --- src/apktool/ApktoolDecodeOutput.ts | 67 ++++++++++++------------------ 1 file changed, 26 insertions(+), 41 deletions(-) diff --git a/src/apktool/ApktoolDecodeOutput.ts b/src/apktool/ApktoolDecodeOutput.ts index 99023f7a8fe..4a577dff95f 100644 --- a/src/apktool/ApktoolDecodeOutput.ts +++ b/src/apktool/ApktoolDecodeOutput.ts @@ -1,7 +1,8 @@ -import type { Node } from "@xmldom/xmldom"; +import type { Element, Node } from "@xmldom/xmldom"; import { parseDecodedAndroidManifest } from "../domain/android/decodedAndroidManifest.js"; import { isUnqualifiedXmlElement, + iterateXmlChildElements, parseXmlDocument, type XmlParseFailure, } from "../domain/xmlDocument.js"; @@ -219,41 +220,32 @@ export const parseDecodedStrings = ( const entries: DecodedStringEntry[] = []; let unparsedCount = 0; const offsets = sourceLineOffsets(stringsXml); - for (const child of childNodes(root)) { - if (!isUnqualifiedXmlElement(child) || child.localName !== "string") - continue; - const name = child.getAttributeNS(null, "name"); - if (name === null || name.length === 0) { - unparsedCount += 1; - continue; - } - const content = childNodes(child); - if (content.some((node) => node.nodeType !== 3 && node.nodeType !== 8)) { - unparsedCount += 1; - continue; - } - if (content.every((node) => node.nodeType === 8)) { - entries.push({ name, value: "" }); - continue; - } - const value = content - .filter((node) => node.nodeType === 3) - .map((node) => { - const start = sourceOffset(node, offsets); - if (start === undefined) return null; - // A literal '<' cannot appear in well-formed XML text. Ending here - // also excludes comments after the document element from this value. - const end = stringsXml.indexOf("<", start); - if (end < start) return null; - return stringsXml.slice(start, end); - }); - if (value.some((part) => part === null)) { - unparsedCount += 1; - continue; - } - entries.push({ name, value: value.join("") }); + for (const child of iterateXmlChildElements(root, "string", "")) { + const entry = readEntry(child); + if (entry === null) unparsedCount += 1; + else entries.push(entry); } return ok({ entries, unparsedCount }); + + function readEntry(child: Element): DecodedStringEntry | null { + const name = child.getAttributeNS(null, "name"); + if (name === null || name.length === 0) return null; + for (let node = child.firstChild; node !== null; node = node.nextSibling) { + if (node.nodeType !== 3 && node.nodeType !== 8) return null; + } + const parts: string[] = []; + for (let node = child.firstChild; node !== null; node = node.nextSibling) { + if (node.nodeType !== 3) continue; + const start = sourceOffset(node, offsets); + if (start === undefined) return null; + // A literal '<' cannot appear in well-formed XML text. Ending here + // also excludes comments after the document element from this value. + const end = stringsXml.indexOf("<", start); + if (end < start) return null; + parts.push(stringsXml.slice(start, end)); + } + return { name, value: parts.join("") }; + } }; const yamlPackageName = ( @@ -278,13 +270,6 @@ const yamlScalar = ( : source.slice(range[0], range[1]); }; -const childNodes = (node: Node): Node[] => { - const children: Node[] = []; - for (let child = node.firstChild; child !== null; child = child.nextSibling) - children.push(child); - return children; -}; - const sourceLineOffsets = (source: string): readonly number[] => { const offsets = [0]; for (const match of source.matchAll(/\r\n?|\n/gu)) {