版博士V2.0程序
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

912 satır
29 KiB

  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const Element_1 = __importDefault(require("../element/Element"));
  7. const HTMLUnknownElement_1 = __importDefault(require("../html-unknown-element/HTMLUnknownElement"));
  8. const Text_1 = __importDefault(require("../text/Text"));
  9. const Comment_1 = __importDefault(require("../comment/Comment"));
  10. const Node_1 = __importDefault(require("../node/Node"));
  11. const TreeWalker_1 = __importDefault(require("../../tree-walker/TreeWalker"));
  12. const DocumentFragment_1 = __importDefault(require("../document-fragment/DocumentFragment"));
  13. const XMLParser_1 = __importDefault(require("../../xml-parser/XMLParser"));
  14. const Event_1 = __importDefault(require("../../event/Event"));
  15. const DOMImplementation_1 = __importDefault(require("../../dom-implementation/DOMImplementation"));
  16. const ElementTag_1 = __importDefault(require("../../config/ElementTag"));
  17. const Attr_1 = __importDefault(require("../attr/Attr"));
  18. const NamespaceURI_1 = __importDefault(require("../../config/NamespaceURI"));
  19. const DocumentType_1 = __importDefault(require("../document-type/DocumentType"));
  20. const ParentNodeUtility_1 = __importDefault(require("../parent-node/ParentNodeUtility"));
  21. const QuerySelector_1 = __importDefault(require("../../query-selector/QuerySelector"));
  22. const DOMException_1 = __importDefault(require("../../exception/DOMException"));
  23. const CookieJar_1 = __importDefault(require("../../cookie/CookieJar"));
  24. const HTMLCollectionFactory_1 = __importDefault(require("../element/HTMLCollectionFactory"));
  25. const DocumentReadyStateEnum_1 = __importDefault(require("./DocumentReadyStateEnum"));
  26. const DocumentReadyStateManager_1 = __importDefault(require("./DocumentReadyStateManager"));
  27. const Selection_1 = __importDefault(require("../../selection/Selection"));
  28. const ProcessingInstruction_1 = __importDefault(require("../processing-instruction/ProcessingInstruction"));
  29. const VisibilityStateEnum_1 = __importDefault(require("./VisibilityStateEnum"));
  30. const PROCESSING_INSTRUCTION_TARGET_REGEXP = /^[a-z][a-z0-9-]+$/;
  31. /**
  32. * Document.
  33. */
  34. class Document extends Node_1.default {
  35. /**
  36. * Creates an instance of Document.
  37. *
  38. */
  39. constructor() {
  40. super();
  41. this.nodeType = Node_1.default.DOCUMENT_NODE;
  42. this.adoptedStyleSheets = [];
  43. this.children = HTMLCollectionFactory_1.default.create();
  44. this.readyState = DocumentReadyStateEnum_1.default.interactive;
  45. this.isConnected = true;
  46. this._activeElement = null;
  47. // Used as an unique identifier which is updated whenever the DOM gets modified.
  48. this._cacheID = 0;
  49. // Public in order to be accessible by the fetch and xhr.
  50. this._cookie = new CookieJar_1.default();
  51. this._isFirstWrite = true;
  52. this._isFirstWriteAfterOpen = false;
  53. this._selection = null;
  54. // Events
  55. this.onreadystatechange = null;
  56. this.onpointerlockchange = null;
  57. this.onpointerlockerror = null;
  58. this.onbeforecopy = null;
  59. this.onbeforecut = null;
  60. this.onbeforepaste = null;
  61. this.onfreeze = null;
  62. this.onresume = null;
  63. this.onsearch = null;
  64. this.onvisibilitychange = null;
  65. this.onfullscreenchange = null;
  66. this.onfullscreenerror = null;
  67. this.onwebkitfullscreenchange = null;
  68. this.onwebkitfullscreenerror = null;
  69. this.onbeforexrselect = null;
  70. this.onabort = null;
  71. this.onbeforeinput = null;
  72. this.onblur = null;
  73. this.oncancel = null;
  74. this.oncanplay = null;
  75. this.oncanplaythrough = null;
  76. this.onchange = null;
  77. this.onclick = null;
  78. this.onclose = null;
  79. this.oncontextlost = null;
  80. this.oncontextmenu = null;
  81. this.oncontextrestored = null;
  82. this.oncuechange = null;
  83. this.ondblclick = null;
  84. this.ondrag = null;
  85. this.ondragend = null;
  86. this.ondragenter = null;
  87. this.ondragleave = null;
  88. this.ondragover = null;
  89. this.ondragstart = null;
  90. this.ondrop = null;
  91. this.ondurationchange = null;
  92. this.onemptied = null;
  93. this.onended = null;
  94. this.onerror = null;
  95. this.onfocus = null;
  96. this.onformdata = null;
  97. this.oninput = null;
  98. this.oninvalid = null;
  99. this.onkeydown = null;
  100. this.onkeypress = null;
  101. this.onkeyup = null;
  102. this.onload = null;
  103. this.onloadeddata = null;
  104. this.onloadedmetadata = null;
  105. this.onloadstart = null;
  106. this.onmousedown = null;
  107. this.onmouseenter = null;
  108. this.onmouseleave = null;
  109. this.onmousemove = null;
  110. this.onmouseout = null;
  111. this.onmouseover = null;
  112. this.onmouseup = null;
  113. this.onmousewheel = null;
  114. this.onpause = null;
  115. this.onplay = null;
  116. this.onplaying = null;
  117. this.onprogress = null;
  118. this.onratechange = null;
  119. this.onreset = null;
  120. this.onresize = null;
  121. this.onscroll = null;
  122. this.onsecuritypolicyviolation = null;
  123. this.onseeked = null;
  124. this.onseeking = null;
  125. this.onselect = null;
  126. this.onslotchange = null;
  127. this.onstalled = null;
  128. this.onsubmit = null;
  129. this.onsuspend = null;
  130. this.ontimeupdate = null;
  131. this.ontoggle = null;
  132. this.onvolumechange = null;
  133. this.onwaiting = null;
  134. this.onwebkitanimationend = null;
  135. this.onwebkitanimationiteration = null;
  136. this.onwebkitanimationstart = null;
  137. this.onwebkittransitionend = null;
  138. this.onwheel = null;
  139. this.onauxclick = null;
  140. this.ongotpointercapture = null;
  141. this.onlostpointercapture = null;
  142. this.onpointerdown = null;
  143. this.onpointermove = null;
  144. this.onpointerrawupdate = null;
  145. this.onpointerup = null;
  146. this.onpointercancel = null;
  147. this.onpointerover = null;
  148. this.onpointerout = null;
  149. this.onpointerenter = null;
  150. this.onpointerleave = null;
  151. this.onselectstart = null;
  152. this.onselectionchange = null;
  153. this.onanimationend = null;
  154. this.onanimationiteration = null;
  155. this.onanimationstart = null;
  156. this.ontransitionrun = null;
  157. this.ontransitionstart = null;
  158. this.ontransitionend = null;
  159. this.ontransitioncancel = null;
  160. this.oncopy = null;
  161. this.oncut = null;
  162. this.onpaste = null;
  163. this.onbeforematch = null;
  164. this.defaultView = this.constructor._defaultView;
  165. this.implementation = new DOMImplementation_1.default(this);
  166. this._readyStateManager = new DocumentReadyStateManager_1.default(this.defaultView);
  167. const doctype = this.implementation.createDocumentType('html', '', '');
  168. const documentElement = this.createElement('html');
  169. const bodyElement = this.createElement('body');
  170. const headElement = this.createElement('head');
  171. this.appendChild(doctype);
  172. this.appendChild(documentElement);
  173. documentElement.appendChild(headElement);
  174. documentElement.appendChild(bodyElement);
  175. }
  176. /**
  177. * Returns character set.
  178. *
  179. * @deprecated
  180. * @returns Character set.
  181. */
  182. get charset() {
  183. return this.characterSet;
  184. }
  185. /**
  186. * Returns character set.
  187. *
  188. * @returns Character set.
  189. */
  190. get characterSet() {
  191. const charset = this.querySelector('meta[charset]')?.getAttributeNS(null, 'charset');
  192. return charset ? charset : 'UTF-8';
  193. }
  194. /**
  195. * Returns title.
  196. *
  197. * @returns Title.
  198. */
  199. get title() {
  200. const el = this.querySelector('title');
  201. if (el) {
  202. return el.textContent;
  203. }
  204. return '';
  205. }
  206. /**
  207. * Returns set title.
  208. *
  209. */
  210. set title(title) {
  211. const el = this.querySelector('title');
  212. if (el) {
  213. el.textContent = title;
  214. }
  215. else {
  216. const titleEl = this.createElement('title');
  217. titleEl.textContent = title;
  218. this.head.appendChild(titleEl);
  219. }
  220. }
  221. /**
  222. * Last element child.
  223. *
  224. * @returns Element.
  225. */
  226. get childElementCount() {
  227. return this.children.length;
  228. }
  229. /**
  230. * First element child.
  231. *
  232. * @returns Element.
  233. */
  234. get firstElementChild() {
  235. return this.children ? this.children[0] || null : null;
  236. }
  237. /**
  238. * Last element child.
  239. *
  240. * @returns Element.
  241. */
  242. get lastElementChild() {
  243. return this.children ? this.children[this.children.length - 1] || null : null;
  244. }
  245. /**
  246. * Returns cookie string.
  247. *
  248. * @returns Cookie.
  249. */
  250. get cookie() {
  251. return this._cookie.getCookiesString(this.defaultView.location, true);
  252. }
  253. /**
  254. * Sets a cookie string.
  255. *
  256. * @param cookie Cookie string.
  257. */
  258. set cookie(cookie) {
  259. this._cookie.setCookiesString(cookie);
  260. }
  261. /**
  262. * Node name.
  263. *
  264. * @returns Node name.
  265. */
  266. get nodeName() {
  267. return '#document';
  268. }
  269. /**
  270. * Returns <html> element.
  271. *
  272. * @returns Element.
  273. */
  274. get documentElement() {
  275. return ParentNodeUtility_1.default.getElementByTagName(this, 'html');
  276. }
  277. /**
  278. * Returns document type element.
  279. *
  280. * @returns Document type.
  281. */
  282. get doctype() {
  283. for (const node of this.childNodes) {
  284. if (node instanceof DocumentType_1.default) {
  285. return node;
  286. }
  287. }
  288. return null;
  289. }
  290. /**
  291. * Returns <body> element.
  292. *
  293. * @returns Element.
  294. */
  295. get body() {
  296. return ParentNodeUtility_1.default.getElementByTagName(this, 'body');
  297. }
  298. /**
  299. * Returns <head> element.
  300. *
  301. * @returns Element.
  302. */
  303. get head() {
  304. return ParentNodeUtility_1.default.getElementByTagName(this, 'head');
  305. }
  306. /**
  307. * Returns CSS style sheets.
  308. *
  309. * @returns CSS style sheets.
  310. */
  311. get styleSheets() {
  312. const styles = (this.querySelectorAll('link[rel="stylesheet"][href],style'));
  313. const styleSheets = [];
  314. for (const style of styles) {
  315. const sheet = style.sheet;
  316. if (sheet) {
  317. styleSheets.push(sheet);
  318. }
  319. }
  320. return styleSheets;
  321. }
  322. /**
  323. * Returns active element.
  324. *
  325. * @returns Active element.
  326. */
  327. get activeElement() {
  328. if (this._activeElement && !this._activeElement.isConnected) {
  329. this._activeElement = null;
  330. }
  331. if (this._activeElement && this._activeElement instanceof Element_1.default) {
  332. let rootNode = (this._activeElement.getRootNode());
  333. let activeElement = this._activeElement;
  334. while (rootNode !== this) {
  335. activeElement = rootNode.host;
  336. rootNode = activeElement ? activeElement.getRootNode() : this;
  337. }
  338. return activeElement;
  339. }
  340. return this._activeElement || this.body || this.documentElement || null;
  341. }
  342. /**
  343. * Returns scrolling element.
  344. *
  345. * @returns Scrolling element.
  346. */
  347. get scrollingElement() {
  348. return this.documentElement;
  349. }
  350. /**
  351. * Returns location.
  352. *
  353. * @returns Location.
  354. */
  355. get location() {
  356. return this.defaultView.location;
  357. }
  358. /**
  359. * Returns scripts.
  360. *
  361. * @returns Scripts.
  362. */
  363. get scripts() {
  364. return this.getElementsByTagName('script');
  365. }
  366. /**
  367. * Returns base URI.
  368. *
  369. * @override
  370. * @returns Base URI.
  371. */
  372. get baseURI() {
  373. const base = this.querySelector('base');
  374. if (base) {
  375. return base.href;
  376. }
  377. return this.defaultView.location.href;
  378. }
  379. /**
  380. * Returns URL.
  381. *
  382. * @returns the URL of the current document.
  383. * */
  384. get URL() {
  385. return this.defaultView.location.href;
  386. }
  387. /**
  388. * Returns document URI.
  389. *
  390. * @returns the URL of the current document.
  391. * */
  392. get documentURI() {
  393. return this.URL;
  394. }
  395. /**
  396. * Returns document visibility state.
  397. *
  398. * @returns the visibility state of the current document.
  399. * */
  400. get visibilityState() {
  401. if (this.defaultView) {
  402. return VisibilityStateEnum_1.default.visible;
  403. }
  404. return VisibilityStateEnum_1.default.hidden;
  405. }
  406. /**
  407. * Returns document hidden state.
  408. *
  409. * @returns the hidden state of the current document.
  410. * */
  411. get hidden() {
  412. if (this.defaultView) {
  413. return false;
  414. }
  415. return true;
  416. }
  417. /**
  418. * Inserts a set of Node objects or DOMString objects after the last child of the ParentNode. DOMString objects are inserted as equivalent Text nodes.
  419. *
  420. * @param nodes List of Node or DOMString.
  421. */
  422. append(...nodes) {
  423. ParentNodeUtility_1.default.append(this, ...nodes);
  424. }
  425. /**
  426. * Inserts a set of Node objects or DOMString objects before the first child of the ParentNode. DOMString objects are inserted as equivalent Text nodes.
  427. *
  428. * @param nodes List of Node or DOMString.
  429. */
  430. prepend(...nodes) {
  431. ParentNodeUtility_1.default.prepend(this, ...nodes);
  432. }
  433. /**
  434. * Replaces the existing children of a node with a specified new set of children.
  435. *
  436. * @param nodes List of Node or DOMString.
  437. */
  438. replaceChildren(...nodes) {
  439. ParentNodeUtility_1.default.replaceChildren(this, ...nodes);
  440. }
  441. /**
  442. * Query CSS selector to find matching elments.
  443. *
  444. * @param selector CSS selector.
  445. * @returns Matching elements.
  446. */
  447. querySelectorAll(selector) {
  448. return QuerySelector_1.default.querySelectorAll(this, selector);
  449. }
  450. /**
  451. * Query CSS Selector to find a matching element.
  452. *
  453. * @param selector CSS selector.
  454. * @returns Matching element.
  455. */
  456. querySelector(selector) {
  457. return QuerySelector_1.default.querySelector(this, selector);
  458. }
  459. /**
  460. * Returns an elements by class name.
  461. *
  462. * @param className Tag name.
  463. * @returns Matching element.
  464. */
  465. getElementsByClassName(className) {
  466. return ParentNodeUtility_1.default.getElementsByClassName(this, className);
  467. }
  468. /**
  469. * Returns an elements by tag name.
  470. *
  471. * @param tagName Tag name.
  472. * @returns Matching element.
  473. */
  474. getElementsByTagName(tagName) {
  475. return ParentNodeUtility_1.default.getElementsByTagName(this, tagName);
  476. }
  477. /**
  478. * Returns an elements by tag name and namespace.
  479. *
  480. * @param namespaceURI Namespace URI.
  481. * @param tagName Tag name.
  482. * @returns Matching element.
  483. */
  484. getElementsByTagNameNS(namespaceURI, tagName) {
  485. return ParentNodeUtility_1.default.getElementsByTagNameNS(this, namespaceURI, tagName);
  486. }
  487. /**
  488. * Returns an element by ID.
  489. *
  490. * @param id ID.
  491. * @returns Matching element.
  492. */
  493. getElementById(id) {
  494. return ParentNodeUtility_1.default.getElementById(this, id);
  495. }
  496. /**
  497. * Returns an element by Name.
  498. *
  499. * @returns Matching element.
  500. * @param name
  501. */
  502. getElementsByName(name) {
  503. const _getElementsByName = (_parentNode, _name) => {
  504. const matches = HTMLCollectionFactory_1.default.create();
  505. for (const child of _parentNode.children) {
  506. if ((child.getAttributeNS(null, 'name') || '') === _name) {
  507. matches.push(child);
  508. }
  509. for (const match of _getElementsByName(child, _name)) {
  510. matches.push(match);
  511. }
  512. }
  513. return matches;
  514. };
  515. return _getElementsByName(this, name);
  516. }
  517. /**
  518. * Clones a node.
  519. *
  520. * @override
  521. * @param [deep=false] "true" to clone deep.
  522. * @returns Cloned node.
  523. */
  524. cloneNode(deep = false) {
  525. this.constructor._defaultView = this.defaultView;
  526. const clone = super.cloneNode(deep);
  527. if (deep) {
  528. for (const node of clone.childNodes) {
  529. if (node.nodeType === Node_1.default.ELEMENT_NODE) {
  530. clone.children.push(node);
  531. }
  532. }
  533. }
  534. return clone;
  535. }
  536. /**
  537. * Append a child node to childNodes.
  538. *
  539. * @override
  540. * @param node Node to append.
  541. * @returns Appended node.
  542. */
  543. appendChild(node) {
  544. // If the type is DocumentFragment, then the child nodes of if it should be moved instead of the actual node.
  545. // See: https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment
  546. if (node.nodeType !== Node_1.default.DOCUMENT_FRAGMENT_NODE) {
  547. if (node.parentNode && node.parentNode['children']) {
  548. const index = node.parentNode['children'].indexOf(node);
  549. if (index !== -1) {
  550. node.parentNode['children'].splice(index, 1);
  551. }
  552. }
  553. if (node !== this && node.nodeType === Node_1.default.ELEMENT_NODE) {
  554. this.children.push(node);
  555. }
  556. }
  557. return super.appendChild(node);
  558. }
  559. /**
  560. * Remove Child element from childNodes array.
  561. *
  562. * @override
  563. * @param node Node to remove.
  564. */
  565. removeChild(node) {
  566. if (node.nodeType === Node_1.default.ELEMENT_NODE) {
  567. const index = this.children.indexOf(node);
  568. if (index !== -1) {
  569. this.children.splice(index, 1);
  570. }
  571. }
  572. return super.removeChild(node);
  573. }
  574. /**
  575. * Inserts a node before another.
  576. *
  577. * @override
  578. * @param newNode Node to insert.
  579. * @param [referenceNode] Node to insert before.
  580. * @returns Inserted node.
  581. */
  582. insertBefore(newNode, referenceNode) {
  583. const returnValue = super.insertBefore(newNode, referenceNode);
  584. // If the type is DocumentFragment, then the child nodes of if it should be moved instead of the actual node.
  585. // See: https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment
  586. if (newNode.nodeType !== Node_1.default.DOCUMENT_FRAGMENT_NODE) {
  587. if (newNode.parentNode && newNode.parentNode['children']) {
  588. const index = newNode.parentNode['children'].indexOf(newNode);
  589. if (index !== -1) {
  590. newNode.parentNode['children'].splice(index, 1);
  591. }
  592. }
  593. this.children.length = 0;
  594. for (const node of this.childNodes) {
  595. if (node.nodeType === Node_1.default.ELEMENT_NODE) {
  596. this.children.push(node);
  597. }
  598. }
  599. }
  600. return returnValue;
  601. }
  602. /**
  603. * Replaces the document HTML with new HTML.
  604. *
  605. * @param html HTML.
  606. */
  607. write(html) {
  608. const root = XMLParser_1.default.parse(this, html, true);
  609. if (this._isFirstWrite || this._isFirstWriteAfterOpen) {
  610. if (this._isFirstWrite) {
  611. if (!this._isFirstWriteAfterOpen) {
  612. this.open();
  613. }
  614. this._isFirstWrite = false;
  615. }
  616. this._isFirstWriteAfterOpen = false;
  617. let documentElement = null;
  618. let documentTypeNode = null;
  619. for (const node of root.childNodes) {
  620. if (node['tagName'] === 'HTML') {
  621. documentElement = node;
  622. }
  623. else if (node.nodeType === Node_1.default.DOCUMENT_TYPE_NODE) {
  624. documentTypeNode = node;
  625. }
  626. if (documentElement && documentTypeNode) {
  627. break;
  628. }
  629. }
  630. if (documentElement) {
  631. if (!this.documentElement) {
  632. if (documentTypeNode) {
  633. this.appendChild(documentTypeNode);
  634. }
  635. this.appendChild(documentElement);
  636. }
  637. else {
  638. const rootBody = root.querySelector('body');
  639. const body = this.querySelector('body');
  640. if (rootBody && body) {
  641. for (const child of rootBody.childNodes.slice()) {
  642. body.appendChild(child);
  643. }
  644. }
  645. }
  646. const body = this.querySelector('body');
  647. if (body) {
  648. for (const child of root.childNodes.slice()) {
  649. if (child['tagName'] !== 'HTML' && child.nodeType !== Node_1.default.DOCUMENT_TYPE_NODE) {
  650. body.appendChild(child);
  651. }
  652. }
  653. }
  654. }
  655. else {
  656. const documentElement = this.createElement('html');
  657. const bodyElement = this.createElement('body');
  658. const headElement = this.createElement('head');
  659. for (const child of root.childNodes.slice()) {
  660. bodyElement.appendChild(child);
  661. }
  662. documentElement.appendChild(headElement);
  663. documentElement.appendChild(bodyElement);
  664. this.appendChild(documentElement);
  665. }
  666. }
  667. else {
  668. const bodyNode = root.querySelector('body');
  669. for (const child of (bodyNode || root).childNodes.slice()) {
  670. this.body.appendChild(child);
  671. }
  672. }
  673. }
  674. /**
  675. * Opens the document.
  676. *
  677. * @returns Document.
  678. */
  679. open() {
  680. this._isFirstWriteAfterOpen = true;
  681. for (const eventType of Object.keys(this._listeners)) {
  682. const listeners = this._listeners[eventType];
  683. if (listeners) {
  684. for (const listener of listeners) {
  685. this.removeEventListener(eventType, listener);
  686. }
  687. }
  688. }
  689. for (const child of this.childNodes.slice()) {
  690. this.removeChild(child);
  691. }
  692. return this;
  693. }
  694. /**
  695. * Closes the document.
  696. */
  697. close() { }
  698. /* eslint-disable jsdoc/valid-types */
  699. /**
  700. * Creates an element.
  701. *
  702. * @param qualifiedName Tag name.
  703. * @param [options] Options.
  704. * @param [options.is] Tag name of a custom element previously defined via customElements.define().
  705. * @returns Element.
  706. */
  707. createElement(qualifiedName, options) {
  708. return this.createElementNS(NamespaceURI_1.default.html, qualifiedName, options);
  709. }
  710. /**
  711. * Creates an element with the specified namespace URI and qualified name.
  712. *
  713. * @param namespaceURI Namespace URI.
  714. * @param qualifiedName Tag name.
  715. * @param [options] Options.
  716. * @param [options.is] Tag name of a custom element previously defined via customElements.define().
  717. * @returns Element.
  718. */
  719. createElementNS(namespaceURI, qualifiedName, options) {
  720. const tagName = String(qualifiedName).toUpperCase();
  721. let customElementClass;
  722. if (this.defaultView && options && options.is) {
  723. customElementClass = this.defaultView.customElements.get(String(options.is));
  724. }
  725. else if (this.defaultView) {
  726. customElementClass = this.defaultView.customElements.get(tagName);
  727. }
  728. const elementClass = customElementClass || ElementTag_1.default[tagName] || HTMLUnknownElement_1.default;
  729. elementClass._ownerDocument = this;
  730. const element = new elementClass();
  731. element.tagName = tagName;
  732. element.ownerDocument = this;
  733. element.namespaceURI = namespaceURI;
  734. if (element instanceof Element_1.default && options && options.is) {
  735. element._isValue = String(options.is);
  736. }
  737. return element;
  738. }
  739. /* eslint-enable jsdoc/valid-types */
  740. /**
  741. * Creates a text node.
  742. *
  743. * @param [data] Text data.
  744. * @returns Text node.
  745. */
  746. createTextNode(data) {
  747. Text_1.default._ownerDocument = this;
  748. return new Text_1.default(data);
  749. }
  750. /**
  751. * Creates a comment node.
  752. *
  753. * @param [data] Text data.
  754. * @returns Text node.
  755. */
  756. createComment(data) {
  757. Comment_1.default._ownerDocument = this;
  758. return new Comment_1.default(data);
  759. }
  760. /**
  761. * Creates a document fragment.
  762. *
  763. * @returns Document fragment.
  764. */
  765. createDocumentFragment() {
  766. DocumentFragment_1.default._ownerDocument = this;
  767. return new DocumentFragment_1.default();
  768. }
  769. /**
  770. * Creates a Tree Walker.
  771. *
  772. * @param root Root.
  773. * @param [whatToShow] What to show.
  774. * @param [filter] Filter.
  775. */
  776. createTreeWalker(root, whatToShow = -1, filter = null) {
  777. return new TreeWalker_1.default(root, whatToShow, filter);
  778. }
  779. /**
  780. * Creates an event.
  781. *
  782. * @deprecated
  783. * @param type Type.
  784. * @returns Event.
  785. */
  786. createEvent(type) {
  787. if (typeof this.defaultView[type] === 'function') {
  788. return new this.defaultView[type]('init');
  789. }
  790. return new Event_1.default('init');
  791. }
  792. /**
  793. * Creates an Attr node.
  794. *
  795. * @param qualifiedName Name.
  796. * @returns Attribute.
  797. */
  798. createAttribute(qualifiedName) {
  799. return this.createAttributeNS(null, qualifiedName.toLowerCase());
  800. }
  801. /**
  802. * Creates a namespaced Attr node.
  803. *
  804. * @param namespaceURI Namespace URI.
  805. * @param qualifiedName Qualified name.
  806. * @returns Element.
  807. */
  808. createAttributeNS(namespaceURI, qualifiedName) {
  809. Attr_1.default._ownerDocument = this;
  810. const attribute = new Attr_1.default();
  811. attribute.namespaceURI = namespaceURI;
  812. attribute.name = qualifiedName;
  813. return attribute;
  814. }
  815. /**
  816. * Imports a node.
  817. *
  818. * @param node Node to import.
  819. * @param [deep=false] Set to "true" if the clone should be deep.
  820. */
  821. importNode(node, deep = false) {
  822. if (!(node instanceof Node_1.default)) {
  823. throw new DOMException_1.default('Parameter 1 was not of type Node.');
  824. }
  825. const clone = node.cloneNode(deep);
  826. clone.ownerDocument = this;
  827. return clone;
  828. }
  829. /**
  830. * Creates a range.
  831. *
  832. * @returns Range.
  833. */
  834. createRange() {
  835. return new this.defaultView.Range();
  836. }
  837. /**
  838. * Adopts a node.
  839. *
  840. * @param node Node to adopt.
  841. * @returns Adopted node.
  842. */
  843. adoptNode(node) {
  844. if (!(node instanceof Node_1.default)) {
  845. throw new DOMException_1.default('Parameter 1 was not of type Node.');
  846. }
  847. const adopted = node.parentNode ? node.parentNode.removeChild(node) : node;
  848. adopted.ownerDocument = this;
  849. return adopted;
  850. }
  851. /**
  852. * Returns selection.
  853. *
  854. * @returns Selection.
  855. */
  856. getSelection() {
  857. if (!this._selection) {
  858. this._selection = new Selection_1.default(this);
  859. }
  860. return this._selection;
  861. }
  862. /**
  863. * Returns a boolean value indicating whether the document or any element inside the document has focus.
  864. *
  865. * @returns "true" if the document has focus.
  866. */
  867. hasFocus() {
  868. return !!this.activeElement;
  869. }
  870. /**
  871. * @override
  872. */
  873. dispatchEvent(event) {
  874. const returnValue = super.dispatchEvent(event);
  875. if (event.bubbles && !event._propagationStopped) {
  876. return this.defaultView.dispatchEvent(event);
  877. }
  878. return returnValue;
  879. }
  880. /**
  881. * Triggered by window when it is ready.
  882. */
  883. _onWindowReady() {
  884. this._readyStateManager.whenComplete().then(() => {
  885. this.readyState = DocumentReadyStateEnum_1.default.complete;
  886. this.dispatchEvent(new Event_1.default('readystatechange'));
  887. this.dispatchEvent(new Event_1.default('load', { bubbles: true }));
  888. });
  889. }
  890. /**
  891. * Creates a Processing Instruction node.
  892. *
  893. * @returns IProcessingInstruction.
  894. * @param target
  895. * @param data
  896. */
  897. createProcessingInstruction(target, data) {
  898. if (!target || !PROCESSING_INSTRUCTION_TARGET_REGEXP.test(target)) {
  899. throw new DOMException_1.default(`Failed to execute 'createProcessingInstruction' on 'Document': The target provided ('${target}') is not a valid name.`);
  900. }
  901. if (data.includes('?>')) {
  902. throw new DOMException_1.default(`Failed to execute 'createProcessingInstruction' on 'Document': The data provided ('?>') contains '?>'`);
  903. }
  904. ProcessingInstruction_1.default._ownerDocument = this;
  905. const processingInstruction = new ProcessingInstruction_1.default(data);
  906. processingInstruction.target = target;
  907. return processingInstruction;
  908. }
  909. }
  910. exports.default = Document;
  911. Document._defaultView = null;
  912. //# sourceMappingURL=Document.js.map