Skip to content

Commit

Permalink
fix: sync types with exports (#717)
Browse files Browse the repository at this point in the history
- [x] `DOMExceptionName`
- [x] convert enums `MIME_TYPE` and `NAMESPACE` to `var` and literal
type to reflect actual code
- [x] widen function parameters from enums to strings
- [x] `hasDefaultHTMLNamespace`
- [x] dropped `/// <reference lib="dom" />` (added `@types/node` to
examples for `console` and `process.exit`)
- [x] `DOMImplementation`, `Document`, `Node`, `DocumentType`
- [x] `NodeList`, `LiveNodeList`
- [x] `Element`, `Attr`, `NamedNodeMap`
- [x] `CharacterData`, `CDATASection`, `Comment`, `Text`
- [x] `DocumentFragment`
- [x] `EntityReference`, `Entity`, `Notation`, `ProcessingInstruction`

fixes #285 
fixes #695
  • Loading branch information
karfau authored Sep 5, 2024
1 parent b862d13 commit ba39faa
Show file tree
Hide file tree
Showing 7 changed files with 1,329 additions and 104 deletions.
3 changes: 2 additions & 1 deletion examples/typescript-node-es6/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
"type": "module",
"scripts": {
"tsc": "tsc",
"test": "node dist/index.js"
"test": "tsc && node dist/index.js"
},
"keywords": [
"test",
"typescript"
],
"license": "MIT",
"devDependencies": {
"@types/node": "14.14.31",
"typescript": "*"
},
"dependencies": {
Expand Down
135 changes: 129 additions & 6 deletions examples/typescript-node-es6/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,145 @@
import {
Attr,
CDATASection,
CharacterData,
Comment,
Document,
DocumentType,
DOMException,
DOMExceptionName,
DOMImplementation,
DOMParser,
ExceptionCode,
hasDefaultHTMLNamespace,
isHTMLMimeType,
isValidMimeType,
LiveNodeList,
MIME_TYPE,
NamedNodeMap,
NAMESPACE,
Node,
NodeList,
onWarningStopParsing,
ParseError,
Text,
XMLSerializer,
} from '@xmldom/xmldom';

const failedAssertions: Error[] = [];
let assertions = 0;
const assert = <T>(
actual: T,
expected: T,
message: string = `#${++assertions}`
) => {
if (actual === expected) {
console.error(`assert ${message} passed: ${actual}`);
} else {
failedAssertions.push(
new Error(
`assert ${message} failed: expected ${JSON.stringify(expected)}, but was ${JSON.stringify(
actual
)}`
)
);
}
};

// lib/conventions
// widen type to string to check that any string can be passed
const mimeHtml: string = MIME_TYPE.HTML;
assert(isHTMLMimeType(mimeHtml), true);
assert(isHTMLMimeType(MIME_TYPE.HTML), true);
assert(hasDefaultHTMLNamespace(mimeHtml), true);
assert(hasDefaultHTMLNamespace(MIME_TYPE.XML_XHTML_APPLICATION), true);
assert(isValidMimeType(mimeHtml), true);
assert(isValidMimeType(MIME_TYPE.XML_SVG_IMAGE), true);
assert(isValidMimeType(MIME_TYPE.XML_APPLICATION), true);

// lib/errors

const domException = new DOMException();
assert(domException.code, 0);
assert(domException.name, 'Error');
assert(domException.message, undefined);
new DOMException('message', DOMExceptionName.SyntaxError);
new DOMException(DOMException.DATA_CLONE_ERR);
new DOMException(ExceptionCode.INDEX_SIZE_ERR, 'message');

const parseError = new ParseError('message');
assert(parseError.message, 'message');
new ParseError('message', {}, domException);

// lib/dom
assert(Node.ATTRIBUTE_NODE, 2);
assert(Node.DOCUMENT_POSITION_CONTAINS, 8);

assert(new NodeList().length, 0);

const impl = new DOMImplementation();
const doc1 = impl.createDocument(null, 'qualifiedName');
assert(doc1.contentType, MIME_TYPE.XML_APPLICATION);
assert(doc1.type, 'xml');
assert(doc1.ATTRIBUTE_NODE, 2);
assert(doc1.DOCUMENT_POSITION_CONTAINS, 8);
assert(doc1 instanceof Node, true);
assert(doc1 instanceof Document, true);
assert(doc1.childNodes instanceof NodeList, true);
assert(doc1.getElementsByClassName('hide') instanceof LiveNodeList, true);

const attr = doc1.createAttribute('attr');
assert(attr.nodeType, Node.ATTRIBUTE_NODE);
assert(attr.ownerDocument, doc1);
assert(attr.value, undefined);
assert(attr instanceof Attr, true);

const element = doc1.createElement('a');
assert(element.nodeType, Node.ELEMENT_NODE);
assert(element.ownerDocument, doc1);
assert(element.attributes instanceof NamedNodeMap, true);

const cdata = doc1.createCDATASection('< &');
assert(cdata instanceof CharacterData, true);
assert(cdata instanceof CDATASection, true);
const comment = doc1.createComment('< &');
assert(comment instanceof CharacterData, true);
assert(comment instanceof Comment, true);
const text = doc1.createTextNode('text');
assert(text instanceof CharacterData, true);
assert(text instanceof Text, true);

impl.createDocument(
NAMESPACE.XML,
'qualifiedName',
impl.createDocumentType('qualifiedName')
);
const doctype = impl.createDocumentType(
'qualifiedName',
'publicId',
'systemId'
);
assert(doctype instanceof Node, true);
assert(doctype instanceof DocumentType, true);
impl.createDocumentType('qualifiedName', 'publicId');
assert(impl.createHTMLDocument().type, 'html');
assert(impl.createHTMLDocument(false).childNodes.length, 0);
assert(impl.createHTMLDocument('title').childNodes.length, 2);

assert(
new DOMParser().parseFromString(`<div/>`, mimeHtml).childNodes.length,
1
);

const source = `<xml xmlns="a">
<child>test</child>
<child/>
</xml>`;
const doc = new DOMParser({
onError: onWarningStopParsing,
}).parseFromString(source, MIME_TYPE.XML_TEXT);
assert(new XMLSerializer().serializeToString(doc), source);

const serialized = new XMLSerializer().serializeToString(doc);

if (source !== serialized) {
throw `expected\n${source}\nbut was\n${serialized}`;
} else {
console.log(serialized);
if (failedAssertions.length > 0) {
failedAssertions.forEach((error) => console.error(error));
process.exit(1);
}
Loading

0 comments on commit ba39faa

Please sign in to comment.