forked from sandflow/imscJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
154 additions
and
38 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import sax from "sax"; | ||
|
||
/** | ||
* @typedef {sax.Tag | sax.QualifiedTag} Node | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} Parser | ||
* @property {(xml: string) => Parser} write | ||
* @property {() => Parser} close | ||
* @property {(node: Node) => void} onopentag | ||
* @property {(text: string) => void} ontext | ||
* @property {() => void} onclosetag | ||
*/ | ||
|
||
export class XMLParser { | ||
/** | ||
* @param {Element} element | ||
* @returns {SAX} | ||
*/ | ||
static toNode(element) { | ||
const attrs = element.attributes; | ||
const node = XMLParser.toNS(element); | ||
node.attributes = {}; | ||
|
||
for (let i = 0, len = attrs.length; i < len; i++) { | ||
const attr = attrs[i]; | ||
node.attributes[attr.name] = XMLParser.toNS(attr); | ||
} | ||
|
||
return node; | ||
} | ||
|
||
static toNS(node) { | ||
return { | ||
name: node.nodeName, | ||
prefix: node.prefix, | ||
local: node.localName, | ||
uri: node.namespaceURI, | ||
value: node.value, | ||
}; | ||
} | ||
|
||
onopentag = (node) => { console.log(node); } | ||
ontext = (str) => { console.log(str); } | ||
onclosetag = () => { } | ||
|
||
write(xmlstring) { | ||
const parser = new DOMParser(); | ||
const doc = parser.parseFromString(xmlstring, "application/xml"); | ||
const errorNode = doc.querySelector("parsererror"); | ||
|
||
if (errorNode) { | ||
throw new Error("XML parsing error: " + errorNode.textContent); | ||
} | ||
|
||
this.process(doc.firstChild); | ||
|
||
return this; | ||
} | ||
|
||
process(element) { | ||
const node = XMLParser.toNode(element); | ||
this.onopentag(node); | ||
|
||
const children = element.childNodes; | ||
|
||
for (let i = 0, len = children.length; i < len; i++) { | ||
const child = children[i]; | ||
|
||
if (child.nodeType === Node.TEXT_NODE) { | ||
this.ontext(child.textContent); | ||
} else if (child.nodeType === Node.ELEMENT_NODE) { | ||
this.process(child); | ||
} | ||
} | ||
|
||
this.onclosetag(); | ||
} | ||
|
||
close() { | ||
return this; | ||
} | ||
} | ||
|
||
/** | ||
* @returns {Parser} | ||
*/ | ||
export function createDOMParser() { | ||
return new XMLParser(); | ||
} | ||
|
||
/** | ||
* @returns {Parser} | ||
*/ | ||
export function createSAXParser() { | ||
return sax.parser(true, { xmlns: true }); | ||
} |
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