Jsahleen has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/161888

Change subject: Selections: Add selection utility functions to ext.cx.utils.js
......................................................................

Selections: Add selection utility functions to ext.cx.utils.js

Functions added:

* mw.cx.getSelection [cross-browser function to get selection]

* mw.cx.getSelectionRange [cross-browser function get selection range]

* mw.cx.setSelectionRange [cross-browser function to set selection range]

Functions were added to utils so they can be used anywhere in cx.

Change-Id: I49ba432c24cc96172ce63b5bf68335cc6bdead12
---
M modules/base/ext.cx.util.js
1 file changed, 81 insertions(+), 0 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ContentTranslation 
refs/changes/88/161888/1

diff --git a/modules/base/ext.cx.util.js b/modules/base/ext.cx.util.js
index 87dc840..8743f21 100644
--- a/modules/base/ext.cx.util.js
+++ b/modules/base/ext.cx.util.js
@@ -102,4 +102,85 @@
 
                return sectionTypes.join( ',' );
        };
+
+       /**
+        * A cross-browser function that returns a selection object
+        * @return {object}
+        */
+       mw.cx.getSelection = function () {
+               // Standards
+               if ( window.getSelection ) {
+                       return window.getSelection();
+               // IE < 9
+               } else if ( document.selection ) {
+                       return document.selection;
+               }
+
+               return null;
+       };
+
+       /**
+        * A cross-browser function that returns
+        * the range of the current selection.
+        * @return {range}
+        */
+       mw.cx.getSelectionRange = function () {
+               var selection = mw.cx.getSelection();
+
+               // Standards
+               if ( selection && selection.getRangeAt ) {
+                       if ( selection.rangeCount > 0 ) {
+                               return selection.getRangeAt( 0 );
+                       }
+               // IE < 9
+               } else if ( selection && selection.createRange ) {
+                       return selection.createRange();
+               }
+
+               return null;
+       };
+
+       /**
+        * A cross-browser function that sets
+        * the selection range.
+        * @param {range} range, the range to use
+        */
+       mw.cx.setSelectionRange = function ( range ) {
+               var selection = mw.cx.getSelection();
+
+               if ( selection ) {
+                       selection.removeAllRanges();
+                       selection.addRange( range );
+               }
+       };
+
+       /**
+        * A cross-browser function that pastes html
+        * at the selection.
+        * @param {string} html, the html to paste
+        */
+       mw.cx.pasteHtmlAtSelection = function ( html ) {
+               var selection, el, frag, node, lastNode;
+               selection = mw.cx.getSelection();
+
+               if ( selection && window.getSelection ) {
+                       selection.deleteContents();
+                       el = document.createElement( 'div' );
+                       el.innerHTML = html;
+                       frag = document.createDocumentFragment();
+                       while ( ( node = el.firstChild ) ) {
+                               lastNode = frag.appendChild( node );
+                       }
+                       selection.insertNode( frag );
+
+                       if ( lastNode ) {
+                               selection.setStartAfter( lastNode );
+                               selection.collapse( true );
+                       }
+
+               } else if ( selection && selection.type !== 'Control' ) {
+                       // IE < 9
+                       selection.createRange().pasteHTML( html );
+               }
+       };
 }( mediaWiki ) );

-- 
To view, visit https://gerrit.wikimedia.org/r/161888
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I49ba432c24cc96172ce63b5bf68335cc6bdead12
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ContentTranslation
Gerrit-Branch: master
Gerrit-Owner: Jsahleen <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to