Skip to content

Commit

Permalink
Rebuild library
Browse files Browse the repository at this point in the history
  • Loading branch information
peitschie committed Oct 2, 2014
1 parent de45b88 commit 7aee6c9
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 55 deletions.
125 changes: 81 additions & 44 deletions lib/rangy-classapplier.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
*
* Copyright 2014, Tim Down
* Licensed under the MIT license.
* Version: 1.3.0-alpha.20140921
* Build date: 21 September 2014
* Version: 1.3.0-peitschie.perf-master
* Build date: 2 October 2014
*/
(function(factory, root) {
if (typeof define == "function" && define.amd) {
Expand Down Expand Up @@ -48,31 +48,46 @@
return str.replace(/^\s\s*/, "").replace(/\s\s*$/, "");
}

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

function addClass(el, className) {
if (el.className) {
if (!hasClass(el, className)) {
el.className += " " + className;
}
} else {
el.className = className;
}
}
addClass = function(el, className) {
return el.classList.add(className);
};

var removeClass = (function() {
function replacer(matched, whiteSpaceBefore, whiteSpaceAfter) {
return (whiteSpaceBefore && whiteSpaceAfter) ? " " : "";
}
removeClass = function(el, className) {
return el.classList.remove(className);
};
} else {
hasClass = function(el, className) {
return el.className && new RegExp("(?:^|\\s)" + className + "(?:\\s|$)").test(el.className);
};

return function(el, className) {
addClass = function(el, className) {
if (el.className) {
el.className = el.className.replace(new RegExp("(^|\\s)" + className + "(\\s|$)"), replacer);
if (!hasClass(el, className)) {
el.className += " " + className;
}
} else {
el.className = className;
}
};
})();

removeClass = (function() {
function replacer(matched, whiteSpaceBefore, whiteSpaceAfter) {
return (whiteSpaceBefore && whiteSpaceAfter) ? " " : "";
}

return function(el, className) {
if (el.className) {
el.className = el.className.replace(new RegExp("(^|\\s)" + className + "(\\s|$)"), replacer);
}
};
})();
}

function sortClassName(className) {
return className && className.split(/\s+/).sort().join(" ");
Expand Down Expand Up @@ -217,18 +232,6 @@
return false;
}

function elementHasProperties(el, props) {
return each(props, function(p, propValue) {
if (typeof propValue == "object") {
if (!elementHasProperties(el[p], propValue)) {
return false;
}
} else if (el[p] !== propValue) {
return false;
}
});
}

var getComputedStyleProperty = dom.getComputedStyleProperty;
var isEditableElement = (function() {
var testEl = document.createElement("div");
Expand Down Expand Up @@ -517,7 +520,7 @@
applier.elementAttributes = elementAttributes;

applier.elementSortedClassName = applier.elementProperties.hasOwnProperty("className") ?
applier.elementProperties.className : className;
sortClassName(applier.elementProperties.className + " " + className) : className;

// Initialize tag names
applier.applyToAnyTagName = false;
Expand Down Expand Up @@ -567,7 +570,7 @@
addClass(el, this.className);
el[p] = sortClassName(el[p]);
if (createCopy) {
elProps[p] = el[p];
elProps[p] = propValue;
}
}

Expand All @@ -578,9 +581,11 @@
elProps[p] = elPropsStyle = {};
}
for (s in props[p]) {
elStyle[s] = propValue[s];
if (createCopy) {
elPropsStyle[s] = elStyle[s];
if (props[p].hasOwnProperty(s)) {
elStyle[s] = propValue[s];
if (createCopy) {
elPropsStyle[s] = elStyle[s];
}
}
}
this.attrExceptions.push(p);
Expand Down Expand Up @@ -704,13 +709,28 @@
return el;
},

elementHasProperties: function(el, props) {
var applier = this;
return each(props, function(p, propValue) {
if (p == "className") {
return hasClass(el, propValue);
} else if (typeof propValue == "object") {
if (!applier.elementHasProperties(el[p], propValue)) {
return false;
}
} else if (el[p] !== propValue) {
return false;
}
});
},

applyToTextNode: function(textNode, positionsToPreserve) {
var parent = textNode.parentNode;
if (parent.childNodes.length == 1 &&
this.useExistingElements &&
isHtmlNamespace(parent) &&
contains(this.tagNames, parent.tagName.toLowerCase()) &&
elementHasProperties(parent, this.elementProperties)) {
this.elementHasProperties(parent, this.elementProperties)) {

addClass(parent, this.className);
} else {
Expand All @@ -724,7 +744,7 @@
return isHtmlNamespace(el) &&
el.tagName.toLowerCase() == this.elementTagName &&
getSortedClassName(el) == this.elementSortedClassName &&
elementHasProperties(el, this.elementProperties) &&
this.elementHasProperties(el, this.elementProperties) &&
!elementHasNonClassAttributes(el, this.attrExceptions) &&
this.isModifiable(el);
},
Expand Down Expand Up @@ -767,6 +787,22 @@
ancestorWithClass = splitNodeAt(ancestorWithClass, range.startContainer, range.startOffset, positionsToPreserve);
}
}

if (this.isRemovable(ancestorWithClass)) {
replaceWithOwnChildrenPreservingPositions(ancestorWithClass, positionsToPreserve);
} else {
removeClass(ancestorWithClass, this.className);
}
},

