import test from 'ava'
import { parse, parseDefaults, stringify } from '../lib'
import { formatAttributes } from '../lib/stringify'
test('stringify() should handle simple conversions', (t) => {
const str1 = '
Text
'
t.is(stringify(parse(str1)), str1)
const str2 = 'Text'
t.is(stringify(parse(str2)), str2)
const str3 = ''
t.is(stringify(parse(str3)), str3)
})
test('stringify() should work for void elements', (t) => {
const meta = ""
t.is(stringify(parse(meta)), meta)
const link = ""
t.is(stringify(parse(link)), link)
})
test('stringify() should build the class attribute properly', (t) => {
const elem = ""
t.is(stringify(parse(elem)), elem)
})
test('stringify() should build data-* attributes properly', (t) => {
const elem = ""
t.is(stringify(parse(elem)), elem)
})
test('stringify() should build the style attribute properly', (t) => {
const elem = ""
t.is(stringify(parse(elem)), elem)
})
test('stringify() should do basic escaping if a value contains either single or double quotes', (t) => {
const html = ''
t.is(stringify(parse(html)), html)
})
test('stringify() should preserve whitespace', (t) => {
const html = [
' ',
' Document
',
' ',
].join('\n')
t.is(stringify(parse(html)), html)
})
test('formatAttributes should stringify attribute lists correctly', (t) => {
t.is(formatAttributes([]), '')
t.is(
formatAttributes([
{
key: 'disabled',
value: null,
},
]),
' disabled',
)
t.is(
formatAttributes([
{
key: 'data-key',
value: '123',
},
]),
" data-key='123'",
)
})
test('stringify() should use single quotes if attribute contains double quote', (t) => {
const html = ``
t.is(stringify(parse(html)), html)
})
test('stringify() should use double quotes if attribute contains single quote', (t) => {
const html = ``
t.is(stringify(parse(html)), html)
})
test('stringify() should prefer double quotes if requested', (t) => {
const input = ``
const output = ``
t.is(
stringify(parse(input), {
...parseDefaults,
preferDoubleQuoteAttributes: true,
}),
output,
)
})