insertBySelector.js 1003 B

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