buffer.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package html // import "github.com/tdewolff/minify/html"
  2. import (
  3. "github.com/tdewolff/parse"
  4. "github.com/tdewolff/parse/html"
  5. )
  6. // Token is a single token unit with an attribute value (if given) and hash of the data.
  7. type Token struct {
  8. html.TokenType
  9. Hash html.Hash
  10. Data []byte
  11. Text []byte
  12. AttrVal []byte
  13. Traits traits
  14. }
  15. // TokenBuffer is a buffer that allows for token look-ahead.
  16. type TokenBuffer struct {
  17. l *html.Lexer
  18. buf []Token
  19. pos int
  20. attrBuffer []*Token
  21. }
  22. // NewTokenBuffer returns a new TokenBuffer.
  23. func NewTokenBuffer(l *html.Lexer) *TokenBuffer {
  24. return &TokenBuffer{
  25. l: l,
  26. buf: make([]Token, 0, 8),
  27. }
  28. }
  29. func (z *TokenBuffer) read(t *Token) {
  30. t.TokenType, t.Data = z.l.Next()
  31. t.Text = z.l.Text()
  32. if t.TokenType == html.AttributeToken {
  33. t.AttrVal = z.l.AttrVal()
  34. if len(t.AttrVal) > 1 && (t.AttrVal[0] == '"' || t.AttrVal[0] == '\'') {
  35. t.AttrVal = parse.TrimWhitespace(t.AttrVal[1 : len(t.AttrVal)-1]) // quotes will be readded in attribute loop if necessary
  36. }
  37. t.Hash = html.ToHash(t.Text)
  38. t.Traits = attrMap[t.Hash]
  39. } else if t.TokenType == html.StartTagToken || t.TokenType == html.EndTagToken {
  40. t.AttrVal = nil
  41. t.Hash = html.ToHash(t.Text)
  42. t.Traits = tagMap[t.Hash]
  43. } else {
  44. t.AttrVal = nil
  45. t.Hash = 0
  46. t.Traits = 0
  47. }
  48. }
  49. // Peek returns the ith element and possibly does an allocation.
  50. // Peeking past an error will panic.
  51. func (z *TokenBuffer) Peek(pos int) *Token {
  52. pos += z.pos
  53. if pos >= len(z.buf) {
  54. if len(z.buf) > 0 && z.buf[len(z.buf)-1].TokenType == html.ErrorToken {
  55. return &z.buf[len(z.buf)-1]
  56. }
  57. c := cap(z.buf)
  58. d := len(z.buf) - z.pos
  59. p := pos - z.pos + 1 // required peek length
  60. var buf []Token
  61. if 2*p > c {
  62. buf = make([]Token, 0, 2*c+p)
  63. } else {
  64. buf = z.buf
  65. }
  66. copy(buf[:d], z.buf[z.pos:])
  67. buf = buf[:p]
  68. pos -= z.pos
  69. for i := d; i < p; i++ {
  70. z.read(&buf[i])
  71. if buf[i].TokenType == html.ErrorToken {
  72. buf = buf[:i+1]
  73. pos = i
  74. break
  75. }
  76. }
  77. z.pos, z.buf = 0, buf
  78. }
  79. return &z.buf[pos]
  80. }
  81. // Shift returns the first element and advances position.
  82. func (z *TokenBuffer) Shift() *Token {
  83. if z.pos >= len(z.buf) {
  84. t := &z.buf[:1][0]
  85. z.read(t)
  86. return t
  87. }
  88. t := &z.buf[z.pos]
  89. z.pos++
  90. return t
  91. }
  92. // Attributes extracts the gives attribute hashes from a tag.
  93. // It returns in the same order pointers to the requested token data or nil.
  94. func (z *TokenBuffer) Attributes(hashes ...html.Hash) []*Token {
  95. n := 0
  96. for {
  97. if t := z.Peek(n); t.TokenType != html.AttributeToken {
  98. break
  99. }
  100. n++
  101. }
  102. if len(hashes) > cap(z.attrBuffer) {
  103. z.attrBuffer = make([]*Token, len(hashes))
  104. } else {
  105. z.attrBuffer = z.attrBuffer[:len(hashes)]
  106. for i := range z.attrBuffer {
  107. z.attrBuffer[i] = nil
  108. }
  109. }
  110. for i := z.pos; i < z.pos+n; i++ {
  111. attr := &z.buf[i]
  112. for j, hash := range hashes {
  113. if hash == attr.Hash {
  114. z.attrBuffer[j] = attr
  115. }
  116. }
  117. }
  118. return z.attrBuffer
  119. }