html-text.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. 'use strict'
  2. var assert = require('assert')
  3. var asciiAlpha = require('../character/ascii-alpha.js')
  4. var asciiAlphanumeric = require('../character/ascii-alphanumeric.js')
  5. var codes = require('../character/codes.js')
  6. var markdownLineEnding = require('../character/markdown-line-ending.js')
  7. var markdownLineEndingOrSpace = require('../character/markdown-line-ending-or-space.js')
  8. var markdownSpace = require('../character/markdown-space.js')
  9. var constants = require('../constant/constants.js')
  10. var types = require('../constant/types.js')
  11. var factorySpace = require('./factory-space.js')
  12. function _interopDefaultLegacy(e) {
  13. return e && typeof e === 'object' && 'default' in e ? e : {default: e}
  14. }
  15. var assert__default = /*#__PURE__*/ _interopDefaultLegacy(assert)
  16. var htmlText = {
  17. name: 'htmlText',
  18. tokenize: tokenizeHtmlText
  19. }
  20. function tokenizeHtmlText(effects, ok, nok) {
  21. var self = this
  22. var marker
  23. var buffer
  24. var index
  25. var returnState
  26. return start
  27. function start(code) {
  28. assert__default['default'](code === codes.lessThan, 'expected `<`')
  29. effects.enter(types.htmlText)
  30. effects.enter(types.htmlTextData)
  31. effects.consume(code)
  32. return open
  33. }
  34. function open(code) {
  35. if (code === codes.exclamationMark) {
  36. effects.consume(code)
  37. return declarationOpen
  38. }
  39. if (code === codes.slash) {
  40. effects.consume(code)
  41. return tagCloseStart
  42. }
  43. if (code === codes.questionMark) {
  44. effects.consume(code)
  45. return instruction
  46. }
  47. if (asciiAlpha(code)) {
  48. effects.consume(code)
  49. return tagOpen
  50. }
  51. return nok(code)
  52. }
  53. function declarationOpen(code) {
  54. if (code === codes.dash) {
  55. effects.consume(code)
  56. return commentOpen
  57. }
  58. if (code === codes.leftSquareBracket) {
  59. effects.consume(code)
  60. buffer = constants.cdataOpeningString
  61. index = 0
  62. return cdataOpen
  63. }
  64. if (asciiAlpha(code)) {
  65. effects.consume(code)
  66. return declaration
  67. }
  68. return nok(code)
  69. }
  70. function commentOpen(code) {
  71. if (code === codes.dash) {
  72. effects.consume(code)
  73. return commentStart
  74. }
  75. return nok(code)
  76. }
  77. function commentStart(code) {
  78. if (code === codes.eof || code === codes.greaterThan) {
  79. return nok(code)
  80. }
  81. if (code === codes.dash) {
  82. effects.consume(code)
  83. return commentStartDash
  84. }
  85. return comment(code)
  86. }
  87. function commentStartDash(code) {
  88. if (code === codes.eof || code === codes.greaterThan) {
  89. return nok(code)
  90. }
  91. return comment(code)
  92. }
  93. function comment(code) {
  94. if (code === codes.eof) {
  95. return nok(code)
  96. }
  97. if (code === codes.dash) {
  98. effects.consume(code)
  99. return commentClose
  100. }
  101. if (markdownLineEnding(code)) {
  102. returnState = comment
  103. return atLineEnding(code)
  104. }
  105. effects.consume(code)
  106. return comment
  107. }
  108. function commentClose(code) {
  109. if (code === codes.dash) {
  110. effects.consume(code)
  111. return end
  112. }
  113. return comment(code)
  114. }
  115. function cdataOpen(code) {
  116. if (code === buffer.charCodeAt(index++)) {
  117. effects.consume(code)
  118. return index === buffer.length ? cdata : cdataOpen
  119. }
  120. return nok(code)
  121. }
  122. function cdata(code) {
  123. if (code === codes.eof) {
  124. return nok(code)
  125. }
  126. if (code === codes.rightSquareBracket) {
  127. effects.consume(code)
  128. return cdataClose
  129. }
  130. if (markdownLineEnding(code)) {
  131. returnState = cdata
  132. return atLineEnding(code)
  133. }
  134. effects.consume(code)
  135. return cdata
  136. }
  137. function cdataClose(code) {
  138. if (code === codes.rightSquareBracket) {
  139. effects.consume(code)
  140. return cdataEnd
  141. }
  142. return cdata(code)
  143. }
  144. function cdataEnd(code) {
  145. if (code === codes.greaterThan) {
  146. return end(code)
  147. }
  148. if (code === codes.rightSquareBracket) {
  149. effects.consume(code)
  150. return cdataEnd
  151. }
  152. return cdata(code)
  153. }
  154. function declaration(code) {
  155. if (code === codes.eof || code === codes.greaterThan) {
  156. return end(code)
  157. }
  158. if (markdownLineEnding(code)) {
  159. returnState = declaration
  160. return atLineEnding(code)
  161. }
  162. effects.consume(code)
  163. return declaration
  164. }
  165. function instruction(code) {
  166. if (code === codes.eof) {
  167. return nok(code)
  168. }
  169. if (code === codes.questionMark) {
  170. effects.consume(code)
  171. return instructionClose
  172. }
  173. if (markdownLineEnding(code)) {
  174. returnState = instruction
  175. return atLineEnding(code)
  176. }
  177. effects.consume(code)
  178. return instruction
  179. }
  180. function instructionClose(code) {
  181. return code === codes.greaterThan ? end(code) : instruction(code)
  182. }
  183. function tagCloseStart(code) {
  184. if (asciiAlpha(code)) {
  185. effects.consume(code)
  186. return tagClose
  187. }
  188. return nok(code)
  189. }
  190. function tagClose(code) {
  191. if (code === codes.dash || asciiAlphanumeric(code)) {
  192. effects.consume(code)
  193. return tagClose
  194. }
  195. return tagCloseBetween(code)
  196. }
  197. function tagCloseBetween(code) {
  198. if (markdownLineEnding(code)) {
  199. returnState = tagCloseBetween
  200. return atLineEnding(code)
  201. }
  202. if (markdownSpace(code)) {
  203. effects.consume(code)
  204. return tagCloseBetween
  205. }
  206. return end(code)
  207. }
  208. function tagOpen(code) {
  209. if (code === codes.dash || asciiAlphanumeric(code)) {
  210. effects.consume(code)
  211. return tagOpen
  212. }
  213. if (
  214. code === codes.slash ||
  215. code === codes.greaterThan ||
  216. markdownLineEndingOrSpace(code)
  217. ) {
  218. return tagOpenBetween(code)
  219. }
  220. return nok(code)
  221. }
  222. function tagOpenBetween(code) {
  223. if (code === codes.slash) {
  224. effects.consume(code)
  225. return end
  226. }
  227. if (code === codes.colon || code === codes.underscore || asciiAlpha(code)) {
  228. effects.consume(code)
  229. return tagOpenAttributeName
  230. }
  231. if (markdownLineEnding(code)) {
  232. returnState = tagOpenBetween
  233. return atLineEnding(code)
  234. }
  235. if (markdownSpace(code)) {
  236. effects.consume(code)
  237. return tagOpenBetween
  238. }
  239. return end(code)
  240. }
  241. function tagOpenAttributeName(code) {
  242. if (
  243. code === codes.dash ||
  244. code === codes.dot ||
  245. code === codes.colon ||
  246. code === codes.underscore ||
  247. asciiAlphanumeric(code)
  248. ) {
  249. effects.consume(code)
  250. return tagOpenAttributeName
  251. }
  252. return tagOpenAttributeNameAfter(code)
  253. }
  254. function tagOpenAttributeNameAfter(code) {
  255. if (code === codes.equalsTo) {
  256. effects.consume(code)
  257. return tagOpenAttributeValueBefore
  258. }
  259. if (markdownLineEnding(code)) {
  260. returnState = tagOpenAttributeNameAfter
  261. return atLineEnding(code)
  262. }
  263. if (markdownSpace(code)) {
  264. effects.consume(code)
  265. return tagOpenAttributeNameAfter
  266. }
  267. return tagOpenBetween(code)
  268. }
  269. function tagOpenAttributeValueBefore(code) {
  270. if (
  271. code === codes.eof ||
  272. code === codes.lessThan ||
  273. code === codes.equalsTo ||
  274. code === codes.greaterThan ||
  275. code === codes.graveAccent
  276. ) {
  277. return nok(code)
  278. }
  279. if (code === codes.quotationMark || code === codes.apostrophe) {
  280. effects.consume(code)
  281. marker = code
  282. return tagOpenAttributeValueQuoted
  283. }
  284. if (markdownLineEnding(code)) {
  285. returnState = tagOpenAttributeValueBefore
  286. return atLineEnding(code)
  287. }
  288. if (markdownSpace(code)) {
  289. effects.consume(code)
  290. return tagOpenAttributeValueBefore
  291. }
  292. effects.consume(code)
  293. marker = undefined
  294. return tagOpenAttributeValueUnquoted
  295. }
  296. function tagOpenAttributeValueQuoted(code) {
  297. if (code === marker) {
  298. effects.consume(code)
  299. return tagOpenAttributeValueQuotedAfter
  300. }
  301. if (code === codes.eof) {
  302. return nok(code)
  303. }
  304. if (markdownLineEnding(code)) {
  305. returnState = tagOpenAttributeValueQuoted
  306. return atLineEnding(code)
  307. }
  308. effects.consume(code)
  309. return tagOpenAttributeValueQuoted
  310. }
  311. function tagOpenAttributeValueQuotedAfter(code) {
  312. if (
  313. code === codes.greaterThan ||
  314. code === codes.slash ||
  315. markdownLineEndingOrSpace(code)
  316. ) {
  317. return tagOpenBetween(code)
  318. }
  319. return nok(code)
  320. }
  321. function tagOpenAttributeValueUnquoted(code) {
  322. if (
  323. code === codes.eof ||
  324. code === codes.quotationMark ||
  325. code === codes.apostrophe ||
  326. code === codes.lessThan ||
  327. code === codes.equalsTo ||
  328. code === codes.graveAccent
  329. ) {
  330. return nok(code)
  331. }
  332. if (code === codes.greaterThan || markdownLineEndingOrSpace(code)) {
  333. return tagOpenBetween(code)
  334. }
  335. effects.consume(code)
  336. return tagOpenAttributeValueUnquoted
  337. }
  338. // We can’t have blank lines in content, so no need to worry about empty
  339. // tokens.
  340. function atLineEnding(code) {
  341. assert__default['default'](returnState, 'expected return state')
  342. assert__default['default'](markdownLineEnding(code), 'expected eol')
  343. effects.exit(types.htmlTextData)
  344. effects.enter(types.lineEnding)
  345. effects.consume(code)
  346. effects.exit(types.lineEnding)
  347. return factorySpace(
  348. effects,
  349. afterPrefix,
  350. types.linePrefix,
  351. self.parser.constructs.disable.null.indexOf('codeIndented') > -1
  352. ? undefined
  353. : constants.tabSize
  354. )
  355. }
  356. function afterPrefix(code) {
  357. effects.enter(types.htmlTextData)
  358. return returnState(code)
  359. }
  360. function end(code) {
  361. if (code === codes.greaterThan) {
  362. effects.consume(code)
  363. effects.exit(types.htmlTextData)
  364. effects.exit(types.htmlText)
  365. return ok
  366. }
  367. return nok(code)
  368. }
  369. }
  370. module.exports = htmlText