insertBySelector.js 1005 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. "use strict";
  2. var memo = {};
  3. /* istanbul ignore next */
  4. function getTarget(target) {
  5. if (typeof memo[target] === "undefined") {
  6. var styleTarget = document.querySelector(target); // Special case to return head of iframe instead of iframe itself
  7. if (window.HTMLIFrameElement && styleTarget instanceof window.HTMLIFrameElement) {
  8. try {
  9. // This will throw an exception if access to iframe is blocked
  10. // due to cross-origin restrictions
  11. styleTarget = styleTarget.contentDocument.head;
  12. } catch (e) {
  13. // istanbul ignore next
  14. styleTarget = null;
  15. }
  16. }
  17. memo[target] = styleTarget;
  18. }
  19. return memo[target];
  20. }
  21. /* istanbul ignore next */
  22. function insertBySelector(insert, style) {
  23. var target = getTarget(insert);
  24. if (!target) {
  25. throw new Error("Couldn't find a style target. This probably means that the value for the 'insert' parameter is invalid.");
  26. }
  27. target.appendChild(style);
  28. }
  29. module.exports = insertBySelector;