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

447 строки
12 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 Event_1 = __importDefault(require("../../event/Event"));
  7. const DOMException_1 = __importDefault(require("../../exception/DOMException"));
  8. const DOMExceptionNameEnum_1 = __importDefault(require("../../exception/DOMExceptionNameEnum"));
  9. const HTMLElement_1 = __importDefault(require("../html-element/HTMLElement"));
  10. const HTMLInputElementSelectionDirectionEnum_1 = __importDefault(require("../html-input-element/HTMLInputElementSelectionDirectionEnum"));
  11. const HTMLInputElementSelectionModeEnum_1 = __importDefault(require("../html-input-element/HTMLInputElementSelectionModeEnum"));
  12. /**
  13. * HTML Text Area Element.
  14. *
  15. * Reference:
  16. * https://developer.mozilla.org/en-US/docs/Web/API/HTMLTextAreaElement.
  17. */
  18. class HTMLTextAreaElement extends HTMLElement_1.default {
  19. constructor() {
  20. super(...arguments);
  21. this.type = 'textarea';
  22. this.defaultValue = '';
  23. // Events
  24. this.oninput = null;
  25. this.onselectionchange = null;
  26. this._value = null;
  27. this._selectionStart = null;
  28. this._selectionEnd = null;
  29. this._selectionDirection = HTMLInputElementSelectionDirectionEnum_1.default.none;
  30. }
  31. /**
  32. * Returns minlength.
  33. *
  34. * @returns Min length.
  35. */
  36. get minLength() {
  37. const minLength = this.getAttributeNS(null, 'minlength');
  38. if (minLength !== null) {
  39. return parseInt(minLength);
  40. }
  41. return -1;
  42. }
  43. /**
  44. * Sets minlength.
  45. *
  46. * @param minLength Min length.
  47. */
  48. set minLength(minlength) {
  49. this.setAttributeNS(null, 'minlength', String(minlength));
  50. }
  51. /**
  52. * Returns maxlength.
  53. *
  54. * @returns Max length.
  55. */
  56. get maxLength() {
  57. const maxLength = this.getAttributeNS(null, 'maxlength');
  58. if (maxLength !== null) {
  59. return parseInt(maxLength);
  60. }
  61. return -1;
  62. }
  63. /**
  64. * Sets maxlength.
  65. *
  66. * @param maxlength Max length.
  67. */
  68. set maxLength(maxLength) {
  69. this.setAttributeNS(null, 'maxlength', String(maxLength));
  70. }
  71. /**
  72. * Returns name.
  73. *
  74. * @returns Name.
  75. */
  76. get name() {
  77. return this.getAttributeNS(null, 'name') || '';
  78. }
  79. /**
  80. * Sets name.
  81. *
  82. * @param name Name.
  83. */
  84. set name(name) {
  85. this.setAttributeNS(null, 'name', name);
  86. }
  87. /**
  88. * Returns placeholder.
  89. *
  90. * @returns Placeholder.
  91. */
  92. get placeholder() {
  93. return this.getAttributeNS(null, 'placeholder') || '';
  94. }
  95. /**
  96. * Sets placeholder.
  97. *
  98. * @param placeholder Placeholder.
  99. */
  100. set placeholder(placeholder) {
  101. this.setAttributeNS(null, 'placeholder', placeholder);
  102. }
  103. /**
  104. * Returns inputmode.
  105. *
  106. * @returns Inputmode.
  107. */
  108. get inputmode() {
  109. return this.getAttributeNS(null, 'inputmode') || '';
  110. }
  111. /**
  112. * Sets inputmode.
  113. *
  114. * @param inputmode Inputmode.
  115. */
  116. set inputmode(inputmode) {
  117. this.setAttributeNS(null, 'inputmode', inputmode);
  118. }
  119. /**
  120. * Returns cols.
  121. *
  122. * @returns Cols.
  123. */
  124. get cols() {
  125. return this.getAttributeNS(null, 'cols') || '';
  126. }
  127. /**
  128. * Sets cols.
  129. *
  130. * @param cols Cols.
  131. */
  132. set cols(cols) {
  133. this.setAttributeNS(null, 'cols', cols);
  134. }
  135. /**
  136. * Returns rows.
  137. *
  138. * @returns Rows.
  139. */
  140. get rows() {
  141. return this.getAttributeNS(null, 'rows') || '';
  142. }
  143. /**
  144. * Sets rows.
  145. *
  146. * @param rows Rows.
  147. */
  148. set rows(rows) {
  149. this.setAttributeNS(null, 'rows', rows);
  150. }
  151. /**
  152. * Returns autocomplete.
  153. *
  154. * @returns Autocomplete.
  155. */
  156. get autocomplete() {
  157. return this.getAttributeNS(null, 'autocomplete') || '';
  158. }
  159. /**
  160. * Sets autocomplete.
  161. *
  162. * @param autocomplete Autocomplete.
  163. */
  164. set autocomplete(autocomplete) {
  165. this.setAttributeNS(null, 'autocomplete', autocomplete);
  166. }
  167. /**
  168. * Returns readOnly.
  169. *
  170. * @returns ReadOnly.
  171. */
  172. get readOnly() {
  173. return this.getAttributeNS(null, 'readonly') !== null;
  174. }
  175. /**
  176. * Sets readOnly.
  177. *
  178. * @param readOnly ReadOnly.
  179. */
  180. set readOnly(readOnly) {
  181. if (!readOnly) {
  182. this.removeAttributeNS(null, 'readonly');
  183. }
  184. else {
  185. this.setAttributeNS(null, 'readonly', '');
  186. }
  187. }
  188. /**
  189. * Returns disabled.
  190. *
  191. * @returns Disabled.
  192. */
  193. get disabled() {
  194. return this.getAttributeNS(null, 'disabled') !== null;
  195. }
  196. /**
  197. * Sets disabled.
  198. *
  199. * @param disabled Disabled.
  200. */
  201. set disabled(disabled) {
  202. if (!disabled) {
  203. this.removeAttributeNS(null, 'disabled');
  204. }
  205. else {
  206. this.setAttributeNS(null, 'disabled', '');
  207. }
  208. }
  209. /**
  210. * Returns autofocus.
  211. *
  212. * @returns Autofocus.
  213. */
  214. get autofocus() {
  215. return this.getAttributeNS(null, 'autofocus') !== null;
  216. }
  217. /**
  218. * Sets autofocus.
  219. *
  220. * @param autofocus Autofocus.
  221. */
  222. set autofocus(autofocus) {
  223. if (!autofocus) {
  224. this.removeAttributeNS(null, 'autofocus');
  225. }
  226. else {
  227. this.setAttributeNS(null, 'autofocus', '');
  228. }
  229. }
  230. /**
  231. * Returns required.
  232. *
  233. * @returns Required.
  234. */
  235. get required() {
  236. return this.getAttributeNS(null, 'required') !== null;
  237. }
  238. /**
  239. * Sets required.
  240. *
  241. * @param required Required.
  242. */
  243. set required(required) {
  244. if (!required) {
  245. this.removeAttributeNS(null, 'required');
  246. }
  247. else {
  248. this.setAttributeNS(null, 'required', '');
  249. }
  250. }
  251. /**
  252. * Returns value.
  253. *
  254. * @returns Value.
  255. */
  256. get value() {
  257. if (this._value === null) {
  258. return this.getAttributeNS(null, 'value') || '';
  259. }
  260. return this._value;
  261. }
  262. /**
  263. * Sets value.
  264. *
  265. * @param value Value.
  266. */
  267. set value(value) {
  268. const oldValue = this._value;
  269. this._value = value;
  270. if (oldValue !== this._value) {
  271. this._selectionStart = this._value.length;
  272. this._selectionEnd = this._value.length;
  273. this._selectionDirection = HTMLInputElementSelectionDirectionEnum_1.default.none;
  274. }
  275. }
  276. /**
  277. * Returns selection start.
  278. *
  279. * @returns Selection start.
  280. */
  281. get selectionStart() {
  282. if (this._selectionStart === null) {
  283. return this.value.length;
  284. }
  285. return this._selectionStart;
  286. }
  287. /**
  288. * Sets selection start.
  289. *
  290. * @param start Start.
  291. */
  292. set selectionStart(start) {
  293. this.setSelectionRange(start, Math.max(start, this.selectionEnd), this._selectionDirection);
  294. }
  295. /**
  296. * Returns selection end.
  297. *
  298. * @returns Selection end.
  299. */
  300. get selectionEnd() {
  301. if (this._selectionEnd === null) {
  302. return this.value.length;
  303. }
  304. return this._selectionEnd;
  305. }
  306. /**
  307. * Sets selection end.
  308. *
  309. * @param end End.
  310. */
  311. set selectionEnd(end) {
  312. this.setSelectionRange(this.selectionStart, end, this._selectionDirection);
  313. }
  314. /**
  315. * Returns selection direction.
  316. *
  317. * @returns Selection direction.
  318. */
  319. get selectionDirection() {
  320. return this._selectionDirection;
  321. }
  322. /**
  323. * Sets selection direction.
  324. *
  325. * @param direction Direction.
  326. */
  327. set selectionDirection(direction) {
  328. this.setSelectionRange(this._selectionStart, this._selectionEnd, direction);
  329. }
  330. /**
  331. * Returns the parent form element.
  332. *
  333. * @returns Form.
  334. */
  335. get form() {
  336. let parent = this.parentNode;
  337. while (parent && parent.tagName !== 'FORM') {
  338. parent = parent.parentNode;
  339. }
  340. return parent;
  341. }
  342. /**
  343. * Returns text length.
  344. *
  345. * @param Text Length.
  346. */
  347. get textLength() {
  348. return this.value.length;
  349. }
  350. /**
  351. * Set selection range.
  352. *
  353. * @param start Start.
  354. * @param end End.
  355. * @param [direction="none"] Direction.
  356. */
  357. setSelectionRange(start, end, direction = 'none') {
  358. this._selectionEnd = Math.min(end, this.value.length);
  359. this._selectionStart = Math.min(start, this._selectionEnd);
  360. this._selectionDirection =
  361. direction === HTMLInputElementSelectionDirectionEnum_1.default.forward ||
  362. direction === HTMLInputElementSelectionDirectionEnum_1.default.backward
  363. ? direction
  364. : HTMLInputElementSelectionDirectionEnum_1.default.none;
  365. this.dispatchEvent(new Event_1.default('select', { bubbles: true, cancelable: true }));
  366. }
  367. /**
  368. * Set range text.
  369. *
  370. * @param replacement Replacement.
  371. * @param [start] Start.
  372. * @param [end] End.
  373. * @param [direction] Direction.
  374. * @param selectionMode
  375. */
  376. setRangeText(replacement, start = null, end = null, selectionMode = HTMLInputElementSelectionModeEnum_1.default.preserve) {
  377. if (start === null) {
  378. start = this._selectionStart;
  379. }
  380. if (end === null) {
  381. end = this._selectionEnd;
  382. }
  383. if (start > end) {
  384. throw new DOMException_1.default('The index is not in the allowed range.', DOMExceptionNameEnum_1.default.invalidStateError);
  385. }
  386. start = Math.min(start, this.value.length);
  387. end = Math.min(end, this.value.length);
  388. const val = this.value;
  389. let selectionStart = this._selectionStart;
  390. let selectionEnd = this._selectionEnd;
  391. this.value = val.slice(0, start) + replacement + val.slice(end);
  392. const newEnd = start + this.value.length;
  393. switch (selectionMode) {
  394. case HTMLInputElementSelectionModeEnum_1.default.select:
  395. this.setSelectionRange(start, newEnd);
  396. break;
  397. case HTMLInputElementSelectionModeEnum_1.default.start:
  398. this.setSelectionRange(start, start);
  399. break;
  400. case HTMLInputElementSelectionModeEnum_1.default.end:
  401. this.setSelectionRange(newEnd, newEnd);
  402. break;
  403. default:
  404. const delta = replacement.length - (end - start);
  405. if (selectionStart > end) {
  406. selectionStart += delta;
  407. }
  408. else if (selectionStart > start) {
  409. selectionStart = start;
  410. }
  411. if (selectionEnd > end) {
  412. selectionEnd += delta;
  413. }
  414. else if (selectionEnd > start) {
  415. selectionEnd = newEnd;
  416. }
  417. this.setSelectionRange(selectionStart, selectionEnd);
  418. break;
  419. }
  420. }
  421. /**
  422. * Checks validity.
  423. *
  424. * @returns "true" if validation does'nt fail.
  425. */
  426. checkValidity() {
  427. return true;
  428. }
  429. /**
  430. * Clones a node.
  431. *
  432. * @override
  433. * @param [deep=false] "true" to clone deep.
  434. * @returns Cloned node.
  435. */
  436. cloneNode(deep = false) {
  437. const clone = super.cloneNode(deep);
  438. clone._value = this._value;
  439. clone._selectionStart = this._selectionStart;
  440. clone._selectionEnd = this._selectionEnd;
  441. clone._selectionDirection = this._selectionDirection;
  442. clone.defaultValue = this.defaultValue;
  443. return clone;
  444. }
  445. }
  446. exports.default = HTMLTextAreaElement;
  447. //# sourceMappingURL=HTMLTextAreaElement.js.map