-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRangeHelper.js
75 lines (62 loc) · 2.56 KB
/
RangeHelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
if (typeof Narmand === "undefined") {
Narmand = {};
}
$.extend(true, Narmand, {
RangeHelper: {
ToggleRangeSurroundingByTag: function (Range, TagName) {
if (this.DoesRangeContainTag(Range, TagName)) {
this.CleanRangeByTag(Range, TagName);
return;
}
var TagElement = document.createElement(TagName);
this.SurroundRangeByElement(Range, TagElement);
},
SurroundRangeByElement: function (Range, Element) {
if (Range.canSurroundContents()) {
Range.surroundContents(Element);
}
},
DoesRangeContainTag: function (Range, TagName) {
TagName = TagName.toLowerCase();
var NodeIterator = Range.createNodeIterator();
while (NodeIterator.hasNext()) {
var CurrentNode = NodeIterator.next();
if (CurrentNode.nodeType === 3 && CurrentNode.parentNode.tagName.toLowerCase() === TagName) {
return true;
}
if (CurrentNode.nodeType === 1 && CurrentNode.tagName.toLowerCase() === TagName) {
return true;
}
}
return false;
},
GetTagElementSurroundingRangeByTagName: function (Range, TagName) {
TagName = TagName.toLowerCase();
var NodeIterator = Range.createNodeIterator();
while (NodeIterator.hasNext()) {
var CurrentNode = NodeIterator.next();
if (CurrentNode.nodeType === 3 && CurrentNode.parentNode.tagName.toLowerCase() === TagName) {
return CurrentNode;
}
if (CurrentNode.nodeType === 1 && CurrentNode.tagName.toLowerCase() === TagName) {
return CurrentNode;
}
}
return null;
},
CleanRangeByTag: function (Range, TagName) {
TagName = TagName.toLowerCase();
var NodeIterator = Range.createNodeIterator();
while (NodeIterator.hasNext()) {
var CurrentNode = NodeIterator.next();
if (CurrentNode.nodeType === 3 && CurrentNode.parentNode.tagName.toLowerCase() === TagName) {
$(CurrentNode).unwrap();
}
if (CurrentNode.nodeType === 1 && CurrentNode.tagName.toLowerCase() === TagName) {
$(CurrentNode).contents().unwrap();
}
}
Range.commonAncestorContainer.normalize();
}
}
});