-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[REF] parser, template_set: factor out parseXML function
For some reason the code of parseXML was duplicated, despite being exactly the same except for some whitespace. Move it out into a common utils file. closes #1569
- Loading branch information
1 parent
9dcbbe5
commit e5629fa
Showing
3 changed files
with
40 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { OwlError } from "./owl_error"; | ||
|
||
/** | ||
* Parses an XML string into an XML document, throwing errors on parser errors | ||
* instead of returning an XML document containing the parseerror. | ||
* | ||
* @param xml the string to parse | ||
* @returns an XML document corresponding to the content of the string | ||
*/ | ||
export function parseXML(xml: string): XMLDocument { | ||
const parser = new DOMParser(); | ||
const doc = parser.parseFromString(xml, "text/xml"); | ||
if (doc.getElementsByTagName("parsererror").length) { | ||
let msg = "Invalid XML in template."; | ||
const parsererrorText = doc.getElementsByTagName("parsererror")[0].textContent; | ||
if (parsererrorText) { | ||
msg += "\nThe parser has produced the following error message:\n" + parsererrorText; | ||
const re = /\d+/g; | ||
const firstMatch = re.exec(parsererrorText); | ||
if (firstMatch) { | ||
const lineNumber = Number(firstMatch[0]); | ||
const line = xml.split("\n")[lineNumber - 1]; | ||
const secondMatch = re.exec(parsererrorText); | ||
if (line && secondMatch) { | ||
const columnIndex = Number(secondMatch[0]) - 1; | ||
if (line[columnIndex]) { | ||
msg += | ||
`\nThe error might be located at xml line ${lineNumber} column ${columnIndex}\n` + | ||
`${line}\n${"-".repeat(columnIndex - 1)}^`; | ||
} | ||
} | ||
} | ||
} | ||
throw new OwlError(msg); | ||
} | ||
|
||
return doc; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters