* 分词
(content, handler)
| 31 | * 分词 |
| 32 | */ |
| 33 | function tokenize(content, handler) { |
| 34 | const stack = [] |
| 35 | let last = content |
| 36 | |
| 37 | stack.last = function() { |
| 38 | return this[this.length - 1] |
| 39 | } |
| 40 | |
| 41 | while (content) { |
| 42 | let isText = true |
| 43 | |
| 44 | if (!stack.last() || !rawTextMap[stack.last()]) { |
| 45 | if (content.indexOf('<!--') === 0) { |
| 46 | // comment |
| 47 | const index = content.indexOf('-->') |
| 48 | |
| 49 | if (index >= 0) { |
| 50 | if (handler.comment) handler.comment(content.substring(4, index)) |
| 51 | content = content.substring(index + 3) |
| 52 | isText = false |
| 53 | } |
| 54 | } else if (content.indexOf('</') === 0) { |
| 55 | // end tag |
| 56 | const match = content.match(endTagReg) |
| 57 | |
| 58 | if (match) { |
| 59 | content = content.substring(match[0].length) |
| 60 | match[0].replace(endTagReg, parseEndTag) |
| 61 | isText = false |
| 62 | } |
| 63 | } else if (content.indexOf('<') === 0) { |
| 64 | // start tag |
| 65 | let match = content.match(startTagReg) |
| 66 | |
| 67 | if (match) { |
| 68 | content = content.substring(match[0].length) |
| 69 | match[0].replace(startTagReg, parseStartTag) |
| 70 | isText = false |
| 71 | } else { |
| 72 | // 检测 doctype |
| 73 | match = content.match(doctypeReg) |
| 74 | |
| 75 | if (match) { |
| 76 | content = content.substring(match[0].length) |
| 77 | isText = false |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | if (isText) { |
| 83 | const indexStart = content.indexOf('<') |
| 84 | const indexEnd = content.indexOf('>') |
| 85 | |
| 86 | // 简单自动纠错,只有 <、只有 >、> 在 < 前面、< 和 > 中间没有内容 |
| 87 | let text = '' |
| 88 | if (indexStart === -1 || indexStart >= 0 && indexEnd === -1 || indexStart > indexEnd || (indexEnd > indexStart && !content.substring(indexStart + 1, indexEnd).trim())) { |
| 89 | text = content.replace(/>/g, '>').replace(/</g, '<') |
| 90 | content = '' |
no test coverage detected