版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

246 строки
8.0 KiB

  1. "use strict";
  2. var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
  3. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
  4. if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
  5. return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
  6. };
  7. var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
  8. if (kind === "m") throw new TypeError("Private method is not writable");
  9. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
  10. if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
  11. return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
  12. };
  13. var __importDefault = (this && this.__importDefault) || function (mod) {
  14. return (mod && mod.__esModule) ? mod : { "default": mod };
  15. };
  16. var _HTMLIFrameElement_contentWindow;
  17. Object.defineProperty(exports, "__esModule", { value: true });
  18. const url_1 = require("url");
  19. const Event_1 = __importDefault(require("../../event/Event"));
  20. const ErrorEvent_1 = __importDefault(require("../../event/events/ErrorEvent"));
  21. const HTMLElement_1 = __importDefault(require("../html-element/HTMLElement"));
  22. const IFrameCrossOriginWindow_1 = __importDefault(require("./IFrameCrossOriginWindow"));
  23. /**
  24. * HTML Iframe Element.
  25. *
  26. * Reference:
  27. * https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement.
  28. */
  29. class HTMLIFrameElement extends HTMLElement_1.default {
  30. constructor() {
  31. super(...arguments);
  32. // Events
  33. this.onload = null;
  34. this.onerror = null;
  35. // Private
  36. _HTMLIFrameElement_contentWindow.set(this, null);
  37. }
  38. /**
  39. * Returns source.
  40. *
  41. * @returns Source.
  42. */
  43. get src() {
  44. return this.getAttribute('src') || '';
  45. }
  46. /**
  47. * Sets source.
  48. *
  49. * @param src Source.
  50. */
  51. set src(src) {
  52. this.setAttribute('src', src);
  53. }
  54. /**
  55. * Returns allow.
  56. *
  57. * @returns Allow.
  58. */
  59. get allow() {
  60. return this.getAttribute('allow') || '';
  61. }
  62. /**
  63. * Sets allow.
  64. *
  65. * @param allow Allow.
  66. */
  67. set allow(allow) {
  68. this.setAttribute('allow', allow);
  69. }
  70. /**
  71. * Returns height.
  72. *
  73. * @returns Height.
  74. */
  75. get height() {
  76. return this.getAttribute('height') || '';
  77. }
  78. /**
  79. * Sets height.
  80. *
  81. * @param height Height.
  82. */
  83. set height(height) {
  84. this.setAttribute('height', height);
  85. }
  86. /**
  87. * Returns width.
  88. *
  89. * @returns Width.
  90. */
  91. get width() {
  92. return this.getAttribute('width') || '';
  93. }
  94. /**
  95. * Sets width.
  96. *
  97. * @param width Width.
  98. */
  99. set width(width) {
  100. this.setAttribute('width', width);
  101. }
  102. /**
  103. * Returns name.
  104. *
  105. * @returns Name.
  106. */
  107. get name() {
  108. return this.getAttribute('name') || '';
  109. }
  110. /**
  111. * Sets name.
  112. *
  113. * @param name Name.
  114. */
  115. set name(name) {
  116. this.setAttribute('name', name);
  117. }
  118. /**
  119. * Returns sandbox.
  120. *
  121. * @returns Sandbox.
  122. */
  123. get sandbox() {
  124. return this.getAttribute('sandbox') || '';
  125. }
  126. /**
  127. * Sets sandbox.
  128. *
  129. * @param sandbox Sandbox.
  130. */
  131. set sandbox(sandbox) {
  132. this.setAttribute('sandbox', sandbox);
  133. }
  134. /**
  135. * Returns srcdoc.
  136. *
  137. * @returns Srcdoc.
  138. */
  139. get srcdoc() {
  140. return this.getAttribute('srcdoc') || '';
  141. }
  142. /**
  143. * Sets sandbox.
  144. *
  145. * @param srcdoc Srcdoc.
  146. */
  147. set srcdoc(srcdoc) {
  148. this.setAttribute('srcdoc', srcdoc);
  149. }
  150. /**
  151. * Returns content document.
  152. *
  153. * @returns Content document.
  154. */
  155. get contentDocument() {
  156. return __classPrivateFieldGet(this, _HTMLIFrameElement_contentWindow, "f")?.document || null;
  157. }
  158. /**
  159. * Returns content window.
  160. *
  161. * @returns Content window.
  162. */
  163. get contentWindow() {
  164. return __classPrivateFieldGet(this, _HTMLIFrameElement_contentWindow, "f") || null;
  165. }
  166. /**
  167. * @override
  168. */
  169. _connectToNode(parentNode = null) {
  170. const isConnected = this.isConnected;
  171. const isParentConnected = parentNode ? parentNode.isConnected : false;
  172. super._connectToNode(parentNode);
  173. if (isParentConnected &&
  174. isConnected !== isParentConnected &&
  175. !this.ownerDocument.defaultView.happyDOM.settings.disableIframePageLoading) {
  176. const src = this.src;
  177. if (src !== null) {
  178. const contentWindow = new this.ownerDocument.defaultView.constructor({
  179. url: src,
  180. settings: {
  181. ...this.ownerDocument.defaultView.happyDOM.settings
  182. }
  183. });
  184. contentWindow.parent = this.ownerDocument.defaultView;
  185. contentWindow.top = this.ownerDocument.defaultView;
  186. if (src === 'about:blank') {
  187. __classPrivateFieldSet(this, _HTMLIFrameElement_contentWindow, contentWindow, "f");
  188. return;
  189. }
  190. if (src.startsWith('javascript:')) {
  191. __classPrivateFieldSet(this, _HTMLIFrameElement_contentWindow, contentWindow, "f");
  192. __classPrivateFieldGet(this, _HTMLIFrameElement_contentWindow, "f").eval(src.replace('javascript:', ''));
  193. return;
  194. }
  195. const originURL = this.ownerDocument.defaultView.location;
  196. const targetURL = new url_1.URL(src, originURL);
  197. const isCORS = (originURL.hostname !== targetURL.hostname &&
  198. !originURL.hostname.endsWith(targetURL.hostname)) ||
  199. originURL.protocol !== targetURL.protocol;
  200. const onError = (error) => {
  201. this.dispatchEvent(new ErrorEvent_1.default('error', {
  202. message: error.message,
  203. error
  204. }));
  205. this.ownerDocument.defaultView.dispatchEvent(new ErrorEvent_1.default('error', {
  206. message: error.message,
  207. error
  208. }));
  209. if (!this['_listeners']['error'] &&
  210. !this.ownerDocument.defaultView['_listeners']['error']) {
  211. this.ownerDocument.defaultView.console.error(error);
  212. }
  213. };
  214. __classPrivateFieldSet(this, _HTMLIFrameElement_contentWindow, null, "f");
  215. this.ownerDocument.defaultView
  216. .fetch(src)
  217. .then((response) => {
  218. response
  219. .text()
  220. .then((text) => {
  221. __classPrivateFieldSet(this, _HTMLIFrameElement_contentWindow, isCORS
  222. ? new IFrameCrossOriginWindow_1.default(this.ownerDocument.defaultView, contentWindow)
  223. : contentWindow, "f");
  224. contentWindow.document.write(text);
  225. this.dispatchEvent(new Event_1.default('load'));
  226. })
  227. .catch(onError);
  228. })
  229. .catch(onError);
  230. }
  231. }
  232. }
  233. /**
  234. * Clones a node.
  235. *
  236. * @override
  237. * @param [deep=false] "true" to clone deep.
  238. * @returns Cloned node.
  239. */
  240. cloneNode(deep = false) {
  241. return super.cloneNode(deep);
  242. }
  243. }
  244. exports.default = HTMLIFrameElement;
  245. _HTMLIFrameElement_contentWindow = new WeakMap();
  246. //# sourceMappingURL=HTMLIFrameElement.js.map