|
| 1 | +const cfg = require('./config.js'), |
| 2 | + isLetter = c => (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); |
| 3 | + |
| 4 | +function CssHandler(tagStyle) { |
| 5 | + var styles = Object.assign(Object.create(null), cfg.userAgentStyles); |
| 6 | + for (var item in tagStyle) |
| 7 | + styles[item] = (styles[item] ? styles[item] + ';' : '') + tagStyle[item]; |
| 8 | + this.styles = styles; |
| 9 | +} |
| 10 | +CssHandler.prototype.getStyle = function(data) { |
| 11 | + this.styles = new parser(data, this.styles).parse(); |
| 12 | +} |
| 13 | +CssHandler.prototype.match = function(name, attrs) { |
| 14 | + var tmp, matched = (tmp = this.styles[name]) ? tmp + ';' : ''; |
| 15 | + if (attrs.class) { |
| 16 | + var items = attrs.class.split(' '); |
| 17 | + for (var i = 0, item; item = items[i]; i++) |
| 18 | + if (tmp = this.styles['.' + item]) |
| 19 | + matched += tmp + ';'; |
| 20 | + } |
| 21 | + if (tmp = this.styles['#' + attrs.id]) |
| 22 | + matched += tmp + ';'; |
| 23 | + return matched; |
| 24 | +} |
| 25 | +module.exports = CssHandler; |
| 26 | + |
| 27 | +function parser(data, init) { |
| 28 | + this.data = data; |
| 29 | + this.floor = 0; |
| 30 | + this.i = 0; |
| 31 | + this.list = []; |
| 32 | + this.res = init; |
| 33 | + this.state = this.Space; |
| 34 | +} |
| 35 | +parser.prototype.parse = function() { |
| 36 | + for (var c; c = this.data[this.i]; this.i++) |
| 37 | + this.state(c); |
| 38 | + return this.res; |
| 39 | +} |
| 40 | +parser.prototype.section = function() { |
| 41 | + return this.data.substring(this.start, this.i); |
| 42 | +} |
| 43 | +// 状态机 |
| 44 | +parser.prototype.Space = function(c) { |
| 45 | + if (c == '.' || c == '#' || isLetter(c)) { |
| 46 | + this.start = this.i; |
| 47 | + this.state = this.Name; |
| 48 | + } else if (c == '/' && this.data[this.i + 1] == '*') |
| 49 | + this.Comment(); |
| 50 | + else if (!cfg.blankChar[c] && c != ';') |
| 51 | + this.state = this.Ignore; |
| 52 | +} |
| 53 | +parser.prototype.Comment = function() { |
| 54 | + this.i = this.data.indexOf('*/', this.i) + 1; |
| 55 | + if (!this.i) this.i = this.data.length; |
| 56 | + this.state = this.Space; |
| 57 | +} |
| 58 | +parser.prototype.Ignore = function(c) { |
| 59 | + if (c == '{') this.floor++; |
| 60 | + else if (c == '}' && !--this.floor) this.state = this.Space; |
| 61 | +} |
| 62 | +parser.prototype.Name = function(c) { |
| 63 | + if (cfg.blankChar[c]) { |
| 64 | + this.list.push(this.section()); |
| 65 | + this.state = this.NameSpace; |
| 66 | + } else if (c == '{') { |
| 67 | + this.list.push(this.section()); |
| 68 | + this.Content(); |
| 69 | + } else if (c == ',') { |
| 70 | + this.list.push(this.section()); |
| 71 | + this.Comma(); |
| 72 | + } else if (!isLetter(c) && (c < '0' || c > '9') && c != '-' && c != '_') |
| 73 | + this.state = this.Ignore; |
| 74 | +} |
| 75 | +parser.prototype.NameSpace = function(c) { |
| 76 | + if (c == '{') this.Content(); |
| 77 | + else if (c == ',') this.Comma(); |
| 78 | + else if (!cfg.blankChar[c]) this.state = this.Ignore; |
| 79 | +} |
| 80 | +parser.prototype.Comma = function() { |
| 81 | + while (cfg.blankChar[this.data[++this.i]]); |
| 82 | + if (this.data[this.i] == '{') this.Content(); |
| 83 | + else { |
| 84 | + this.start = this.i--; |
| 85 | + this.state = this.Name; |
| 86 | + } |
| 87 | +} |
| 88 | +parser.prototype.Content = function() { |
| 89 | + this.start = ++this.i; |
| 90 | + if ((this.i = this.data.indexOf('}', this.i)) == -1) this.i = this.data.length; |
| 91 | + var content = this.section(); |
| 92 | + for (var i = 0, item; item = this.list[i++];) |
| 93 | + if (this.res[item]) this.res[item] += ';' + content; |
| 94 | + else this.res[item] = content; |
| 95 | + this.list = []; |
| 96 | + this.state = this.Space; |
| 97 | +} |
0 commit comments