Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

moved validation function to masha.js, made it available by default

  • Loading branch information...
commit 6e07ececa74768840209b73f8b64ad0f80d74b09 1 parent d0cf942
@Harut Harut authored
View
17 README.md
@@ -80,7 +80,7 @@ where
* Filter function, that allows to ignore specified HTMLElement as selection target. Should return true if element must be ignored; otherwise false.
* Comma-separated tag names, classes or ids of ignored elements. For example: *'ul, .ignored-cls, #ignored-id'*.
* 'location' — an object used for get url hash from and write it to. The only significant methods are getHash, setHash and addHashchange. You can redefine, for example, to write URL not in address bar but into a custom popup, or for handle address bar URL manually.
-* 'validate' — If true, the checksum of each selection is written in hash and they are validated on page load. Attention! There is no checksum algorithm provided by default and you should provide it to use validation! See 'Validation' section below.
+* 'validate' — If true, activates MaSha range validation, useful if page changes can break selection. For each range MaSha calculates a checksum, including information about first and last words. The checksum is included into hash and it is checked on page load. If calculated checksum doesn't match one from url, the selection is not restored.
* 'enableHaschange' — If true, hashchange event is handled.
* 'onMark' — Callback function that fired on text selection.
* 'onUnmark' — Callback function that fired on text deselection.
@@ -96,22 +96,7 @@ If you use mainstream ierange library instead of bundled one please add this lin
window.DOMRange = DOMRange;
```
-## Validation
-Validation algorythm is not included in MaSha by default, because we didn't invent The Best Algorythm Ever yet. And we don't want to force users to use bad (draft) algorythms.
-
-To use validation you should implement `MaSha.prototype.getPositionChecksum` method. This method accepts word sequence iterator (a function returning the next word of sequence on each call or null if words are) and returns a string checksum. The getPositionChecksum method is called twice for each range: one for start position and one for end position (with reversed iterator).
-
-The checksum is included into hash and it is checked on page load. If calculated checksum doesn't one from url, the selection is not restored.
-
-```javascript
- MaSha.prototype.getPositionChecksum = function(wordsIterator){
- var word1 = wordsIterator();
- var word2 = wordsIterator();
- var sum = makeSomeCalculations(word1, word2);
- return sum;
- }
-```
## MultiMaSha
View
1  demo/demo.html
@@ -13,7 +13,6 @@
<script type="text/javascript" src="../src/js/ierange.js"></script>
<![ENDIF]-->
<script type="text/javascript" src="../src/js/masha.js"></script>
- <script type="text/javascript" src="validation.js"></script>
<link rel="stylesheet" type="text/css" href="../src/css/masha.css">
<style>
View
1  demo/multimasha.html
@@ -13,7 +13,6 @@
<script type="text/javascript" src="../src/js/ierange.js"></script>
<![ENDIF]-->
<script type="text/javascript" src="../src/js/masha.js"></script>
- <script type="text/javascript" src="validation.js"></script>
<link rel="stylesheet" type="text/css" href="../src/css/masha.css">
<style>
View
1  demo/social.html
@@ -13,7 +13,6 @@
<script type="text/javascript" src="../src/js/ierange.js"></script>
<![ENDIF]-->
<script type="text/javascript" src="../src/js/masha.js"></script>
- <script type="text/javascript" src="validation.js"></script>
<link rel="stylesheet" type="text/css" href="../src/css/masha.css">
<style>
View
20 demo/validation.js
@@ -1,20 +0,0 @@
-(function(){
- function to_latin(ch){
- if (ch){
- var allowed_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
- var integer = ch.charCodeAt(0) % allowed_chars.length;
- return allowed_chars.charAt(integer);
- }
- return '';
- }
-
- MaSha.prototype.getPositionChecksum = function(words_iterator){
- var sum = '';
- for (var i=0; i<3;i++){
- var part = (words_iterator() || '').charAt(0);
- sum += to_latin(part);
- }
- return sum;
- }
-})();
-
View
25 src/js/masha.js
@@ -40,7 +40,7 @@ var MaSha = function(options) {
this.init();
};
-MaSha.version = "15.05.2012-13:21:00"; // filled automatically by hook
+MaSha.version = "15.05.2012-15:38:50"; // filled automatically by hook
MaSha.defaultOptions = {
'regexp': "[^\\s,;:\u2013.!?<>\u2026\\n\u00a0\\*]+",
@@ -450,12 +450,25 @@ MaSha.prototype = {
getPositionChecksum: function(wordsIterator){
/*
- * Should be impemented by user
- * We don't want to include validation algorythm, because we
- * didn't invent The Best Algorythm Ever yet. :(
- * And we don't want to force users to use bad algorythms.
+ * Used in validation. This method accepts word sequence iterator (a function returning
+ * the next word of sequence on each call or null if words are) and returns a string checksum.
+ * The getPositionChecksum method is called twice for each range: one for start position and
+ * one for end position (with reversed iterator).
+ *
+ * The checksum is included into hash and it is checked on page load. If calculated checksum
+ * doesn't one from url, the selection is not restored.
*/
- return '';
+ var sum = '';
+ for (var i=0; i<3;i++){
+ var part = (wordsIterator() || '').charAt(0);
+ if (part){
+ var allowedChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
+ var integer = part.charCodeAt(0) % allowedChars.length;
+ part = allowedChars.charAt(integer);
+ }
+ sum += part;
+ }
+ return sum;
},
deserializePosition: function(bits, pos){
View
44 src/js/masha.min.js
@@ -2,7 +2,7 @@
"")}function C(a,b){var c="";document.defaultView&&document.defaultView.getComputedStyle?c=document.defaultView.getComputedStyle(a,"").getPropertyValue(b):a.currentStyle&&(b=b.replace(/\-(\w)/g,function(a,b){return b.toUpperCase()}),c=a.currentStyle[b]);return c}function r(a,b){for(;a&&!l(a,b);)a=a.parentNode;return a||null}function t(a,b){for(var c=m(a),d=null;d=c();)if(1===d.nodeType&&l(d,b))return d;return null}function u(a){for(var a=m(a),b=null;(b=a())&&3!==b.nodeType;);return b}function v(a,
b){if(a.getElementsByClassName)return a.getElementsByClassName(b);for(var c=[],d,e=m(a);d=e();)1==d.nodeType&&l(d,b)&&c.push(d);return c}function o(a){for(var b=[],c=m(a);a=c();)3===a.nodeType&&b.push(a);return b}function w(a){return RegExp("(^|\\s+)"+a+"(?:$|\\s+)","g")}function l(a,b){return w(b).test(a.className)}function x(a,b){w(b).test(a.className)||(a.className=a.className+" "+b)}function s(a,b){var c=w(b);c.test(a.className)&&(a.className=n(a.className.replace(c,"$1")))}function A(a,b){for(var c=
0,d=b.length;c<d;c++)if(b[c]===a)return c;return-1}function g(a,b,c){a.addEventListener?a.addEventListener(b,c,!1):a.attachEvent&&a.attachEvent("on"+b,c)}function y(a){a.preventDefault?a.preventDefault():a.returnValue=!1}function D(a){if(null==a.pageX){var b=document.documentElement,c=document.body;return{x:a.clientX+(b&&b.scrollLeft||c&&c.scrollLeft||0)-(b.clientLeft||0),y:a.clientY+(b&&b.scrollTop||c&&c.scrollTop||0)-(b.clientTop||0)}}return{x:a.pageX,y:a.pageY}}var z=function(){};z.prototype={setHash:function(a){window.location.hash=
-a},getHash:function(){return window.location.hash},addHashchange:function(a){g(window,"hashchange",a)}};var j=function(a){"select_message"in a&&(a.selectMessage=a.select_message);"enable_haschange"in a&&(a.enableHaschange=a.enable_haschange);"is_block"in a&&(a.isBlock=a.is_block);this.options=q({},j.defaultOptions,a);q(this,{counter:0,savedSel:[],ranges:{},childs:[],blocks:{}});this.init()};j.version="15.05.2012-13:21:00";j.defaultOptions={regexp:"[^\\s,;:\u2013.!?<>\u2026\\n\u00a0\\*]+",selectable:"selectable-content",
+a},getHash:function(){return window.location.hash},addHashchange:function(a){g(window,"hashchange",a)}};var j=function(a){"select_message"in a&&(a.selectMessage=a.select_message);"enable_haschange"in a&&(a.enableHaschange=a.enable_haschange);"is_block"in a&&(a.isBlock=a.is_block);this.options=q({},j.defaultOptions,a);q(this,{counter:0,savedSel:[],ranges:{},childs:[],blocks:{}});this.init()};j.version="15.05.2012-15:38:50";j.defaultOptions={regexp:"[^\\s,;:\u2013.!?<>\u2026\\n\u00a0\\*]+",selectable:"selectable-content",
marker:"txtselect_marker",ignored:null,selectMessage:null,location:new z,validate:!1,enableHaschange:!0,onMark:null,onUnmark:null,onHashRead:function(){var a=t(this.selectable,"user_selection_true");a&&!this.hashWasRead&&(this.hashWasRead=!0,window.setTimeout(function(){for(var b=0,c=0;a;)b+=a.offsetLeft,c+=a.offsetTop,a=a.offsetParent;window.scrollTo(b,c-150)},1))},isBlock:function(a){return"BR"==a.nodeName||-1==A(C(a,"display"),["inline","none"])}};j.prototype={init:function(){function a(a){var b=
RegExp(c.options.regexp,"g"),f=window.getSelection().toString();""!=f&&b.test(f)&&c.rangeIsSelectable()&&(a=c.getMarkerCoords(c.marker,a),c.marker.style.top=a.y+"px",c.marker.style.left=a.x+"px",x(c.marker,"show"))}function b(a){y(a);a.stopPropagation?a.stopPropagation():a.cancelBubble=!0;a=a.target||a.srcElement;if(!l(this,"masha-marker-bar")||l(a,"masha-social")||l(a,"masha-marker"))if(s(c.marker,"show"),c.rangeIsSelectable()&&(c.addSelection(),c.updateHash(),c.options.onMark&&c.options.onMark.call(c),
c.options.selectMessage&&c._showMessage(),l(a,"masha-social")&&(a=a.getAttribute("data-pattern"))))a=a.replace("{url}",encodeURIComponent(window.location.toString())),c.openShareWindow(a)}this.selectable="string"==typeof this.options.selectable?document.getElementById(this.options.selectable):this.options.selectable;"string"==typeof this.options.marker?(this.marker=document.getElementById(this.options.marker),null===this.marker&&(this.marker=document.createElement("a"),this.marker.setAttribute("id",
@@ -15,24 +15,24 @@ a.nodeType&&(a=u(a));b=a.data.substring(0,b).match(this.regexp);null!=b?("start"
this.lastHash=a="#sel="+a.join(";");this.options.location.setHash(a)},readHash:function(){var a=this.splittedHash();if(a){for(var b=0;b<a.length;b++)this.deserializeSelection(a[b]);this.updateHash();this.options.onHashRead&&this.options.onHashRead.call(this)}},splittedHash:function(){var a=this.options.location.getHash();if(!a)return null;a=a.replace(/^#/,"").replace(/;+$/,"");if(!/^sel\=(?:\d+\:\d+(?:\:[^:;]*)?\,|%2C\d+\:\d+(?:\:[^:;]*)?;)*\d+\:\d+(?:\:[^:;]*)?\,|%2C\d+\:\d+(?:\:[^:;]*)?$/.test(a))return null;
a=a.substring(4,a.length);return a.split(";")},deserializeSelection:function(a){var b=window.getSelection();0<b.rangeCount&&b.removeAllRanges();(a=this.deserializeRange(a))&&this.addSelection(a)},deserializeRange:function(a){var b=/^([0-9A-Za-z:]+)(?:,|%2C)([0-9A-Za-z:]+)$/.exec(a),c=b[1].split(":"),b=b[2].split(":");if(parseInt(c[0],10)<parseInt(b[0],10)||c[0]==b[0]&&parseInt(c[1],10)<=parseInt(b[1],10)){var d=this.deserializePosition(c,"start"),e=this.deserializePosition(b,"end");if(d.node&&e.node){var f=
document.createRange();f.setStart(d.node,d.offset);f.setEnd(e.node,e.offset);if(!this.options.validate||this.validateRange(f,c[2],b[2]))return f}}window.console&&"function"==typeof window.console.warn&&window.console.warn("Cannot deserialize range: "+a);return null},validateRange:function(a,b,c){var d=!0,e;b&&(e=this.getPositionChecksum(a.getWordIterator(this.regexp)),d=d&&b==e);c&&(e=this.getPositionChecksum(a.getWordIterator(this.regexp,!0)),d=d&&c==e);return d},getRangeChecksum:function(a){sum1=
-this.getPositionChecksum(a.getWordIterator(this.regexp));sum2=this.getPositionChecksum(a.getWordIterator(this.regexp,!0));return[sum1,sum2]},getPositionChecksum:function(){return""},deserializePosition:function(a,b){for(var c=this.blocks[parseInt(a[0],10)],d,e=0;c;){for(var f=RegExp(this.options.regexp,"ig");null!=(myArray=f.exec(c.data));)if(e++,e==a[1])return"start"==b&&(d=myArray.index),"end"==b&&(d=f.lastIndex),{node:c,offset:parseInt(d,10)};(c=(c=this.nextNode(c,/.*/))?c._container:null)&&this.isFirstTextNode(c)&&
-(c=null)}return{node:null,offset:0}},serializeRange:function(a){var b=this.words(a.startContainer,a.startOffset,"start"),c=this.words(a.endContainer,a.endOffset,"end");this.options.validate&&(a=this.getRangeChecksum(a),b+=":"+a[0],c+=":"+a[1]);return b+","+c},checkSelection:function(a){this.checkPosition(a,a.startOffset,a.startContainer,"start");this.checkPosition(a,a.endOffset,a.endContainer,"end");this.checkBrackets(a);this.checkSentence(a);return a},checkPosition:function(a,b,c,d){function e(a){return null!=
-a.match(j.regexp)}function f(a){return null==a.match(j.regexp)}function k(a,b,c){for(;0<b&&c(a.data.charAt(b-1));)b--;return b}function h(a,b,c){for(;b<a.data.length&&c(a.data.charAt(b));)b++;return b}var j=this;if(1==c.nodeType&&0<b)if(b<c.childNodes.length)c=c.childNodes[b],b=0;else{var g=o(c);g.length&&(c=g[g.length-1],b=c.data.length)}if("start"==d){if(1==c.nodeType&&""!=n(c.textContent||c.innerText))c=u(c),b=0;if(3!=c.nodeType||null==c.data.substring(b).match(this.regexp))b=this.nextNode(c),
-c=b._container,b=b._offset;b=h(c,b,f);b=k(c,b,e);a.setStart(c,b)}if("end"==d){if(1==c.nodeType&&""!=n(c.textContent||c.innerText)&&0!=b)c=c.childNodes[a.endOffset-1],g=o(c),c=g[g.length-1],b=c.data.length;if(3!=c.nodeType||null==c.data.substring(0,b).match(this.regexp))b=this.prevNode(c),c=b._container,b=b._offset;b=k(c,b,f);b=h(c,b,e);a.setEnd(c,b)}},checkBrackets:function(a){this._checkBrackets(a,"(",")",/\(|\)/g,/\(x*\)/g);this._checkBrackets(a,"\u00ab","\u00bb",/\\u00ab|\\u00bb/g,/\u00abx*\u00bb/g)},
-_checkBrackets:function(a,b,c,d,e){var f=a.toString();if(d=f.match(d)){for(var d=d.join(""),k=d.length+1;d.length<k;)k=d.length,d=d.replace(e,"x");d.charAt(d.length-1)==c&&f.charAt(f.length-1)==c&&(1==a.endOffset?(c=this.prevNode(a.endContainer),a.setEnd(c.container,c.offset)):a.setEnd(a.endContainer,a.endOffset-1));d.charAt(0)==b&&f.charAt(0)==b&&(a.startOffset==a.startContainer.data.length?(c=this.nextNode(a.endContainer),a.setStart(c.container,c.offset)):a.setStart(a.startContainer,a.startOffset+
-1))}},checkSentence:function(a){function b(){a.setEnd(c._container,c._offset+1)}var c,d;if(a.endOffset==a.endContainer.data.length){c=this.nextNode(a.endContainer,/.*/);if(!c)return null;d=c._container.data.charAt(0)}else c={_container:a.endContainer,_offset:a.endOffset},d=a.endContainer.data.charAt(a.endOffset);if(d.match(/\.|\?|\!/)){d=a.toString();if(d.match(/(\.|\?|\!)\s+[A-Z\u0410-\u042f\u0401]/)||0==a.startOffset&&a.startContainer.previousSibling&&1==a.startContainer.previousSibling.nodeType&&
-"selection_index"==a.startContainer.previousSibling.className)return b();for(var e,f=a.getElementIterator();e=f();)if(1==e.nodeType&&"selection_index"==e.className)return b();return d.charAt(0).match(/[A-Z\u0410-\u042f\u0401]/)&&(d=a.startContainer.data.substring(0,a.startOffset),d.match(/\S/)||(d=this.prevNode(a.startContainer,/\W*/)._container.data),d=n(d),d.charAt(d.length-1).match(/(\.|\?|\!)/))?b():null}},mergeSelections:function(a){var b=[],c=a.getElementIterator(),d=c(),e=d,f=r(d,"user_selection_true");
-f&&(f=/(num\d+)(?:$| )/.exec(f.className)[1],a.setStart(u(t(this.selectable,f)),0),b.push(f));for(;d;)1==d.nodeType&&l(d,"user_selection_true")&&(e=/(num\d+)(?:$|)/.exec(d.className)[0],-1==A(e,b)&&b.push(e)),e=d,d=c();if(e=r(e,"user_selection_true"))e=/(num\d+)(?:$| )/.exec(e.className)[1],c=(c=v(this.selectable,e))?c[c.length-1]:null,c=o(c),c=c[c.length-1],a.setEnd(c,c.length);b.length&&(c=a.startContainer,d=a.startOffset,e=a.endContainer,f=a.endOffset,this.deleteSelections(b),a.setStart(c,d),a.setEnd(e,
-f));return a},addSelection:function(a){var a=a||this.getFirstRange(),a=this.checkSelection(a),a=this.mergeSelections(a),b="num"+this.counter;this.ranges[b]=this.serializeRange(a);a.wrapSelection(b+" user_selection_true");this.addSelectionEvents(b)},addSelectionEvents:function(a){for(var b=!1,c=this,d=v(this.selectable,a),e=d.length;e--;)g(d[e],"mouseover",function(){for(var a=d.length;a--;)x(d[a],"hover");window.clearTimeout(b)}),g(d[e],"mouseout",function(a){for(a=a.relatedTarget;a&&a.parentNode&&
-a.className!=this.className;)a=a.parentNode;if(!a||a.className!=this.className)b=window.setTimeout(function(){for(var a=d.length;a--;)s(d[a],"hover")},2E3)});e=document.createElement("a");e.className="txtsel_close";e.href="#";var f=document.createElement("span");f.className="closewrap";f.appendChild(e);g(e,"click",function(b){y(b);c.deleteSelections([a]);c.updateHash();c.options.onUnmark&&c.options.onUnmark.call(c)});d[d.length-1].appendChild(f);this.counter++;window.getSelection().removeAllRanges()},
-getFirstRange:function(){var a=window.getSelection();return a.rangeCount?a.getRangeAt(0):null},enumerateElements:function(){function a(b){for(var b=b.childNodes,e=!1,f=!1,k=0;k<b.length;++k){var h=b.item(k),g=h.nodeType;if(3!=g||h.nodeValue.match(c.regexp))3==g?f||(j.captureCount++,e=document.createElement("span"),e.id="selection_index"+j.captureCount,e.className="selection_index",h.parentNode.insertBefore(e,h),k++,c.blocks[j.captureCount]=h,e=f=!0):1==g&&!c.isIgnored(h)&&(c.options.isBlock(h)?(h=
-a(h),e=e||h,f=!1):f||(f=a(h),e=e||f))}return e}var b=this.selectable;j.captureCount=j.captureCount||0;var c=this;a(b)},isFirstTextNode:function(a){for(var a=[a.previousSibling,a.parentNode.previousSibling],b=a.length;b--;)if(a[b]&&1==a[b].nodeType&&"selection_index"==a[b].className)return!0;return!1},getFirstTextNode:function(a){return!a?null:(a=document.getElementById("selection_index"+a))?1==a.nextSibling.nodeType?a.nextSibling.childNodes[0]:a.nextSibling:null},getNum:function(a){for(;a.parentNode;){for(;a.previousSibling;){for(a=
-a.previousSibling;1==a.nodeType&&a.childNodes.length;)a=a.lastChild;if(1==a.nodeType&&"selection_index"==a.className)return a.id.replace("selection_index","")}a=a.parentNode}return null},constructIgnored:function(a){if("function"==typeof a)return a;if("string"==typeof a){for(var b=[],c=[],d=[],a=a.split(","),e=0;e<a.length;e++){var f=n(a[e]);"#"==f.charAt(0)?b.push(f.substr(1)):"."==f.charAt(0)?c.push(f.substr(1)):d.push(f)}return function(a){var e;for(e=b.length;e--;)if(a.id==b[e])return!0;for(e=
-c.length;e--;)if(l(a,c[e]))return!0;for(e=d.length;e--;)if(a.tagName==d[e].toUpperCase())return!0;return!1}}return function(){return!1}},rangeIsSelectable:function(){var a,b,c,d=!0,e=this.getFirstRange();if(!e)return!1;for(e=e.getElementIterator();a=e();)if(3==a.nodeType&&null!=a.data.match(this.regexp)&&(b=b||a,c=a),a=d&&3==a.nodeType?a.parentNode:a,d=!1,1==a.nodeType){for(;a!=this.selectable&&a.parentNode;){if(this.isIgnored(a))return!1;a=a.parentNode}if(a!=this.selectable)return!1}b=r(b,"user_selection_true");
-c=r(c,"user_selection_true");return b&&c?(d=/(?:^| )(num\d+)(?:$| )/,d.exec(b.className)[1]!=d.exec(c.className)[1]):!0},initMessage:function(){var a=this;this.msg="string"==typeof this.options.selectMessage?document.getElementById(this.options.selectMessage):this.options.selectMessage;this.close_button=this.getCloseButton();this.msg_autoclose=null;g(this.close_button,"click",function(b){y(b);a.hideMessage();a.saveMessageClosed();clearTimeout(a.msg_autoclose)})},getCloseButton:function(){return this.msg.getElementsByTagName("a")[0]},
-getMessageClosed:function(){return window.localStorage?!!localStorage.masha_warning:!!document.cookie.match(/(?:^|;)\s*masha-warning=/)},saveMessageClosed:function(){window.localStorage?localStorage.masha_warning="true":this.get_closed()||(document.cookie+="; masha-warning=true")},_showMessage:function(){var a=this;this.getMessageClosed()||(this.showMessage(),clearTimeout(this.msg_autoclose),this.msg_autoclose=setTimeout(function(){a.hideMessage()},1E4))},showMessage:function(){x(this.msg,"show")},
-hideMessage:function(){s(this.msg,"show")}};var p=window.Range||document.createRange().constructor;p.prototype.splitBoundaries=function(){var a=this.startContainer,b=this.startOffset,c=this.endContainer,d=this.endOffset,e=a===c;3==c.nodeType&&d<c.length&&c.splitText(d);3==a.nodeType&&0<b&&(a=a.splitText(b),e&&(d-=b,c=a),b=0);this.setStart(a,b);this.setEnd(c,d)};p.prototype.getTextNodes=function(){for(var a=this.getElementIterator(),b=[],c;c=a();)3==c.nodeType&&b.push(c);return b};p.prototype.getElementIterator=
-function(a){return a?m(null,this.endContainer,this.startContainer,!0):m(null,this.startContainer,this.endContainer)};p.prototype.getWordIterator=function(a,b){var c=this.getElementIterator(b),d,e=0,f=0,g=!1,h,j=this;return function(){if(e==f&&!g){do{do d=c();while(d&&3!=d.nodeType);g=!d;g||(value=d.nodeValue,d==j.endContainer&&(value=value.substr(0,j.endOffset)),d==j.startContainer&&(value=value.substr(j.startOffset)),h=value.match(a))}while(d&&!h);h&&(e=b?0:h.length-1,f=b?h.length-1:0)}else b?f--:
-f++;return g?null:h[f]}};p.prototype.wrapSelection=function(a){this.splitBoundaries();for(var b=this.getTextNodes(),c=b.length;c--;){var d=document.createElement("span");d.className=a;b[c].parentNode.insertBefore(d,b[c]);d.appendChild(b[c])}};j.LocationHandler=z;window.MaSha=j;window.jQuery&&(window.jQuery.fn.masha=function(a){a=a||{};a=q({selectable:this[0]},a);return new j(a)});var B=function(a){this.prefix=a};B.prototype={setHash:function(a){a=a.replace("sel",this.prefix).replace(/^#/,"");a.length==
-this.prefix.length+1&&(a="");var b=this.getHashPart();window.location.hash.replace(/^#\|?/,"");a=b?window.location.hash.replace(b,a):window.location.hash+"|"+a;a="#"+a.replace("||","").replace(/^#?\|?|\|$/g,"");window.location.hash=a},addHashchange:j.LocationHandler.prototype.addHashchange,getHashPart:function(){for(var a=window.location.hash.replace(/^#\|?/,"").split(/\||%7C/),b=0;b<a.length;b++)if(a[b].substr(0,this.prefix.length+1)==this.prefix+"=")return a[b];return""},getHash:function(){return this.getHashPart().replace(this.prefix,
-"sel")}};window.MultiMaSha=function(a,b,c){for(var b=b||function(a){return a.id},d=0;d<a.length;d++){var e=a[d],f=b(e);if(f){e=q({},c||{},{selectable:e,location:new B(f)});new j(e)}}}})();
+this.getPositionChecksum(a.getWordIterator(this.regexp));sum2=this.getPositionChecksum(a.getWordIterator(this.regexp,!0));return[sum1,sum2]},getPositionChecksum:function(a){for(var b="",c=0;3>c;c++){var d=(a()||"").charAt(0);d&&(d="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890".charAt(d.charCodeAt(0)%62));b+=d}return b},deserializePosition:function(a,b){for(var c=this.blocks[parseInt(a[0],10)],d,e=0;c;){for(var f=RegExp(this.options.regexp,"ig");null!=(myArray=f.exec(c.data));)if(e++,
+e==a[1])return"start"==b&&(d=myArray.index),"end"==b&&(d=f.lastIndex),{node:c,offset:parseInt(d,10)};(c=(c=this.nextNode(c,/.*/))?c._container:null)&&this.isFirstTextNode(c)&&(c=null)}return{node:null,offset:0}},serializeRange:function(a){var b=this.words(a.startContainer,a.startOffset,"start"),c=this.words(a.endContainer,a.endOffset,"end");this.options.validate&&(a=this.getRangeChecksum(a),b+=":"+a[0],c+=":"+a[1]);return b+","+c},checkSelection:function(a){this.checkPosition(a,a.startOffset,a.startContainer,
+"start");this.checkPosition(a,a.endOffset,a.endContainer,"end");this.checkBrackets(a);this.checkSentence(a);return a},checkPosition:function(a,b,c,d){function e(a){return null!=a.match(j.regexp)}function f(a){return null==a.match(j.regexp)}function k(a,b,c){for(;0<b&&c(a.data.charAt(b-1));)b--;return b}function h(a,b,c){for(;b<a.data.length&&c(a.data.charAt(b));)b++;return b}var j=this;if(1==c.nodeType&&0<b)if(b<c.childNodes.length)c=c.childNodes[b],b=0;else{var g=o(c);g.length&&(c=g[g.length-1],
+b=c.data.length)}if("start"==d){if(1==c.nodeType&&""!=n(c.textContent||c.innerText))c=u(c),b=0;if(3!=c.nodeType||null==c.data.substring(b).match(this.regexp))b=this.nextNode(c),c=b._container,b=b._offset;b=h(c,b,f);b=k(c,b,e);a.setStart(c,b)}if("end"==d){if(1==c.nodeType&&""!=n(c.textContent||c.innerText)&&0!=b)c=c.childNodes[a.endOffset-1],g=o(c),c=g[g.length-1],b=c.data.length;if(3!=c.nodeType||null==c.data.substring(0,b).match(this.regexp))b=this.prevNode(c),c=b._container,b=b._offset;b=k(c,b,
+f);b=h(c,b,e);a.setEnd(c,b)}},checkBrackets:function(a){this._checkBrackets(a,"(",")",/\(|\)/g,/\(x*\)/g);this._checkBrackets(a,"\u00ab","\u00bb",/\\u00ab|\\u00bb/g,/\u00abx*\u00bb/g)},_checkBrackets:function(a,b,c,d,e){var f=a.toString();if(d=f.match(d)){for(var d=d.join(""),k=d.length+1;d.length<k;)k=d.length,d=d.replace(e,"x");d.charAt(d.length-1)==c&&f.charAt(f.length-1)==c&&(1==a.endOffset?(c=this.prevNode(a.endContainer),a.setEnd(c.container,c.offset)):a.setEnd(a.endContainer,a.endOffset-1));
+d.charAt(0)==b&&f.charAt(0)==b&&(a.startOffset==a.startContainer.data.length?(c=this.nextNode(a.endContainer),a.setStart(c.container,c.offset)):a.setStart(a.startContainer,a.startOffset+1))}},checkSentence:function(a){function b(){a.setEnd(c._container,c._offset+1)}var c,d;if(a.endOffset==a.endContainer.data.length){c=this.nextNode(a.endContainer,/.*/);if(!c)return null;d=c._container.data.charAt(0)}else c={_container:a.endContainer,_offset:a.endOffset},d=a.endContainer.data.charAt(a.endOffset);if(d.match(/\.|\?|\!/)){d=
+a.toString();if(d.match(/(\.|\?|\!)\s+[A-Z\u0410-\u042f\u0401]/)||0==a.startOffset&&a.startContainer.previousSibling&&1==a.startContainer.previousSibling.nodeType&&"selection_index"==a.startContainer.previousSibling.className)return b();for(var e,f=a.getElementIterator();e=f();)if(1==e.nodeType&&"selection_index"==e.className)return b();return d.charAt(0).match(/[A-Z\u0410-\u042f\u0401]/)&&(d=a.startContainer.data.substring(0,a.startOffset),d.match(/\S/)||(d=this.prevNode(a.startContainer,/\W*/)._container.data),
+d=n(d),d.charAt(d.length-1).match(/(\.|\?|\!)/))?b():null}},mergeSelections:function(a){var b=[],c=a.getElementIterator(),d=c(),e=d,f=r(d,"user_selection_true");f&&(f=/(num\d+)(?:$| )/.exec(f.className)[1],a.setStart(u(t(this.selectable,f)),0),b.push(f));for(;d;)1==d.nodeType&&l(d,"user_selection_true")&&(e=/(num\d+)(?:$|)/.exec(d.className)[0],-1==A(e,b)&&b.push(e)),e=d,d=c();if(e=r(e,"user_selection_true"))e=/(num\d+)(?:$| )/.exec(e.className)[1],c=(c=v(this.selectable,e))?c[c.length-1]:null,c=
+o(c),c=c[c.length-1],a.setEnd(c,c.length);b.length&&(c=a.startContainer,d=a.startOffset,e=a.endContainer,f=a.endOffset,this.deleteSelections(b),a.setStart(c,d),a.setEnd(e,f));return a},addSelection:function(a){var a=a||this.getFirstRange(),a=this.checkSelection(a),a=this.mergeSelections(a),b="num"+this.counter;this.ranges[b]=this.serializeRange(a);a.wrapSelection(b+" user_selection_true");this.addSelectionEvents(b)},addSelectionEvents:function(a){for(var b=!1,c=this,d=v(this.selectable,a),e=d.length;e--;)g(d[e],
+"mouseover",function(){for(var a=d.length;a--;)x(d[a],"hover");window.clearTimeout(b)}),g(d[e],"mouseout",function(a){for(a=a.relatedTarget;a&&a.parentNode&&a.className!=this.className;)a=a.parentNode;if(!a||a.className!=this.className)b=window.setTimeout(function(){for(var a=d.length;a--;)s(d[a],"hover")},2E3)});e=document.createElement("a");e.className="txtsel_close";e.href="#";var f=document.createElement("span");f.className="closewrap";f.appendChild(e);g(e,"click",function(b){y(b);c.deleteSelections([a]);
+c.updateHash();c.options.onUnmark&&c.options.onUnmark.call(c)});d[d.length-1].appendChild(f);this.counter++;window.getSelection().removeAllRanges()},getFirstRange:function(){var a=window.getSelection();return a.rangeCount?a.getRangeAt(0):null},enumerateElements:function(){function a(b){for(var b=b.childNodes,e=!1,f=!1,k=0;k<b.length;++k){var h=b.item(k),g=h.nodeType;if(3!=g||h.nodeValue.match(c.regexp))3==g?f||(j.captureCount++,e=document.createElement("span"),e.id="selection_index"+j.captureCount,
+e.className="selection_index",h.parentNode.insertBefore(e,h),k++,c.blocks[j.captureCount]=h,e=f=!0):1==g&&!c.isIgnored(h)&&(c.options.isBlock(h)?(h=a(h),e=e||h,f=!1):f||(f=a(h),e=e||f))}return e}var b=this.selectable;j.captureCount=j.captureCount||0;var c=this;a(b)},isFirstTextNode:function(a){for(var a=[a.previousSibling,a.parentNode.previousSibling],b=a.length;b--;)if(a[b]&&1==a[b].nodeType&&"selection_index"==a[b].className)return!0;return!1},getFirstTextNode:function(a){return!a?null:(a=document.getElementById("selection_index"+
+a))?1==a.nextSibling.nodeType?a.nextSibling.childNodes[0]:a.nextSibling:null},getNum:function(a){for(;a.parentNode;){for(;a.previousSibling;){for(a=a.previousSibling;1==a.nodeType&&a.childNodes.length;)a=a.lastChild;if(1==a.nodeType&&"selection_index"==a.className)return a.id.replace("selection_index","")}a=a.parentNode}return null},constructIgnored:function(a){if("function"==typeof a)return a;if("string"==typeof a){for(var b=[],c=[],d=[],a=a.split(","),e=0;e<a.length;e++){var f=n(a[e]);"#"==f.charAt(0)?
+b.push(f.substr(1)):"."==f.charAt(0)?c.push(f.substr(1)):d.push(f)}return function(a){var e;for(e=b.length;e--;)if(a.id==b[e])return!0;for(e=c.length;e--;)if(l(a,c[e]))return!0;for(e=d.length;e--;)if(a.tagName==d[e].toUpperCase())return!0;return!1}}return function(){return!1}},rangeIsSelectable:function(){var a,b,c,d=!0,e=this.getFirstRange();if(!e)return!1;for(e=e.getElementIterator();a=e();)if(3==a.nodeType&&null!=a.data.match(this.regexp)&&(b=b||a,c=a),a=d&&3==a.nodeType?a.parentNode:a,d=!1,1==
+a.nodeType){for(;a!=this.selectable&&a.parentNode;){if(this.isIgnored(a))return!1;a=a.parentNode}if(a!=this.selectable)return!1}b=r(b,"user_selection_true");c=r(c,"user_selection_true");return b&&c?(d=/(?:^| )(num\d+)(?:$| )/,d.exec(b.className)[1]!=d.exec(c.className)[1]):!0},initMessage:function(){var a=this;this.msg="string"==typeof this.options.selectMessage?document.getElementById(this.options.selectMessage):this.options.selectMessage;this.close_button=this.getCloseButton();this.msg_autoclose=
+null;g(this.close_button,"click",function(b){y(b);a.hideMessage();a.saveMessageClosed();clearTimeout(a.msg_autoclose)})},getCloseButton:function(){return this.msg.getElementsByTagName("a")[0]},getMessageClosed:function(){return window.localStorage?!!localStorage.masha_warning:!!document.cookie.match(/(?:^|;)\s*masha-warning=/)},saveMessageClosed:function(){window.localStorage?localStorage.masha_warning="true":this.get_closed()||(document.cookie+="; masha-warning=true")},_showMessage:function(){var a=
+this;this.getMessageClosed()||(this.showMessage(),clearTimeout(this.msg_autoclose),this.msg_autoclose=setTimeout(function(){a.hideMessage()},1E4))},showMessage:function(){x(this.msg,"show")},hideMessage:function(){s(this.msg,"show")}};var p=window.Range||document.createRange().constructor;p.prototype.splitBoundaries=function(){var a=this.startContainer,b=this.startOffset,c=this.endContainer,d=this.endOffset,e=a===c;3==c.nodeType&&d<c.length&&c.splitText(d);3==a.nodeType&&0<b&&(a=a.splitText(b),e&&
+(d-=b,c=a),b=0);this.setStart(a,b);this.setEnd(c,d)};p.prototype.getTextNodes=function(){for(var a=this.getElementIterator(),b=[],c;c=a();)3==c.nodeType&&b.push(c);return b};p.prototype.getElementIterator=function(a){return a?m(null,this.endContainer,this.startContainer,!0):m(null,this.startContainer,this.endContainer)};p.prototype.getWordIterator=function(a,b){var c=this.getElementIterator(b),d,e=0,f=0,g=!1,h,j=this;return function(){if(e==f&&!g){do{do d=c();while(d&&3!=d.nodeType);g=!d;g||(value=
+d.nodeValue,d==j.endContainer&&(value=value.substr(0,j.endOffset)),d==j.startContainer&&(value=value.substr(j.startOffset)),h=value.match(a))}while(d&&!h);h&&(e=b?0:h.length-1,f=b?h.length-1:0)}else b?f--:f++;return g?null:h[f]}};p.prototype.wrapSelection=function(a){this.splitBoundaries();for(var b=this.getTextNodes(),c=b.length;c--;){var d=document.createElement("span");d.className=a;b[c].parentNode.insertBefore(d,b[c]);d.appendChild(b[c])}};j.LocationHandler=z;window.MaSha=j;window.jQuery&&(window.jQuery.fn.masha=
+function(a){a=a||{};a=q({selectable:this[0]},a);return new j(a)});var B=function(a){this.prefix=a};B.prototype={setHash:function(a){a=a.replace("sel",this.prefix).replace(/^#/,"");a.length==this.prefix.length+1&&(a="");var b=this.getHashPart();window.location.hash.replace(/^#\|?/,"");a=b?window.location.hash.replace(b,a):window.location.hash+"|"+a;a="#"+a.replace("||","").replace(/^#?\|?|\|$/g,"");window.location.hash=a},addHashchange:j.LocationHandler.prototype.addHashchange,getHashPart:function(){for(var a=
+window.location.hash.replace(/^#\|?/,"").split(/\||%7C/),b=0;b<a.length;b++)if(a[b].substr(0,this.prefix.length+1)==this.prefix+"=")return a[b];return""},getHash:function(){return this.getHashPart().replace(this.prefix,"sel")}};window.MultiMaSha=function(a,b,c){for(var b=b||function(a){return a.id},d=0;d<a.length;d++){var e=a[d],f=b(e);if(f){e=q({},c||{},{selectable:e,location:new B(f)});new j(e)}}}})();
Please sign in to comment.
Something went wrong with that request. Please try again.