mirror of
https://github.com/morluto/rea.git
synced 2026-12-05 14:31:22 +00:00
refactor(android): share decoded string child traversal (#16743)
This commit is contained in:
1 parent
f47c701b15
commit
3a7984c418
1 file changed
+26
-41
@@ -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)) {
|
||||
|
||||
Reference in new issue
Block a user