CssHandler.js 3.4 KB

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