CssHandler.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. var cfg = require('./config.js'),
  2. isLetter = c => (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
  3. class CssHandler {
  4. constructor(tagStyle) {
  5. var styles = Object.assign({}, cfg.userAgentStyles);
  6. for (var item in tagStyle)
  7. styles[item] = (styles[item] ? styles[item] + ';' : '') + tagStyle[item];
  8. this.styles = styles;
  9. }
  10. getStyle(data) {
  11. this.styles = new CssParser(data, this.styles).parse();
  12. }
  13. match(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. }
  26. module.exports = CssHandler;
  27. class CssParser {
  28. constructor(data, init) {
  29. this.data = data;
  30. this.floor = 0;
  31. this.i = 0;
  32. this.list = [];
  33. this.res = init;
  34. this.state = this.Space;
  35. }
  36. parse() {
  37. for (var c; c = this.data[this.i]; this.i++)
  38. this.state(c);
  39. return this.res;
  40. }
  41. section() {
  42. return this.data.substring(this.start, this.i);
  43. }
  44. // 状态机
  45. Space(c) {
  46. if (c == '.' || c == '#' || isLetter(c)) {
  47. this.start = this.i;
  48. this.state = this.Name;
  49. } else if (c == '/' && this.data[this.i + 1] == '*')
  50. this.Comment();
  51. else if (!cfg.blankChar[c] && c != ';')
  52. this.state = this.Ignore;
  53. }
  54. Comment() {
  55. this.i = this.data.indexOf('*/', this.i) + 1;
  56. if (!this.i) this.i = this.data.length;
  57. this.state = this.Space;
  58. }
  59. Ignore(c) {
  60. if (c == '{') this.floor++;
  61. else if (c == '}' && !--this.floor) this.state = this.Space;
  62. }
  63. Name(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. NameSpace(c) {
  77. if (c == '{') this.Content();
  78. else if (c == ',') this.Comma();
  79. else if (!cfg.blankChar[c]) this.state = this.Ignore;
  80. }
  81. Comma() {
  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. Content() {
  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. }
  99. }