Skip to content

Commit

Permalink
Update lib files
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim committed May 10, 2015
1 parent 9c1c06d commit a7b5eec
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 93 deletions.
16 changes: 10 additions & 6 deletions lib/rangy-classapplier.js
Expand Up @@ -9,8 +9,8 @@
*
* Copyright 2015, Tim Down
* Licensed under the MIT license.
* Version: 1.3.0-beta.2
* Build date: 22 March 2015
* Version: 1.3.0
* Build date: 10 May 2015
*/
(function(factory, root) {
if (typeof define == "function" && define.amd) {
Expand All @@ -28,11 +28,12 @@
var dom = api.dom;
var DomPosition = dom.DomPosition;
var contains = dom.arrayContains;
var forEach = api.util.forEach;
var util = api.util;
var forEach = util.forEach;


var defaultTagName = "span";
var createElementNSSupported = api.util.isHostMethod(document, "createElementNS");
var createElementNSSupported = util.isHostMethod(document, "createElementNS");

function each(obj, func) {
for (var i in obj) {
Expand Down Expand Up @@ -553,8 +554,10 @@
applier.attrExceptions = [];
var el = document.createElement(applier.elementTagName);
applier.elementProperties = applier.copyPropertiesToElement(elementPropertiesFromOptions, el, true);
each(elementAttributes, function(attrName) {
each(elementAttributes, function(attrName, attrValue) {
applier.attrExceptions.push(attrName);
// Ensure each attribute value is a string
elementAttributes[attrName] = "" + attrValue;
});
applier.elementAttributes = elementAttributes;

Expand Down Expand Up @@ -1092,7 +1095,8 @@
};

api.CssClassApplier = api.ClassApplier = ClassApplier;
api.createCssClassApplier = api.createClassApplier = createClassApplier;
api.createClassApplier = createClassApplier;
util.createAliasForDeprecatedMethod(api, "createCssClassApplier", "createClassApplier", module);
});

return rangy;
Expand Down
97 changes: 64 additions & 33 deletions lib/rangy-core.js
Expand Up @@ -4,8 +4,8 @@
*
* Copyright 2015, Tim Down
* Licensed under the MIT license.
* Version: 1.3.0-beta.2
* Build date: 22 March 2015
* Version: 1.3.0
* Build date: 10 May 2015
*/

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

var api = {
version: "1.3.0-beta.2",
version: "1.3.0",
initialized: false,
isBrowser: isBrowser,
supported: true,
Expand Down Expand Up @@ -306,6 +306,24 @@
}
}

function deprecationNotice(deprecated, replacement, module) {
if (module) {
deprecated += " in module " + module.name;
}
api.warn("DEPRECATED: " + deprecated + " is deprecated. Please use " +
replacement + " instead.");
}

function createAliasForDeprecatedMethod(owner, deprecated, replacement, module) {
owner[deprecated] = function() {
deprecationNotice(deprecated, replacement, module);
return owner[replacement].apply(owner, util.toArray(arguments));
};
}

util.deprecationNotice = deprecationNotice;
util.createAliasForDeprecatedMethod = createAliasForDeprecatedMethod;

// Allow external scripts to initialize this library in case it's loaded after the document has loaded
api.init = init;

Expand Down Expand Up @@ -336,6 +354,7 @@

if (isBrowser) {
api.shim = api.createMissingNativeApi = shim;
createAliasForDeprecatedMethod(api, "createMissingNativeApi", "shim");
}

function Module(name, dependencies, initializer) {
Expand Down Expand Up @@ -379,7 +398,7 @@
},

deprecationNotice: function(deprecated, replacement) {
api.warn("DEPRECATED: " + deprecated + " in module " + this.name + "is deprecated. Please use " +
api.warn("DEPRECATED: " + deprecated + " in module " + this.name + " is deprecated. Please use " +
replacement + " instead");
},

Expand Down Expand Up @@ -800,7 +819,7 @@
};
} else if (typeof document.documentElement.currentStyle != UNDEF) {
getComputedStyleProperty = function(el, propName) {
return el.currentStyle[propName];
return el.currentStyle ? el.currentStyle[propName] : "";
};
} else {
module.fail("No means of obtaining computed style properties found");
Expand Down Expand Up @@ -980,6 +999,10 @@
return range.document || getDocument(range.startContainer);
}

function getRangeRoot(range) {
return getRootContainer(range.startContainer);
}

function getBoundaryBeforeNode(node) {
return new DomPosition(node.parentNode, getNodeIndex(node));
}
Expand Down Expand Up @@ -1317,26 +1340,21 @@
}
}

