CssHandler.js 2.8 KB

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