Files
reverse-engineering-anything/src/android/AndroidManifestXml.ts
T
morluto 61af8abeb6 fix(android): share decoded Android XML admission (#12158)
* fix(android): share decoded manifest root and package admission

Refs #12139

* fix(apktool): keep foreign XML out of decoded string entries

Refs #12139
2026-11-19 11:07:25 +08:00

40 lines
1.7 KiB
TypeScript

import type { Attr, Element, Node } from "@xmldom/xmldom";
import type { AndroidManifestLocation } from "../domain/android/androidManifestComponents.js";
import { XML_LINE_ENDING } from "../domain/xmlDocument.js";
const ANDROID_NAMESPACE = "http://schemas.android.com/apk/res/android";
export const childNodes = (element: Element): readonly Node[] =>
Array.from(element.childNodes);
/** Namespace identity, never the spelling of a bound prefix, selects a fact. */
export const isAndroidAttribute = (attribute: Attr): boolean =>
attribute.namespaceURI === ANDROID_NAMESPACE ||
(attribute.namespaceURI === null && attribute.prefix === "android");
export const androidAttribute = (
element: Element,
localName: string,
): string | null => {
for (let index = 0; index < element.attributes.length; index += 1) {
const attribute = element.attributes.item(index);
if (attribute === null || attribute.localName !== localName) continue;
if (isAndroidAttribute(attribute)) return attribute.nodeValue;
}
return null;
};
/** Build the source line index once; all locations address the same retained text. */
export const createManifestLocationReader = (source: string) => {
const starts = [0];
for (const match of source.matchAll(XML_LINE_ENDING))
starts.push(match.index + match[0].length);
return (element: Element): AndroidManifestLocation | null => {
const line = element.lineNumber;
const column = element.columnNumber;
if (line === undefined || column === undefined || line < 1 || column < 1)
return null;
const start = starts[line - 1];
if (start === undefined) return null;
const offset = start + column - 1;
return source[offset] === "<" ? { line, column, offset } : null;
};
};