AI销管
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CssHandler.js 2.8 KiB

2 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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) {
  59. this.list = [];
  60. this.state = this.Space;
  61. }
  62. }
  63. parser.prototype.Name = function(c) {
  64. if (cfg.blankChar[c]) {
  65. this.list.push(this.section());
  66. this.state = this.NameSpace;
  67. } else if (c == '{') {
  68. this.list.push(this.section());
  69. this.Content();
  70. } else if (c == ',') {
  71. this.list.push(this.section());
  72. this.Comma();
  73. } else if (!isLetter(c) && (c < '0' || c > '9') && c != '-' && c != '_')
  74. this.state = this.Ignore;
  75. }
  76. parser.prototype.NameSpace = function(c) {
  77. if (c == '{') this.Content();
  78. else if (c == ',') this.Comma();
  79. else if (!cfg.blankChar[c]) this.state = this.Ignore;
  80. }
  81. parser.prototype.Comma = function() {
  82. while (cfg.blankChar[this.data[++this.i]]);
  83. if (this.data[this.i] == '{') this.Content();
  84. else {
  85. this.start = this.i--;
  86. this.state = this.Name;
  87. }
  88. }
  89. parser.prototype.Content = function() {
  90. this.start = ++this.i;
  91. if ((this.i = this.data.indexOf('}', this.i)) == -1) this.i = this.data.length;
  92. var content = this.section();
  93. for (var i = 0, item; item = this.list[i++];)
  94. if (this.res[item]) this.res[item] += ';' + content;
  95. else this.res[item] = content;
  96. this.list = [];
  97. this.state = this.Space;
  98. }