text.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. var __importDefault = (this && this.__importDefault) || function (mod) {
  18. return (mod && mod.__esModule) ? mod : { "default": mod };
  19. };
  20. Object.defineProperty(exports, "__esModule", { value: true });
  21. var type_1 = __importDefault(require("./type"));
  22. var node_1 = __importDefault(require("./node"));
  23. /**
  24. * TextNode to contain a text element in DOM tree.
  25. * @param {string} value [description]
  26. */
  27. var TextNode = /** @class */ (function (_super) {
  28. __extends(TextNode, _super);
  29. function TextNode(rawText, parentNode) {
  30. var _this = _super.call(this, parentNode) || this;
  31. _this.rawText = rawText;
  32. /**
  33. * Node Type declaration.
  34. * @type {Number}
  35. */
  36. _this.nodeType = type_1.default.TEXT_NODE;
  37. return _this;
  38. }
  39. Object.defineProperty(TextNode.prototype, "trimmedText", {
  40. /**
  41. * Returns text with all whitespace trimmed except single leading/trailing non-breaking space
  42. */
  43. get: function () {
  44. if (this._trimmedText !== undefined)
  45. return this._trimmedText;
  46. var text = this.rawText;
  47. var i = 0;
  48. var startPos;
  49. var endPos;
  50. while (i >= 0 && i < text.length) {
  51. if (/\S/.test(text[i])) {
  52. if (startPos === undefined) {
  53. startPos = i;
  54. i = text.length;
  55. }
  56. else {
  57. endPos = i;
  58. i = void 0;
  59. }
  60. }
  61. if (startPos === undefined)
  62. i++;
  63. else
  64. i--;
  65. }
  66. if (startPos === undefined)
  67. startPos = 0;
  68. if (endPos === undefined)
  69. endPos = text.length - 1;
  70. var hasLeadingSpace = startPos > 0 && /[^\S\r\n]/.test(text[startPos - 1]);
  71. var hasTrailingSpace = endPos < (text.length - 1) && /[^\S\r\n]/.test(text[endPos + 1]);
  72. this._trimmedText = (hasLeadingSpace ? ' ' : '') + text.slice(startPos, endPos + 1) + (hasTrailingSpace ? ' ' : '');
  73. return this._trimmedText;
  74. },
  75. enumerable: false,
  76. configurable: true
  77. });
  78. Object.defineProperty(TextNode.prototype, "text", {
  79. /**
  80. * Get unescaped text value of current node and its children.
  81. * @return {string} text content
  82. */
  83. get: function () {
  84. return this.rawText;
  85. },
  86. enumerable: false,
  87. configurable: true
  88. });
  89. Object.defineProperty(TextNode.prototype, "isWhitespace", {
  90. /**
  91. * Detect if the node contains only white space.
  92. * @return {bool}
  93. */
  94. get: function () {
  95. return /^(\s|&nbsp;)*$/.test(this.rawText);
  96. },
  97. enumerable: false,
  98. configurable: true
  99. });
  100. TextNode.prototype.toString = function () {
  101. return this.text;
  102. };
  103. return TextNode;
  104. }(node_1.default));
  105. exports.default = TextNode;