diff --git a/lib/dom.js b/lib/dom.js index 22cdb5cd2..b20c47aa1 100644 --- a/lib/dom.js +++ b/lib/dom.js @@ -1,10 +1,3 @@ -/* - * DOM Level 2 - * Object DOMException - * @see http://www.w3.org/TR/REC-DOM-Level-1/ecma-script-language-binding.html - * @see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/ecma-script-binding.html - */ - function copy(src,dest){ for(var p in src){ dest[p] = src[p]; @@ -66,7 +59,12 @@ var INVALID_MODIFICATION_ERR = ExceptionCode.INVALID_MODIFICATION_ERR = ((Exce var NAMESPACE_ERR = ExceptionCode.NAMESPACE_ERR = ((ExceptionMessage[14]="Invalid namespace"),14); var INVALID_ACCESS_ERR = ExceptionCode.INVALID_ACCESS_ERR = ((ExceptionMessage[15]="Invalid access"),15); - +/** + * DOM Level 2 + * Object DOMException + * @see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/ecma-script-binding.html + * @see http://www.w3.org/TR/REC-DOM-Level-1/ecma-script-language-binding.html + */ function DOMException(code, message) { if(message instanceof Error){ var error = message; @@ -1251,6 +1249,7 @@ try{ //if(typeof require == 'function'){ exports.Node = Node; + exports.DOMException = DOMException; exports.DOMImplementation = DOMImplementation; exports.XMLSerializer = XMLSerializer; //} diff --git a/readme.md b/readme.md index 159ff286d..b0a9072f7 100644 --- a/readme.md +++ b/readme.md @@ -98,6 +98,30 @@ DOM level2 method and attribute: normalize() isSupported(feature, version) hasAttributes() +* [DOMException](http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/ecma-script-binding.html) + The DOMException class has the following constants (and `value` of type `Number`): + + 1. `DOMException.INDEX_SIZE_ERR` (`1`) + 1. `DOMException.DOMSTRING_SIZE_ERR` (`2`) + 1. `DOMException.HIERARCHY_REQUEST_ERR` (`3`) + 1. `DOMException.WRONG_DOCUMENT_ERR` (`4`) + 1. `DOMException.INVALID_CHARACTER_ERR` (`5`) + 1. `DOMException.NO_DATA_ALLOWED_ERR` (`6`) + 1. `DOMException.NO_MODIFICATION_ALLOWED_ERR` (`7`) + 1. `DOMException.NOT_FOUND_ERR` (`8`) + 1. `DOMException.NOT_SUPPORTED_ERR` (`9`) + 1. `DOMException.INUSE_ATTRIBUTE_ERR` (`10`) + 1. `DOMException.INVALID_STATE_ERR` (`11`) + 1. `DOMException.SYNTAX_ERR` (`12`) + 1. `DOMException.INVALID_MODIFICATION_ERR` (`13`) + 1. `DOMException.NAMESPACE_ERR` (`14`) + 1. `DOMException.INVALID_ACCESS_ERR` (`15`) + + The DOMException object has the following properties: + code + This property is of type Number. + + * extends the Error type thrown as part of DOM API: * [DOMImplementation](http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-102161490) @@ -228,7 +252,8 @@ DOM level 3 support: DOM extension by xmldom --- - * [Node] Source position extension; + +* [Node] Source position extension; attribute: //Numbered starting from '1' diff --git a/test/assert.js b/test/assert.js deleted file mode 100644 index 5ecc5cdf4..000000000 --- a/test/assert.js +++ /dev/null @@ -1,130 +0,0 @@ -/* eslint strict: off */ - -var node_assert = require('assert') - -function isAssertionError(error) { - return error.name.startsWith('AssertionError') -} - -/** - * Picks the location of the assertion from the stacktrace if available. - * - * @param {Error} error - */ -function locateAssertion(error) { - return ( - (error && - error.stack && - error.stack - .split('\n') - .find((l) => l.includes(__dirname) && !l.includes(__filename))) || - '[No stacktrace available]' - ) -} - -/** - * The existing vows testsuite makes heavy use of `console.assert(actual+'' == expected, ...)`. - * With the assumption that the equivalent call to an assertion method that can fail a test, - * is `assert(expected == actual, ...)` (the non strict assert using loose equality) - * this function is provided to solve: - * - * - `...` often contains a repetition of `actual` to have it in the test message - * - we want to make sure `assert.isTrue` is checked to cater for any weird type coercion - * - we would "prefer" using `assert.isEqual` for better failure message and test maintainability - * - easy switch to this method by - * - * by just making both assertions :) - * It prefixes each assertion with `(strict ===)` or `(strict ===)`, so it's clear which one fails - * - * @param {*} actual The the value returned by the code under test - * @param {*} expected The expected value - * @param {...*} messages values to use as assertion message, no need to repeat expected or actual - * @returns {undefined} if both assertion pass - * @throws {AssertionError} if one of the assertion fails - * - * @see https://nodejs.org/docs/latest-v10.x/api/assert.html#assert_class_assert_assertionerror - * @see https://nodejs.org/docs/latest-v10.x/api/assert.html#assert_strict_mode - * @see https://github.com/vowsjs/vows#assertistrueactual-message - * @see https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html - */ -function assert(actual, expected, ...messages) { - // in case console.assert gets converted without changing == to ,: fail - // it makes no sense to call this method with only one boolean argument - if (typeof actual === 'boolean' && arguments.length === 1) - throw new Error('convert equal to ,') - let caught - // the assertion with the better error message comes first so we can benefit - try { - node_assert.strict.equal(actual, expected) - } catch (error) { - if (!isAssertionError(error)) throw error - caught = error - } - // the original assertion comes second, it has to always be executed - node_assert( - actual == expected, - ['(loose ==)', ...messages, actual, locateAssertion(caught)].join(' ') - ) - if (caught) { - const msg = [ - '(strict ===) ', - ...messages, - caught.message, - locateAssertion(caught), - ] - if ((this.strictThrows || assert.strictThrows)()) { - caught.message = msg.join(' ') - throw caught - } - ;(this.log || assert.log)(...msg) - } -} - -assert.log = console.warn -assert.strictThrows = () => process.env.XMLDOM_ASSERT_STRICT || false - -/** - * This is a consistent way of skipping an assertion since it currently fails: - * Change `assert(exp, act, ...)` to `assert.skip(exp, act, ...)` - * - * - it prints something to the output when the assertion fails (like `console.assert`) - * - it fails if no assertion fails (no more need to skip it, if it's no longer needed, remove it) - * - it fails if the reason for skipping is not provided - * - * @param {*} expected The expected value - * @param {*} actual The the value returned by the code under test - * @param {...*} messages values to use as assertion message, no need to repeat expected or actual - * @returns {undefined} if the assertion fails - * @throws {AssertionError} if no assertion fails - */ -function skip(actual, expected, ...messages) { - try { - assert.apply(this, arguments) - } catch (error) { - if (!isAssertionError(error)) { - throw error - } - ;(this.log || assert.log)( - 'Skipped assertion fails as expected:\n ', - error.message, - 'in', - locateAssertion(error) - ) - return - } - const error = new Error( - `Skipped assertion is not failing: '''${messages.join(' ')}'''\n ` - ) - error.message += locateAssertion(error) - throw error -} - -assert.skip = skip - -// provide access to used node assertions to avoid cumbersome duplicate imports -// and avoid forgetting to import .strict methods -assert.fail = node_assert.strict.fail -assert.equal = node_assert.strict.equal -assert.isTrue = (actual) => assert(actual, true) - -module.exports = assert diff --git a/test/assert.vows.checks.js b/test/assert.vows.checks.js deleted file mode 100644 index b13422588..000000000 --- a/test/assert.vows.checks.js +++ /dev/null @@ -1,218 +0,0 @@ -'use strict' - -const vows = require('vows') -const assert = require('assert').strict -const a = require('./assert') - -const values = [ - undefined, - null, - true, - false, - NaN, - 0, - 1, - Infinity, - -Infinity, - '', - '0', - '1', - 'true', - 'false', - '[object Object]', - '() => {}', - [], - {}, - () => {}, -] -const describe = (...args) => - args - .map((a) => { - const type = typeof a - let str = JSON.stringify(a) - if (str === undefined) { - str = String(a) - } - return `${type} ${str}` - }) - .join(', ') - -const suite = vows.describe('assert') -values.forEach((actual) => { - values.forEach((expected) => { - // `NaN === NaN` and `NaN == NaN` are false so skip NaN as expected - if (typeof expected === 'number' && isNaN(expected)) return - - if (expected == actual) { - // actual and expected are loose equal - - if (expected !== actual) { - // actual and expected are loose equal but not strict equal - suite.addBatch({ - [`assert(${describe( - actual, - expected - )}) {strictThrows: false} logs`]: () => { - const logs = [] - const t = { - strictThrows: () => false, - log: (...args) => { - logs.push(args.join(' ')) - // console.warn(...args) - }, - } - a.call(t, actual, expected, 'msg1', 'msg2') - assert.equal( - logs.length, - 1, - 'expected 1 log entry but was ' + logs.length - ) - const missing = [ - '(strict ===)', - 'msg1', - 'msg2', - `${__filename}:`, - ].filter((em) => !logs[0].includes(em)) - assert( - missing.length === 0, - `Expected all of [${describe(...missing)}] in '''${logs[0]}'''.` - ) - }, - [`assert(${describe( - actual, - expected - )}) {strictThrows: true} throws`]: () => { - const t = { - strictThrows: () => true, - } - // assert.throws doesn't work here since the check is not triggered for AssertionError - let caught - try { - a.call(t, actual, expected, 'msg1', 'msg2') - } catch (error) { - caught = error - } - assert( - caught && caught.name.startsWith('AssertionError'), - 'expected an AssertionError to be thrown' - ) - const missing = [ - '(strict ===)', - 'msg1', - 'msg2', - `${__filename}:`, - ].filter((em) => !caught.message.includes(em)) - assert( - missing.length === 0, - `Expected all of [${describe(...missing)}] in '''${ - caught.message - }'''.` - ) - }, - [`assert.skip(${describe(actual, expected)}) logs`]: () => { - const logs = [] - const t = { - strictThrows: () => true, - log: (...args) => { - logs.push(args.join(' ')) - // console.warn(...args) - }, - } - a.skip.call(t, actual, expected, 'msg1', 'msg2') - assert.equal( - logs.length, - 1, - 'expected 1 log entry but was ' + logs.length - ) - const missing = [ - 'Skipped', - '(strict ===)', - 'msg1', - 'msg2', - `${__filename}:`, - ].filter((em) => !logs[0].includes(em)) - assert( - missing.length === 0, - `Expected all of [${describe(...missing)}] in '''${logs[0]}'''.` - ) - }, - }) - } else { - // actual and expected are loose and strict equal - suite.addBatch({ - [`assert.skip(${describe( - actual, - expected - )}) throws since no asssertion fails`]: () => { - // assert.throws doesn't work here since the check is not triggered for AssertionError - let caught - try { - a.skip(actual, expected, 'msg1', 'msg2') - } catch (error) { - caught = error - } - assert(caught, 'expected an nError to be thrown') - const missing = [ - 'Skipped', - 'not failing', - 'msg1', - 'msg2', - `${__filename}:`, - ].filter((em) => !caught.message.includes(em)) - assert( - missing.length === 0, - `Expected all of [${describe(...missing)}] in '''${ - caught.message - }'''.` - ) - }, - }) - } - } else { - // actual and expected are NOT loose equal - suite.addBatch({ - [`assert(${describe(actual, expected)}) loose fails`]: () => { - assert.throws( - () => a(actual, expected, 'msg1', 'msg2'), - (e) => { - const check = - /AssertionError/.test(e.name) && - ['(loose ==)', 'msg1', 'msg2'].every((em) => - e.message.includes(em) - ) - return check || console.error(e) - } - ) - }, - [`assert.skip(${describe(actual, expected)}) logs`]: () => { - const logs = [] - const t = { - log: (...args) => { - logs.push(args.join(' ')) - // console.warn(...args) - }, - } - a.skip.call(t, actual, expected, 'msg1', 'msg2') - assert.equal( - logs.length, - 1, - 'expected 1 log entry but was ' + logs.length - ) - const missing = [ - 'Skipped', - '(loose ==)', - 'msg1', - 'msg2', - `${__filename}:`, - ].filter((em) => !logs[0].includes(em)) - assert( - missing.length === 0, - `Expected all of [${describe(...missing)}] in '''${logs[0]}'''.` - ) - }, - }) - } - }) -}) - -suite.export(module) diff --git a/test/dom/attr.test.js b/test/dom/attr.test.js index ccdee4020..03766727a 100644 --- a/test/dom/attr.test.js +++ b/test/dom/attr.test.js @@ -1,71 +1,62 @@ 'use strict' +const { DOMParser } = require('../../lib/dom-parser') +const { DOMException } = require('../../lib/dom') -var DOMParser = require('../../lib/dom-parser').DOMParser -const assert = require('../assert') - -// Create a Test Suite describe('XML attrs', () => { it('can properly set attribute', () => { - var root = new DOMParser().parseFromString('', 'text/xml') + const root = new DOMParser().parseFromString('', 'text/xml') .documentElement root.setAttribute('a', '1') - assert(root.attributes[0].localName, 'a') + expect(root.attributes[0].localName).toBe('a') + root.setAttribute('b', 2) root.setAttribute('a', 1) root.setAttribute('a', 1) root.setAttribute('a', 1) - assert(root.attributes.length, 2) - try { - var c = root.ownerDocument.createElement('c') + expect(root.attributes.length).toBe(2) + + const c = root.ownerDocument.createElement('c') + expect(() => { c.setAttributeNode(root.attributes.item(0)) - } catch (e) { - assert(e.code, 10) - return - } - assert.fail('expected error but none was thrown') + }).toThrow(new DOMException(DOMException.INUSE_ATTRIBUTE_ERR)) }) it('can properly set ns attribute', () => { - var root = new DOMParser().parseFromString( + const root = new DOMParser().parseFromString( "", 'text/xml' ).documentElement - var child = root.firstChild + + const child = root.firstChild child.setAttributeNS('a', 'a:a', '1') child.setAttributeNS('b', 'b:b', '2') child.setAttributeNS('b', 'b:a', '1') - assert(child.attributes.length, 3, 'after adding 3', child) + expect(child.attributes.length).toBe(3) child.setAttribute('a', 1) child.setAttributeNS('b', 'b:b', '2') - assert(child.attributes.length, 4, 'after adding 4 and one with namespace') - try { - var c = root.ownerDocument.createElement('c') + expect(child.attributes.length).toBe(4) + + const c = root.ownerDocument.createElement('c') + expect(() => { c.setAttributeNodeNS(root.attributes.item(0)) - } catch (e) { - assert(e.code, 10, 'wrong error code') - return - } - assert.fail('expected error but none was thrown') + }).toThrow(new DOMException(DOMException.INUSE_ATTRIBUTE_ERR)) }) it('can properly override attribute', () => { - var root = new DOMParser().parseFromString( + const root = new DOMParser().parseFromString( "", 'text/xml' ).documentElement root.setAttributeNS('a', 'a:a', '1') - assert(root.attributes.length, 4) - //not standart - // root.firstChild.setAttributeNode(root.attributes[0]); - // assert(root.attributes.length, 0); + expect(root.attributes.length).toBe(4) }) it('properly supports attribute namespace', () => { - var root = new DOMParser().parseFromString( + const root = new DOMParser().parseFromString( "", 'text/xml' ).documentElement - assert(root.getAttributeNS('a', 'b'), 'e') + expect(root.getAttributeNS('a', 'b')).toBe('e') }) xit('can properly override ns attribute', () => {}) diff --git a/test/dom/clone.test.js b/test/dom/clone.test.js index 701c0391b..698e75674 100644 --- a/test/dom/clone.test.js +++ b/test/dom/clone.test.js @@ -1,34 +1,31 @@ 'use strict' -var XMLSerializer = require('../../lib/dom-parser').XMLSerializer -var DOMParser = require('../../lib/dom-parser').DOMParser -const assert = require('../assert') +const { DOMParser, XMLSerializer } = require('../../lib/dom-parser') -// Create a Test Suite describe('XML Namespace Parse', () => { it('can properly set clone', () => { - var doc1 = new DOMParser().parseFromString( + const doc1 = new DOMParser().parseFromString( "text1text2", 'text/xml' ) - var doc1s = new XMLSerializer().serializeToString(doc1) - var n = doc1.cloneNode(true) - assert(n, doc1s) + const doc1s = new XMLSerializer().serializeToString(doc1) + const n = doc1.cloneNode(true) + expect(n.toString()).toBe(doc1s.toString()) }) it('can properly import', () => { - var doc1 = new DOMParser().parseFromString("") - var doc2 = new DOMParser().parseFromString( + const doc1 = new DOMParser().parseFromString("") + const doc2 = new DOMParser().parseFromString( "text1text2", 'text/xml' ) - var doc3 = new DOMParser().parseFromString( + const doc3 = new DOMParser().parseFromString( "text1text2" ) - var n = doc1.importNode(doc2.documentElement, true) + const n = doc1.importNode(doc2.documentElement, true) doc1.documentElement.appendChild(n) - assert(doc1, doc3 + '') - assert.isTrue(doc2 != doc3 + '') + expect(doc1.toString()).toBe(doc3.toString()) + expect(doc2.toString()).not.toBe(doc3.toString()) }) }) diff --git a/test/dom/element.test.js b/test/dom/element.test.js index 392a22208..ad9cc41db 100644 --- a/test/dom/element.test.js +++ b/test/dom/element.test.js @@ -1,59 +1,54 @@ 'use strict' -var DOMParser = require('../../lib/dom-parser').DOMParser -var assert = require('../assert') -var XMLSerializer = require('../../lib/dom-parser').XMLSerializer +const { DOMParser, XMLSerializer } = require('../../lib/dom-parser') // Create a Test Suite describe('XML Namespace Parse', () => { // See: http://jsfiddle.net/bigeasy/ShcXP/1/ it('supports Document_getElementsByTagName', () => { - var doc = new DOMParser().parseFromString('') - assert(doc.getElementsByTagName('*').length, 2, 'on doc') - assert( - doc.documentElement.getElementsByTagName('*').length, - 1, - 'on doc.documentElement' - ) + const doc = new DOMParser().parseFromString('') + expect(doc.getElementsByTagName('*')).toHaveLength(2) + expect(doc.documentElement.getElementsByTagName('*')).toHaveLength(1) }) it('supports getElementsByTagName', () => { - var doc = new DOMParser().parseFromString( + const doc = new DOMParser().parseFromString( '' + '' + '' + '', 'text/xml' ) - var childs = doc.documentElement.getElementsByTagName('child') - assert(childs.item(0).getAttribute('attr'), '1', childs.item(0) + '') - assert(childs.item(1).getAttribute('attr'), '2', childs.item(1) + '') - assert(childs.item(2).getAttribute('attr'), '3', childs.item(2) + '') - assert(childs.length, 3, 'documentElement children length') - - var childs = doc.getElementsByTagName('child') - assert(childs.item(0).getAttribute('attr'), '1', childs.item(0) + '') - assert(childs.item(1).getAttribute('attr'), '2', childs.item(1) + '') - assert(childs.item(2).getAttribute('attr'), '3', childs.item(2) + '') - assert(childs.length, 3, 'doc children length') - - var childs = doc.documentElement.getElementsByTagName('*') - for (var i = 0, buf = []; i < childs.length; i++) { - buf.push(childs[i].tagName) + + const childs1 = doc.documentElement.getElementsByTagName('child') + expect(childs1.item(0).getAttribute('attr')).toBe('1') + expect(childs1.item(1).getAttribute('attr')).toBe('2') + expect(childs1.item(2).getAttribute('attr')).toBe('3') + expect(childs1).toHaveLength(3) + + const childs2 = doc.getElementsByTagName('child') + expect(childs2.item(0).getAttribute('attr')).toBe('1') + expect(childs2.item(1).getAttribute('attr')).toBe('2') + expect(childs2.item(2).getAttribute('attr')).toBe('3') + expect(childs2).toHaveLength(3) + + const childs3 = doc.documentElement.getElementsByTagName('*') + for (let i = 0, buf = []; i < childs3.length; i++) { + buf.push(childs3[i].tagName) } - assert(childs.length, 7, buf) + expect(childs3).toHaveLength(7) - var feed = new DOMParser().parseFromString( + const feed = new DOMParser().parseFromString( 'foo' ) - var entries = feed.documentElement.getElementsByTagName('entry') - assert.equal(entries.length, 1, 'assert entry nodelist length ==1') - assert(entries[0].nodeName, 'entry') - assert(feed.documentElement.childNodes.item(0).nodeName, 'entry') + const entries = feed.documentElement.getElementsByTagName('entry') + expect(entries).toHaveLength(1) + expect(entries[0].nodeName).toBe('entry') + expect(feed.documentElement.childNodes.item(0).nodeName).toBe('entry') }) it('supports getElementsByTagNameNS', () => { - var doc = new DOMParser().parseFromString( + const doc = new DOMParser().parseFromString( '' + '' + '' + @@ -61,119 +56,112 @@ describe('XML Namespace Parse', () => { 'text/xml' ) - var childs = doc.documentElement.getElementsByTagNameNS( + const childs1 = doc.documentElement.getElementsByTagNameNS( 'http://test.com', '*' ) - var i = 0 - assert(childs.length, 6) + expect(childs1).toHaveLength(6) - var childs = doc.getElementsByTagNameNS('http://test.com', '*') - assert(childs.length, 7) + const childs2 = doc.getElementsByTagNameNS('http://test.com', '*') + expect(childs2).toHaveLength(7) - var childs = doc.documentElement.getElementsByTagNameNS( + const childs3 = doc.documentElement.getElementsByTagNameNS( 'http://test.com', 'test' ) - assert(childs.length, 3) + expect(childs3).toHaveLength(3) - var childs = doc.getElementsByTagNameNS('http://test.com', 'test') - assert(childs.length, 3) + const childs4 = doc.getElementsByTagNameNS('http://test.com', 'test') + expect(childs4).toHaveLength(3) - var childs = doc.getElementsByTagNameNS('*', 'test') - assert(childs.length, 4) + const childs5 = doc.getElementsByTagNameNS('*', 'test') + expect(childs5).toHaveLength(4) - var childs = doc.documentElement.getElementsByTagNameNS('*', 'test') - assert(childs.length, 4) + const childs6 = doc.documentElement.getElementsByTagNameNS('*', 'test') + expect(childs6).toHaveLength(4) }) it('supports getElementById', () => { - var doc = new DOMParser().parseFromString( + const doc = new DOMParser().parseFromString( '' + '' + '', 'text/xml' ) - assert.isTrue(doc.getElementById('root') != null, 'root') - assert(doc.getElementById('a1').getAttribute('title'), '1', 'first') - assert(doc.getElementById('a2').getAttribute('title'), '2', 'second') - assert(doc.getElementById('a2').getAttribute('title2'), '', 'empty') + expect(doc.getElementById('root')).not.toBeNull() + expect(doc.getElementById('a1').getAttribute('title')).toBe('1') + expect(doc.getElementById('a2').getAttribute('title')).toBe('2') + expect(doc.getElementById('a2').getAttribute('title2')).toBe('') }) it('can properly append exist child', () => { - var doc = new DOMParser().parseFromString( + const doc = new DOMParser().parseFromString( '' + '' + '', 'text/xml' ) - var doc1 = doc - var str1 = new XMLSerializer().serializeToString(doc) - var doc2 = doc1.cloneNode(true) - var doc3 = doc1.cloneNode(true) - var doc4 = doc1.cloneNode(true) + const doc1 = doc + const str1 = new XMLSerializer().serializeToString(doc) + const doc2 = doc1.cloneNode(true) + const doc3 = doc1.cloneNode(true) + const doc4 = doc1.cloneNode(true) doc3.documentElement.appendChild(doc3.documentElement.lastChild) doc4.documentElement.appendChild(doc4.documentElement.firstChild) - var str2 = new XMLSerializer().serializeToString(doc2) - var str3 = new XMLSerializer().serializeToString(doc3) - var str4 = new XMLSerializer().serializeToString(doc4) - assert(str1, str2, 'str1 == str2') - assert(str2, str3, 'str2 == str3') - assert.isTrue(str3 != str4, 'str4 != str3:' + str3) - assert(str3.length, str4.length, 'str3 and str4 have same length') + const str2 = new XMLSerializer().serializeToString(doc2) + const str3 = new XMLSerializer().serializeToString(doc3) + const str4 = new XMLSerializer().serializeToString(doc4) + expect(str1).toBe(str2) + expect(str2).toBe(str3) + expect(str3).not.toBe(str4) + expect(str3.length).toBe(str4.length) }) it('can properly append exist other child', () => { - var doc = new DOMParser().parseFromString( + const doc = new DOMParser().parseFromString( '' + '' + '', 'text/xml' ) - var doc1 = doc - var str1 = new XMLSerializer().serializeToString(doc) - var doc2 = doc1.cloneNode(true) + const doc1 = doc + const str1 = new XMLSerializer().serializeToString(doc) + const doc2 = doc1.cloneNode(true) - assert(doc2.documentElement.lastChild.childNodes.length, 0, 'initially 0') + expect(doc2.documentElement.lastChild.childNodes).toHaveLength(0) doc2.documentElement.appendChild(doc2.documentElement.firstChild.firstChild) - var str2 = new XMLSerializer().serializeToString(doc2) + const str2 = new XMLSerializer().serializeToString(doc2) - assert( - doc2.documentElement.lastChild.childNodes.length, - 1, - '1 after adding' - ) - assert.isTrue(str1 != str2, 'str1 != str2') - assert.isTrue(str1.length != str2.length, 'str1/length != str2.length') - var doc3 = new DOMParser().parseFromString(str2, 'text/xml') + expect(doc2.documentElement.lastChild.childNodes).toHaveLength(1) + expect(str1).not.toBe(str2) + expect(str1).not.toHaveLength(str2.length) + const doc3 = new DOMParser().parseFromString(str2, 'text/xml') doc3.documentElement.firstChild.appendChild(doc3.documentElement.lastChild) - var str3 = new XMLSerializer().serializeToString(doc3) - assert(str1, str3, 'final assertion') + const str3 = new XMLSerializer().serializeToString(doc3) + expect(str1).toBe(str3) }) it('can properly set textContent', () => { - var doc = new DOMParser().parseFromString('') - var a = doc.documentElement.firstChild - var b = a.nextSibling + const doc = new DOMParser().parseFromString('') + const a = doc.documentElement.firstChild + const b = a.nextSibling a.textContent = 'hello' - assert( - doc.documentElement.toString(), + expect(doc.documentElement.toString()).toBe( 'hello' ) b.textContent = 'there' - assert( - doc.documentElement.toString(), + expect(doc.documentElement.toString()).toBe( 'hellothere' ) b.textContent = '' - assert(doc.documentElement.toString(), 'hello') + expect(doc.documentElement.toString()).toBe('hello') doc.documentElement.textContent = 'bye' - assert(doc.documentElement.toString(), 'bye') + expect(doc.documentElement.toString()).toBe('bye') }) xit('nested append failed', () => {}) diff --git a/test/dom/fragment.test.js b/test/dom/fragment.test.js index ed39ebb23..d584fc7ce 100644 --- a/test/dom/fragment.test.js +++ b/test/dom/fragment.test.js @@ -1,16 +1,15 @@ 'use strict' -var DOMParser = require('../../lib/dom-parser').DOMParser -const assert = require('../assert') +const { DOMParser } = require('../../lib/dom-parser') describe('DOM DocumentFragment', () => { // see: http://jsfiddle.net/9Wmh2/1/ it('append empty fragment', () => { - var document = new DOMParser().parseFromString('

') - var fragment = document.createDocumentFragment() + const document = new DOMParser().parseFromString('

') + const fragment = document.createDocumentFragment() document.getElementById('p').insertBefore(fragment, null) fragment.appendChild(document.createTextNode('a')) document.getElementById('p').insertBefore(fragment, null) - assert(document.toString(), '

a

') + expect(document.toString()).toBe('

a

') }) }) diff --git a/test/dom/ns-test.test.js b/test/dom/ns-test.test.js index 8fd180922..5ad560e6d 100644 --- a/test/dom/ns-test.test.js +++ b/test/dom/ns-test.test.js @@ -1,12 +1,11 @@ 'use strict' -var assert = require('../assert') -var DOMParser = require('../../lib/dom-parser').DOMParser +const { DOMParser } = require('../../lib/dom-parser') // Create a Test Suite describe('XML Namespace Parse', () => { it('supports testlitecns', () => { - var doc = new DOMParser({ + const doc = new DOMParser({ xmlns: { c: 'http://www.xidea.org/lite/core', '': 'http://www.w3.org/1999/xhtml', @@ -15,55 +14,42 @@ describe('XML Namespace Parse', () => { '', 'text/xml' ) - var el = doc.getElementsByTagName('c:var')[0] - assert(el.namespaceURI, 'http://www.xidea.org/lite/core') - assert( - doc, + const el = doc.getElementsByTagName('c:var')[0] + expect(el.namespaceURI).toBe('http://www.xidea.org/lite/core') + expect(doc.toString()).toBe( '' ) }) //ignore default prefix xml attribute it('test', () => { - var w3 = 'http://www.w3.org/1999/xhtml' - var n1 = 'http://www.frankston.com/public' - var n2 = 'http://rmf.vc/n2' - var hx = + const w3 = 'http://www.w3.org/1999/xhtml' + const n1 = 'http://www.frankston.com/public' + const n2 = 'http://rmf.vc/n2' + const hx = '' - var doc = new DOMParser().parseFromString(hx, 'text/xml') - var els = [].slice.call( + const doc = new DOMParser().parseFromString(hx, 'text/xml') + const els = [].slice.call( doc.documentElement.getElementsByTagNameNS(n1, 'foo') ) - for (var _i = 0, els_1 = els; _i < els_1.length; _i++) { - var el = els_1[_i] + for (let _i = 0, els_1 = els; _i < els_1.length; _i++) { + const el = els_1[_i] - var te = doc.createElementNS(n1, 'test') + const te = doc.createElementNS(n1, 'test') te.setAttributeNS(n1, 'bar', 'valx') - var te = doc.createElementNS(n1, 'test') - te.setAttributeNS(n1, 'bar', 'valx') - assert.equal( - String(te), - '', - `1. i ${_i}, ${el}` - ) + expect(te.toString()).toBe('') el.appendChild(te) - var tx = doc.createElementNS(n2, 'test') + const tx = doc.createElementNS(n2, 'test') tx.setAttributeNS(n2, 'bar', 'valx') - assert.equal( - String(tx), - '', - `2. i ${_i}, ${el}` - ) + expect(tx.toString()).toBe('') el.appendChild(tx) } - var sr = String(doc) - assert( - sr, + expect(doc.toString()).toBe( '' ) }) diff --git a/test/dom/serializer.test.js b/test/dom/serializer.test.js index 045b15e04..215c218b1 100644 --- a/test/dom/serializer.test.js +++ b/test/dom/serializer.test.js @@ -1,21 +1,19 @@ 'use strict' -var DOMParser = require('../../lib/dom-parser').DOMParser -const assert = require('../assert') +const { DOMParser } = require('../../lib/dom-parser') describe('XML Serializer', () => { it('supports text node containing "]]>"', () => { - var doc = new DOMParser().parseFromString('', 'text/xml') + const doc = new DOMParser().parseFromString('', 'text/xml') doc.documentElement.appendChild(doc.createTextNode('hello ]]> there')) - assert(doc.documentElement.firstChild.toString(), 'hello ]]> there') + expect(doc.documentElement.firstChild.toString()).toBe('hello ]]> there') }) it('supports ', 'text/html') - assert( - doc.documentElement.firstChild.toString(), + expect(doc.documentElement.firstChild.toString()).toBe( '' ) })