vendor/assets/javascripts/rangy-serializer.js in rangy-rails-1.3alpha.780.0 vs vendor/assets/javascripts/rangy-serializer.js in rangy-rails-1.3alpha.804.0
- old
+ new
@@ -8,9 +8,288 @@
*
* Depends on Rangy core.
*
* Copyright 2013, Tim Down
* Licensed under the MIT license.
- * Version: 1.3alpha.780M
- * Build date: 17 May 2013
+ * Version: 1.3alpha.804
+ * Build date: 8 December 2013
*/
-rangy.createModule("Serializer",["WrappedSelection"],function(a,b){function f(a){return a.replace(/</g,"<").replace(/>/g,">")}function g(a,b){b=b||[];var c=a.nodeType,d=a.childNodes,e=d.length,h=[c,a.nodeName,e].join(":"),i="",j="";switch(c){case 3:i=f(a.nodeValue);break;case 8:i="<!--"+f(a.nodeValue)+"-->";break;default:i="<"+h+">",j="</>"}i&&b.push(i);for(var k=0;k<e;++k)g(d[k],b);return j&&b.push(j),b}function h(a){var b=g(a).join("");return d(b).toString(16)}function i(a,b,c){var d=[],f=a;c=c||e.getDocument(a).documentElement;while(f&&f!=c)d.push(e.getNodeIndex(f,!0)),f=f.parentNode;return d.join("/")+":"+b}function j(a,c,d){c||(c=(d||document).documentElement);var f=a.split(":"),g=c,h=f[0]?f[0].split("/"):[],i=h.length,j;while(i--){j=parseInt(h[i],10);if(!(j<g.childNodes.length))throw b.createError("deserializePosition() failed: node "+e.inspectNode(g)+" has no child with index "+j+", "+i);g=g.childNodes[j]}return new e.DomPosition(g,parseInt(f[1],10))}function k(c,d,f){f=f||a.DomRange.getRangeDocument(c).documentElement;if(!e.isOrIsAncestorOf(f,c.commonAncestorContainer))throw b.createError("serializeRange(): range "+c.inspect()+" is not wholly contained within specified root node "+e.inspectNode(f));var g=i(c.startContainer,c.startOffset,f)+","+i(c.endContainer,c.endOffset,f);return d||(g+="{"+h(f)+"}"),g}function l(c,d,f){d?f=f||e.getDocument(d):(f=f||document,d=f.documentElement);var g=/^([^,]+),([^,\{]+)(\{([^}]+)\})?$/.exec(c),i=g[4],k=h(d);if(i&&i!==h(d))throw b.createError("deserializeRange(): checksums of serialized range root node ("+i+") and target root node ("+k+") do not match");var l=j(g[1],d,f),m=j(g[2],d,f),n=a.createRange(f);return n.setStartAndEnd(l.node,l.offset,m.node,m.offset),n}function m(a,b,c){b||(b=(c||document).documentElement);var d=/^([^,]+),([^,]+)(\{([^}]+)\})?$/.exec(a),e=d[3];return!e||e===h(b)}function n(b,c,d){b=a.getSelection(b);var e=b.getAllRanges(),f=[];for(var g=0,h=e.length;g<h;++g)f[g]=k(e[g],c,d);return f.join("|")}function o(b,c,d){c?d=d||e.getWindow(c):(d=d||window,c=d.document.documentElement);var f=b.split("|"),g=a.getSelection(d),h=[];for(var i=0,j=f.length;i<j;++i)h[i]=l(f[i],c,d.document);return g.setRanges(h),g}function p(a,b,c){var d;b?d=c?c.document:e.getDocument(b):(c=c||window,b=c.document.documentElement);var f=a.split("|");for(var g=0,h=f.length;g<h;++g)if(!m(f[g],b,d))return!1;return!0}function r(a){var b=a.split(/[;,]/);for(var c=0,d=b.length,e,f;c<d;++c){e=b[c].split("=");if(e[0].replace(/^\s+/,"")==q){f=e[1];if(f)return decodeURIComponent(f.replace(/\s+$/,""))}}return null}function s(a){a=a||window;var b=r(a.document.cookie);b&&o(b,a.doc)}function t(b,c){b=b||window,c=typeof c=="object"?c:{};var d=c.expires?";expires="+c.expires.toUTCString():"",e=c.path?";path="+c.path:"",f=c.domain?";domain="+c.domain:"",g=c.secure?";secure":"",h=n(a.getSelection(b));b.document.cookie=encodeURIComponent(q)+"="+encodeURIComponent(h)+d+e+f+g}var c="undefined";(typeof encodeURIComponent==c||typeof decodeURIComponent==c)&&b.fail("Global object is missing encodeURIComponent and/or decodeURIComponent method");var d=function(){function a(a){var b=[];for(var c=0,d=a.length,e;c<d;++c)e=a.charCodeAt(c),e<128?b.push(e):e<2048?b.push(e>>6|192,e&63|128):b.push(e>>12|224,e>>6&63|128,e&63|128);return b}function c(){var a=[];for(var b=0,c,d;b<256;++b){d=b,c=8;while(c--)(d&1)==1?d=d>>>1^3988292384:d>>>=1;a[b]=d>>>0}return a}function d(){return b||(b=c()),b}var b=null;return function(b){var c=a(b),e=-1,f=d();for(var g=0,h=c.length,i;g<h;++g)i=(e^c[g])&255,e=e>>>8^f[i];return(e^-1)>>>0}}(),e=a.dom,q="rangySerializedSelection";a.serializePosition=i,a.deserializePosition=j,a.serializeRange=k,a.deserializeRange=l,a.canDeserializeRange=m,a.serializeSelection=n,a.deserializeSelection=o,a.canDeserializeSelection=p,a.restoreSelectionFromCookie=s,a.saveSelectionCookie=t,a.getElementChecksum=h,a.nodeToInfoString=g})
\ No newline at end of file
+rangy.createModule("Serializer", ["WrappedSelection"], function(api, module) {
+ var UNDEF = "undefined";
+
+ // encodeURIComponent and decodeURIComponent are required for cookie handling
+ if (typeof encodeURIComponent == UNDEF || typeof decodeURIComponent == UNDEF) {
+ module.fail("Global object is missing encodeURIComponent and/or decodeURIComponent method");
+ }
+
+ // Checksum for checking whether range can be serialized
+ var crc32 = (function() {
+ function utf8encode(str) {
+ var utf8CharCodes = [];
+
+ for (var i = 0, len = str.length, c; i < len; ++i) {
+ c = str.charCodeAt(i);
+ if (c < 128) {
+ utf8CharCodes.push(c);
+ } else if (c < 2048) {
+ utf8CharCodes.push((c >> 6) | 192, (c & 63) | 128);
+ } else {
+ utf8CharCodes.push((c >> 12) | 224, ((c >> 6) & 63) | 128, (c & 63) | 128);
+ }
+ }
+ return utf8CharCodes;
+ }
+
+ var cachedCrcTable = null;
+
+ function buildCRCTable() {
+ var table = [];
+ for (var i = 0, j, crc; i < 256; ++i) {
+ crc = i;
+ j = 8;
+ while (j--) {
+ if ((crc & 1) == 1) {
+ crc = (crc >>> 1) ^ 0xEDB88320;
+ } else {
+ crc >>>= 1;
+ }
+ }
+ table[i] = crc >>> 0;
+ }
+ return table;
+ }
+
+ function getCrcTable() {
+ if (!cachedCrcTable) {
+ cachedCrcTable = buildCRCTable();
+ }
+ return cachedCrcTable;
+ }
+
+ return function(str) {
+ var utf8CharCodes = utf8encode(str), crc = -1, crcTable = getCrcTable();
+ for (var i = 0, len = utf8CharCodes.length, y; i < len; ++i) {
+ y = (crc ^ utf8CharCodes[i]) & 0xFF;
+ crc = (crc >>> 8) ^ crcTable[y];
+ }
+ return (crc ^ -1) >>> 0;
+ };
+ })();
+
+ var dom = api.dom;
+
+ function escapeTextForHtml(str) {
+ return str.replace(/</g, "<").replace(/>/g, ">");
+ }
+
+ function nodeToInfoString(node, infoParts) {
+ infoParts = infoParts || [];
+ var nodeType = node.nodeType, children = node.childNodes, childCount = children.length;
+ var nodeInfo = [nodeType, node.nodeName, childCount].join(":");
+ var start = "", end = "";
+ switch (nodeType) {
+ case 3: // Text node
+ start = escapeTextForHtml(node.nodeValue);
+ break;
+ case 8: // Comment
+ start = "<!--" + escapeTextForHtml(node.nodeValue) + "-->";
+ break;
+ default:
+ start = "<" + nodeInfo + ">";
+ end = "</>";
+ break;
+ }
+ if (start) {
+ infoParts.push(start);
+ }
+ for (var i = 0; i < childCount; ++i) {
+ nodeToInfoString(children[i], infoParts);
+ }
+ if (end) {
+ infoParts.push(end);
+ }
+ return infoParts;
+ }
+
+ // Creates a string representation of the specified element's contents that is similar to innerHTML but omits all
+ // attributes and comments and includes child node counts. This is done instead of using innerHTML to work around
+ // IE <= 8's policy of including element properties in attributes, which ruins things by changing an element's
+ // innerHTML whenever the user changes an input within the element.
+ function getElementChecksum(el) {
+ var info = nodeToInfoString(el).join("");
+ return crc32(info).toString(16);
+ }
+
+ function serializePosition(node, offset, rootNode) {
+ var pathParts = [], n = node;
+ rootNode = rootNode || dom.getDocument(node).documentElement;
+ while (n && n != rootNode) {
+ pathParts.push(dom.getNodeIndex(n, true));
+ n = n.parentNode;
+ }
+ return pathParts.join("/") + ":" + offset;
+ }
+
+ function deserializePosition(serialized, rootNode, doc) {
+ if (!rootNode) {
+ rootNode = (doc || document).documentElement;
+ }
+ var parts = serialized.split(":");
+ var node = rootNode;
+ var nodeIndices = parts[0] ? parts[0].split("/") : [], i = nodeIndices.length, nodeIndex;
+
+ while (i--) {
+ nodeIndex = parseInt(nodeIndices[i], 10);
+ if (nodeIndex < node.childNodes.length) {
+ node = node.childNodes[nodeIndex];
+ } else {
+ throw module.createError("deserializePosition() failed: node " + dom.inspectNode(node) +
+ " has no child with index " + nodeIndex + ", " + i);
+ }
+ }
+
+ return new dom.DomPosition(node, parseInt(parts[1], 10));
+ }
+
+ function serializeRange(range, omitChecksum, rootNode) {
+ rootNode = rootNode || api.DomRange.getRangeDocument(range).documentElement;
+ if (!dom.isOrIsAncestorOf(rootNode, range.commonAncestorContainer)) {
+ throw module.createError("serializeRange(): range " + range.inspect() +
+ " is not wholly contained within specified root node " + dom.inspectNode(rootNode));
+ }
+ var serialized = serializePosition(range.startContainer, range.startOffset, rootNode) + "," +
+ serializePosition(range.endContainer, range.endOffset, rootNode);
+ if (!omitChecksum) {
+ serialized += "{" + getElementChecksum(rootNode) + "}";
+ }
+ return serialized;
+ }
+
+ var deserializeRegex = /^([^,]+),([^,\{]+)(\{([^}]+)\})?$/;
+
+ function deserializeRange(serialized, rootNode, doc) {
+ if (rootNode) {
+ doc = doc || dom.getDocument(rootNode);
+ } else {
+ doc = doc || document;
+ rootNode = doc.documentElement;
+ }
+ var result = deserializeRegex.exec(serialized);
+ var checksum = result[4], rootNodeChecksum = getElementChecksum(rootNode);
+ if (checksum && checksum !== getElementChecksum(rootNode)) {
+ throw module.createError("deserializeRange(): checksums of serialized range root node (" + checksum +
+ ") and target root node (" + rootNodeChecksum + ") do not match");
+ }
+ var start = deserializePosition(result[1], rootNode, doc), end = deserializePosition(result[2], rootNode, doc);
+ var range = api.createRange(doc);
+ range.setStartAndEnd(start.node, start.offset, end.node, end.offset);
+ return range;
+ }
+
+ function canDeserializeRange(serialized, rootNode, doc) {
+ if (!rootNode) {
+ rootNode = (doc || document).documentElement;
+ }
+ var result = deserializeRegex.exec(serialized);
+ var checksum = result[3];
+ return !checksum || checksum === getElementChecksum(rootNode);
+ }
+
+ function serializeSelection(selection, omitChecksum, rootNode) {
+ selection = api.getSelection(selection);
+ var ranges = selection.getAllRanges(), serializedRanges = [];
+ for (var i = 0, len = ranges.length; i < len; ++i) {
+ serializedRanges[i] = serializeRange(ranges[i], omitChecksum, rootNode);
+ }
+ return serializedRanges.join("|");
+ }
+
+ function deserializeSelection(serialized, rootNode, win) {
+ if (rootNode) {
+ win = win || dom.getWindow(rootNode);
+ } else {
+ win = win || window;
+ rootNode = win.document.documentElement;
+ }
+ var serializedRanges = serialized.split("|");
+ var sel = api.getSelection(win);
+ var ranges = [];
+
+ for (var i = 0, len = serializedRanges.length; i < len; ++i) {
+ ranges[i] = deserializeRange(serializedRanges[i], rootNode, win.document);
+ }
+ sel.setRanges(ranges);
+
+ return sel;
+ }
+
+ function canDeserializeSelection(serialized, rootNode, win) {
+ var doc;
+ if (rootNode) {
+ doc = win ? win.document : dom.getDocument(rootNode);
+ } else {
+ win = win || window;
+ rootNode = win.document.documentElement;
+ }
+ var serializedRanges = serialized.split("|");
+
+ for (var i = 0, len = serializedRanges.length; i < len; ++i) {
+ if (!canDeserializeRange(serializedRanges[i], rootNode, doc)) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ var cookieName = "rangySerializedSelection";
+
+ function getSerializedSelectionFromCookie(cookie) {
+ var parts = cookie.split(/[;,]/);
+ for (var i = 0, len = parts.length, nameVal, val; i < len; ++i) {
+ nameVal = parts[i].split("=");
+ if (nameVal[0].replace(/^\s+/, "") == cookieName) {
+ val = nameVal[1];
+ if (val) {
+ return decodeURIComponent(val.replace(/\s+$/, ""));
+ }
+ }
+ }
+ return null;
+ }
+
+ function restoreSelectionFromCookie(win) {
+ win = win || window;
+ var serialized = getSerializedSelectionFromCookie(win.document.cookie);
+ if (serialized) {
+ deserializeSelection(serialized, win.doc);
+ }
+ }
+
+ function saveSelectionCookie(win, props) {
+ win = win || window;
+ props = (typeof props == "object") ? props : {};
+ var expires = props.expires ? ";expires=" + props.expires.toUTCString() : "";
+ var path = props.path ? ";path=" + props.path : "";
+ var domain = props.domain ? ";domain=" + props.domain : "";
+ var secure = props.secure ? ";secure" : "";
+ var serialized = serializeSelection(api.getSelection(win));
+ win.document.cookie = encodeURIComponent(cookieName) + "=" + encodeURIComponent(serialized) + expires + path + domain + secure;
+ }
+
+ api.serializePosition = serializePosition;
+ api.deserializePosition = deserializePosition;
+
+ api.serializeRange = serializeRange;
+ api.deserializeRange = deserializeRange;
+ api.canDeserializeRange = canDeserializeRange;
+
+ api.serializeSelection = serializeSelection;
+ api.deserializeSelection = deserializeSelection;
+ api.canDeserializeSelection = canDeserializeSelection;
+
+ api.restoreSelectionFromCookie = restoreSelectionFromCookie;
+ api.saveSelectionCookie = saveSelectionCookie;
+
+ api.getElementChecksum = getElementChecksum;
+ api.nodeToInfoString = nodeToInfoString;
+});