CssHandler.js 2.8 KB

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