版博士V2.0程序
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

RangeUtility.js 3.7 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 DOMException_1 = __importDefault(require("../exception/DOMException"));
  7. const DOMExceptionNameEnum_1 = __importDefault(require("../exception/DOMExceptionNameEnum"));
  8. const NodeTypeEnum_1 = __importDefault(require("../nodes/node/NodeTypeEnum"));
  9. const NodeUtility_1 = __importDefault(require("../nodes/node/NodeUtility"));
  10. /**
  11. * Range utility.
  12. *
  13. * Based on:
  14. * https://github.com/jsdom/jsdom/blob/master/lib/jsdom/living/range/boundary-point.js.
  15. */
  16. class RangeUtility {
  17. /**
  18. * Compares boundary points.
  19. *
  20. * Based on logic from:
  21. * https://github.com/jsdom/jsdom/blob/master/lib/jsdom/living/range/boundary-point.js
  22. *
  23. * @see https://dom.spec.whatwg.org/#concept-range-bp-after
  24. * @param pointA Point A.
  25. * @param pointB Point B.
  26. * @returns A number, -1, 0, or 1, indicating whether the corresponding boundary-point of the Range is respectively before, equal to, or after the corresponding boundary-point of sourceRange.
  27. */
  28. static compareBoundaryPointsPosition(pointA, pointB) {
  29. if (pointA.node === pointB.node) {
  30. if (pointA.offset === pointB.offset) {
  31. return 0;
  32. }
  33. else if (pointA.offset < pointB.offset) {
  34. return -1;
  35. }
  36. return 1;
  37. }
  38. if (NodeUtility_1.default.isFollowing(pointA.node, pointB.node)) {
  39. return this.compareBoundaryPointsPosition(pointB, pointA) === -1 ? 1 : -1;
  40. }
  41. if (NodeUtility_1.default.isInclusiveAncestor(pointA.node, pointB.node)) {
  42. let child = pointB.node;
  43. while (child.parentNode !== pointA.node) {
  44. child = child.parentNode;
  45. }
  46. if (child.parentNode.childNodes.indexOf(child) < pointA.offset) {
  47. return 1;
  48. }
  49. }
  50. return -1;
  51. }
  52. /**
  53. * Validates a boundary point.
  54. *
  55. * @throws DOMException
  56. * @param point Boundary point.
  57. */
  58. static validateBoundaryPoint(point) {
  59. if (point.node.nodeType === NodeTypeEnum_1.default.documentTypeNode) {
  60. throw new DOMException_1.default(`DocumentType Node can't be used as boundary point.`, DOMExceptionNameEnum_1.default.invalidNodeTypeError);
  61. }
  62. if (point.offset > NodeUtility_1.default.getNodeLength(point.node)) {
  63. throw new DOMException_1.default(`Offset out of bound.`, DOMExceptionNameEnum_1.default.indexSizeError);
  64. }
  65. }
  66. /**
  67. * Returns "true" if contained.
  68. *
  69. * @param node Node.
  70. * @param range Range.
  71. * @returns "true" if contained.
  72. */
  73. static isContained(node, range) {
  74. return (this.compareBoundaryPointsPosition({ node, offset: 0 }, { node: range.startContainer, offset: range.startOffset }) === 1 &&
  75. this.compareBoundaryPointsPosition({ node, offset: NodeUtility_1.default.getNodeLength(node) }, { node: range.endContainer, offset: range.endOffset }) === -1);
  76. }
  77. /**
  78. * Returns "true" if partially contained.
  79. *
  80. * @param node Node.
  81. * @param range Range.
  82. * @returns "true" if partially contained.
  83. */
  84. static isPartiallyContained(node, range) {
  85. return ((NodeUtility_1.default.isInclusiveAncestor(node, range.startContainer) &&
  86. !NodeUtility_1.default.isInclusiveAncestor(node, range.endContainer)) ||
  87. (!NodeUtility_1.default.isInclusiveAncestor(node, range.startContainer) &&
  88. NodeUtility_1.default.isInclusiveAncestor(node, range.endContainer)));
  89. }
  90. }
  91. exports.default = RangeUtility;
  92. //# sourceMappingURL=RangeUtility.js.map