|
- import test from 'ava'
- import Window from 'window';
-
- import documentOuterHTML from '../src'
-
- function makeDocument(html) {
- const window = new Window()
- const parser = new window.DOMParser()
- const doc = parser.parseFromString(html, 'text/html')
- return doc
- }
-
- function normalise(html) {
- // Remove whitespace around string and between tags.
- return html.trim().replace(/>\s+</g, '><')
- }
-
- test('should work with only an html node', t => {
- const html = `
- <html>
- <head></head>
- <body><p>blub.</p></body>
- </html>
- `
- const doc = makeDocument(html)
- t.is(
- normalise(documentOuterHTML(doc)),
- normalise(html)
- )
- })
-
- test('should work with comments', t => {
- const html = `
- <!-- comment before -->
- <html>
- <head></head>
- <body><p>blub.</p></body>
- </html>
- <!-- comment after -->
- `
- const doc = makeDocument(html)
- t.is(
- normalise(documentOuterHTML(doc)),
- normalise(html)
- )
- })
-
- test('should work with doctype', t => {
- const html = `
- <!DOCTYPE html>
- <html>
- <head></head>
- <body><p>blub.</p></body>
- </html>
- `
- const doc = makeDocument(html)
- t.is(
- normalise(documentOuterHTML(doc)),
- normalise(html)
- )
- })
-
- test('should work without a documentElement', t => {
- const html = `
- <!DOCTYPE html>
- <!-- some comment -->
- `
- const doc = makeDocument(html)
- // The html (+head&body) element will have been created automatically. Remove it.
- doc.removeChild(doc.documentElement)
- t.is(
- normalise(documentOuterHTML(doc)),
- normalise(html)
- )
- })
-
- test('should even work on a completely empty document', t => {
- const html = ``
- const doc = makeDocument(html)
- // The html (+head&body) element will have been created automatically. Remove it.
- doc.removeChild(doc.documentElement)
- t.is(
- normalise(documentOuterHTML(doc)),
- normalise(html)
- )
- })
|