8000 fix: sync types with exports by karfau · Pull Request #717 · xmldom/xmldom · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: sync types with exports #717

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

Merged
merged 13 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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