(string)
| 277 | |
| 278 | // See https://github.com/Normal-Tangerine8609/Scriptable-Widgets/blob/main/scripts/rss-parser.js |
| 279 | function parseXML(string) { |
| 280 | let main = { |
| 281 | isRoot: true, |
| 282 | name: "root", |
| 283 | children: [], |
| 284 | }; |
| 285 | |
| 286 | let target = main; |
| 287 | let goBack = {}; |
| 288 | let parser = new XMLParser(string); |
| 289 | |
| 290 | parser.didStartElement = (name, attrs) => { |
| 291 | let backTo = Symbol(); |
| 292 | goBack[backTo] = target; |
| 293 | let newTarget = { |
| 294 | name, |
| 295 | attrs, |
| 296 | innerText: "", |
| 297 | children: [], |
| 298 | end: backTo, |
| 299 | }; |
| 300 | target.children.push(newTarget); |
| 301 | target = newTarget; |
| 302 | }; |
| 303 | |
| 304 | parser.didEndElement = (name) => { |
| 305 | let sym = target.end; |
| 306 | delete target.end; |
| 307 | target = goBack[sym]; |
| 308 | }; |
| 309 | |
| 310 | parser.foundCharacters = (text) => { |
| 311 | target.innerText += |
| 312 | target.innerText === "" ? text.trim() : " " + text.trim(); |
| 313 | }; |
| 314 | |
| 315 | parser.parseErrorOccurred = () => { |
| 316 | console.warn( |
| 317 | "A parse error occurred, ensure the document is formatted properly.", |
| 318 | ); |
| 319 | }; |
| 320 | |
| 321 | parser.parse(); |
| 322 | |
| 323 | if (!main.isRoot) { |
| 324 | console.warn( |
| 325 | "A parse error occurred, ensure the document is formatted properly.", |
| 326 | ); |
| 327 | } |
| 328 | |
| 329 | delete main.isRoot; |
| 330 | return traverse(main); |
| 331 | |
| 332 | function traverse(node) { |
| 333 | let newNode = {}; |
| 334 | |
| 335 | for (let child of node.children) { |
| 336 | let newChild = traverse(child); |
no test coverage detected