Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Tokenizer.js 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. module.exports = Tokenizer;
  2. var decodeCodePoint = require("entities/lib/decode_codepoint.js");
  3. var entityMap = require("entities/maps/entities.json");
  4. var legacyMap = require("entities/maps/legacy.json");
  5. var xmlMap = require("entities/maps/xml.json");
  6. var i = 0;
  7. var TEXT = i++;
  8. var BEFORE_TAG_NAME = i++; //after <
  9. var IN_TAG_NAME = i++;
  10. var IN_SELF_CLOSING_TAG = i++;
  11. var BEFORE_CLOSING_TAG_NAME = i++;
  12. var IN_CLOSING_TAG_NAME = i++;
  13. var AFTER_CLOSING_TAG_NAME = i++;
  14. //attributes
  15. var BEFORE_ATTRIBUTE_NAME = i++;
  16. var IN_ATTRIBUTE_NAME = i++;
  17. var AFTER_ATTRIBUTE_NAME = i++;
  18. var BEFORE_ATTRIBUTE_VALUE = i++;
  19. var IN_ATTRIBUTE_VALUE_DQ = i++; // "
  20. var IN_ATTRIBUTE_VALUE_SQ = i++; // '
  21. var IN_ATTRIBUTE_VALUE_NQ = i++;
  22. //declarations
  23. var BEFORE_DECLARATION = i++; // !
  24. var IN_DECLARATION = i++;
  25. //processing instructions
  26. var IN_PROCESSING_INSTRUCTION = i++; // ?
  27. //comments
  28. var BEFORE_COMMENT = i++;
  29. var IN_COMMENT = i++;
  30. var AFTER_COMMENT_1 = i++;
  31. var AFTER_COMMENT_2 = i++;
  32. //cdata
  33. var BEFORE_CDATA_1 = i++; // [
  34. var BEFORE_CDATA_2 = i++; // C
  35. var BEFORE_CDATA_3 = i++; // D
  36. var BEFORE_CDATA_4 = i++; // A
  37. var BEFORE_CDATA_5 = i++; // T
  38. var BEFORE_CDATA_6 = i++; // A
  39. var IN_CDATA = i++; // [
  40. var AFTER_CDATA_1 = i++; // ]
  41. var AFTER_CDATA_2 = i++; // ]
  42. //special tags
  43. var BEFORE_SPECIAL = i++; //S
  44. var BEFORE_SPECIAL_END = i++; //S
  45. var BEFORE_SCRIPT_1 = i++; //C
  46. var BEFORE_SCRIPT_2 = i++; //R
  47. var BEFORE_SCRIPT_3 = i++; //I
  48. var BEFORE_SCRIPT_4 = i++; //P
  49. var BEFORE_SCRIPT_5 = i++; //T
  50. var AFTER_SCRIPT_1 = i++; //C
  51. var AFTER_SCRIPT_2 = i++; //R
  52. var AFTER_SCRIPT_3 = i++; //I
  53. var AFTER_SCRIPT_4 = i++; //P
  54. var AFTER_SCRIPT_5 = i++; //T
  55. var BEFORE_STYLE_1 = i++; //T
  56. var BEFORE_STYLE_2 = i++; //Y
  57. var BEFORE_STYLE_3 = i++; //L
  58. var BEFORE_STYLE_4 = i++; //E
  59. var AFTER_STYLE_1 = i++; //T
  60. var AFTER_STYLE_2 = i++; //Y
  61. var AFTER_STYLE_3 = i++; //L
  62. var AFTER_STYLE_4 = i++; //E
  63. var BEFORE_ENTITY = i++; //&
  64. var BEFORE_NUMERIC_ENTITY = i++; //#
  65. var IN_NAMED_ENTITY = i++;
  66. var IN_NUMERIC_ENTITY = i++;
  67. var IN_HEX_ENTITY = i++; //X
  68. var j = 0;
  69. var SPECIAL_NONE = j++;
  70. var SPECIAL_SCRIPT = j++;
  71. var SPECIAL_STYLE = j++;
  72. function whitespace(c) {
  73. return c === " " || c === "\n" || c === "\t" || c === "\f" || c === "\r";
  74. }
  75. function ifElseState(upper, SUCCESS, FAILURE) {
  76. var lower = upper.toLowerCase();
  77. if (upper === lower) {
  78. return function(c) {
  79. if (c === lower) {
  80. this._state = SUCCESS;
  81. } else {
  82. this._state = FAILURE;
  83. this._index--;
  84. }
  85. };
  86. } else {
  87. return function(c) {
  88. if (c === lower || c === upper) {
  89. this._state = SUCCESS;
  90. } else {
  91. this._state = FAILURE;
  92. this._index--;
  93. }
  94. };
  95. }
  96. }
  97. function consumeSpecialNameChar(upper, NEXT_STATE) {
  98. var lower = upper.toLowerCase();
  99. return function(c) {
  100. if (c === lower || c === upper) {
  101. this._state = NEXT_STATE;
  102. } else {
  103. this._state = IN_TAG_NAME;
  104. this._index--; //consume the token again
  105. }
  106. };
  107. }
  108. function Tokenizer(options, cbs) {
  109. this._state = TEXT;
  110. this._buffer = "";
  111. this._sectionStart = 0;
  112. this._index = 0;
  113. this._bufferOffset = 0; //chars removed from _buffer
  114. this._baseState = TEXT;
  115. this._special = SPECIAL_NONE;
  116. this._cbs = cbs;
  117. this._running = true;
  118. this._ended = false;
  119. this._xmlMode = !!(options && options.xmlMode);
  120. this._decodeEntities = !!(options && options.decodeEntities);
  121. }
  122. Tokenizer.prototype._stateText = function(c) {
  123. if (c === "<") {
  124. if (this._index > this._sectionStart) {
  125. this._cbs.ontext(this._getSection());
  126. }
  127. this._state = BEFORE_TAG_NAME;
  128. this._sectionStart = this._index;
  129. } else if (
  130. this._decodeEntities &&
  131. this._special === SPECIAL_NONE &&
  132. c === "&"
  133. ) {
  134. if (this._index > this._sectionStart) {
  135. this._cbs.ontext(this._getSection());
  136. }
  137. this._baseState = TEXT;
  138. this._state = BEFORE_ENTITY;
  139. this._sectionStart = this._index;
  140. }
  141. };
  142. Tokenizer.prototype._stateBeforeTagName = function(c) {
  143. if (c === "/") {
  144. this._state = BEFORE_CLOSING_TAG_NAME;
  145. } else if (c === "<") {
  146. this._cbs.ontext(this._getSection());
  147. this._sectionStart = this._index;
  148. } else if (c === ">" || this._special !== SPECIAL_NONE || whitespace(c)) {
  149. this._state = TEXT;
  150. } else if (c === "!") {
  151. this._state = BEFORE_DECLARATION;
  152. this._sectionStart = this._index + 1;
  153. } else if (c === "?") {
  154. this._state = IN_PROCESSING_INSTRUCTION;
  155. this._sectionStart = this._index + 1;
  156. } else {
  157. this._state =
  158. !this._xmlMode && (c === "s" || c === "S")
  159. ? BEFORE_SPECIAL
  160. : IN_TAG_NAME;
  161. this._sectionStart = this._index;
  162. }
  163. };
  164. Tokenizer.prototype._stateInTagName = function(c) {
  165. if (c === "/" || c === ">" || whitespace(c)) {
  166. this._emitToken("onopentagname");
  167. this._state = BEFORE_ATTRIBUTE_NAME;
  168. this._index--;
  169. }
  170. };
  171. Tokenizer.prototype._stateBeforeCloseingTagName = function(c) {
  172. if (whitespace(c));
  173. else if (c === ">") {
  174. this._state = TEXT;
  175. } else if (this._special !== SPECIAL_NONE) {
  176. if (c === "s" || c === "S") {
  177. this._state = BEFORE_SPECIAL_END;
  178. } else {
  179. this._state = TEXT;
  180. this._index--;
  181. }
  182. } else {
  183. this._state = IN_CLOSING_TAG_NAME;
  184. this._sectionStart = this._index;
  185. }
  186. };
  187. Tokenizer.prototype._stateInCloseingTagName = function(c) {
  188. if (c === ">" || whitespace(c)) {
  189. this._emitToken("onclosetag");
  190. this._state = AFTER_CLOSING_TAG_NAME;
  191. this._index--;
  192. }
  193. };
  194. Tokenizer.prototype._stateAfterCloseingTagName = function(c) {
  195. //skip everything until ">"
  196. if (c === ">") {
  197. this._state = TEXT;
  198. this._sectionStart = this._index + 1;
  199. }
  200. };
  201. Tokenizer.prototype._stateBeforeAttributeName = function(c) {
  202. if (c === ">") {
  203. this._cbs.onopentagend();
  204. this._state = TEXT;
  205. this._sectionStart = this._index + 1;
  206. } else if (c === "/") {
  207. this._state = IN_SELF_CLOSING_TAG;
  208. } else if (!whitespace(c)) {
  209. this._state = IN_ATTRIBUTE_NAME;
  210. this._sectionStart = this._index;
  211. }
  212. };
  213. Tokenizer.prototype._stateInSelfClosingTag = function(c) {
  214. if (c === ">") {
  215. this._cbs.onselfclosingtag();
  216. this._state = TEXT;
  217. this._sectionStart = this._index + 1;
  218. } else if (!whitespace(c)) {
  219. this._state = BEFORE_ATTRIBUTE_NAME;
  220. this._index--;
  221. }
  222. };
  223. Tokenizer.prototype._stateInAttributeName = function(c) {
  224. if (c === "=" || c === "/" || c === ">" || whitespace(c)) {
  225. this._cbs.onattribname(this._getSection());
  226. this._sectionStart = -1;
  227. this._state = AFTER_ATTRIBUTE_NAME;
  228. this._index--;
  229. }
  230. };
  231. Tokenizer.prototype._stateAfterAttributeName = function(c) {
  232. if (c === "=") {
  233. this._state = BEFORE_ATTRIBUTE_VALUE;
  234. } else if (c === "/" || c === ">") {
  235. this._cbs.onattribend();
  236. this._state = BEFORE_ATTRIBUTE_NAME;
  237. this._index--;
  238. } else if (!whitespace(c)) {
  239. this._cbs.onattribend();
  240. this._state = IN_ATTRIBUTE_NAME;
  241. this._sectionStart = this._index;
  242. }
  243. };
  244. Tokenizer.prototype._stateBeforeAttributeValue = function(c) {
  245. if (c === '"') {
  246. this._state = IN_ATTRIBUTE_VALUE_DQ;
  247. this._sectionStart = this._index + 1;
  248. } else if (c === "'") {
  249. this._state = IN_ATTRIBUTE_VALUE_SQ;
  250. this._sectionStart = this._index + 1;
  251. } else if (!whitespace(c)) {
  252. this._state = IN_ATTRIBUTE_VALUE_NQ;
  253. this._sectionStart = this._index;
  254. this._index--; //reconsume token
  255. }
  256. };
  257. Tokenizer.prototype._stateInAttributeValueDoubleQuotes = function(c) {
  258. if (c === '"') {
  259. this._emitToken("onattribdata");
  260. this._cbs.onattribend();
  261. this._state = BEFORE_ATTRIBUTE_NAME;
  262. } else if (this._decodeEntities && c === "&") {
  263. this._emitToken("onattribdata");
  264. this._baseState = this._state;
  265. this._state = BEFORE_ENTITY;
  266. this._sectionStart = this._index;
  267. }
  268. };
  269. Tokenizer.prototype._stateInAttributeValueSingleQuotes = function(c) {
  270. if (c === "'") {
  271. this._emitToken("onattribdata");
  272. this._cbs.onattribend();
  273. this._state = BEFORE_ATTRIBUTE_NAME;
  274. } else if (this._decodeEntities && c === "&") {
  275. this._emitToken("onattribdata");
  276. this._baseState = this._state;
  277. this._state = BEFORE_ENTITY;
  278. this._sectionStart = this._index;
  279. }
  280. };
  281. Tokenizer.prototype._stateInAttributeValueNoQuotes = function(c) {
  282. if (whitespace(c) || c === ">") {
  283. this._emitToken("onattribdata");
  284. this._cbs.onattribend();
  285. this._state = BEFORE_ATTRIBUTE_NAME;
  286. this._index--;
  287. } else if (this._decodeEntities && c === "&") {
  288. this._emitToken("onattribdata");
  289. this._baseState = this._state;
  290. this._state = BEFORE_ENTITY;
  291. this._sectionStart = this._index;
  292. }
  293. };
  294. Tokenizer.prototype._stateBeforeDeclaration = function(c) {
  295. this._state =
  296. c === "["
  297. ? BEFORE_CDATA_1
  298. : c === "-"
  299. ? BEFORE_COMMENT
  300. : IN_DECLARATION;
  301. };
  302. Tokenizer.prototype._stateInDeclaration = function(c) {
  303. if (c === ">") {
  304. this._cbs.ondeclaration(this._getSection());
  305. this._state = TEXT;
  306. this._sectionStart = this._index + 1;
  307. }
  308. };
  309. Tokenizer.prototype._stateInProcessingInstruction = function(c) {
  310. if (c === ">") {
  311. this._cbs.onprocessinginstruction(this._getSection());
  312. this._state = TEXT;
  313. this._sectionStart = this._index + 1;
  314. }
  315. };
  316. Tokenizer.prototype._stateBeforeComment = function(c) {
  317. if (c === "-") {
  318. this._state = IN_COMMENT;
  319. this._sectionStart = this._index + 1;
  320. } else {
  321. this._state = IN_DECLARATION;
  322. }
  323. };
  324. Tokenizer.prototype._stateInComment = function(c) {
  325. if (c === "-") this._state = AFTER_COMMENT_1;
  326. };
  327. Tokenizer.prototype._stateAfterComment1 = function(c) {
  328. if (c === "-") {
  329. this._state = AFTER_COMMENT_2;
  330. } else {
  331. this._state = IN_COMMENT;
  332. }
  333. };
  334. Tokenizer.prototype._stateAfterComment2 = function(c) {
  335. if (c === ">") {
  336. //remove 2 trailing chars
  337. this._cbs.oncomment(
  338. this._buffer.substring(this._sectionStart, this._index - 2)
  339. );
  340. this._state = TEXT;
  341. this._sectionStart = this._index + 1;
  342. } else if (c !== "-") {
  343. this._state = IN_COMMENT;
  344. }
  345. // else: stay in AFTER_COMMENT_2 (`--->`)
  346. };
  347. Tokenizer.prototype._stateBeforeCdata1 = ifElseState(
  348. "C",
  349. BEFORE_CDATA_2,
  350. IN_DECLARATION
  351. );
  352. Tokenizer.prototype._stateBeforeCdata2 = ifElseState(
  353. "D",
  354. BEFORE_CDATA_3,
  355. IN_DECLARATION
  356. );
  357. Tokenizer.prototype._stateBeforeCdata3 = ifElseState(
  358. "A",
  359. BEFORE_CDATA_4,
  360. IN_DECLARATION
  361. );
  362. Tokenizer.prototype._stateBeforeCdata4 = ifElseState(
  363. "T",
  364. BEFORE_CDATA_5,
  365. IN_DECLARATION
  366. );
  367. Tokenizer.prototype._stateBeforeCdata5 = ifElseState(
  368. "A",
  369. BEFORE_CDATA_6,
  370. IN_DECLARATION
  371. );
  372. Tokenizer.prototype._stateBeforeCdata6 = function(c) {
  373. if (c === "[") {
  374. this._state = IN_CDATA;
  375. this._sectionStart = this._index + 1;
  376. } else {
  377. this._state = IN_DECLARATION;
  378. this._index--;
  379. }
  380. };
  381. Tokenizer.prototype._stateInCdata = function(c) {
  382. if (c === "]") this._state = AFTER_CDATA_1;
  383. };
  384. Tokenizer.prototype._stateAfterCdata1 = function(c) {
  385. if (c === "]") this._state = AFTER_CDATA_2;
  386. else this._state = IN_CDATA;
  387. };
  388. Tokenizer.prototype._stateAfterCdata2 = function(c) {
  389. if (c === ">") {
  390. //remove 2 trailing chars
  391. this._cbs.oncdata(
  392. this._buffer.substring(this._sectionStart, this._index - 2)
  393. );
  394. this._state = TEXT;
  395. this._sectionStart = this._index + 1;
  396. } else if (c !== "]") {
  397. this._state = IN_CDATA;
  398. }
  399. //else: stay in AFTER_CDATA_2 (`]]]>`)
  400. };
  401. Tokenizer.prototype._stateBeforeSpecial = function(c) {
  402. if (c === "c" || c === "C") {
  403. this._state = BEFORE_SCRIPT_1;
  404. } else if (c === "t" || c === "T") {
  405. this._state = BEFORE_STYLE_1;
  406. } else {
  407. this._state = IN_TAG_NAME;
  408. this._index--; //consume the token again
  409. }
  410. };
  411. Tokenizer.prototype._stateBeforeSpecialEnd = function(c) {
  412. if (this._special === SPECIAL_SCRIPT && (c === "c" || c === "C")) {
  413. this._state = AFTER_SCRIPT_1;
  414. } else if (this._special === SPECIAL_STYLE && (c === "t" || c === "T")) {
  415. this._state = AFTER_STYLE_1;
  416. } else this._state = TEXT;
  417. };
  418. Tokenizer.prototype._stateBeforeScript1 = consumeSpecialNameChar(
  419. "R",
  420. BEFORE_SCRIPT_2
  421. );
  422. Tokenizer.prototype._stateBeforeScript2 = consumeSpecialNameChar(
  423. "I",
  424. BEFORE_SCRIPT_3
  425. );
  426. Tokenizer.prototype._stateBeforeScript3 = consumeSpecialNameChar(
  427. "P",
  428. BEFORE_SCRIPT_4
  429. );
  430. Tokenizer.prototype._stateBeforeScript4 = consumeSpecialNameChar(
  431. "T",
  432. BEFORE_SCRIPT_5
  433. );
  434. Tokenizer.prototype._stateBeforeScript5 = function(c) {
  435. if (c === "/" || c === ">" || whitespace(c)) {
  436. this._special = SPECIAL_SCRIPT;
  437. }
  438. this._state = IN_TAG_NAME;
  439. this._index--; //consume the token again
  440. };
  441. Tokenizer.prototype._stateAfterScript1 = ifElseState("R", AFTER_SCRIPT_2, TEXT);
  442. Tokenizer.prototype._stateAfterScript2 = ifElseState("I", AFTER_SCRIPT_3, TEXT);
  443. Tokenizer.prototype._stateAfterScript3 = ifElseState("P", AFTER_SCRIPT_4, TEXT);
  444. Tokenizer.prototype._stateAfterScript4 = ifElseState("T", AFTER_SCRIPT_5, TEXT);
  445. Tokenizer.prototype._stateAfterScript5 = function(c) {
  446. if (c === ">" || whitespace(c)) {
  447. this._special = SPECIAL_NONE;
  448. this._state = IN_CLOSING_TAG_NAME;
  449. this._sectionStart = this._index - 6;
  450. this._index--; //reconsume the token
  451. } else this._state = TEXT;
  452. };
  453. Tokenizer.prototype._stateBeforeStyle1 = consumeSpecialNameChar(
  454. "Y",
  455. BEFORE_STYLE_2
  456. );
  457. Tokenizer.prototype._stateBeforeStyle2 = consumeSpecialNameChar(
  458. "L",
  459. BEFORE_STYLE_3
  460. );
  461. Tokenizer.prototype._stateBeforeStyle3 = consumeSpecialNameChar(
  462. "E",
  463. BEFORE_STYLE_4
  464. );
  465. Tokenizer.prototype._stateBeforeStyle4 = function(c) {
  466. if (c === "/" || c === ">" || whitespace(c)) {
  467. this._special = SPECIAL_STYLE;
  468. }
  469. this._state = IN_TAG_NAME;
  470. this._index--; //consume the token again
  471. };
  472. Tokenizer.prototype._stateAfterStyle1 = ifElseState("Y", AFTER_STYLE_2, TEXT);
  473. Tokenizer.prototype._stateAfterStyle2 = ifElseState("L", AFTER_STYLE_3, TEXT);
  474. Tokenizer.prototype._stateAfterStyle3 = ifElseState("E", AFTER_STYLE_4, TEXT);
  475. Tokenizer.prototype._stateAfterStyle4 = function(c) {
  476. if (c === ">" || whitespace(c)) {
  477. this._special = SPECIAL_NONE;
  478. this._state = IN_CLOSING_TAG_NAME;
  479. this._sectionStart = this._index - 5;
  480. this._index--; //reconsume the token
  481. } else this._state = TEXT;
  482. };
  483. Tokenizer.prototype._stateBeforeEntity = ifElseState(
  484. "#",
  485. BEFORE_NUMERIC_ENTITY,
  486. IN_NAMED_ENTITY
  487. );
  488. Tokenizer.prototype._stateBeforeNumericEntity = ifElseState(
  489. "X",
  490. IN_HEX_ENTITY,
  491. IN_NUMERIC_ENTITY
  492. );
  493. //for entities terminated with a semicolon
  494. Tokenizer.prototype._parseNamedEntityStrict = function() {
  495. //offset = 1
  496. if (this._sectionStart + 1 < this._index) {
  497. var entity = this._buffer.substring(
  498. this._sectionStart + 1,
  499. this._index
  500. ),
  501. map = this._xmlMode ? xmlMap : entityMap;
  502. if (map.hasOwnProperty(entity)) {
  503. this._emitPartial(map[entity]);
  504. this._sectionStart = this._index + 1;
  505. }
  506. }
  507. };
  508. //parses legacy entities (without trailing semicolon)
  509. Tokenizer.prototype._parseLegacyEntity = function() {
  510. var start = this._sectionStart + 1,
  511. limit = this._index - start;
  512. if (limit > 6) limit = 6; //the max length of legacy entities is 6
  513. while (limit >= 2) {
  514. //the min length of legacy entities is 2
  515. var entity = this._buffer.substr(start, limit);
  516. if (legacyMap.hasOwnProperty(entity)) {
  517. this._emitPartial(legacyMap[entity]);
  518. this._sectionStart += limit + 1;
  519. return;
  520. } else {
  521. limit--;
  522. }
  523. }
  524. };
  525. Tokenizer.prototype._stateInNamedEntity = function(c) {
  526. if (c === ";") {
  527. this._parseNamedEntityStrict();
  528. if (this._sectionStart + 1 < this._index && !this._xmlMode) {
  529. this._parseLegacyEntity();
  530. }
  531. this._state = this._baseState;
  532. } else if (
  533. (c < "a" || c > "z") &&
  534. (c < "A" || c > "Z") &&
  535. (c < "0" || c > "9")
  536. ) {
  537. if (this._xmlMode);
  538. else if (this._sectionStart + 1 === this._index);
  539. else if (this._baseState !== TEXT) {
  540. if (c !== "=") {
  541. this._parseNamedEntityStrict();
  542. }
  543. } else {
  544. this._parseLegacyEntity();
  545. }
  546. this._state = this._baseState;
  547. this._index--;
  548. }
  549. };
  550. Tokenizer.prototype._decodeNumericEntity = function(offset, base) {
  551. var sectionStart = this._sectionStart + offset;
  552. if (sectionStart !== this._index) {
  553. //parse entity
  554. var entity = this._buffer.substring(sectionStart, this._index);
  555. var parsed = parseInt(entity, base);
  556. this._emitPartial(decodeCodePoint(parsed));
  557. this._sectionStart = this._index;
  558. } else {
  559. this._sectionStart--;
  560. }
  561. this._state = this._baseState;
  562. };
  563. Tokenizer.prototype._stateInNumericEntity = function(c) {
  564. if (c === ";") {
  565. this._decodeNumericEntity(2, 10);
  566. this._sectionStart++;
  567. } else if (c < "0" || c > "9") {
  568. if (!this._xmlMode) {
  569. this._decodeNumericEntity(2, 10);
  570. } else {
  571. this._state = this._baseState;
  572. }
  573. this._index--;
  574. }
  575. };
  576. Tokenizer.prototype._stateInHexEntity = function(c) {
  577. if (c === ";") {
  578. this._decodeNumericEntity(3, 16);
  579. this._sectionStart++;
  580. } else if (
  581. (c < "a" || c > "f") &&
  582. (c < "A" || c > "F") &&
  583. (c < "0" || c > "9")
  584. ) {
  585. if (!this._xmlMode) {
  586. this._decodeNumericEntity(3, 16);
  587. } else {
  588. this._state = this._baseState;
  589. }
  590. this._index--;
  591. }
  592. };
  593. Tokenizer.prototype._cleanup = function() {
  594. if (this._sectionStart < 0) {
  595. this._buffer = "";
  596. this._bufferOffset += this._index;
  597. this._index = 0;
  598. } else if (this._running) {
  599. if (this._state === TEXT) {
  600. if (this._sectionStart !== this._index) {
  601. this._cbs.ontext(this._buffer.substr(this._sectionStart));
  602. }
  603. this._buffer = "";
  604. this._bufferOffset += this._index;
  605. this._index = 0;
  606. } else if (this._sectionStart === this._index) {
  607. //the section just started
  608. this._buffer = "";
  609. this._bufferOffset += this._index;
  610. this._index = 0;
  611. } else {
  612. //remove everything unnecessary
  613. this._buffer = this._buffer.substr(this._sectionStart);
  614. this._index -= this._sectionStart;
  615. this._bufferOffset += this._sectionStart;
  616. }
  617. this._sectionStart = 0;
  618. }
  619. };
  620. //TODO make events conditional
  621. Tokenizer.prototype.write = function(chunk) {
  622. if (this._ended) this._cbs.onerror(Error(".write() after done!"));
  623. this._buffer += chunk;
  624. this._parse();
  625. };
  626. Tokenizer.prototype._parse = function() {
  627. while (this._index < this._buffer.length && this._running) {
  628. var c = this._buffer.charAt(this._index);
  629. if (this._state === TEXT) {
  630. this._stateText(c);
  631. } else if (this._state === BEFORE_TAG_NAME) {
  632. this._stateBeforeTagName(c);
  633. } else if (this._state === IN_TAG_NAME) {
  634. this._stateInTagName(c);
  635. } else if (this._state === BEFORE_CLOSING_TAG_NAME) {
  636. this._stateBeforeCloseingTagName(c);
  637. } else if (this._state === IN_CLOSING_TAG_NAME) {
  638. this._stateInCloseingTagName(c);
  639. } else if (this._state === AFTER_CLOSING_TAG_NAME) {
  640. this._stateAfterCloseingTagName(c);
  641. } else if (this._state === IN_SELF_CLOSING_TAG) {
  642. this._stateInSelfClosingTag(c);
  643. } else if (this._state === BEFORE_ATTRIBUTE_NAME) {
  644. /*
  645. * attributes
  646. */
  647. this._stateBeforeAttributeName(c);
  648. } else if (this._state === IN_ATTRIBUTE_NAME) {
  649. this._stateInAttributeName(c);
  650. } else if (this._state === AFTER_ATTRIBUTE_NAME) {
  651. this._stateAfterAttributeName(c);
  652. } else if (this._state === BEFORE_ATTRIBUTE_VALUE) {
  653. this._stateBeforeAttributeValue(c);
  654. } else if (this._state === IN_ATTRIBUTE_VALUE_DQ) {
  655. this._stateInAttributeValueDoubleQuotes(c);
  656. } else if (this._state === IN_ATTRIBUTE_VALUE_SQ) {
  657. this._stateInAttributeValueSingleQuotes(c);
  658. } else if (this._state === IN_ATTRIBUTE_VALUE_NQ) {
  659. this._stateInAttributeValueNoQuotes(c);
  660. } else if (this._state === BEFORE_DECLARATION) {
  661. /*
  662. * declarations
  663. */
  664. this._stateBeforeDeclaration(c);
  665. } else if (this._state === IN_DECLARATION) {
  666. this._stateInDeclaration(c);
  667. } else if (this._state === IN_PROCESSING_INSTRUCTION) {
  668. /*
  669. * processing instructions
  670. */
  671. this._stateInProcessingInstruction(c);
  672. } else if (this._state === BEFORE_COMMENT) {
  673. /*
  674. * comments
  675. */
  676. this._stateBeforeComment(c);
  677. } else if (this._state === IN_COMMENT) {
  678. this._stateInComment(c);
  679. } else if (this._state === AFTER_COMMENT_1) {
  680. this._stateAfterComment1(c);
  681. } else if (this._state === AFTER_COMMENT_2) {
  682. this._stateAfterComment2(c);
  683. } else if (this._state === BEFORE_CDATA_1) {
  684. /*
  685. * cdata
  686. */
  687. this._stateBeforeCdata1(c);
  688. } else if (this._state === BEFORE_CDATA_2) {
  689. this._stateBeforeCdata2(c);
  690. } else if (this._state === BEFORE_CDATA_3) {
  691. this._stateBeforeCdata3(c);
  692. } else if (this._state === BEFORE_CDATA_4) {
  693. this._stateBeforeCdata4(c);
  694. } else if (this._state === BEFORE_CDATA_5) {
  695. this._stateBeforeCdata5(c);
  696. } else if (this._state === BEFORE_CDATA_6) {
  697. this._stateBeforeCdata6(c);
  698. } else if (this._state === IN_CDATA) {
  699. this._stateInCdata(c);
  700. } else if (this._state === AFTER_CDATA_1) {
  701. this._stateAfterCdata1(c);
  702. } else if (this._state === AFTER_CDATA_2) {
  703. this._stateAfterCdata2(c);
  704. } else if (this._state === BEFORE_SPECIAL) {
  705. /*
  706. * special tags
  707. */
  708. this._stateBeforeSpecial(c);
  709. } else if (this._state === BEFORE_SPECIAL_END) {
  710. this._stateBeforeSpecialEnd(c);
  711. } else if (this._state === BEFORE_SCRIPT_1) {
  712. /*
  713. * script
  714. */
  715. this._stateBeforeScript1(c);
  716. } else if (this._state === BEFORE_SCRIPT_2) {
  717. this._stateBeforeScript2(c);
  718. } else if (this._state === BEFORE_SCRIPT_3) {
  719. this._stateBeforeScript3(c);
  720. } else if (this._state === BEFORE_SCRIPT_4) {
  721. this._stateBeforeScript4(c);
  722. } else if (this._state === BEFORE_SCRIPT_5) {
  723. this._stateBeforeScript5(c);
  724. } else if (this._state === AFTER_SCRIPT_1) {
  725. this._stateAfterScript1(c);
  726. } else if (this._state === AFTER_SCRIPT_2) {
  727. this._stateAfterScript2(c);
  728. } else if (this._state === AFTER_SCRIPT_3) {
  729. this._stateAfterScript3(c);
  730. } else if (this._state === AFTER_SCRIPT_4) {
  731. this._stateAfterScript4(c);
  732. } else if (this._state === AFTER_SCRIPT_5) {
  733. this._stateAfterScript5(c);
  734. } else if (this._state === BEFORE_STYLE_1) {
  735. /*
  736. * style
  737. */
  738. this._stateBeforeStyle1(c);
  739. } else if (this._state === BEFORE_STYLE_2) {
  740. this._stateBeforeStyle2(c);
  741. } else if (this._state === BEFORE_STYLE_3) {
  742. this._stateBeforeStyle3(c);
  743. } else if (this._state === BEFORE_STYLE_4) {
  744. this._stateBeforeStyle4(c);
  745. } else if (this._state === AFTER_STYLE_1) {
  746. this._stateAfterStyle1(c);
  747. } else if (this._state === AFTER_STYLE_2) {
  748. this._stateAfterStyle2(c);
  749. } else if (this._state === AFTER_STYLE_3) {
  750. this._stateAfterStyle3(c);
  751. } else if (this._state === AFTER_STYLE_4) {
  752. this._stateAfterStyle4(c);
  753. } else if (this._state === BEFORE_ENTITY) {
  754. /*
  755. * entities
  756. */
  757. this._stateBeforeEntity(c);
  758. } else if (this._state === BEFORE_NUMERIC_ENTITY) {
  759. this._stateBeforeNumericEntity(c);
  760. } else if (this._state === IN_NAMED_ENTITY) {
  761. this._stateInNamedEntity(c);
  762. } else if (this._state === IN_NUMERIC_ENTITY) {
  763. this._stateInNumericEntity(c);
  764. } else if (this._state === IN_HEX_ENTITY) {
  765. this._stateInHexEntity(c);
  766. } else {
  767. this._cbs.onerror(Error("unknown _state"), this._state);
  768. }
  769. this._index++;
  770. }
  771. this._cleanup();
  772. };
  773. Tokenizer.prototype.pause = function() {
  774. this._running = false;
  775. };
  776. Tokenizer.prototype.resume = function() {
  777. this._running = true;
  778. if (this._index < this._buffer.length) {
  779. this._parse();
  780. }
  781. if (this._ended) {
  782. this._finish();
  783. }
  784. };
  785. Tokenizer.prototype.end = function(chunk) {
  786. if (this._ended) this._cbs.onerror(Error(".end() after done!"));
  787. if (chunk) this.write(chunk);
  788. this._ended = true;
  789. if (this._running) this._finish();
  790. };
  791. Tokenizer.prototype._finish = function() {
  792. //if there is remaining data, emit it in a reasonable way
  793. if (this._sectionStart < this._index) {
  794. this._handleTrailingData();
  795. }
  796. this._cbs.onend();
  797. };
  798. Tokenizer.prototype._handleTrailingData = function() {
  799. var data = this._buffer.substr(this._sectionStart);
  800. if (
  801. this._state === IN_CDATA ||
  802. this._state === AFTER_CDATA_1 ||
  803. this._state === AFTER_CDATA_2
  804. ) {
  805. this._cbs.oncdata(data);
  806. } else if (
  807. this._state === IN_COMMENT ||
  808. this._state === AFTER_COMMENT_1 ||
  809. this._state === AFTER_COMMENT_2
  810. ) {
  811. this._cbs.oncomment(data);
  812. } else if (this._state === IN_NAMED_ENTITY && !this._xmlMode) {
  813. this._parseLegacyEntity();
  814. if (this._sectionStart < this._index) {
  815. this._state = this._baseState;
  816. this._handleTrailingData();
  817. }
  818. } else if (this._state === IN_NUMERIC_ENTITY && !this._xmlMode) {
  819. this._decodeNumericEntity(2, 10);
  820. if (this._sectionStart < this._index) {
  821. this._state = this._baseState;
  822. this._handleTrailingData();
  823. }
  824. } else if (this._state === IN_HEX_ENTITY && !this._xmlMode) {
  825. this._decodeNumericEntity(3, 16);
  826. if (this._sectionStart < this._index) {
  827. this._state = this._baseState;
  828. this._handleTrailingData();
  829. }
  830. } else if (
  831. this._state !== IN_TAG_NAME &&
  832. this._state !== BEFORE_ATTRIBUTE_NAME &&
  833. this._state !== BEFORE_ATTRIBUTE_VALUE &&
  834. this._state !== AFTER_ATTRIBUTE_NAME &&
  835. this._state !== IN_ATTRIBUTE_NAME &&
  836. this._state !== IN_ATTRIBUTE_VALUE_SQ &&
  837. this._state !== IN_ATTRIBUTE_VALUE_DQ &&
  838. this._state !== IN_ATTRIBUTE_VALUE_NQ &&
  839. this._state !== IN_CLOSING_TAG_NAME
  840. ) {
  841. this._cbs.ontext(data);
  842. }
  843. //else, ignore remaining data
  844. //TODO add a way to remove current tag
  845. };
  846. Tokenizer.prototype.reset = function() {
  847. Tokenizer.call(
  848. this,
  849. { xmlMode: this._xmlMode, decodeEntities: this._decodeEntities },
  850. this._cbs
  851. );
  852. };
  853. Tokenizer.prototype.getAbsoluteIndex = function() {
  854. return this._bufferOffset + this._index;
  855. };
  856. Tokenizer.prototype._getSection = function() {
  857. return this._buffer.substring(this._sectionStart, this._index);
  858. };
  859. Tokenizer.prototype._emitToken = function(name) {
  860. this._cbs[name](this._getSection());
  861. this._sectionStart = -1;
  862. };
  863. Tokenizer.prototype._emitPartial = function(value) {
  864. if (this._baseState !== TEXT) {
  865. this._cbs.onattribdata(value); //TODO implement the new event
  866. } else {
  867. this._cbs.ontext(value);
  868. }
  869. };