Skip to content

Commit

Permalink
Refactor to simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed May 31, 2023
1 parent aaae592 commit 02c2683
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
19 changes: 4 additions & 15 deletions packages/rehype-dom-stringify/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,8 @@ function serialize(node) {
return '<DOCTYPE html>'
}

// Element.
if ('outerHTML' in node) {
return node.outerHTML
}

// Comment, text, fragment.
if ('textContent' in node) {
const div = document.createElement('div')
div.append(node)
return div.innerHTML
/* c8 ignore next 4 */
}

// ?
return ''
// Comment, element, fragment, text.
const template = document.createElement('template')
template.content.append(node)
return template.innerHTML
}
8 changes: 4 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ test('parse', async (t) => {
.data('settings', {fragment: false})
.processSync('<title>Hi</title><h2>Hello world!')
.toString(),
'<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Hi</title></head><body><h2>Hello world!</h2></body></html>',
'<html><head><title>Hi</title></head><body><h2>Hello world!</h2></body></html>',
'should stringify a complete document'
)

Expand All @@ -101,13 +101,13 @@ test('parse', async (t) => {
.data('settings', {fragment: false})
.processSync('<!doctype html>')
.toString(),
'<DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>',
'<DOCTYPE html><html><head></head><body></body></html>',
'should stringify a doctype'
)

assert.equal(
processor().processSync('<!doctype html>').toString(),
'<DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>',
'<DOCTYPE html><html><head></head><body></body></html>',
'should support empty documents'
)

Expand Down Expand Up @@ -184,7 +184,7 @@ test('parse', async (t) => {
.data('settings', {fragment: false})
.processSync('<title>Hi</title><h2>Hello world!')
.toString(),
'<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Hi</title></head><body><h2>Hello world!</h2></body></html>',
'<html><head><title>Hi</title></head><body><h2>Hello world!</h2></body></html>',
'should parse a complete document'
)

Expand Down

0 comments on commit 02c2683

Please sign in to comment.