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

doctype-to-string/test/ index.js
41 lines
1.2 KiB

  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 doctype is null', t => {
  14. t.is(doctypeToString(null), '')
  15. })
  16. test('should throw if argument is not a DocumentType', t => {
  17. t.throws(() => doctypeToString('a string, for example.'), TypeError)
  18. })
  19. test('should work for a HTML5-ish doctype', t => {
  20. const doctype = '<!DOCTYPE html>'
  21. const doc = makeStubDocument(doctype)
  22. t.is(doctypeToString(doc.doctype), doctype)
  23. })
  24. test('should work for a HTML4-ish doctype', t => {
  25. const doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'
  26. const doc = makeStubDocument(doctype)
  27. t.is(doctypeToString(doc.doctype), doctype)
  28. })
  29. test('should work for a doctype without public id', t => {
  30. const doctype = '<!DOCTYPE html SYSTEM "http://www.w3.org/TR/html4/loose.dtd">'
  31. const doc = makeStubDocument(doctype)
  32. t.is(doctypeToString(doc.doctype), doctype)
  33. })