Skip to content
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

Open
wants to merge 3 commits into
base: task/typedefs
Choose a base branch
from
Open

Conversation

littlespex
Copy link
Owner

see: #242

Comment on lines +81 to +85
* @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()) {
Copy link
Owner Author

@littlespex littlespex Jun 19, 2024

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.

Comment on lines +7 to +16
/**
* @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 {
Copy link
Owner Author

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.

Comment on lines +23 to +30
<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>
Copy link
Owner Author

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.

Comment on lines +31 to +44
"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"
}
},
Copy link
Owner Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant