Skip to content

Commit 7aee6c9

Browse files
committed
Rebuild library
1 parent de45b88 commit 7aee6c9

File tree

6 files changed

+92
-55
lines changed

6 files changed

+92
-55
lines changed

lib/rangy-classapplier.js

Lines changed: 81 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
*
1010
* Copyright 2014, Tim Down
1111
* Licensed under the MIT license.
12-
* Version: 1.3.0-alpha.20140921
13-
* Build date: 21 September 2014
12+
* Version: 1.3.0-peitschie.perf-master
13+
* Build date: 2 October 2014
1414
*/
1515
(function(factory, root) {
1616
if (typeof define == "function" && define.amd) {
@@ -48,31 +48,46 @@
4848
return str.replace(/^\s\s*/, "").replace(/\s\s*$/, "");
4949
}
5050

51-
function hasClass(el, className) {
52-
return el.className && new RegExp("(?:^|\\s)" + className + "(?:\\s|$)").test(el.className);
53-
}
51+
var hasClass, addClass, removeClass;
52+
if (api.util.isHostObject(document.createElement("div"), "classList")) {
53+
hasClass = function(el, className) {
54+
return el.classList.contains(className);
55+
};
5456

55-
function addClass(el, className) {
56-
if (el.className) {
57-
if (!hasClass(el, className)) {
58-
el.className += " " + className;
59-
}
60-
} else {
61-
el.className = className;
62-
}
63-
}
57+
addClass = function(el, className) {
58+
return el.classList.add(className);
59+
};
6460

65-
var removeClass = (function() {
66-
function replacer(matched, whiteSpaceBefore, whiteSpaceAfter) {
67-
return (whiteSpaceBefore && whiteSpaceAfter) ? " " : "";
68-
}
61+
removeClass = function(el, className) {
62+
return el.classList.remove(className);
63+
};
64+
} else {
65+
hasClass = function(el, className) {
66+
return el.className && new RegExp("(?:^|\\s)" + className + "(?:\\s|$)").test(el.className);
67+
};
6968

70-
return function(el, className) {
69+
addClass = function(el, className) {
7170
if (el.className) {
72-
el.className = el.className.replace(new RegExp("(^|\\s)" + className + "(\\s|$)"), replacer);
71+
if (!hasClass(el, className)) {
72+
el.className += " " + className;
73+
}
74+
} else {
75+
el.className = className;
7376
}
7477
};
75-
})();
78+
79+
removeClass = (function() {
80+
function replacer(matched, whiteSpaceBefore, whiteSpaceAfter) {
81+
return (whiteSpaceBefore && whiteSpaceAfter) ? " " : "";
82+
}
83+
84+
return function(el, className) {
85+
if (el.className) {
86+
el.className = el.className.replace(new RegExp("(^|\\s)" + className + "(\\s|$)"), replacer);
87+
}
88+
};
89+
})();
90+
}
7691

7792
function sortClassName(className) {
7893
return className && className.split(/\s+/).sort().join(" ");
@@ -217,18 +232,6 @@
217232
return false;
218233
}
219234

220-
function elementHasProperties(el, props) {
221-
return each(props, function(p, propValue) {
222-
if (typeof propValue == "object") {
223-
if (!elementHasProperties(el[p], propValue)) {
224-
return false;
225-
}
226-
} else if (el[p] !== propValue) {
227-
return false;
228-
}
229-
});
230-
}
231-
232235
var getComputedStyleProperty = dom.getComputedStyleProperty;
233236
var isEditableElement = (function() {
234237
var testEl = document.createElement("div");
@@ -517,7 +520,7 @@
517520
applier.elementAttributes = elementAttributes;
518521

519522
applier.elementSortedClassName = applier.elementProperties.hasOwnProperty("className") ?
520-
applier.elementProperties.className : className;
523+
sortClassName(applier.elementProperties.className + " " + className) : className;
521524

522525
// Initialize tag names
523526
applier.applyToAnyTagName = false;
@@ -567,7 +570,7 @@
567570
addClass(el, this.className);
568571
el[p] = sortClassName(el[p]);
569572
if (createCopy) {
570-
elProps[p] = el[p];
573+
elProps[p] = propValue;
571574
}
572575
}
573576

@@ -578,9 +581,11 @@
578581
elProps[p] = elPropsStyle = {};
579582
}
580583
for (s in props[p]) {
581-
elStyle[s] = propValue[s];
582-
if (createCopy) {
583-
elPropsStyle[s] = elStyle[s];
584+
if (props[p].hasOwnProperty(s)) {
585+
elStyle[s] = propValue[s];
586+
if (createCopy) {
587+
elPropsStyle[s] = elStyle[s];
588+
}
584589
}
585590
}
586591
this.attrExceptions.push(p);
@@ -704,13 +709,28 @@
704709
return el;
705710
},
706711

712+
elementHasProperties: function(el, props) {
713+
var applier = this;
714+
return each(props, function(p, propValue) {
715+
if (p == "className") {
716+
return hasClass(el, propValue);
717+
} else if (typeof propValue == "object") {
718+
if (!applier.elementHasProperties(el[p], propValue)) {
719+
return false;
720+
}
721+
} else if (el[p] !== propValue) {
722+
return false;
723+
}
724+
});
725+
},
726+
707727
applyToTextNode: function(textNode, positionsToPreserve) {
708728
var parent = textNode.parentNode;
709729
if (parent.childNodes.length == 1 &&
710730
this.useExistingElements &&
711731
isHtmlNamespace(parent) &&
712732
contains(this.tagNames, parent.tagName.toLowerCase()) &&
713-
elementHasProperties(parent, this.elementProperties)) {
733+
this.elementHasProperties(parent, this.elementProperties)) {
714734

715735
addClass(parent, this.className);
716736
} else {
@@ -724,7 +744,7 @@
724744
return isHtmlNamespace(el) &&
725745
el.tagName.toLowerCase() == this.elementTagName &&
726746
getSortedClassName(el) == this.elementSortedClassName &&
727-
elementHasProperties(el, this.elementProperties) &&
747+
this.elementHasProperties(el, this.elementProperties) &&
728748
!elementHasNonClassAttributes(el, this.attrExceptions) &&
729749
this.isModifiable(el);
730750
},
@@ -767,6 +787,22 @@
767787
ancestorWithClass = splitNodeAt(ancestorWithClass, range.startContainer, range.startOffset, positionsToPreserve);
768788
}
769789
}
790+
791+
if (this.isRemovable(ancestorWithClass)) {
792+
replaceWithOwnChildrenPreservingPositions(ancestorWithClass, positionsToPreserve);
793+
} else {
794+
removeClass(ancestorWithClass, this.className);
795+
}
796+
},
797+
798+
splitAncestorWithClass: function(container, offset, positionsToPreserve) {
799+
var ancestorWithClass = this.getSelfOrAncestorWithClass(container);
800+
if (ancestorWithClass) {
801+
splitNodeAt(ancestorWithClass, container, offset, positionsToPreserve);
802+
}
803+
},
804+
805+
undoToAncestor: function(ancestorWithClass, positionsToPreserve) {
770806
if (this.isRemovable(ancestorWithClass)) {
771807
replaceWithOwnChildrenPreservingPositions(ancestorWithClass, positionsToPreserve);
772808
} else {
@@ -841,16 +877,17 @@
841877
var lastTextNode = textNodes[textNodes.length - 1];
842878

843879
if (textNodes.length) {
880+
this.splitAncestorWithClass(range.endContainer, range.endOffset, positionsToPreserve);
881+
this.splitAncestorWithClass(range.startContainer, range.startOffset, positionsToPreserve);
844882
for (var i = 0, len = textNodes.length; i < len; ++i) {
845883
textNode = textNodes[i];
846884
ancestorWithClass = this.getSelfOrAncestorWithClass(textNode);
847885
if (ancestorWithClass && this.isModifiable(textNode)) {
848-
this.undoToTextNode(textNode, range, ancestorWithClass, positionsToPreserve);
886+
this.undoToAncestor(ancestorWithClass, positionsToPreserve);
849887
}
850-
851-
// Ensure the range is still valid
852-
range.setStartAndEnd(textNodes[0], 0, lastTextNode, lastTextNode.length);
853888
}
889+
// Ensure the range is still valid
890+
range.setStartAndEnd(textNodes[0], 0, lastTextNode, lastTextNode.length);
854891

855892

856893
if (this.normalize) {

lib/rangy-core.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*
55
* Copyright 2014, Tim Down
66
* Licensed under the MIT license.
7-
* Version: 1.3.0-alpha.20140921
8-
* Build date: 21 September 2014
7+
* Version: 1.3.0-peitschie.perf-master
8+
* Build date: 2 October 2014
99
*/
1010

1111
(function(factory, root) {
@@ -98,7 +98,7 @@
9898
};
9999

100100
var api = {
101-
version: "1.3.0-alpha.20140921",
101+
version: "1.3.0-peitschie.perf-master",
102102
initialized: false,
103103
isBrowser: isBrowser,
104104
supported: true,

lib/rangy-highlighter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
*
77
* Copyright 2014, Tim Down
88
* Licensed under the MIT license.
9-
* Version: 1.3.0-alpha.20140921
10-
* Build date: 21 September 2014
9+
* Version: 1.3.0-peitschie.perf-master
10+
* Build date: 2 October 2014
1111
*/
1212
(function(factory, root) {
1313
if (typeof define == "function" && define.amd) {

lib/rangy-selectionsaverestore.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
*
1010
* Copyright 2014, Tim Down
1111
* Licensed under the MIT license.
12-
* Version: 1.3.0-alpha.20140921
13-
* Build date: 21 September 2014
12+
* Version: 1.3.0-peitschie.perf-master
13+
* Build date: 2 October 2014
1414
*/
1515
(function(factory, root) {
1616
if (typeof define == "function" && define.amd) {

lib/rangy-serializer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
*
1111
* Copyright 2014, Tim Down
1212
* Licensed under the MIT license.
13-
* Version: 1.3.0-alpha.20140921
14-
* Build date: 21 September 2014
13+
* Version: 1.3.0-peitschie.perf-master
14+
* Build date: 2 October 2014
1515
*/
1616
(function(factory, root) {
1717
if (typeof define == "function" && define.amd) {

lib/rangy-textrange.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
*
2727
* Copyright 2014, Tim Down
2828
* Licensed under the MIT license.
29-
* Version: 1.3.0-alpha.20140921
30-
* Build date: 21 September 2014
29+
* Version: 1.3.0-peitschie.perf-master
30+
* Build date: 2 October 2014
3131
*/
3232

3333
/**

0 commit comments

Comments
 (0)