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

doctype-to-string/test/ index.js
37 lines
1.1 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 work for a HTML5-ish doctype', t => {
  17. const doctype = '<!DOCTYPE html>'
  18. const doc = makeStubDocument(doctype)
  19. t.is(doctypeToString(doc.doctype), doctype)
  20. })
  21. test('should work for a HTML4-ish doctype', t => {
  22. const doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'
  23. const doc = makeStubDocument(doctype)
  24. t.is(doctypeToString(doc.doctype), doctype)
  25. })
  26. test('should work for a doctype without public id', t => {
  27. const doctype = '<!DOCTYPE html SYSTEM "http://www.w3.org/TR/html4/loose.dtd">'
  28. const doc = makeStubDocument(doctype)
  29. t.is(doctypeToString(doc.doctype), doctype)
  30. })