-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add DOM Parser #2
base: task/typedefs
Are you sure you want to change the base?
Conversation
* @param {?Parser} parser XML parser | ||
* @returns {?TT} Opaque in-memory representation of an IMSC1 document | ||
*/ | ||
|
||
export function fromXML(xmlstring, errorHandler, metadataHandler) { | ||
const p = sax.parser(true, { xmlns: true }); | ||
export function fromXML(xmlstring, errorHandler, metadataHandler, parser = createDOMParser()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Allow the parser to be passed in as long as it adheres to the interface. The also changes the default to the DOMParser
approach.
/** | ||
* @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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid a massive refactor of fromXML()
, the parser interface matches sax interface, and the XMLParser
simply crawls the result of DOMParser
and mimics the sax behavior.
<button onclick="generateRenders('imsc-tests/imsc1', true)">Generate IMSC 1 reference | ||
renders - SAX</button> | ||
<button onclick="generateRenders('imsc-tests/imsc1_1', true)">Generate IMSC 1.1 reference | ||
renders - SAX</button> | ||
<button onclick="generateRenders('imsc-tests/imsc1', false)">Generate IMSC 1 reference | ||
renders - DOM</button> | ||
<button onclick="generateRenders('imsc-tests/imsc1_1', false)">Generate IMSC 1.1 reference | ||
renders- DOM</button> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added buttons for both parsers and made sure the generated files match.
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"import": "./dist/index.js" | ||
}, | ||
"./*.js": { | ||
"types": "./dist/*.d.ts", | ||
"import": "./dist/*.js" | ||
}, | ||
"./*": { | ||
"types": "./dist/*.d.ts", | ||
"import": "./dist/*.js" | ||
} | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added granular file imports for anyone installing imscJs
via npm. This way fromXML
can be imported directly, and avoid bundling sax
unless createSAXParser
is explicitly imported.
see: #242