2ad2bc79e370ad926c4dee77aeb8ff7063641d73dc314cc52735c22efc6043ae69c9da2dfa26304b40d9d478a3ed7615166d52366a1d118faaf9962c428553 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. 'use strict';
  2. const { DOCUMENT_MODE } = require('./html');
  3. //Const
  4. const VALID_DOCTYPE_NAME = 'html';
  5. const VALID_SYSTEM_ID = 'about:legacy-compat';
  6. const QUIRKS_MODE_SYSTEM_ID = 'http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd';
  7. const QUIRKS_MODE_PUBLIC_ID_PREFIXES = [
  8. '+//silmaril//dtd html pro v0r11 19970101//',
  9. '-//as//dtd html 3.0 aswedit + extensions//',
  10. '-//advasoft ltd//dtd html 3.0 aswedit + extensions//',
  11. '-//ietf//dtd html 2.0 level 1//',
  12. '-//ietf//dtd html 2.0 level 2//',
  13. '-//ietf//dtd html 2.0 strict level 1//',
  14. '-//ietf//dtd html 2.0 strict level 2//',
  15. '-//ietf//dtd html 2.0 strict//',
  16. '-//ietf//dtd html 2.0//',
  17. '-//ietf//dtd html 2.1e//',
  18. '-//ietf//dtd html 3.0//',
  19. '-//ietf//dtd html 3.2 final//',
  20. '-//ietf//dtd html 3.2//',
  21. '-//ietf//dtd html 3//',
  22. '-//ietf//dtd html level 0//',
  23. '-//ietf//dtd html level 1//',
  24. '-//ietf//dtd html level 2//',
  25. '-//ietf//dtd html level 3//',
  26. '-//ietf//dtd html strict level 0//',
  27. '-//ietf//dtd html strict level 1//',
  28. '-//ietf//dtd html strict level 2//',
  29. '-//ietf//dtd html strict level 3//',
  30. '-//ietf//dtd html strict//',
  31. '-//ietf//dtd html//',
  32. '-//metrius//dtd metrius presentational//',
  33. '-//microsoft//dtd internet explorer 2.0 html strict//',
  34. '-//microsoft//dtd internet explorer 2.0 html//',
  35. '-//microsoft//dtd internet explorer 2.0 tables//',
  36. '-//microsoft//dtd internet explorer 3.0 html strict//',
  37. '-//microsoft//dtd internet explorer 3.0 html//',
  38. '-//microsoft//dtd internet explorer 3.0 tables//',
  39. '-//netscape comm. corp.//dtd html//',
  40. '-//netscape comm. corp.//dtd strict html//',
  41. "-//o'reilly and associates//dtd html 2.0//",
  42. "-//o'reilly and associates//dtd html extended 1.0//",
  43. "-//o'reilly and associates//dtd html extended relaxed 1.0//",
  44. '-//sq//dtd html 2.0 hotmetal + extensions//',
  45. '-//softquad software//dtd hotmetal pro 6.0::19990601::extensions to html 4.0//',
  46. '-//softquad//dtd hotmetal pro 4.0::19971010::extensions to html 4.0//',
  47. '-//spyglass//dtd html 2.0 extended//',
  48. '-//sun microsystems corp.//dtd hotjava html//',
  49. '-//sun microsystems corp.//dtd hotjava strict html//',
  50. '-//w3c//dtd html 3 1995-03-24//',
  51. '-//w3c//dtd html 3.2 draft//',
  52. '-//w3c//dtd html 3.2 final//',
  53. '-//w3c//dtd html 3.2//',
  54. '-//w3c//dtd html 3.2s draft//',
  55. '-//w3c//dtd html 4.0 frameset//',
  56. '-//w3c//dtd html 4.0 transitional//',
  57. '-//w3c//dtd html experimental 19960712//',
  58. '-//w3c//dtd html experimental 970421//',
  59. '-//w3c//dtd w3 html//',
  60. '-//w3o//dtd w3 html 3.0//',
  61. '-//webtechs//dtd mozilla html 2.0//',
  62. '-//webtechs//dtd mozilla html//'
  63. ];
  64. const QUIRKS_MODE_NO_SYSTEM_ID_PUBLIC_ID_PREFIXES = QUIRKS_MODE_PUBLIC_ID_PREFIXES.concat([
  65. '-//w3c//dtd html 4.01 frameset//',
  66. '-//w3c//dtd html 4.01 transitional//'
  67. ]);
  68. const QUIRKS_MODE_PUBLIC_IDS = ['-//w3o//dtd w3 html strict 3.0//en//', '-/w3c/dtd html 4.0 transitional/en', 'html'];
  69. const LIMITED_QUIRKS_PUBLIC_ID_PREFIXES = ['-//w3c//dtd xhtml 1.0 frameset//', '-//w3c//dtd xhtml 1.0 transitional//'];
  70. const LIMITED_QUIRKS_WITH_SYSTEM_ID_PUBLIC_ID_PREFIXES = LIMITED_QUIRKS_PUBLIC_ID_PREFIXES.concat([
  71. '-//w3c//dtd html 4.01 frameset//',
  72. '-//w3c//dtd html 4.01 transitional//'
  73. ]);
  74. //Utils
  75. function enquoteDoctypeId(id) {
  76. const quote = id.indexOf('"') !== -1 ? "'" : '"';
  77. return quote + id + quote;
  78. }
  79. function hasPrefix(publicId, prefixes) {
  80. for (let i = 0; i < prefixes.length; i++) {
  81. if (publicId.indexOf(prefixes[i]) === 0) {
  82. return true;
  83. }
  84. }
  85. return false;
  86. }
  87. //API
  88. exports.isConforming = function(token) {
  89. return (
  90. token.name === VALID_DOCTYPE_NAME &&
  91. token.publicId === null &&
  92. (token.systemId === null || token.systemId === VALID_SYSTEM_ID)
  93. );
  94. };
  95. exports.getDocumentMode = function(token) {
  96. if (token.name !== VALID_DOCTYPE_NAME) {
  97. return DOCUMENT_MODE.QUIRKS;
  98. }
  99. const systemId = token.systemId;
  100. if (systemId && systemId.toLowerCase() === QUIRKS_MODE_SYSTEM_ID) {
  101. return DOCUMENT_MODE.QUIRKS;
  102. }
  103. let publicId = token.publicId;
  104. if (publicId !== null) {
  105. publicId = publicId.toLowerCase();
  106. if (QUIRKS_MODE_PUBLIC_IDS.indexOf(publicId) > -1) {
  107. return DOCUMENT_MODE.QUIRKS;
  108. }
  109. let prefixes = systemId === null ? QUIRKS_MODE_NO_SYSTEM_ID_PUBLIC_ID_PREFIXES : QUIRKS_MODE_PUBLIC_ID_PREFIXES;
  110. if (hasPrefix(publicId, prefixes)) {
  111. return DOCUMENT_MODE.QUIRKS;
  112. }
  113. prefixes =
  114. systemId === null ? LIMITED_QUIRKS_PUBLIC_ID_PREFIXES : LIMITED_QUIRKS_WITH_SYSTEM_ID_PUBLIC_ID_PREFIXES;
  115. if (hasPrefix(publicId, prefixes)) {
  116. return DOCUMENT_MODE.LIMITED_QUIRKS;
  117. }
  118. }
  119. return DOCUMENT_MODE.NO_QUIRKS;
  120. };
  121. exports.serializeContent = function(name, publicId, systemId) {
  122. let str = '!DOCTYPE ';
  123. if (name) {
  124. str += name;
  125. }
  126. if (publicId) {
  127. str += ' PUBLIC ' + enquoteDoctypeId(publicId);
  128. } else if (systemId) {
  129. str += ' SYSTEM';
  130. }
  131. if (systemId !== null) {
  132. str += ' ' + enquoteDoctypeId(systemId);
  133. }
  134. return str;
  135. };