-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathplist-parser.ts
More file actions
162 lines (136 loc) · 4.28 KB
/
Copy pathplist-parser.ts
File metadata and controls
162 lines (136 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import {DOMParser, type Element, Node} from '@xmldom/xmldom';
import {getLogger} from '../logger.js';
import type {PlistArray, PlistDictionary, PlistValue} from '../types.js';
import {PlistService} from './plist-service.js';
import {
cleanXmlWithReplacementChar,
ensureString,
findFirstReplacementCharacter,
fixMultipleXmlDeclarations,
hasUnicodeReplacementCharacter,
isValidXml,
removeExtraContentAfterPlist,
trimBeforeXmlDeclaration,
} from './utils.js';
const errorLog = getLogger('PlistError');
/**
* Parses an XML plist string into a JavaScript object
*
* @param xmlData - XML plist data as string or Buffer
* @returns Parsed JavaScript object
*/
export function parsePlist(xmlData: string | Buffer): PlistDictionary {
let xmlStr = ensureString(xmlData);
xmlStr = trimBeforeXmlDeclaration(xmlStr);
if (hasUnicodeReplacementCharacter(xmlStr)) {
const badCharPos = findFirstReplacementCharacter(xmlStr);
xmlStr = cleanXmlWithReplacementChar(xmlStr, badCharPos);
}
if (!isValidXml(xmlStr)) {
if (PlistService.isVerboseErrorLoggingEnabled()) {
errorLog.debug(`Invalid XML: missing root element - XML content: ${xmlStr.substring(0, 200)}...`);
}
throw new Error('Invalid XML: missing root element or malformed XML');
}
xmlStr = fixMultipleXmlDeclarations(xmlStr);
xmlStr = removeExtraContentAfterPlist(xmlStr);
const parser = new DOMParser({
errorHandler(level, message) {
if (level === 'fatalError') {
throw new Error(`Fatal XML parsing error: ${message}`);
}
return true;
},
});
const doc = parser.parseFromString(xmlStr, 'text/xml');
if (!doc) {
throw new Error('Invalid XML response');
}
const plistElements = doc.getElementsByTagName('plist');
if (plistElements.length === 0) {
throw new Error('No plist element found in XML');
}
const rootDict = doc.getElementsByTagName('dict')[0];
if (!rootDict) {
return {};
}
return parseDict(rootDict);
/**
* Parse a plist XML node into its corresponding JavaScript value.
*/
function parseNode(node: Element): PlistValue {
if (!node) {
return null;
}
switch (node.nodeName) {
case 'dict':
return parseDict(node);
case 'array':
return parseArray(node);
case 'string':
return node.textContent || '';
case 'integer':
return parseInt(node.textContent || '0', 10);
case 'real':
return parseFloat(node.textContent || '0');
case 'true':
return true;
case 'false':
return false;
case 'date':
return new Date(node.textContent || '');
case 'data':
if (!node.textContent) {
return null;
}
try {
return Buffer.from(node.textContent, 'base64');
} catch {
return node.textContent;
}
default:
return node.textContent || null;
}
}
/**
* Parse a plist `<dict>` element into a JavaScript object.
* Only direct-child `<key>` elements are considered: getElementsByTagName
* is recursive and would flatten keys of nested dicts into the parent.
*/
function parseDict(dictNode: Element): PlistDictionary {
const obj: PlistDictionary = {};
let childNode = dictNode.firstChild;
while (childNode) {
const keyNode = childNode;
childNode = childNode.nextSibling;
if (keyNode.nodeType !== Node.ELEMENT_NODE || keyNode.nodeName !== 'key') {
continue;
}
const keyName = keyNode.textContent || '';
let valueNode = keyNode.nextSibling;
while (valueNode && valueNode.nodeType !== Node.ELEMENT_NODE) {
valueNode = valueNode.nextSibling;
}
if (valueNode) {
obj[keyName] = parseNode(valueNode as Element);
// Skip ahead of the parsed value so the loop doesn't re-visit it
childNode = valueNode.nextSibling;
}
}
return obj;
}
/**
* Parse a plist `<array>` element into a JavaScript array.
*/
function parseArray(arrayNode: Element): PlistArray {
const result: PlistArray = [];
let childNode = arrayNode.firstChild;
while (childNode) {
if (childNode.nodeType === Node.ELEMENT_NODE) {
result.push(parseNode(childNode as Element));
}
childNode = childNode.nextSibling;
}
return result;
}
}