substance
Version:
Substance is a JavaScript library for web-based content editing. It provides building blocks for realizing custom text editors and web-based publishing system. It is developed to power our online editing platform [Substance](http://substance.io).
84 lines (75 loc) • 3.04 kB
JavaScript
import { test as substanceTest } from 'substance-test'
import { platform, prettyPrintXML } from 'substance'
if (platform.inBrowser) {
prettyPrintTests('BrowserDOMElement')
}
prettyPrintTests('MemoryDOMElement')
function prettyPrintTests (impl) {
const LABEL = 'prettyPrintXML (' + impl + ')'
const test = (title, fn) => substanceTest(`${LABEL}: ${title}`, t => {
// before
if (impl === 'MemoryDOMElement') platform.values.inBrowser = false
try {
fn(t)
} finally {
// after
platform._reset()
}
})
test('preserve XML declaration', t => {
const xmlStr = '<article />'
const expected = ' \n<article />'
const actual = prettyPrintXML(xmlStr)
t.equal(actual, expected, 'prettyPrinted XML should be correct')
t.end()
})
test('preserve DOCTYPE declaration', t => {
const xmlStr = '<article />'
const expected = ' \n \n<article />'
const actual = prettyPrintXML(xmlStr)
t.equal(actual, expected, 'prettyPrinted XML should be correct')
t.end()
})
test('skip white-space outside the root element', t => {
const xmlStr = `
<article />`
const expected = ' \n \n<article />'
const actual = prettyPrintXML(xmlStr)
t.equal(actual, expected, 'prettyPrinted XML should be correct')
t.end()
})
test('layout structural elements', t => {
const xmlStr = '<article><front><title /></front><body><p /></body><back><references /></back></article>'
const actual = prettyPrintXML(xmlStr)
const expected = `
<article>
<front>
<title />
</front>
<body>
<p />
</body>
<back>
<references />
</back>
</article>`
t.equal(actual, expected, 'prettyPrinted XML should be correct')
t.end()
})
test('layout mixed elements', t => {
const xmlStr = '<article><front><title>Hello <b>World</b>!</title></front><body><p>Bla blupp</p></body></article>'
const actual = prettyPrintXML(xmlStr)
const expected = `
<article>
<front>
<title>Hello <b>World</b>!</title>
</front>
<body>
<p>Bla blupp</p>
</body>
</article>`
t.equal(actual, expected, 'prettyPrinted XML should be correct')
t.end()
})
}