function isOrphan(node) {
return (crashyTextNodes && dom.isBrokenNode(node)) ||
!arrayContains(rootContainerNodeTypes, node.nodeType) && !getDocumentOrFragmentContainer(node, true);
}

function isValidOffset(node, offset) {
return offset <= (isCharacterDataNode(node) ? node.length : node.childNodes.length);
}

function isRangeValid(range) {
return (!!range.startContainer && !!range.endContainer &&
!isOrphan(range.startContainer) &&
!isOrphan(range.endContainer) &&
!(crashyTextNodes && (dom.isBrokenNode(range.startContainer) || dom.isBrokenNode(range.endContainer))) &&
getRootContainer(range.startContainer) == getRootContainer(range.endContainer) &&
isValidOffset(range.startContainer, range.startOffset) &&
isValidOffset(range.endContainer, range.endOffset));
}

function assertRangeValid(range) {
if (!isRangeValid(range)) {
throw new Error("Range error: Range is no longer valid after DOM mutation (" + range.inspect() + ")");
throw new Error("Range error: Range is not valid. This usually happens after DOM mutation. Range: (" + range.inspect() + ")");
}
}

Expand Down Expand Up @@ -1630,13 +1648,14 @@
// with it (as in WebKit) or not (as in Gecko pre-1.9, and the default)
intersectsNode: function(node, touchingIsIntersecting) {
assertRangeValid(this);
assertNode(node, "NOT_FOUND_ERR");
if (getDocument(node) !== getRangeDocument(this)) {
if (getRootContainer(node) != getRangeRoot(this)) {
return false;
}

var parent = node.parentNode, offset = getNodeIndex(node);
assertNode(parent, "NOT_FOUND_ERR");
if (!parent) {
return true;
}

var startComparison = comparePoints(parent, offset, this.endContainer, this.endOffset),
endComparison = comparePoints(parent, offset + 1, this.startContainer, this.startOffset);
Expand Down Expand Up @@ -2047,10 +2066,22 @@
};

var normalizeStart = true;
var sibling;

if (isCharacterDataNode(ec)) {
if (ec.length == eo) {
if (eo == ec.length) {
mergeForward(ec);
} else if (eo == 0) {
sibling = ec.previousSibling;
if (sibling && sibling.nodeType == ec.nodeType) {
eo = sibling.length;
if (sc == ec) {
normalizeStart = false;
}
sibling.appendData(ec.data);
removeNode(ec);
ec = sibling;
}
}
} else {
if (eo > 0) {
Expand All @@ -2066,6 +2097,16 @@
if (isCharacterDataNode(sc)) {
if (so == 0) {
mergeBackward(sc);
} else if (so == sc.length) {
sibling = sc.nextSibling;
if (sibling && sibling.nodeType == sc.nodeType) {
if (ec == sibling) {
ec = sc;
eo += sc.length;
}
sc.appendData(sibling.data);
removeNode(sibling);
}
}
} else {
if (so < sc.childNodes.length) {
Expand Down Expand Up @@ -2735,15 +2776,8 @@
return new DomRange(doc);
};

api.createIframeRange = function(iframeEl) {
module.deprecationNotice("createIframeRange()", "createRange(iframeEl)");
return api.createRange(iframeEl);
};

api.createIframeRangyRange = function(iframeEl) {
module.deprecationNotice("createIframeRangyRange()", "createRangyRange(iframeEl)");
return api.createRangyRange(iframeEl);
};
util.createAliasForDeprecatedMethod(api, "createIframeRange", "createRange");
util.createAliasForDeprecatedMethod(api, "createIframeRangyRange", "createRangyRange");

api.addShimListener(function(win) {
var doc = win.document;
Expand Down Expand Up @@ -2781,8 +2815,8 @@
var rangesEqual = DomRange.rangesEqual;


// Utility function to support direction parameters in the API that may be a string ("backward" or "forward") or a
// Boolean (true for backwards).
// Utility function to support direction parameters in the API that may be a string ("backward", "backwards",
// "forward" or "forwards") or a Boolean (true for backwards).
function isDirectionBackward(dir) {
return (typeof dir == "string") ? /^backward(s)?$/i.test(dir) : !!dir;
}
Expand Down Expand Up @@ -3198,10 +3232,7 @@

api.getSelection = getSelection;

api.getIframeSelection = function(iframeEl) {
module.deprecationNotice("getIframeSelection()", "getSelection(iframeEl)");
return api.getSelection(dom.getIframeWindow(iframeEl));
};
util.createAliasForDeprecatedMethod(api, "getIframeSelection", "getSelection");

var selProto = WrappedSelection.prototype;

Expand Down Expand Up @@ -3621,7 +3652,7 @@
selProto.callMethodOnEachRange = function(methodName, params) {
var results = [];
this.eachRange( function(range) {
results.push( range[methodName].apply(range, params) );
results.push( range[methodName].apply(range, params || []) );
} );
return results;
};
Expand Down
10 changes: 5 additions & 5 deletions lib/rangy-highlighter.js
Expand Up @@ -6,8 +6,8 @@
*
* Copyright 2015, Tim Down
* Licensed under the MIT license.
* Version: 1.3.0-beta.2
* Build date: 22 March 2015
* Version: 1.3.0
* Build date: 10 May 2015
*/
(function(factory, root) {
if (typeof define == "function" && define.amd) {
Expand Down Expand Up @@ -460,7 +460,7 @@

options = createOptions(options, {
containerElementId: null,
selection: api.getSelection(),
selection: api.getSelection(this.doc),
exclusive: true
});

Expand Down Expand Up @@ -495,15 +495,15 @@
},

unhighlightSelection: function(selection) {
selection = selection || api.getSelection();
selection = selection || api.getSelection(this.doc);
var intersectingHighlights = this.getIntersectingHighlights( selection.getAllRanges() );
this.removeHighlights(intersectingHighlights);
selection.removeAllRanges();
return intersectingHighlights;
},

getHighlightsInSelection: function(selection) {
selection = selection || api.getSelection();
selection = selection || api.getSelection(this.doc);
return this.getIntersectingHighlights(selection.getAllRanges());
},

Expand Down
14 changes: 8 additions & 6 deletions lib/rangy-selectionsaverestore.js
Expand Up @@ -9,8 +9,8 @@
*
* Copyright 2015, Tim Down
* Licensed under the MIT license.
* Version: 1.3.0-beta.2
* Build date: 22 March 2015
* Version: 1.3.0
* Build date: 10 May 2015
*/
(function(factory, root) {
if (typeof define == "function" && define.amd) {
Expand All @@ -27,7 +27,7 @@
rangy.createModule("SaveRestore", ["WrappedRange"], function(api, module) {
var dom = api.dom;
var removeNode = dom.removeNode;

var isDirectionBackward = api.Selection.isDirectionBackward;
var markerTextChar = "\ufeff";

function gEBI(id, doc) {
Expand Down Expand Up @@ -69,8 +69,9 @@
return r2.compareBoundaryPoints(r1.START_TO_START, r1);
}

function saveRange(range, backward) {
function saveRange(range, direction) {
var startEl, endEl, doc = api.DomRange.getRangeDocument(range), text = range.toString();
var backward = isDirectionBackward(direction);

if (range.collapsed) {
endEl = insertRangeBoundaryMarker(range, false);
Expand Down Expand Up @@ -131,8 +132,9 @@
return range;
}

function saveRanges(ranges, backward) {
function saveRanges(ranges, direction) {
var rangeInfos = [], range, doc;
var backward = isDirectionBackward(direction);

// Order the ranges by position within the DOM, latest first, cloning the array to leave the original untouched
ranges = ranges.slice(0);
Expand Down Expand Up @@ -171,7 +173,7 @@

// Ensure current selection is unaffected
if (backward) {
sel.setSingleRange(ranges[0], "backward");
sel.setSingleRange(ranges[0], backward);
} else {
sel.setRanges(ranges);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/rangy-serializer.js
Expand Up @@ -10,8 +10,8 @@
*
* Copyright 2015, Tim Down
* Licensed under the MIT license.
* Version: 1.3.0-beta.2
* Build date: 22 March 2015
* Version: 1.3.0
* Build date: 10 May 2015
*/
(function(factory, root) {
if (typeof define == "function" && define.amd) {
Expand Down

0 comments on commit a7b5eec

Please sign in to comment.