splitAncestorWithClass: function(container, offset, positionsToPreserve) {
var ancestorWithClass = this.getSelfOrAncestorWithClass(container);
if (ancestorWithClass) {
splitNodeAt(ancestorWithClass, container, offset, positionsToPreserve);
}
},

undoToAncestor: function(ancestorWithClass, positionsToPreserve) {
if (this.isRemovable(ancestorWithClass)) {
replaceWithOwnChildrenPreservingPositions(ancestorWithClass, positionsToPreserve);
} else {
Expand Down Expand Up @@ -841,16 +877,17 @@
var lastTextNode = textNodes[textNodes.length - 1];

if (textNodes.length) {
this.splitAncestorWithClass(range.endContainer, range.endOffset, positionsToPreserve);
this.splitAncestorWithClass(range.startContainer, range.startOffset, positionsToPreserve);
for (var i = 0, len = textNodes.length; i < len; ++i) {
textNode = textNodes[i];
ancestorWithClass = this.getSelfOrAncestorWithClass(textNode);
if (ancestorWithClass && this.isModifiable(textNode)) {
this.undoToTextNode(textNode, range, ancestorWithClass, positionsToPreserve);
this.undoToAncestor(ancestorWithClass, positionsToPreserve);
}

// Ensure the range is still valid
range.setStartAndEnd(textNodes[0], 0, lastTextNode, lastTextNode.length);
}
// Ensure the range is still valid
range.setStartAndEnd(textNodes[0], 0, lastTextNode, lastTextNode.length);


if (this.normalize) {
Expand Down
6 changes: 3 additions & 3 deletions lib/rangy-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*
* Copyright 2014, Tim Down
* Licensed under the MIT license.
* Version: 1.3.0-alpha.20140921
* Build date: 21 September 2014
* Version: 1.3.0-peitschie.perf-master
* Build date: 2 October 2014
*/

(function(factory, root) {
Expand Down Expand Up @@ -98,7 +98,7 @@
};

var api = {
version: "1.3.0-alpha.20140921",
version: "1.3.0-peitschie.perf-master",
initialized: false,
isBrowser: isBrowser,
supported: true,
Expand Down
4 changes: 2 additions & 2 deletions lib/rangy-highlighter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*
* Copyright 2014, Tim Down
* Licensed under the MIT license.
* Version: 1.3.0-alpha.20140921
* Build date: 21 September 2014
* Version: 1.3.0-peitschie.perf-master
* Build date: 2 October 2014
*/
(function(factory, root) {
if (typeof define == "function" && define.amd) {
Expand Down
4 changes: 2 additions & 2 deletions lib/rangy-selectionsaverestore.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
*
* Copyright 2014, Tim Down
* Licensed under the MIT license.
* Version: 1.3.0-alpha.20140921
* Build date: 21 September 2014
* Version: 1.3.0-peitschie.perf-master
* Build date: 2 October 2014
*/
(function(factory, root) {
if (typeof define == "function" && define.amd) {
Expand Down
4 changes: 2 additions & 2 deletions lib/rangy-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
*
* Copyright 2014, Tim Down
* Licensed under the MIT license.
* Version: 1.3.0-alpha.20140921
* Build date: 21 September 2014
* Version: 1.3.0-peitschie.perf-master
* Build date: 2 October 2014
*/
(function(factory, root) {
if (typeof define == "function" && define.amd) {
Expand Down
4 changes: 2 additions & 2 deletions lib/rangy-textrange.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
*
* Copyright 2014, Tim Down
* Licensed under the MIT license.
* Version: 1.3.0-alpha.20140921
* Build date: 21 September 2014
* Version: 1.3.0-peitschie.perf-master
* Build date: 2 October 2014
*/

/**
Expand Down

0 comments on commit 7aee6c9

Please sign in to comment.