本文整理汇总了TypeScript中@simple-dom/dom-test-helper.moduleWithDocument函数的典型用法代码示例。如果您正苦于以下问题:TypeScript moduleWithDocument函数的具体用法?TypeScript moduleWithDocument怎么用?TypeScript moduleWithDocument使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了moduleWithDocument函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: moduleWithDocument
moduleWithDocument('Document', (helper) => {
QUnit.test('creating a document node', (assert) => {
const { document } = helper;
// https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType
assert.strictEqual(document.nodeType, NodeType.DOCUMENT_NODE, 'document has node type of 9');
// https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeName
assert.strictEqual(document.nodeName, '#document', 'document node has the name #document');
// https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeValue
assert.strictEqual(document.nodeValue, null, 'for the document itself, nodeValue returns null');
// https://developer.mozilla.org/en-US/docs/Web/API/Node/ownerDocument
assert.strictEqual(document.ownerDocument, null, 'for the document itself, ownerDocument returns null');
/* istanbul ignore if */
if (document.firstChild === null) {
assert.ok(false, 'document has firstChild');
} else {
assert.strictEqual(document.firstChild.ownerDocument, document);
assert.strictEqual(document.firstChild.nodeType, 10);
assert.strictEqual(document.firstChild.nodeName, 'html');
assert.strictEqual(document.doctype, document.firstChild, 'doctype is document.firstChild');
}
/* istanbul ignore if */
if (document.lastChild === null) {
assert.ok(false, 'document has lastChild');
} else {
/* istanbul ignore else */
if (document.lastChild.nodeType === NodeType.ELEMENT_NODE) {
assert.strictEqual(document.lastChild.namespaceURI, Namespace.HTML, 'documentElement is HTML namespace');
}
assert.strictEqual(document.lastChild.ownerDocument, document);
assert.strictEqual(document.lastChild.nodeType, 1);
assert.strictEqual(document.lastChild.nodeName, 'HTML');
assert.strictEqual(document.documentElement, document.lastChild, 'documentElement is document.lastChild');
/* istanbul ignore if */
if (document.documentElement.firstChild === null) {
assert.ok(false, 'documentElement has firstChild');
} else {
assert.strictEqual(document.documentElement.firstChild.ownerDocument, document);
assert.strictEqual(document.documentElement.firstChild.nodeType, 1);
assert.strictEqual(document.documentElement.firstChild.nodeName, 'HEAD');
assert.strictEqual(document.documentElement.firstChild.firstChild, null);
assert.strictEqual(document.head, document.documentElement.firstChild, 'head is documentElement.firstChild');
}
/* istanbul ignore if */
if (document.documentElement.lastChild === null) {
assert.ok(false, 'documentElement has firstChild');
} else {
assert.strictEqual(document.documentElement.lastChild.ownerDocument, document);
assert.strictEqual(document.documentElement.lastChild.nodeType, 1);
assert.strictEqual(document.documentElement.lastChild.nodeName, 'BODY');
assert.strictEqual(document.documentElement.lastChild.firstChild, null);
assert.strictEqual(document.body, document.documentElement.lastChild, 'body is documentElement.firstChild');
}
}
});
});
示例2: moduleWithDocument
moduleWithDocument('Node', (helper) => {
QUnit.test('#insertBefore', (assert) => {
const doc = helper.document;
const body = doc.body;
const div = doc.createElement('div');
const span = doc.createElement('span');
const p = doc.createElement('p');
const frag = doc.createDocumentFragment();
frag.appendChild(p);
// setup previous sibling
frag.appendChild(span);
assert.strictEqual(span.previousSibling, p, 'precond');
const appendChildReturn = body.appendChild(div);
assert.strictEqual(appendChildReturn, div, 'appendChild should return the node it is appending');
body.insertBefore(span, div);
assert.strictEqual(span.parentNode, body, 'nodes parent is set');
assert.strictEqual(span.previousSibling, null, 'nodes previous sibling is cleared');
assert.strictEqual(span.nextSibling, div, 'nodes next sibling is set');
assert.strictEqual(div.previousSibling, span, 'next sibling\'s previous sibling is set');
assert.strictEqual(div.nextSibling, null, 'next sibling\'s next sibling is set');
assert.strictEqual(div.parentNode, body, 'next sibling\'s parent is set');
assert.strictEqual(body.firstChild, span, 'parents first child is set');
assert.strictEqual(body.lastChild, div, 'parents last child is set');
});
QUnit.test('nodeValue is mutable', (assert) => {
const doc = helper.document;
const text = doc.createTextNode('hello world');
const comment = doc.createComment('goodbye cruel world');
assert.strictEqual(text.nodeValue, 'hello world', 'precond - node value is set');
assert.strictEqual(comment.nodeValue, 'goodbye cruel world', 'precond - node value is set');
text.nodeValue = text.nodeValue.toUpperCase();
comment.nodeValue = comment.nodeValue.toUpperCase();
assert.strictEqual(text.nodeValue, 'HELLO WORLD');
assert.strictEqual(comment.nodeValue, 'GOODBYE CRUEL WORLD', 'precond - node value is set');
});
});
示例3: moduleWithDocument
moduleWithDocument('Serializer', (helper, hooks) => {
let serializer: Serializer;
hooks.beforeEach(() => {
serializer = new Serializer(voidMap);
});
hooks.afterEach(() => {
serializer = undefined as any;
});
QUnit.test('serializes single element correctly', (assert) => {
const { element } = helper;
const actual = serializer.serialize(element('div'));
assert.equal(actual, '<div></div>');
});
QUnit.test('serializes element with attribute number value correctly', (assert) => {
const { element } = helper;
const actual = serializer.serialize(element('div', {
height: 500,
}));
assert.equal(actual, '<div height="500"></div>');
});
QUnit.test('serializes single void element correctly', (assert) => {
const { element } = helper;
const actual = serializer.serialize(element('img', { src: 'foo' }));
assert.equal(actual, '<img src="foo">');
});
QUnit.test('serializes complex tree correctly', (assert) => {
const { element, fragment, text } = helper;
const actual = serializer.serialize(fragment(
element('div', { id: 'foo' },
element('b', {},
text('Foo & Bar'),
),
text('!'),
element('img', { src: 'foo' }),
),
));
assert.equal(actual, '<div id="foo"><b>Foo & Bar</b>!<img src="foo"></div>');
});
QUnit.test('does not serialize siblings of an element', (assert) => {
const { element } = helper;
const htmlEl = element('html');
const head = element('head');
const body = element('body');
head.appendChild(element('meta', { content: 'foo' }));
head.appendChild(element('meta', { content: 'bar' }));
htmlEl.appendChild(head);
htmlEl.appendChild(body);
let actual = serializer.serialize(head);
assert.equal(actual, '<head><meta content="foo"><meta content="bar"></head>');
actual = serializer.serialize(body);
assert.equal(actual, '<body></body>');
});
QUnit.test('serializes children but not self', (assert) => {
const { element, fragment, text } = helper;
const actual = serializer.serializeChildren(fragment(
element('div', { id: 'foo' },
element('b', {},
text('Foo & Bar')),
text('!'),
element('img', { src: 'foo' }))).firstChild!);
assert.equal(actual, '<b>Foo & Bar</b>!<img src="foo">');
});
QUnit.test('serializes raw HTML', (assert) => {
const { element, fragment, text } = helper;
let actual = serializer.serialize(fragment(
element('div', { id: 'foo' },
text('<p></p>'))));
assert.equal(actual, '<div id="foo"><p></p></div>');
const el = element('div', { id: 'foo' });
// Chrome requires parent to be an element
const parent = element('div', undefined, el);
// serializes a raw
el.insertAdjacentHTML(InsertPosition.beforebegin, '<p>beforebegin</p>');
el.insertAdjacentHTML(InsertPosition.afterbegin, '<p>afterbegin</p>');
el.insertAdjacentHTML(InsertPosition.beforeend, '<p>beforeend</p>');
el.insertAdjacentHTML(InsertPosition.afterend, '<p>afterend</p>');
actual = serializer.serializeChildren(parent);
assert.equal(actual, '<p>beforebegin</p><div id="foo"><p>afterbegin</p><p>beforeend</p></div><p>afterend</p>');
});
//.........这里部分代码省略.........
示例4: moduleWithDocument
moduleWithDocument('Basic HTML parsing', (helper, hooks) => {
let parser: Parser;
hooks.beforeEach(() => {
parser = new Parser(tokenize as Tokenizer, helper.document, voidMap);
});
hooks.afterEach(() => {
parser = undefined as any;
});
QUnit.test('simple parse', (assert) => {
const fragment = parser.parse('<div>Hello</div>')!;
assert.ok(fragment);
const node = fragment.firstChild!;
assert.ok(node);
assert.equal(node.nodeType, 1);
assert.equal(node.nodeName.toLowerCase(), 'div');
assert.ok(node.firstChild);
assert.equal(node.firstChild!.nodeType, 3);
assert.equal(node.firstChild!.nodeValue, 'Hello');
});
QUnit.test('nested parse', (assert) => {
// tslint:disable-next-line:max-line-length
const fragment = parser.parse('text before<div>Hello</div>text between<div id=foo title="Hello World">World</div>text after');
assert.ok(fragment);
let node: SimpleText | SimpleElement | null = null;
node = fragment!.firstChild as SimpleText;
assert.ok(node);
assert.equal(node.nodeType, 3);
assert.equal(node.nodeValue, 'text before');
node = node.nextSibling as SimpleElement;
assert.ok(node);
assert.equal(node.nodeType, 1);
assert.equal(node.nodeName, 'DIV');
assert.ok(node.firstChild);
assert.equal(node.firstChild!.nodeType, 3);
assert.equal(node.firstChild!.nodeValue, 'Hello');
node = node.nextSibling as SimpleText;
assert.ok(node);
assert.equal(node.nodeType, 3);
assert.equal(node.nodeValue, 'text between');
node = (node.nextSibling as SimpleElement);
assert.ok(node);
assert.equal(node.nodeType, 1);
assert.equal(node.nodeName, 'DIV');
const expectedValues: { [attr: string]: string } = {
id: 'foo',
title: 'Hello World',
};
assert.equal(node.attributes.length, 2);
assert.equal(node.attributes[0].value, expectedValues[node.attributes[0].name]);
assert.equal(node.attributes[1].value, expectedValues[node.attributes[1].name]);
assert.equal(node.attributes.length, 2);
assert.ok(node.firstChild);
assert.equal(node.firstChild!.nodeType, 3);
assert.equal(node.firstChild!.nodeValue, 'World');
node = node.nextSibling as SimpleText;
assert.ok(node);
assert.equal(node.nodeType, 3);
assert.equal(node.nodeValue, 'text after');
});
QUnit.test('void tags', (assert) => {
const fragment = parser.parse('<div>Hello<br>World<img src="http://example.com/image.png"></div>')!;
assert.ok(fragment);
let node: SimpleText | SimpleElement = fragment.firstChild as SimpleElement;
assert.ok(node);
assert.equal(node.nodeType, 1);
assert.equal(node.nodeName, 'DIV');
node = node.firstChild as SimpleText;
assert.ok(node);
assert.equal(node.nodeType, 3);
assert.equal(node.nodeValue, 'Hello');
node = node.nextSibling as SimpleElement;
assert.ok(node);
assert.equal(node.nodeType, 1);
assert.equal(node.nodeName, 'BR');
node = node.nextSibling as SimpleText;
assert.ok(node);
assert.equal(node.nodeType, 3);
assert.equal(node.nodeValue, 'World');
node = node.nextSibling as SimpleElement;
assert.ok(node);
assert.equal(node.nodeType, 1);
assert.equal(node.nodeName, 'IMG');
assert.equal(node.getAttribute('src'), 'http://example.com/image.png');
assert.equal(node.nextSibling, null);
});
});
示例5: moduleWithDocument
moduleWithDocument('Element', (helper) => {
// See http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-core.html#ID-B63ED1A3
// tslint:disable-next-line:max-line-length
QUnit.test('appending a document fragment appends the fragment\'s children and not the fragment itself', (assert) => {
const { document } = helper;
const frag = document.createDocumentFragment();
const elem = document.createElement('div');
const body = document.body;
assert.strictEqual(body.firstChild, null, 'body has no children');
frag.appendChild(elem);
body.appendChild(frag);
assert.strictEqual(body.firstChild!.nodeName, 'DIV', 'fragment\'s child is added as child of document');
});
QUnit.test('create HTML-namespaced div element', (assert) => {
const { document } = helper;
const svg = document.createElementNS(Namespace.HTML, 'div');
assert.strictEqual(svg.namespaceURI, Namespace.HTML, 'has HTML namespace');
assert.strictEqual(svg.nodeName, 'DIV', 'nodeName is uppercased');
assert.strictEqual(svg.tagName, 'DIV', 'tagName is uppercased');
});
QUnit.test('create svg element', (assert) => {
const { document } = helper;
const svg = document.createElementNS(Namespace.SVG, 'svg');
assert.strictEqual(svg.namespaceURI, Namespace.SVG, 'has svg namespace');
assert.strictEqual(svg.nodeName, 'svg', 'nodeName is svg');
assert.strictEqual(svg.tagName, 'svg', 'tagName is svg');
});
// See http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-core.html#ID-B63ED1A3
// tslint:disable-next-line:max-line-length
QUnit.test('appending a document fragment (via insertBefore) appends the fragment\'s children and not the fragment itself', (assert) => {
const { document } = helper;
const frag = document.createDocumentFragment();
const elem = document.createElement('div');
const existing = document.createElement('main');
const body = document.body;
body.appendChild(existing);
assert.strictEqual(body.firstChild!.nodeName, 'MAIN', 'sanity check: the main element was actually inserted');
assert.strictEqual(body.lastChild!.nodeName, 'MAIN', 'sanity check: the main element was actually inserted');
frag.appendChild(elem);
body.insertBefore(frag, existing);
assert.strictEqual(body.firstChild!.nodeName, 'DIV', 'The body\'s first child is now DIV');
assert.strictEqual(body.lastChild!.nodeName, 'MAIN', 'The body\'s last child is now MAIN');
});
QUnit.test('insert a document fragment before a node with a previousSibling', (assert) => {
const { document } = helper;
const parent = document.createElement('div');
const before = document.createComment('before');
const after = document.createComment('after');
parent.appendChild(before);
parent.appendChild(after);
const frag = document.createDocumentFragment();
const child1 = document.createElement('p');
const child2 = document.createElement('p');
frag.appendChild(child1);
frag.appendChild(child2);
assert.strictEqual(after.previousSibling, before);
parent.insertBefore(frag, after);
assert.strictEqual(frag.firstChild, null);
assert.strictEqual(frag.lastChild, null);
assert.strictEqual(child1.parentNode, parent);
assert.strictEqual(child2.parentNode, parent);
assert.strictEqual(before.previousSibling, null);
assert.strictEqual(before.nextSibling, child1);
assert.strictEqual(child1.previousSibling, before);
assert.strictEqual(child1.nextSibling, child2);
assert.strictEqual(child2.previousSibling, child1);
assert.strictEqual(child2.nextSibling, after);
assert.strictEqual(after.previousSibling, child2);
assert.strictEqual(after.nextSibling, null);
});
QUnit.test('insert an empty document fragment does nothing', (assert) => {
const { document } = helper;
//.........这里部分代码省略.........