CssHandler.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // 小程序富文本插件 https://github.com/jin-yufeng/Parser
  2. "use strict";
  3. function _classCallCheck(t, s) {
  4. if (!(t instanceof s))
  5. throw new TypeError("Cannot call a class as a function");
  6. }
  7. var _createClass = (function () {
  8. function t(t, s) {
  9. for (var i = 0; i < s.length; i++) {
  10. var e = s[i];
  11. (e.enumerable = e.enumerable || !1),
  12. (e.configurable = !0),
  13. "value" in e && (e.writable = !0),
  14. Object.defineProperty(t, e.key, e);
  15. }
  16. }
  17. return function (s, i, e) {
  18. return i && t(s.prototype, i), e && t(s, e), s;
  19. };
  20. })(),
  21. cfg = require("./config.js"),
  22. CssHandler = (function () {
  23. function t(s) {
  24. var i = this;
  25. _classCallCheck(this, t),
  26. (this.getStyle = function (t) {
  27. return (i.styles = new CssParser(t, i.styles).parse());
  28. });
  29. var e = Object.assign({}, cfg.userAgentStyles);
  30. for (var a in s) e[a] = (e[a] ? e[a] + ";" : "") + s[a];
  31. this.styles = e;
  32. }
  33. return (
  34. _createClass(t, [
  35. {
  36. key: "match",
  37. value: function (t, s) {
  38. var i,
  39. e = (i = this.styles[t]) ? i + ";" : "";
  40. if (s.class)
  41. for (var a, h = s.class.split(" "), n = 0; (a = h[n]); n++)
  42. (i = this.styles["." + a]) && (e += i + ";");
  43. return (i = this.styles["#" + s.id]) && (e += i + ";"), e;
  44. },
  45. },
  46. ]),
  47. t
  48. );
  49. })();
  50. module.exports = CssHandler;
  51. var CssParser = (function () {
  52. function t(s, i) {
  53. var e = this;
  54. _classCallCheck(this, t),
  55. (this.section = function () {
  56. return e.data.substring(e.start, e.i);
  57. }),
  58. (this.isLetter = function (t) {
  59. return (t >= "a" && t <= "z") || (t >= "A" && t <= "Z");
  60. }),
  61. (this.data = s),
  62. (this.floor = 0),
  63. (this.i = 0),
  64. (this.list = []),
  65. (this.res = i),
  66. (this.state = this.Space);
  67. }
  68. return (
  69. _createClass(t, [
  70. {
  71. key: "parse",
  72. value: function () {
  73. for (var t; (t = this.data[this.i]); this.i++) this.state(t);
  74. return this.res;
  75. },
  76. },
  77. {
  78. key: "Space",
  79. value: function (t) {
  80. "." == t || "#" == t || this.isLetter(t)
  81. ? ((this.start = this.i), (this.state = this.Name))
  82. : "/" == t && "*" == this.data[this.i + 1]
  83. ? this.Comment()
  84. : cfg.blankChar[t] || ";" == t || (this.state = this.Ignore);
  85. },
  86. },
  87. {
  88. key: "Comment",
  89. value: function () {
  90. (this.i = this.data.indexOf("*/", this.i) + 1),
  91. this.i || (this.i = this.data.length),
  92. (this.state = this.Space);
  93. },
  94. },
  95. {
  96. key: "Ignore",
  97. value: function (t) {
  98. "{" == t
  99. ? this.floor++
  100. : "}" != t || --this.floor || (this.state = this.Space);
  101. },
  102. },
  103. {
  104. key: "Name",
  105. value: function (t) {
  106. cfg.blankChar[t]
  107. ? (this.list.push(this.section()), (this.state = this.NameSpace))
  108. : "{" == t
  109. ? (this.list.push(this.section()), this.Content())
  110. : "," == t
  111. ? (this.list.push(this.section()), this.Comma())
  112. : !this.isLetter(t) &&
  113. (t < "0" || t > "9") &&
  114. "-" != t &&
  115. "_" != t &&
  116. (this.state = this.Ignore);
  117. },
  118. },
  119. {
  120. key: "NameSpace",
  121. value: function (t) {
  122. "{" == t
  123. ? this.Content()
  124. : "," == t
  125. ? this.Comma()
  126. : cfg.blankChar[t] || (this.state = this.Ignore);
  127. },
  128. },
  129. {
  130. key: "Comma",
  131. value: function () {
  132. for (; cfg.blankChar[this.data[++this.i]]; );
  133. "{" == this.data[this.i]
  134. ? this.Content()
  135. : ((this.start = this.i--), (this.state = this.Name));
  136. },
  137. },
  138. {
  139. key: "Content",
  140. value: function () {
  141. (this.start = ++this.i),
  142. -1 == (this.i = this.data.indexOf("}", this.i)) &&
  143. (this.i = this.data.length);
  144. for (var t, s = this.section(), i = 0; (t = this.list[i++]); )
  145. this.res[t] ? (this.res[t] += ";" + s) : (this.res[t] = s);
  146. (this.list = []), (this.state = this.Space);
  147. },
  148. },
  149. ]),
  150. t
  151. );
  152. })();