cacheMapUtil.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.CSS_FILE_STYLE = exports.ATTR_CACHE_MAP = void 0;
  7. exports.existPath = existPath;
  8. exports.getStyleAndHash = getStyleAndHash;
  9. exports.prepare = prepare;
  10. exports.reset = reset;
  11. exports.serialize = serialize;
  12. var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
  13. var _canUseDom = _interopRequireDefault(require("rc-util/lib/Dom/canUseDom"));
  14. var _StyleContext = require("../StyleContext");
  15. var ATTR_CACHE_MAP = exports.ATTR_CACHE_MAP = 'data-ant-cssinjs-cache-path';
  16. /**
  17. * This marks style from the css file.
  18. * Which means not exist in `<style />` tag.
  19. */
  20. var CSS_FILE_STYLE = exports.CSS_FILE_STYLE = '_FILE_STYLE__';
  21. function serialize(cachePathMap) {
  22. return Object.keys(cachePathMap).map(function (path) {
  23. var hash = cachePathMap[path];
  24. return "".concat(path, ":").concat(hash);
  25. }).join(';');
  26. }
  27. var cachePathMap;
  28. var fromCSSFile = true;
  29. /**
  30. * @private Test usage only. Can save remove if no need.
  31. */
  32. function reset(mockCache) {
  33. var fromFile = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  34. cachePathMap = mockCache;
  35. fromCSSFile = fromFile;
  36. }
  37. function prepare() {
  38. if (!cachePathMap) {
  39. cachePathMap = {};
  40. if ((0, _canUseDom.default)()) {
  41. var div = document.createElement('div');
  42. div.className = ATTR_CACHE_MAP;
  43. div.style.position = 'fixed';
  44. div.style.visibility = 'hidden';
  45. div.style.top = '-9999px';
  46. document.body.appendChild(div);
  47. var content = getComputedStyle(div).content || '';
  48. content = content.replace(/^"/, '').replace(/"$/, '');
  49. // Fill data
  50. content.split(';').forEach(function (item) {
  51. var _item$split = item.split(':'),
  52. _item$split2 = (0, _slicedToArray2.default)(_item$split, 2),
  53. path = _item$split2[0],
  54. hash = _item$split2[1];
  55. cachePathMap[path] = hash;
  56. });
  57. // Remove inline record style
  58. var inlineMapStyle = document.querySelector("style[".concat(ATTR_CACHE_MAP, "]"));
  59. if (inlineMapStyle) {
  60. var _inlineMapStyle$paren;
  61. fromCSSFile = false;
  62. (_inlineMapStyle$paren = inlineMapStyle.parentNode) === null || _inlineMapStyle$paren === void 0 || _inlineMapStyle$paren.removeChild(inlineMapStyle);
  63. }
  64. document.body.removeChild(div);
  65. }
  66. }
  67. }
  68. function existPath(path) {
  69. prepare();
  70. return !!cachePathMap[path];
  71. }
  72. function getStyleAndHash(path) {
  73. var hash = cachePathMap[path];
  74. var styleStr = null;
  75. if (hash && (0, _canUseDom.default)()) {
  76. if (fromCSSFile) {
  77. styleStr = CSS_FILE_STYLE;
  78. } else {
  79. var _style = document.querySelector("style[".concat(_StyleContext.ATTR_MARK, "=\"").concat(cachePathMap[path], "\"]"));
  80. if (_style) {
  81. styleStr = _style.innerHTML;
  82. } else {
  83. // Clean up since not exist anymore
  84. delete cachePathMap[path];
  85. }
  86. }
  87. }
  88. return [styleStr, hash];
  89. }