Convert a DOM DocumentType into a string, e.g. "<!DOCTYPE html>"

index.js 1.1 KiB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import test from 'ava'
  2. import Window from 'window';
  3. import doctypeToString from '../src'
  4. function makeStubDocument(doctype) {
  5. const window = new Window()
  6. const parser = new window.DOMParser()
  7. const doc = parser.parseFromString(
  8. `${doctype}<html></html>`,
  9. 'text/html'
  10. )
  11. return doc
  12. }
  13. test('should return empty string if absent', t => {
  14. const doctype = ''
  15. const doc = makeStubDocument()
  16. t.is(doctypeToString(doc.doctype), doctype)
  17. })
  18. test('should work for a HTML5-ish doctype', t => {
  19. const doctype = '<!DOCTYPE html>'
  20. const doc = makeStubDocument(doctype)
  21. t.is(doctypeToString(doc.doctype), doctype)
  22. })
  23. test('should work for a HTML4-ish doctype', t => {
  24. const doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'
  25. const doc = makeStubDocument(doctype)
  26. t.is(doctypeToString(doc.doctype), doctype)
  27. })
  28. test('should work for a doctype without public id', t => {
  29. const doctype = '<!DOCTYPE html SYSTEM "http://www.w3.org/TR/html4/loose.dtd">'
  30. const doc = makeStubDocument(doctype)
  31. t.is(doctypeToString(doc.doctype), doctype)
  32. })