lex.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. // Package html is an HTML5 lexer following the specifications at http://www.w3.org/TR/html5/syntax.html.
  2. package html // import "github.com/tdewolff/parse/html"
  3. import (
  4. "io"
  5. "strconv"
  6. "github.com/tdewolff/parse"
  7. "github.com/tdewolff/parse/buffer"
  8. )
  9. // TokenType determines the type of token, eg. a number or a semicolon.
  10. type TokenType uint32
  11. // TokenType values.
  12. const (
  13. ErrorToken TokenType = iota // extra token when errors occur
  14. CommentToken
  15. DoctypeToken
  16. StartTagToken
  17. StartTagCloseToken
  18. StartTagVoidToken
  19. EndTagToken
  20. AttributeToken
  21. TextToken
  22. SvgToken
  23. MathToken
  24. )
  25. // String returns the string representation of a TokenType.
  26. func (tt TokenType) String() string {
  27. switch tt {
  28. case ErrorToken:
  29. return "Error"
  30. case CommentToken:
  31. return "Comment"
  32. case DoctypeToken:
  33. return "Doctype"
  34. case StartTagToken:
  35. return "StartTag"
  36. case StartTagCloseToken:
  37. return "StartTagClose"
  38. case StartTagVoidToken:
  39. return "StartTagVoid"
  40. case EndTagToken:
  41. return "EndTag"
  42. case AttributeToken:
  43. return "Attribute"
  44. case TextToken:
  45. return "Text"
  46. case SvgToken:
  47. return "Svg"
  48. case MathToken:
  49. return "Math"
  50. }
  51. return "Invalid(" + strconv.Itoa(int(tt)) + ")"
  52. }
  53. ////////////////////////////////////////////////////////////////
  54. // Lexer is the state for the lexer.
  55. type Lexer struct {
  56. r *buffer.Lexer
  57. err error
  58. rawTag Hash
  59. inTag bool
  60. text []byte
  61. attrVal []byte
  62. }
  63. // NewLexer returns a new Lexer for a given io.Reader.
  64. func NewLexer(r io.Reader) *Lexer {
  65. return &Lexer{
  66. r: buffer.NewLexer(r),
  67. }
  68. }
  69. // Err returns the error encountered during lexing, this is often io.EOF but also other errors can be returned.
  70. func (l *Lexer) Err() error {
  71. if err := l.r.Err(); err != nil {
  72. return err
  73. }
  74. return l.err
  75. }
  76. // Restore restores the NULL byte at the end of the buffer.
  77. func (l *Lexer) Restore() {
  78. l.r.Restore()
  79. }
  80. // Next returns the next Token. It returns ErrorToken when an error was encountered. Using Err() one can retrieve the error message.
  81. func (l *Lexer) Next() (TokenType, []byte) {
  82. l.text = nil
  83. var c byte
  84. if l.inTag {
  85. l.attrVal = nil
  86. for { // before attribute name state
  87. if c = l.r.Peek(0); c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' {
  88. l.r.Move(1)
  89. continue
  90. }
  91. break
  92. }
  93. if c == 0 {
  94. l.err = parse.NewErrorLexer("unexpected null character", l.r)
  95. return ErrorToken, nil
  96. } else if c != '>' && (c != '/' || l.r.Peek(1) != '>') {
  97. return AttributeToken, l.shiftAttribute()
  98. }
  99. start := l.r.Pos()
  100. l.inTag = false
  101. if c == '/' {
  102. l.r.Move(2)
  103. l.text = l.r.Lexeme()[start:]
  104. return StartTagVoidToken, l.r.Shift()
  105. }
  106. l.r.Move(1)
  107. l.text = l.r.Lexeme()[start:]
  108. return StartTagCloseToken, l.r.Shift()
  109. }
  110. if l.rawTag != 0 {
  111. if rawText := l.shiftRawText(); len(rawText) > 0 {
  112. l.rawTag = 0
  113. return TextToken, rawText
  114. }
  115. l.rawTag = 0
  116. }
  117. for {
  118. c = l.r.Peek(0)
  119. if c == '<' {
  120. c = l.r.Peek(1)
  121. if l.r.Pos() > 0 {
  122. if c == '/' && l.r.Peek(2) != 0 || 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || c == '!' || c == '?' {
  123. return TextToken, l.r.Shift()
  124. }
  125. } else if c == '/' && l.r.Peek(2) != 0 {
  126. l.r.Move(2)
  127. if c = l.r.Peek(0); c != '>' && !('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z') {
  128. return CommentToken, l.shiftBogusComment()
  129. }
  130. return EndTagToken, l.shiftEndTag()
  131. } else if 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' {
  132. l.r.Move(1)
  133. l.inTag = true
  134. return l.shiftStartTag()
  135. } else if c == '!' {
  136. l.r.Move(2)
  137. return l.readMarkup()
  138. } else if c == '?' {
  139. l.r.Move(1)
  140. return CommentToken, l.shiftBogusComment()
  141. }
  142. } else if c == 0 {
  143. if l.r.Pos() > 0 {
  144. return TextToken, l.r.Shift()
  145. }
  146. l.err = parse.NewErrorLexer("unexpected null character", l.r)
  147. return ErrorToken, nil
  148. }
  149. l.r.Move(1)
  150. }
  151. }
  152. // Text returns the textual representation of a token. This excludes delimiters and additional leading/trailing characters.
  153. func (l *Lexer) Text() []byte {
  154. return l.text
  155. }
  156. // AttrVal returns the attribute value when an AttributeToken was returned from Next.
  157. func (l *Lexer) AttrVal() []byte {
  158. return l.attrVal
  159. }
  160. ////////////////////////////////////////////////////////////////
  161. // The following functions follow the specifications at http://www.w3.org/html/wg/drafts/html/master/syntax.html
  162. func (l *Lexer) shiftRawText() []byte {
  163. if l.rawTag == Plaintext {
  164. for {
  165. if l.r.Peek(0) == 0 {
  166. return l.r.Shift()
  167. }
  168. l.r.Move(1)
  169. }
  170. } else { // RCDATA, RAWTEXT and SCRIPT
  171. for {
  172. c := l.r.Peek(0)
  173. if c == '<' {
  174. if l.r.Peek(1) == '/' {
  175. mark := l.r.Pos()
  176. l.r.Move(2)
  177. for {
  178. if c = l.r.Peek(0); !('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z') {
  179. break
  180. }
  181. l.r.Move(1)
  182. }
  183. if h := ToHash(parse.ToLower(parse.Copy(l.r.Lexeme()[mark+2:]))); h == l.rawTag { // copy so that ToLower doesn't change the case of the underlying slice
  184. l.r.Rewind(mark)
  185. return l.r.Shift()
  186. }
  187. } else if l.rawTag == Script && l.r.Peek(1) == '!' && l.r.Peek(2) == '-' && l.r.Peek(3) == '-' {
  188. l.r.Move(4)
  189. inScript := false
  190. for {
  191. c := l.r.Peek(0)
  192. if c == '-' && l.r.Peek(1) == '-' && l.r.Peek(2) == '>' {
  193. l.r.Move(3)
  194. break
  195. } else if c == '<' {
  196. isEnd := l.r.Peek(1) == '/'
  197. if isEnd {
  198. l.r.Move(2)
  199. } else {
  200. l.r.Move(1)
  201. }
  202. mark := l.r.Pos()
  203. for {
  204. if c = l.r.Peek(0); !('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z') {
  205. break
  206. }
  207. l.r.Move(1)
  208. }
  209. if h := ToHash(parse.ToLower(parse.Copy(l.r.Lexeme()[mark:]))); h == Script { // copy so that ToLower doesn't change the case of the underlying slice
  210. if !isEnd {
  211. inScript = true
  212. } else {
  213. if !inScript {
  214. l.r.Rewind(mark - 2)
  215. return l.r.Shift()
  216. }
  217. inScript = false
  218. }
  219. }
  220. } else if c == 0 {
  221. return l.r.Shift()
  222. }
  223. l.r.Move(1)
  224. }
  225. } else {
  226. l.r.Move(1)
  227. }
  228. } else if c == 0 {
  229. return l.r.Shift()
  230. } else {
  231. l.r.Move(1)
  232. }
  233. }
  234. }
  235. }
  236. func (l *Lexer) readMarkup() (TokenType, []byte) {
  237. if l.at('-', '-') {
  238. l.r.Move(2)
  239. for {
  240. if l.r.Peek(0) == 0 {
  241. return CommentToken, l.r.Shift()
  242. } else if l.at('-', '-', '>') {
  243. l.text = l.r.Lexeme()[4:]
  244. l.r.Move(3)
  245. return CommentToken, l.r.Shift()
  246. } else if l.at('-', '-', '!', '>') {
  247. l.text = l.r.Lexeme()[4:]
  248. l.r.Move(4)
  249. return CommentToken, l.r.Shift()
  250. }
  251. l.r.Move(1)
  252. }
  253. } else if l.at('[', 'C', 'D', 'A', 'T', 'A', '[') {
  254. l.r.Move(7)
  255. for {
  256. if l.r.Peek(0) == 0 {
  257. return TextToken, l.r.Shift()
  258. } else if l.at(']', ']', '>') {
  259. l.r.Move(3)
  260. return TextToken, l.r.Shift()
  261. }
  262. l.r.Move(1)
  263. }
  264. } else {
  265. if l.atCaseInsensitive('d', 'o', 'c', 't', 'y', 'p', 'e') {
  266. l.r.Move(7)
  267. if l.r.Peek(0) == ' ' {
  268. l.r.Move(1)
  269. }
  270. for {
  271. if c := l.r.Peek(0); c == '>' || c == 0 {
  272. l.text = l.r.Lexeme()[9:]
  273. if c == '>' {
  274. l.r.Move(1)
  275. }
  276. return DoctypeToken, l.r.Shift()
  277. }
  278. l.r.Move(1)
  279. }
  280. }
  281. }
  282. return CommentToken, l.shiftBogusComment()
  283. }
  284. func (l *Lexer) shiftBogusComment() []byte {
  285. for {
  286. c := l.r.Peek(0)
  287. if c == '>' {
  288. l.text = l.r.Lexeme()[2:]
  289. l.r.Move(1)
  290. return l.r.Shift()
  291. } else if c == 0 {
  292. l.text = l.r.Lexeme()[2:]
  293. return l.r.Shift()
  294. }
  295. l.r.Move(1)
  296. }
  297. }
  298. func (l *Lexer) shiftStartTag() (TokenType, []byte) {
  299. for {
  300. if c := l.r.Peek(0); c == ' ' || c == '>' || c == '/' && l.r.Peek(1) == '>' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == 0 {
  301. break
  302. }
  303. l.r.Move(1)
  304. }
  305. l.text = parse.ToLower(l.r.Lexeme()[1:])
  306. if h := ToHash(l.text); h == Textarea || h == Title || h == Style || h == Xmp || h == Iframe || h == Script || h == Plaintext || h == Svg || h == Math {
  307. if h == Svg {
  308. l.inTag = false
  309. return SvgToken, l.shiftXml(h)
  310. } else if h == Math {
  311. l.inTag = false
  312. return MathToken, l.shiftXml(h)
  313. }
  314. l.rawTag = h
  315. }
  316. return StartTagToken, l.r.Shift()
  317. }
  318. func (l *Lexer) shiftAttribute() []byte {
  319. nameStart := l.r.Pos()
  320. var c byte
  321. for { // attribute name state
  322. if c = l.r.Peek(0); c == ' ' || c == '=' || c == '>' || c == '/' && l.r.Peek(1) == '>' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == 0 {
  323. break
  324. }
  325. l.r.Move(1)
  326. }
  327. nameEnd := l.r.Pos()
  328. for { // after attribute name state
  329. if c = l.r.Peek(0); c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' {
  330. l.r.Move(1)
  331. continue
  332. }
  333. break
  334. }
  335. if c == '=' {
  336. l.r.Move(1)
  337. for { // before attribute value state
  338. if c = l.r.Peek(0); c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' {
  339. l.r.Move(1)
  340. continue
  341. }
  342. break
  343. }
  344. attrPos := l.r.Pos()
  345. delim := c
  346. if delim == '"' || delim == '\'' { // attribute value single- and double-quoted state
  347. l.r.Move(1)
  348. for {
  349. c := l.r.Peek(0)
  350. if c == delim {
  351. l.r.Move(1)
  352. break
  353. } else if c == 0 {
  354. break
  355. }
  356. l.r.Move(1)
  357. }
  358. } else { // attribute value unquoted state
  359. for {
  360. if c := l.r.Peek(0); c == ' ' || c == '>' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == 0 {
  361. break
  362. }
  363. l.r.Move(1)
  364. }
  365. }
  366. l.attrVal = l.r.Lexeme()[attrPos:]
  367. } else {
  368. l.r.Rewind(nameEnd)
  369. l.attrVal = nil
  370. }
  371. l.text = parse.ToLower(l.r.Lexeme()[nameStart:nameEnd])
  372. return l.r.Shift()
  373. }
  374. func (l *Lexer) shiftEndTag() []byte {
  375. for {
  376. c := l.r.Peek(0)
  377. if c == '>' {
  378. l.text = l.r.Lexeme()[2:]
  379. l.r.Move(1)
  380. break
  381. } else if c == 0 {
  382. l.text = l.r.Lexeme()[2:]
  383. break
  384. }
  385. l.r.Move(1)
  386. }
  387. end := len(l.text)
  388. for end > 0 {
  389. if c := l.text[end-1]; c == ' ' || c == '\t' || c == '\n' || c == '\r' {
  390. end--
  391. continue
  392. }
  393. break
  394. }
  395. l.text = l.text[:end]
  396. return parse.ToLower(l.r.Shift())
  397. }
  398. func (l *Lexer) shiftXml(rawTag Hash) []byte {
  399. inQuote := false
  400. for {
  401. c := l.r.Peek(0)
  402. if c == '"' {
  403. inQuote = !inQuote
  404. l.r.Move(1)
  405. } else if c == '<' && !inQuote {
  406. if l.r.Peek(1) == '/' {
  407. mark := l.r.Pos()
  408. l.r.Move(2)
  409. for {
  410. if c = l.r.Peek(0); !('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z') {
  411. break
  412. }
  413. l.r.Move(1)
  414. }
  415. if h := ToHash(parse.ToLower(parse.Copy(l.r.Lexeme()[mark+2:]))); h == rawTag { // copy so that ToLower doesn't change the case of the underlying slice
  416. break
  417. }
  418. } else {
  419. l.r.Move(1)
  420. }
  421. } else if c == 0 {
  422. return l.r.Shift()
  423. }
  424. l.r.Move(1)
  425. }
  426. for {
  427. c := l.r.Peek(0)
  428. if c == '>' {
  429. l.r.Move(1)
  430. break
  431. } else if c == 0 {
  432. break
  433. }
  434. l.r.Move(1)
  435. }
  436. return l.r.Shift()
  437. }
  438. ////////////////////////////////////////////////////////////////
  439. func (l *Lexer) at(b ...byte) bool {
  440. for i, c := range b {
  441. if l.r.Peek(i) != c {
  442. return false
  443. }
  444. }
  445. return true
  446. }
  447. func (l *Lexer) atCaseInsensitive(b ...byte) bool {
  448. for i, c := range b {
  449. if l.r.Peek(i) != c && (l.r.Peek(i)+('a'-'A')) != c {
  450. return false
  451. }
  452. }
  453. return true
  454. }