conventions.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. 'use strict'
  2. /**
  3. * "Shallow freezes" an object to render it immutable.
  4. * Uses `Object.freeze` if available,
  5. * otherwise the immutability is only in the type.
  6. *
  7. * Is used to create "enum like" objects.
  8. *
  9. * @template T
  10. * @param {T} object the object to freeze
  11. * @param {Pick<ObjectConstructor, 'freeze'> = Object} oc `Object` by default,
  12. * allows to inject custom object constructor for tests
  13. * @returns {Readonly<T>}
  14. *
  15. * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze
  16. */
  17. function freeze(object, oc) {
  18. if (oc === undefined) {
  19. oc = Object
  20. }
  21. return oc && typeof oc.freeze === 'function' ? oc.freeze(object) : object
  22. }
  23. /**
  24. * All mime types that are allowed as input to `DOMParser.parseFromString`
  25. *
  26. * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString#Argument02 MDN
  27. * @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#domparsersupportedtype WHATWG HTML Spec
  28. * @see DOMParser.prototype.parseFromString
  29. */
  30. var MIME_TYPE = freeze({
  31. /**
  32. * `text/html`, the only mime type that triggers treating an XML document as HTML.
  33. *
  34. * @see DOMParser.SupportedType.isHTML
  35. * @see https://www.iana.org/assignments/media-types/text/html IANA MimeType registration
  36. * @see https://en.wikipedia.org/wiki/HTML Wikipedia
  37. * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString MDN
  38. * @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-domparser-parsefromstring WHATWG HTML Spec
  39. */
  40. HTML: 'text/html',
  41. /**
  42. * Helper method to check a mime type if it indicates an HTML document
  43. *
  44. * @param {string} [value]
  45. * @returns {boolean}
  46. *
  47. * @see https://www.iana.org/assignments/media-types/text/html IANA MimeType registration
  48. * @see https://en.wikipedia.org/wiki/HTML Wikipedia
  49. * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString MDN
  50. * @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-domparser-parsefromstring */
  51. isHTML: function (value) {
  52. return value === MIME_TYPE.HTML
  53. },
  54. /**
  55. * `application/xml`, the standard mime type for XML documents.
  56. *
  57. * @see https://www.iana.org/assignments/media-types/application/xml IANA MimeType registration
  58. * @see https://tools.ietf.org/html/rfc7303#section-9.1 RFC 7303
  59. * @see https://en.wikipedia.org/wiki/XML_and_MIME Wikipedia
  60. */
  61. XML_APPLICATION: 'application/xml',
  62. /**
  63. * `text/html`, an alias for `application/xml`.
  64. *
  65. * @see https://tools.ietf.org/html/rfc7303#section-9.2 RFC 7303
  66. * @see https://www.iana.org/assignments/media-types/text/xml IANA MimeType registration
  67. * @see https://en.wikipedia.org/wiki/XML_and_MIME Wikipedia
  68. */
  69. XML_TEXT: 'text/xml',
  70. /**
  71. * `application/xhtml+xml`, indicates an XML document that has the default HTML namespace,
  72. * but is parsed as an XML document.
  73. *
  74. * @see https://www.iana.org/assignments/media-types/application/xhtml+xml IANA MimeType registration
  75. * @see https://dom.spec.whatwg.org/#dom-domimplementation-createdocument WHATWG DOM Spec
  76. * @see https://en.wikipedia.org/wiki/XHTML Wikipedia
  77. */
  78. XML_XHTML_APPLICATION: 'application/xhtml+xml',
  79. /**
  80. * `image/svg+xml`,
  81. *
  82. * @see https://www.iana.org/assignments/media-types/image/svg+xml IANA MimeType registration
  83. * @see https://www.w3.org/TR/SVG11/ W3C SVG 1.1
  84. * @see https://en.wikipedia.org/wiki/Scalable_Vector_Graphics Wikipedia
  85. */
  86. XML_SVG_IMAGE: 'image/svg+xml',
  87. })
  88. /**
  89. * Namespaces that are used in this code base.
  90. *
  91. * @see http://www.w3.org/TR/REC-xml-names
  92. */
  93. var NAMESPACE = freeze({
  94. /**
  95. * The XHTML namespace.
  96. *
  97. * @see http://www.w3.org/1999/xhtml
  98. */
  99. HTML: 'http://www.w3.org/1999/xhtml',
  100. /**
  101. * Checks if `uri` equals `NAMESPACE.HTML`.
  102. *
  103. * @param {string} [uri]
  104. *
  105. * @see NAMESPACE.HTML
  106. */
  107. isHTML: function (uri) {
  108. return uri === NAMESPACE.HTML
  109. },
  110. /**
  111. * The SVG namespace.
  112. *
  113. * @see http://www.w3.org/2000/svg
  114. */
  115. SVG: 'http://www.w3.org/2000/svg',
  116. /**
  117. * The `xml:` namespace.
  118. *
  119. * @see http://www.w3.org/XML/1998/namespace
  120. */
  121. XML: 'http://www.w3.org/XML/1998/namespace',
  122. /**
  123. * The `xmlns:` namespace
  124. *
  125. * @see https://www.w3.org/2000/xmlns/
  126. */
  127. XMLNS: 'http://www.w3.org/2000/xmlns/',
  128. })
  129. exports.freeze = freeze;
  130. exports.MIME_TYPE = MIME_TYPE;
  131. exports.NAMESPACE = NAMESPACE;