conventions.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. * Since we can not rely on `Object.assign` we provide a simplified version
  25. * that is sufficient for our needs.
  26. *
  27. * @param {Object} target
  28. * @param {Object | null | undefined} source
  29. *
  30. * @returns {Object} target
  31. * @throws TypeError if target is not an object
  32. *
  33. * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
  34. * @see https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-object.assign
  35. */
  36. function assign(target, source) {
  37. if (target === null || typeof target !== 'object') {
  38. throw new TypeError('target is not an object')
  39. }
  40. for (var key in source) {
  41. if (Object.prototype.hasOwnProperty.call(source, key)) {
  42. target[key] = source[key]
  43. }
  44. }
  45. return target
  46. }
  47. /**
  48. * All mime types that are allowed as input to `DOMParser.parseFromString`
  49. *
  50. * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString#Argument02 MDN
  51. * @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#domparsersupportedtype WHATWG HTML Spec
  52. * @see DOMParser.prototype.parseFromString
  53. */
  54. var MIME_TYPE = freeze({
  55. /**
  56. * `text/html`, the only mime type that triggers treating an XML document as HTML.
  57. *
  58. * @see DOMParser.SupportedType.isHTML
  59. * @see https://www.iana.org/assignments/media-types/text/html IANA MimeType registration
  60. * @see https://en.wikipedia.org/wiki/HTML Wikipedia
  61. * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString MDN
  62. * @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-domparser-parsefromstring WHATWG HTML Spec
  63. */
  64. HTML: 'text/html',
  65. /**
  66. * Helper method to check a mime type if it indicates an HTML document
  67. *
  68. * @param {string} [value]
  69. * @returns {boolean}
  70. *
  71. * @see https://www.iana.org/assignments/media-types/text/html IANA MimeType registration
  72. * @see https://en.wikipedia.org/wiki/HTML Wikipedia
  73. * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString MDN
  74. * @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-domparser-parsefromstring */
  75. isHTML: function (value) {
  76. return value === MIME_TYPE.HTML
  77. },
  78. /**
  79. * `application/xml`, the standard mime type for XML documents.
  80. *
  81. * @see https://www.iana.org/assignments/media-types/application/xml IANA MimeType registration
  82. * @see https://tools.ietf.org/html/rfc7303#section-9.1 RFC 7303
  83. * @see https://en.wikipedia.org/wiki/XML_and_MIME Wikipedia
  84. */
  85. XML_APPLICATION: 'application/xml',
  86. /**
  87. * `text/html`, an alias for `application/xml`.
  88. *
  89. * @see https://tools.ietf.org/html/rfc7303#section-9.2 RFC 7303
  90. * @see https://www.iana.org/assignments/media-types/text/xml IANA MimeType registration
  91. * @see https://en.wikipedia.org/wiki/XML_and_MIME Wikipedia
  92. */
  93. XML_TEXT: 'text/xml',
  94. /**
  95. * `application/xhtml+xml`, indicates an XML document that has the default HTML namespace,
  96. * but is parsed as an XML document.
  97. *
  98. * @see https://www.iana.org/assignments/media-types/application/xhtml+xml IANA MimeType registration
  99. * @see https://dom.spec.whatwg.org/#dom-domimplementation-createdocument WHATWG DOM Spec
  100. * @see https://en.wikipedia.org/wiki/XHTML Wikipedia
  101. */
  102. XML_XHTML_APPLICATION: 'application/xhtml+xml',
  103. /**
  104. * `image/svg+xml`,
  105. *
  106. * @see https://www.iana.org/assignments/media-types/image/svg+xml IANA MimeType registration
  107. * @see https://www.w3.org/TR/SVG11/ W3C SVG 1.1
  108. * @see https://en.wikipedia.org/wiki/Scalable_Vector_Graphics Wikipedia
  109. */
  110. XML_SVG_IMAGE: 'image/svg+xml',
  111. })
  112. /**
  113. * Namespaces that are used in this code base.
  114. *
  115. * @see http://www.w3.org/TR/REC-xml-names
  116. */
  117. var NAMESPACE = freeze({
  118. /**
  119. * The XHTML namespace.
  120. *
  121. * @see http://www.w3.org/1999/xhtml
  122. */
  123. HTML: 'http://www.w3.org/1999/xhtml',
  124. /**
  125. * Checks if `uri` equals `NAMESPACE.HTML`.
  126. *
  127. * @param {string} [uri]
  128. *
  129. * @see NAMESPACE.HTML
  130. */
  131. isHTML: function (uri) {
  132. return uri === NAMESPACE.HTML
  133. },
  134. /**
  135. * The SVG namespace.
  136. *
  137. * @see http://www.w3.org/2000/svg
  138. */
  139. SVG: 'http://www.w3.org/2000/svg',
  140. /**
  141. * The `xml:` namespace.
  142. *
  143. * @see http://www.w3.org/XML/1998/namespace
  144. */
  145. XML: 'http://www.w3.org/XML/1998/namespace',
  146. /**
  147. * The `xmlns:` namespace
  148. *
  149. * @see https://www.w3.org/2000/xmlns/
  150. */
  151. XMLNS: 'http://www.w3.org/2000/xmlns/',
  152. })
  153. exports.assign = assign;
  154. exports.freeze = freeze;
  155. exports.MIME_TYPE = MIME_TYPE;
  156. exports.NAMESPACE = NAMESPACE;