styleDomAPI.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. "use strict";
  2. /* istanbul ignore next */
  3. function apply(styleElement, options, obj) {
  4. var css = "";
  5. if (obj.supports) {
  6. css += "@supports (".concat(obj.supports, ") {");
  7. }
  8. if (obj.media) {
  9. css += "@media ".concat(obj.media, " {");
  10. }
  11. var needLayer = typeof obj.layer !== "undefined";
  12. if (needLayer) {
  13. css += "@layer".concat(obj.layer.length > 0 ? " ".concat(obj.layer) : "", " {");
  14. }
  15. css += obj.css;
  16. if (needLayer) {
  17. css += "}";
  18. }
  19. if (obj.media) {
  20. css += "}";
  21. }
  22. if (obj.supports) {
  23. css += "}";
  24. }
  25. var sourceMap = obj.sourceMap;
  26. if (sourceMap && typeof btoa !== "undefined") {
  27. css += "\n/*# sourceMappingURL=data:application/json;base64,".concat(btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap)))), " */");
  28. }
  29. // For old IE
  30. /* istanbul ignore if */
  31. options.styleTagTransform(css, styleElement, options.options);
  32. }
  33. function removeStyleElement(styleElement) {
  34. // istanbul ignore if
  35. if (styleElement.parentNode === null) {
  36. return false;
  37. }
  38. styleElement.parentNode.removeChild(styleElement);
  39. }
  40. /* istanbul ignore next */
  41. function domAPI(options) {
  42. if (typeof document === "undefined") {
  43. return {
  44. update: function update() {},
  45. remove: function remove() {}
  46. };
  47. }
  48. var styleElement = options.insertStyleElement(options);
  49. return {
  50. update: function update(obj) {
  51. apply(styleElement, options, obj);
  52. },
  53. remove: function remove() {
  54. removeStyleElement(styleElement);
  55. }
  56. };
  57. }
  58. module.exports = domAPI;