styleDomAPI.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. } // For old IE
  29. /* istanbul ignore if */
  30. options.styleTagTransform(css, styleElement, options.options);
  31. }
  32. function removeStyleElement(styleElement) {
  33. // istanbul ignore if
  34. if (styleElement.parentNode === null) {
  35. return false;
  36. }
  37. styleElement.parentNode.removeChild(styleElement);
  38. }
  39. /* istanbul ignore next */
  40. function domAPI(options) {
  41. var styleElement = options.insertStyleElement(options);
  42. return {
  43. update: function update(obj) {
  44. apply(styleElement, options, obj);
  45. },
  46. remove: function remove() {
  47. removeStyleElement(styleElement);
  48. }
  49. };
  50. }
  51. module.exports = domAPI;