版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

194 lines
4.7 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 HTMLElement_1 = __importDefault(require("../html-element/HTMLElement"));
  7. const ScriptUtility_1 = __importDefault(require("./ScriptUtility"));
  8. /**
  9. * HTML Script Element.
  10. *
  11. * Reference:
  12. * https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement.
  13. */
  14. class HTMLScriptElement extends HTMLElement_1.default {
  15. constructor() {
  16. super(...arguments);
  17. this.onerror = null;
  18. this.onload = null;
  19. this._evaluateScript = true;
  20. }
  21. /**
  22. * Returns type.
  23. *
  24. * @returns Type.
  25. */
  26. get type() {
  27. return this.getAttributeNS(null, 'type') || '';
  28. }
  29. /**
  30. * Sets type.
  31. *
  32. * @param type Type.
  33. */
  34. set type(type) {
  35. this.setAttributeNS(null, 'type', type);
  36. }
  37. /**
  38. * Returns source.
  39. *
  40. * @returns Source.
  41. */
  42. get src() {
  43. return this.getAttributeNS(null, 'src') || '';
  44. }
  45. /**
  46. * Sets source.
  47. *
  48. * @param source Source.
  49. */
  50. set src(src) {
  51. this.setAttributeNS(null, 'src', src);
  52. }
  53. /**
  54. * Returns charset.
  55. *
  56. * @returns Charset.
  57. */
  58. get charset() {
  59. return this.getAttributeNS(null, 'charset') || '';
  60. }
  61. /**
  62. * Sets charset.
  63. *
  64. * @param charset Charset.
  65. */
  66. set charset(charset) {
  67. this.setAttributeNS(null, 'charset', charset);
  68. }
  69. /**
  70. * Returns lang.
  71. *
  72. * @returns Lang.
  73. */
  74. get lang() {
  75. return this.getAttributeNS(null, 'lang') || '';
  76. }
  77. /**
  78. * Sets lang.
  79. *
  80. * @param lang Lang.
  81. */
  82. set lang(lang) {
  83. this.setAttributeNS(null, 'lang', lang);
  84. }
  85. /**
  86. * Returns async.
  87. *
  88. * @returns Async.
  89. */
  90. get async() {
  91. return this.getAttributeNS(null, 'async') !== null;
  92. }
  93. /**
  94. * Sets async.
  95. *
  96. * @param async Async.
  97. */
  98. set async(async) {
  99. if (!async) {
  100. this.removeAttributeNS(null, 'async');
  101. }
  102. else {
  103. this.setAttributeNS(null, 'async', '');
  104. }
  105. }
  106. /**
  107. * Returns defer.
  108. *
  109. * @returns Defer.
  110. */
  111. get defer() {
  112. return this.getAttributeNS(null, 'defer') !== null;
  113. }
  114. /**
  115. * Sets defer.
  116. *
  117. * @param defer Defer.
  118. */
  119. set defer(defer) {
  120. if (!defer) {
  121. this.removeAttributeNS(null, 'defer');
  122. }
  123. else {
  124. this.setAttributeNS(null, 'defer', '');
  125. }
  126. }
  127. /**
  128. * Returns text.
  129. *
  130. * @returns Text.
  131. */
  132. get text() {
  133. return this.textContent;
  134. }
  135. /**
  136. * Sets text.
  137. *
  138. * @param text Text.
  139. */
  140. set text(text) {
  141. this.textContent = text;
  142. }
  143. /**
  144. * The setAttributeNode() method adds a new Attr node to the specified element.
  145. *
  146. * @override
  147. * @param attribute Attribute.
  148. * @returns Replaced attribute.
  149. */
  150. setAttributeNode(attribute) {
  151. const replacedAttribute = super.setAttributeNode(attribute);
  152. if (attribute.name === 'src' && attribute.value !== null && this.isConnected) {
  153. ScriptUtility_1.default.loadExternalScript(this);
  154. }
  155. return replacedAttribute;
  156. }
  157. /**
  158. * Clones a node.
  159. *
  160. * @override
  161. * @param [deep=false] "true" to clone deep.
  162. * @returns Cloned node.
  163. */
  164. cloneNode(deep = false) {
  165. return super.cloneNode(deep);
  166. }
  167. /**
  168. * @override
  169. */
  170. _connectToNode(parentNode = null) {
  171. const isConnected = this.isConnected;
  172. const isParentConnected = parentNode ? parentNode.isConnected : false;
  173. super._connectToNode(parentNode);
  174. if (isParentConnected && isConnected !== isParentConnected && this._evaluateScript) {
  175. const src = this.getAttributeNS(null, 'src');
  176. if (src !== null) {
  177. ScriptUtility_1.default.loadExternalScript(this);
  178. }
  179. else if (!this.ownerDocument.defaultView.happyDOM.settings.disableJavaScriptEvaluation) {
  180. const textContent = this.textContent;
  181. const type = this.getAttributeNS(null, 'type');
  182. if (textContent &&
  183. (type === null ||
  184. type === 'application/x-ecmascript' ||
  185. type === 'application/x-javascript' ||
  186. type.startsWith('text/javascript'))) {
  187. this.ownerDocument.defaultView.eval(textContent);
  188. }
  189. }
  190. }
  191. }
  192. }
  193. exports.default = HTMLScriptElement;
  194. //# sourceMappingURL=HTMLScriptElement.js.map