style.spec.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*const cssSuffix = {
  2. //需要加后缀如 px s(秒)等css属性摘出来
  3. //其实用正则更简洁一些,不过可能 可读性可维护性不如key value
  4. //动画属性(Animation)
  5. animationDelay: 's',
  6. //CSS 边框属性(Border 和 Outline)
  7. borderBottomWidth: 'px',
  8. borderLeftWidth: 'px',
  9. borderRightWidth: 'px',
  10. borderTopWidth: 'px',
  11. borderWidth: 'px',
  12. outlineWidth: 'px',
  13. borderBottomLeftRadius: 'px',
  14. borderBottomRightRadius: 'px',
  15. borderRadius: 'px',
  16. borderTopLeftRadius: 'px',
  17. borderTopRightRadius: 'px',
  18. //Box 属性
  19. rotation: 'deg',
  20. //CSS 尺寸属性(Dimension)
  21. height: 'px',
  22. maxHeight: 'px',
  23. maxWidth: 'px',
  24. minHeight: 'px',
  25. minWidth: 'px',
  26. width: 'px',
  27. //CSS 字体属性(Font) font-variant:small-caps; 段落设置为小型大写字母字体
  28. fontSize: 'px',
  29. //CSS 外边距属性(Margin)
  30. margin: 'px',
  31. marginLeft: 'px',
  32. marginRight: 'px',
  33. marginTop: 'px',
  34. marginBottom: 'px',
  35. //多列属性(Multi-column)
  36. columnGap: 'px',
  37. WebkitColumnGap: 'px',
  38. MozColumnGap: 'px',
  39. columnRuleWidth: 'px',
  40. WebkitColumnRuleWidth: 'px',
  41. MozColumnRuleWidth: 'px',
  42. columnWidth: 'px',
  43. WebkitColumnWidth: 'px',
  44. MozColumnWidth: 'px',
  45. //CSS 内边距属性(Padding)
  46. padding: 'px',
  47. paddingLeft: 'px',
  48. paddingRight: 'px',
  49. paddingTop: 'px',
  50. paddingBottom: 'px',
  51. //CSS 定位属性(Positioning)
  52. left: 'px',
  53. right: 'px',
  54. top: 'px',
  55. bottom: 'px',
  56. //CSS 文本属性(Text)
  57. letterSpacing: 'px',
  58. lineHeight: 'px'
  59. }
  60. function patchStyle(dom, prevStyle, newStyle) {
  61. var _style = '';
  62. for (var name in newStyle) {
  63. var _type = typeof newStyle[name];
  64. //backgroundColor 替换为 background-color Webkit替换为-webkit- ms单独替换一次
  65. var cssName = name.replace(/([A-Z])/g, '-$1').toLowerCase().replace(/^ms-/i, '-ms-');
  66. switch (_type) {
  67. case 'string':
  68. _style = newStyle[name].trim();
  69. break;
  70. case 'number':
  71. _style = newStyle[name];
  72. if (cssSuffix[name]) {
  73. _style += newStyle[name] !== 0 ? +cssSuffix[name] : '';
  74. }
  75. break;
  76. case 'boolean':
  77. _style = '';
  78. break;
  79. default:
  80. _style = newStyle[name]
  81. break;
  82. }
  83. dom.style[cssName] = _style;
  84. }
  85. for (var key in prevStyle) {
  86. if (!(key in newStyle)) {
  87. key = name.replace(/([A-Z])/g, '-$1').toLowerCase().replace(/^ms-/i, '-ms-');
  88. dom.style[key] = '';
  89. }
  90. }
  91. }
  92. describe("style", function () {
  93. it("patchStyle", function () {
  94. var dom = document.createElement('div');
  95. var sameStyle = {};
  96. patchStyle(dom, sameStyle, sameStyle);
  97. expect(dom.style).toEqual({});
  98. dom.style.color = "red";
  99. patchStyle(dom, {
  100. color: "red"
  101. }, { color: "green" });
  102. expect(dom.style).toEqual({ color: "green" });
  103. patchStyle(dom, {
  104. color: "red"
  105. }, { color: null });
  106. expect(dom.style).toEqual({ color: "" });
  107. patchStyle(dom, {
  108. color: "red"
  109. }, { color: false });
  110. expect(dom.style).toEqual({ color: "" });
  111. dom.style.backgroundColor = "black";
  112. patchStyle(dom, dom.style, {});
  113. expect(dom.style).toEqual({ backgroundColor: "", color: "" });
  114. });
  115. // it("cssName", function () {
  116. // expect(cssName("xxx")).toBe(null);
  117. // });
  118. });*/