移动储能车V1版本
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

decode.js 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.fromCodePoint = exports.replaceCodePoint = exports.decodeCodePoint = exports.xmlDecodeTree = exports.htmlDecodeTree = exports.EntityDecoder = exports.DecodingMode = exports.BinTrieFlags = void 0;
  7. exports.determineBranch = determineBranch;
  8. exports.decodeHTML = decodeHTML;
  9. exports.decodeHTMLAttribute = decodeHTMLAttribute;
  10. exports.decodeHTMLStrict = decodeHTMLStrict;
  11. exports.decodeXML = decodeXML;
  12. const decode_data_html_js_1 = __importDefault(require("./generated/decode-data-html.js"));
  13. const decode_data_xml_js_1 = __importDefault(require("./generated/decode-data-xml.js"));
  14. const decode_codepoint_js_1 = require("./decode-codepoint.js");
  15. var CharCodes;
  16. (function (CharCodes) {
  17. CharCodes[CharCodes["NUM"] = 35] = "NUM";
  18. CharCodes[CharCodes["SEMI"] = 59] = "SEMI";
  19. CharCodes[CharCodes["EQUALS"] = 61] = "EQUALS";
  20. CharCodes[CharCodes["ZERO"] = 48] = "ZERO";
  21. CharCodes[CharCodes["NINE"] = 57] = "NINE";
  22. CharCodes[CharCodes["LOWER_A"] = 97] = "LOWER_A";
  23. CharCodes[CharCodes["LOWER_F"] = 102] = "LOWER_F";
  24. CharCodes[CharCodes["LOWER_X"] = 120] = "LOWER_X";
  25. CharCodes[CharCodes["LOWER_Z"] = 122] = "LOWER_Z";
  26. CharCodes[CharCodes["UPPER_A"] = 65] = "UPPER_A";
  27. CharCodes[CharCodes["UPPER_F"] = 70] = "UPPER_F";
  28. CharCodes[CharCodes["UPPER_Z"] = 90] = "UPPER_Z";
  29. })(CharCodes || (CharCodes = {}));
  30. /** Bit that needs to be set to convert an upper case ASCII character to lower case */
  31. const TO_LOWER_BIT = 32;
  32. var BinTrieFlags;
  33. (function (BinTrieFlags) {
  34. BinTrieFlags[BinTrieFlags["VALUE_LENGTH"] = 49152] = "VALUE_LENGTH";
  35. BinTrieFlags[BinTrieFlags["BRANCH_LENGTH"] = 16256] = "BRANCH_LENGTH";
  36. BinTrieFlags[BinTrieFlags["JUMP_TABLE"] = 127] = "JUMP_TABLE";
  37. })(BinTrieFlags || (exports.BinTrieFlags = BinTrieFlags = {}));
  38. function isNumber(code) {
  39. return code >= CharCodes.ZERO && code <= CharCodes.NINE;
  40. }
  41. function isHexadecimalCharacter(code) {
  42. return ((code >= CharCodes.UPPER_A && code <= CharCodes.UPPER_F) ||
  43. (code >= CharCodes.LOWER_A && code <= CharCodes.LOWER_F));
  44. }
  45. function isAsciiAlphaNumeric(code) {
  46. return ((code >= CharCodes.UPPER_A && code <= CharCodes.UPPER_Z) ||
  47. (code >= CharCodes.LOWER_A && code <= CharCodes.LOWER_Z) ||
  48. isNumber(code));
  49. }
  50. /**
  51. * Checks if the given character is a valid end character for an entity in an attribute.
  52. *
  53. * Attribute values that aren't terminated properly aren't parsed, and shouldn't lead to a parser error.
  54. * See the example in https://html.spec.whatwg.org/multipage/parsing.html#named-character-reference-state
  55. */
  56. function isEntityInAttributeInvalidEnd(code) {
  57. return code === CharCodes.EQUALS || isAsciiAlphaNumeric(code);
  58. }
  59. var EntityDecoderState;
  60. (function (EntityDecoderState) {
  61. EntityDecoderState[EntityDecoderState["EntityStart"] = 0] = "EntityStart";
  62. EntityDecoderState[EntityDecoderState["NumericStart"] = 1] = "NumericStart";
  63. EntityDecoderState[EntityDecoderState["NumericDecimal"] = 2] = "NumericDecimal";
  64. EntityDecoderState[EntityDecoderState["NumericHex"] = 3] = "NumericHex";
  65. EntityDecoderState[EntityDecoderState["NamedEntity"] = 4] = "NamedEntity";
  66. })(EntityDecoderState || (EntityDecoderState = {}));
  67. var DecodingMode;
  68. (function (DecodingMode) {
  69. /** Entities in text nodes that can end with any character. */
  70. DecodingMode[DecodingMode["Legacy"] = 0] = "Legacy";
  71. /** Only allow entities terminated with a semicolon. */
  72. DecodingMode[DecodingMode["Strict"] = 1] = "Strict";
  73. /** Entities in attributes have limitations on ending characters. */
  74. DecodingMode[DecodingMode["Attribute"] = 2] = "Attribute";
  75. })(DecodingMode || (exports.DecodingMode = DecodingMode = {}));
  76. /**
  77. * Token decoder with support of writing partial entities.
  78. */
  79. class EntityDecoder {
  80. constructor(
  81. /** The tree used to decode entities. */
  82. decodeTree,
  83. /**
  84. * The function that is called when a codepoint is decoded.
  85. *
  86. * For multi-byte named entities, this will be called multiple times,
  87. * with the second codepoint, and the same `consumed` value.
  88. *
  89. * @param codepoint The decoded codepoint.
  90. * @param consumed The number of bytes consumed by the decoder.
  91. */
  92. emitCodePoint,
  93. /** An object that is used to produce errors. */
  94. errors) {
  95. this.decodeTree = decodeTree;
  96. this.emitCodePoint = emitCodePoint;
  97. this.errors = errors;
  98. /** The current state of the decoder. */
  99. this.state = EntityDecoderState.EntityStart;
  100. /** Characters that were consumed while parsing an entity. */
  101. this.consumed = 1;
  102. /**
  103. * The result of the entity.
  104. *
  105. * Either the result index of a numeric entity, or the codepoint of a
  106. * numeric entity.
  107. */
  108. this.result = 0;
  109. /** The current index in the decode tree. */
  110. this.treeIndex = 0;
  111. /** The number of characters that were consumed in excess. */
  112. this.excess = 1;
  113. /** The mode in which the decoder is operating. */
  114. this.decodeMode = DecodingMode.Strict;
  115. }
  116. /** Resets the instance to make it reusable. */
  117. startEntity(decodeMode) {
  118. this.decodeMode = decodeMode;
  119. this.state = EntityDecoderState.EntityStart;
  120. this.result = 0;
  121. this.treeIndex = 0;
  122. this.excess = 1;
  123. this.consumed = 1;
  124. }
  125. /**
  126. * Write an entity to the decoder. This can be called multiple times with partial entities.
  127. * If the entity is incomplete, the decoder will return -1.
  128. *
  129. * Mirrors the implementation of `getDecoder`, but with the ability to stop decoding if the
  130. * entity is incomplete, and resume when the next string is written.
  131. *
  132. * @param input The string containing the entity (or a continuation of the entity).
  133. * @param offset The offset at which the entity begins. Should be 0 if this is not the first call.
  134. * @returns The number of characters that were consumed, or -1 if the entity is incomplete.
  135. */
  136. write(input, offset) {
  137. switch (this.state) {
  138. case EntityDecoderState.EntityStart: {
  139. if (input.charCodeAt(offset) === CharCodes.NUM) {
  140. this.state = EntityDecoderState.NumericStart;
  141. this.consumed += 1;
  142. return this.stateNumericStart(input, offset + 1);
  143. }
  144. this.state = EntityDecoderState.NamedEntity;
  145. return this.stateNamedEntity(input, offset);
  146. }
  147. case EntityDecoderState.NumericStart: {
  148. return this.stateNumericStart(input, offset);
  149. }
  150. case EntityDecoderState.NumericDecimal: {
  151. return this.stateNumericDecimal(input, offset);
  152. }
  153. case EntityDecoderState.NumericHex: {
  154. return this.stateNumericHex(input, offset);
  155. }
  156. case EntityDecoderState.NamedEntity: {
  157. return this.stateNamedEntity(input, offset);
  158. }
  159. }
  160. }
  161. /**
  162. * Switches between the numeric decimal and hexadecimal states.
  163. *
  164. * Equivalent to the `Numeric character reference state` in the HTML spec.
  165. *
  166. * @param input The string containing the entity (or a continuation of the entity).
  167. * @param offset The current offset.
  168. * @returns The number of characters that were consumed, or -1 if the entity is incomplete.
  169. */
  170. stateNumericStart(input, offset) {
  171. if (offset >= input.length) {
  172. return -1;
  173. }
  174. if ((input.charCodeAt(offset) | TO_LOWER_BIT) === CharCodes.LOWER_X) {
  175. this.state = EntityDecoderState.NumericHex;
  176. this.consumed += 1;
  177. return this.stateNumericHex(input, offset + 1);
  178. }
  179. this.state = EntityDecoderState.NumericDecimal;
  180. return this.stateNumericDecimal(input, offset);
  181. }
  182. addToNumericResult(input, start, end, base) {
  183. if (start !== end) {
  184. const digitCount = end - start;
  185. this.result =
  186. this.result * Math.pow(base, digitCount) +
  187. Number.parseInt(input.substr(start, digitCount), base);
  188. this.consumed += digitCount;
  189. }
  190. }
  191. /**
  192. * Parses a hexadecimal numeric entity.
  193. *
  194. * Equivalent to the `Hexademical character reference state` in the HTML spec.
  195. *
  196. * @param input The string containing the entity (or a continuation of the entity).
  197. * @param offset The current offset.
  198. * @returns The number of characters that were consumed, or -1 if the entity is incomplete.
  199. */
  200. stateNumericHex(input, offset) {
  201. const startIndex = offset;
  202. while (offset < input.length) {
  203. const char = input.charCodeAt(offset);
  204. if (isNumber(char) || isHexadecimalCharacter(char)) {
  205. offset += 1;
  206. }
  207. else {
  208. this.addToNumericResult(input, startIndex, offset, 16);
  209. return this.emitNumericEntity(char, 3);
  210. }
  211. }
  212. this.addToNumericResult(input, startIndex, offset, 16);
  213. return -1;
  214. }
  215. /**
  216. * Parses a decimal numeric entity.
  217. *
  218. * Equivalent to the `Decimal character reference state` in the HTML spec.
  219. *
  220. * @param input The string containing the entity (or a continuation of the entity).
  221. * @param offset The current offset.
  222. * @returns The number of characters that were consumed, or -1 if the entity is incomplete.
  223. */
  224. stateNumericDecimal(input, offset) {
  225. const startIndex = offset;
  226. while (offset < input.length) {
  227. const char = input.charCodeAt(offset);
  228. if (isNumber(char)) {
  229. offset += 1;
  230. }
  231. else {
  232. this.addToNumericResult(input, startIndex, offset, 10);
  233. return this.emitNumericEntity(char, 2);
  234. }
  235. }
  236. this.addToNumericResult(input, startIndex, offset, 10);
  237. return -1;
  238. }
  239. /**
  240. * Validate and emit a numeric entity.
  241. *
  242. * Implements the logic from the `Hexademical character reference start
  243. * state` and `Numeric character reference end state` in the HTML spec.
  244. *
  245. * @param lastCp The last code point of the entity. Used to see if the
  246. * entity was terminated with a semicolon.
  247. * @param expectedLength The minimum number of characters that should be
  248. * consumed. Used to validate that at least one digit
  249. * was consumed.
  250. * @returns The number of characters that were consumed.
  251. */
  252. emitNumericEntity(lastCp, expectedLength) {
  253. var _a;
  254. // Ensure we consumed at least one digit.
  255. if (this.consumed <= expectedLength) {
  256. (_a = this.errors) === null || _a === void 0 ? void 0 : _a.absenceOfDigitsInNumericCharacterReference(this.consumed);
  257. return 0;
  258. }
  259. // Figure out if this is a legit end of the entity
  260. if (lastCp === CharCodes.SEMI) {
  261. this.consumed += 1;
  262. }
  263. else if (this.decodeMode === DecodingMode.Strict) {
  264. return 0;
  265. }
  266. this.emitCodePoint((0, decode_codepoint_js_1.replaceCodePoint)(this.result), this.consumed);
  267. if (this.errors) {
  268. if (lastCp !== CharCodes.SEMI) {
  269. this.errors.missingSemicolonAfterCharacterReference();
  270. }
  271. this.errors.validateNumericCharacterReference(this.result);
  272. }
  273. return this.consumed;
  274. }
  275. /**
  276. * Parses a named entity.
  277. *
  278. * Equivalent to the `Named character reference state` in the HTML spec.
  279. *
  280. * @param input The string containing the entity (or a continuation of the entity).
  281. * @param offset The current offset.
  282. * @returns The number of characters that were consumed, or -1 if the entity is incomplete.
  283. */
  284. stateNamedEntity(input, offset) {
  285. const { decodeTree } = this;
  286. let current = decodeTree[this.treeIndex];
  287. // The mask is the number of bytes of the value, including the current byte.
  288. let valueLength = (current & BinTrieFlags.VALUE_LENGTH) >> 14;
  289. for (; offset < input.length; offset++, this.excess++) {
  290. const char = input.charCodeAt(offset);
  291. this.treeIndex = determineBranch(decodeTree, current, this.treeIndex + Math.max(1, valueLength), char);
  292. if (this.treeIndex < 0) {
  293. return this.result === 0 ||
  294. // If we are parsing an attribute
  295. (this.decodeMode === DecodingMode.Attribute &&
  296. // We shouldn't have consumed any characters after the entity,
  297. (valueLength === 0 ||
  298. // And there should be no invalid characters.
  299. isEntityInAttributeInvalidEnd(char)))
  300. ? 0
  301. : this.emitNotTerminatedNamedEntity();
  302. }
  303. current = decodeTree[this.treeIndex];
  304. valueLength = (current & BinTrieFlags.VALUE_LENGTH) >> 14;
  305. // If the branch is a value, store it and continue
  306. if (valueLength !== 0) {
  307. // If the entity is terminated by a semicolon, we are done.
  308. if (char === CharCodes.SEMI) {
  309. return this.emitNamedEntityData(this.treeIndex, valueLength, this.consumed + this.excess);
  310. }
  311. // If we encounter a non-terminated (legacy) entity while parsing strictly, then ignore it.
  312. if (this.decodeMode !== DecodingMode.Strict) {
  313. this.result = this.treeIndex;
  314. this.consumed += this.excess;
  315. this.excess = 0;
  316. }
  317. }
  318. }
  319. return -1;
  320. }
  321. /**
  322. * Emit a named entity that was not terminated with a semicolon.
  323. *
  324. * @returns The number of characters consumed.
  325. */
  326. emitNotTerminatedNamedEntity() {
  327. var _a;
  328. const { result, decodeTree } = this;
  329. const valueLength = (decodeTree[result] & BinTrieFlags.VALUE_LENGTH) >> 14;
  330. this.emitNamedEntityData(result, valueLength, this.consumed);
  331. (_a = this.errors) === null || _a === void 0 ? void 0 : _a.missingSemicolonAfterCharacterReference();
  332. return this.consumed;
  333. }
  334. /**
  335. * Emit a named entity.
  336. *
  337. * @param result The index of the entity in the decode tree.
  338. * @param valueLength The number of bytes in the entity.
  339. * @param consumed The number of characters consumed.
  340. *
  341. * @returns The number of characters consumed.
  342. */
  343. emitNamedEntityData(result, valueLength, consumed) {
  344. const { decodeTree } = this;
  345. this.emitCodePoint(valueLength === 1
  346. ? decodeTree[result] & ~BinTrieFlags.VALUE_LENGTH
  347. : decodeTree[result + 1], consumed);
  348. if (valueLength === 3) {
  349. // For multi-byte values, we need to emit the second byte.
  350. this.emitCodePoint(decodeTree[result + 2], consumed);
  351. }
  352. return consumed;
  353. }
  354. /**
  355. * Signal to the parser that the end of the input was reached.
  356. *
  357. * Remaining data will be emitted and relevant errors will be produced.
  358. *
  359. * @returns The number of characters consumed.
  360. */
  361. end() {
  362. var _a;
  363. switch (this.state) {
  364. case EntityDecoderState.NamedEntity: {
  365. // Emit a named entity if we have one.
  366. return this.result !== 0 &&
  367. (this.decodeMode !== DecodingMode.Attribute ||
  368. this.result === this.treeIndex)
  369. ? this.emitNotTerminatedNamedEntity()
  370. : 0;
  371. }
  372. // Otherwise, emit a numeric entity if we have one.
  373. case EntityDecoderState.NumericDecimal: {
  374. return this.emitNumericEntity(0, 2);
  375. }
  376. case EntityDecoderState.NumericHex: {
  377. return this.emitNumericEntity(0, 3);
  378. }
  379. case EntityDecoderState.NumericStart: {
  380. (_a = this.errors) === null || _a === void 0 ? void 0 : _a.absenceOfDigitsInNumericCharacterReference(this.consumed);
  381. return 0;
  382. }
  383. case EntityDecoderState.EntityStart: {
  384. // Return 0 if we have no entity.
  385. return 0;
  386. }
  387. }
  388. }
  389. }
  390. exports.EntityDecoder = EntityDecoder;
  391. /**
  392. * Creates a function that decodes entities in a string.
  393. *
  394. * @param decodeTree The decode tree.
  395. * @returns A function that decodes entities in a string.
  396. */
  397. function getDecoder(decodeTree) {
  398. let returnValue = "";
  399. const decoder = new EntityDecoder(decodeTree, (data) => (returnValue += (0, decode_codepoint_js_1.fromCodePoint)(data)));
  400. return function decodeWithTrie(input, decodeMode) {
  401. let lastIndex = 0;
  402. let offset = 0;
  403. while ((offset = input.indexOf("&", offset)) >= 0) {
  404. returnValue += input.slice(lastIndex, offset);
  405. decoder.startEntity(decodeMode);
  406. const length = decoder.write(input,
  407. // Skip the "&"
  408. offset + 1);
  409. if (length < 0) {
  410. lastIndex = offset + decoder.end();
  411. break;
  412. }
  413. lastIndex = offset + length;
  414. // If `length` is 0, skip the current `&` and continue.
  415. offset = length === 0 ? lastIndex + 1 : lastIndex;
  416. }
  417. const result = returnValue + input.slice(lastIndex);
  418. // Make sure we don't keep a reference to the final string.
  419. returnValue = "";
  420. return result;
  421. };
  422. }
  423. /**
  424. * Determines the branch of the current node that is taken given the current
  425. * character. This function is used to traverse the trie.
  426. *
  427. * @param decodeTree The trie.
  428. * @param current The current node.
  429. * @param nodeIdx The index right after the current node and its value.
  430. * @param char The current character.
  431. * @returns The index of the next node, or -1 if no branch is taken.
  432. */
  433. function determineBranch(decodeTree, current, nodeIndex, char) {
  434. const branchCount = (current & BinTrieFlags.BRANCH_LENGTH) >> 7;
  435. const jumpOffset = current & BinTrieFlags.JUMP_TABLE;
  436. // Case 1: Single branch encoded in jump offset
  437. if (branchCount === 0) {
  438. return jumpOffset !== 0 && char === jumpOffset ? nodeIndex : -1;
  439. }
  440. // Case 2: Multiple branches encoded in jump table
  441. if (jumpOffset) {
  442. const value = char - jumpOffset;
  443. return value < 0 || value >= branchCount
  444. ? -1
  445. : decodeTree[nodeIndex + value] - 1;
  446. }
  447. // Case 3: Multiple branches encoded in dictionary
  448. // Binary search for the character.
  449. let lo = nodeIndex;
  450. let hi = lo + branchCount - 1;
  451. while (lo <= hi) {
  452. const mid = (lo + hi) >>> 1;
  453. const midValue = decodeTree[mid];
  454. if (midValue < char) {
  455. lo = mid + 1;
  456. }
  457. else if (midValue > char) {
  458. hi = mid - 1;
  459. }
  460. else {
  461. return decodeTree[mid + branchCount];
  462. }
  463. }
  464. return -1;
  465. }
  466. const htmlDecoder = getDecoder(decode_data_html_js_1.default);
  467. const xmlDecoder = getDecoder(decode_data_xml_js_1.default);
  468. /**
  469. * Decodes an HTML string.
  470. *
  471. * @param htmlString The string to decode.
  472. * @param mode The decoding mode.
  473. * @returns The decoded string.
  474. */
  475. function decodeHTML(htmlString, mode = DecodingMode.Legacy) {
  476. return htmlDecoder(htmlString, mode);
  477. }
  478. /**
  479. * Decodes an HTML string in an attribute.
  480. *
  481. * @param htmlAttribute The string to decode.
  482. * @returns The decoded string.
  483. */
  484. function decodeHTMLAttribute(htmlAttribute) {
  485. return htmlDecoder(htmlAttribute, DecodingMode.Attribute);
  486. }
  487. /**
  488. * Decodes an HTML string, requiring all entities to be terminated by a semicolon.
  489. *
  490. * @param htmlString The string to decode.
  491. * @returns The decoded string.
  492. */
  493. function decodeHTMLStrict(htmlString) {
  494. return htmlDecoder(htmlString, DecodingMode.Strict);
  495. }
  496. /**
  497. * Decodes an XML string, requiring all entities to be terminated by a semicolon.
  498. *
  499. * @param xmlString The string to decode.
  500. * @returns The decoded string.
  501. */
  502. function decodeXML(xmlString) {
  503. return xmlDecoder(xmlString, DecodingMode.Strict);
  504. }
  505. // Re-export for use by eg. htmlparser2
  506. var decode_data_html_js_2 = require("./generated/decode-data-html.js");
  507. Object.defineProperty(exports, "htmlDecodeTree", { enumerable: true, get: function () { return __importDefault(decode_data_html_js_2).default; } });
  508. var decode_data_xml_js_2 = require("./generated/decode-data-xml.js");
  509. Object.defineProperty(exports, "xmlDecodeTree", { enumerable: true, get: function () { return __importDefault(decode_data_xml_js_2).default; } });
  510. var decode_codepoint_js_2 = require("./decode-codepoint.js");
  511. Object.defineProperty(exports, "decodeCodePoint", { enumerable: true, get: function () { return __importDefault(decode_codepoint_js_2).default; } });
  512. Object.defineProperty(exports, "replaceCodePoint", { enumerable: true, get: function () { return decode_codepoint_js_2.replaceCodePoint; } });
  513. Object.defineProperty(exports, "fromCodePoint", { enumerable: true, get: function () { return decode_codepoint_js_2.fromCodePoint; } });
  514. //# sourceMappingURL=decode.js.map