/usr/share/javascript/dijit/selection.js is in libjs-dojo-dijit 1.10.2+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 | define("dijit/selection", [
"dojo/_base/array",
"dojo/dom", // dom.byId
"dojo/_base/lang",
"dojo/sniff", // has("ie") has("opera")
"dojo/_base/window",
"dijit/focus"
], function(array, dom, lang, has, baseWindow, focus){
// module:
// dijit/selection
// Note that this class is using feature detection, but doesn't use has() because sometimes on IE the outer window
// may be running in standards mode (ie, IE9 mode) but an iframe may be in compatibility mode. So the code path
// used will vary based on the window.
var SelectionManager = function(win){
// summary:
// Class for monitoring / changing the selection (typically highlighted text) in a given window
// win: Window
// The window to monitor/adjust the selection on.
var doc = win.document;
this.getType = function(){
// summary:
// Get the selection type (like doc.select.type in IE).
if(doc.getSelection){
// W3C path
var stype = "text";
// Check if the actual selection is a CONTROL (IMG, TABLE, HR, etc...).
var oSel;
try{
oSel = win.getSelection();
}catch(e){ /*squelch*/ }
if(oSel && oSel.rangeCount == 1){
var oRange = oSel.getRangeAt(0);
if( (oRange.startContainer == oRange.endContainer) &&
((oRange.endOffset - oRange.startOffset) == 1) &&
(oRange.startContainer.nodeType != 3 /* text node*/)
){
stype = "control";
}
}
return stype; //String
}else{
// IE6-8
return doc.selection.type.toLowerCase();
}
};
this.getSelectedText = function(){
// summary:
// Return the text (no html tags) included in the current selection or null if no text is selected
if(doc.getSelection){
// W3C path
var selection = win.getSelection();
return selection ? selection.toString() : ""; //String
}else{
// IE6-8
if(this.getType() == 'control'){
return null;
}
return doc.selection.createRange().text;
}
};
this.getSelectedHtml = function(){
// summary:
// Return the html text of the current selection or null if unavailable
if(doc.getSelection){
// W3C path
var selection = win.getSelection();
if(selection && selection.rangeCount){
var i;
var html = "";
for(i = 0; i < selection.rangeCount; i++){
//Handle selections spanning ranges, such as Opera
var frag = selection.getRangeAt(i).cloneContents();
var div = doc.createElement("div");
div.appendChild(frag);
html += div.innerHTML;
}
return html; //String
}
return null;
}else{
// IE6-8
if(this.getType() == 'control'){
return null;
}
return doc.selection.createRange().htmlText;
}
};
this.getSelectedElement = function(){
// summary:
// Retrieves the selected element (if any), just in the case that
// a single element (object like and image or a table) is
// selected.
if(this.getType() == "control"){
if(doc.getSelection){
// W3C path
var selection = win.getSelection();
return selection.anchorNode.childNodes[ selection.anchorOffset ];
}else{
// IE6-8
var range = doc.selection.createRange();
if(range && range.item){
return doc.selection.createRange().item(0);
}
}
}
return null;
};
this.getParentElement = function(){
// summary:
// Get the parent element of the current selection
if(this.getType() == "control"){
var p = this.getSelectedElement();
if(p){ return p.parentNode; }
}else{
if(doc.getSelection){
var selection = doc.getSelection();
if(selection){
var node = selection.anchorNode;
while(node && (node.nodeType != 1)){ // not an element
node = node.parentNode;
}
return node;
}
}else{
var r = doc.selection.createRange();
r.collapse(true);
return r.parentElement();
}
}
return null;
};
this.hasAncestorElement = function(/*String*/ tagName /* ... */){
// summary:
// Check whether current selection has a parent element which is
// of type tagName (or one of the other specified tagName)
// tagName: String
// The tag name to determine if it has an ancestor of.
return this.getAncestorElement.apply(this, arguments) != null; //Boolean
};
this.getAncestorElement = function(/*String*/ tagName /* ... */){
// summary:
// Return the parent element of the current selection which is of
// type tagName (or one of the other specified tagName)
// tagName: String
// The tag name to determine if it has an ancestor of.
var node = this.getSelectedElement() || this.getParentElement();
return this.getParentOfType(node, arguments); //DOMNode
};
this.isTag = function(/*DomNode*/ node, /*String[]*/ tags){
// summary:
// Function to determine if a node is one of an array of tags.
// node:
// The node to inspect.
// tags:
// An array of tag name strings to check to see if the node matches.
if(node && node.tagName){
var _nlc = node.tagName.toLowerCase();
for(var i=0; i<tags.length; i++){
var _tlc = String(tags[i]).toLowerCase();
if(_nlc == _tlc){
return _tlc; // String
}
}
}
return "";
};
this.getParentOfType = function(/*DomNode*/ node, /*String[]*/ tags){
// summary:
// Function to locate a parent node that matches one of a set of tags
// node:
// The node to inspect.
// tags:
// An array of tag name strings to check to see if the node matches.
while(node){
if(this.isTag(node, tags).length){
return node; // DOMNode
}
node = node.parentNode;
}
return null;
};
this.collapse = function(/*Boolean*/ beginning){
// summary:
// Function to collapse (clear), the current selection
// beginning: Boolean
// Indicates whether to collapse the cursor to the beginning of the selection or end.
if(doc.getSelection){
// W3C path
var selection = win.getSelection();
if(selection.removeAllRanges){ // Mozilla
if(beginning){
selection.collapseToStart();
}else{
selection.collapseToEnd();
}
}else{ // Safari
// pulled from WebCore/ecma/kjs_window.cpp, line 2536
selection.collapse(beginning);
}
}else{
// IE6-8
var range = doc.selection.createRange();
range.collapse(beginning);
range.select();
}
};
this.remove = function(){
// summary:
// Function to delete the currently selected content from the document.
var sel = doc.selection;
if(doc.getSelection){
// W3C path
sel = win.getSelection();
sel.deleteFromDocument();
return sel; //Selection
}else{
// IE6-8
if(sel.type.toLowerCase() != "none"){
sel.clear();
}
return sel; //Selection
}
};
this.selectElementChildren = function(/*DomNode*/ element, /*Boolean?*/ nochangefocus){
// summary:
// clear previous selection and select the content of the node
// (excluding the node itself)
// element: DOMNode
// The element you wish to select the children content of.
// nochangefocus: Boolean
// Indicates if the focus should change or not.
var range;
element = dom.byId(element);
if(doc.getSelection){
// W3C
var selection = win.getSelection();
if(has("opera")){
//Opera's selectAllChildren doesn't seem to work right
//against <body> nodes and possibly others ... so
//we use the W3C range API
if(selection.rangeCount){
range = selection.getRangeAt(0);
}else{
range = doc.createRange();
}
range.setStart(element, 0);
range.setEnd(element,(element.nodeType == 3) ? element.length : element.childNodes.length);
selection.addRange(range);
}else{
selection.selectAllChildren(element);
}
}else{
// IE6-8
range = element.ownerDocument.body.createTextRange();
range.moveToElementText(element);
if(!nochangefocus){
try{
range.select(); // IE throws an exception here if the widget is hidden. See #5439
}catch(e){ /* squelch */}
}
}
};
this.selectElement = function(/*DomNode*/ element, /*Boolean?*/ nochangefocus){
// summary:
// clear previous selection and select element (including all its children)
// element: DOMNode
// The element to select.
// nochangefocus: Boolean
// Boolean indicating if the focus should be changed. IE only.
var range;
element = dom.byId(element); // TODO: remove for 2.0 or sooner, spec listed above doesn't allow for string
if(doc.getSelection){
// W3C path
var selection = doc.getSelection();
range = doc.createRange();
if(selection.removeAllRanges){ // Mozilla
// FIXME: does this work on Safari?
if(has("opera")){
//Opera works if you use the current range on
//the selection if present.
if(selection.getRangeAt(0)){
range = selection.getRangeAt(0);
}
}
range.selectNode(element);
selection.removeAllRanges();
selection.addRange(range);
}
}else{
// IE6-8
try{
var tg = element.tagName ? element.tagName.toLowerCase() : "";
if(tg === "img" || tg === "table"){
range = baseWindow.body(doc).createControlRange();
}else{
range = baseWindow.body(doc).createRange();
}
range.addElement(element);
if(!nochangefocus){
range.select();
}
}catch(e){
this.selectElementChildren(element, nochangefocus);
}
}
};
this.inSelection = function(node){
// summary:
// This function determines if 'node' is
// in the current selection.
// tags:
// public
if(node){
var newRange;
var range;
if(doc.getSelection){
// WC3
var sel = win.getSelection();
if(sel && sel.rangeCount > 0){
range = sel.getRangeAt(0);
}
if(range && range.compareBoundaryPoints && doc.createRange){
try{
newRange = doc.createRange();
newRange.setStart(node, 0);
if(range.compareBoundaryPoints(range.START_TO_END, newRange) === 1){
return true;
}
}catch(e){ /* squelch */}
}
}else{
// IE6-8, so we can't use the range object as the pseudo
// range doesn't implement the boundary checking, we have to
// use IE specific crud.
range = doc.selection.createRange();
try{
newRange = node.ownerDocument.body.createTextRange();
newRange.moveToElementText(node);
}catch(e2){/* squelch */}
if(range && newRange){
// We can finally compare similar to W3C
if(range.compareEndPoints("EndToStart", newRange) === 1){
return true;
}
}
}
}
return false; // Boolean
};
this.getBookmark = function(){
// summary:
// Retrieves a bookmark that can be used with moveToBookmark to reselect the currently selected range.
// TODO: merge additional code from Editor._getBookmark into this method
var bm, rg, tg, sel = doc.selection, cf = focus.curNode;
if(doc.getSelection){
// W3C Range API for selections.
sel = win.getSelection();
if(sel){
if(sel.isCollapsed){
tg = cf? cf.tagName : "";
if(tg){
// Create a fake rangelike item to restore selections.
tg = tg.toLowerCase();
if(tg == "textarea" ||
(tg == "input" && (!cf.type || cf.type.toLowerCase() == "text"))){
sel = {
start: cf.selectionStart,
end: cf.selectionEnd,
node: cf,
pRange: true
};
return {isCollapsed: (sel.end <= sel.start), mark: sel}; //Object.
}
}
bm = {isCollapsed:true};
if(sel.rangeCount){
bm.mark = sel.getRangeAt(0).cloneRange();
}
}else{
rg = sel.getRangeAt(0);
bm = {isCollapsed: false, mark: rg.cloneRange()};
}
}
}else if(sel){
// If the current focus was a input of some sort and no selection, don't bother saving
// a native bookmark. This is because it causes issues with dialog/page selection restore.
// So, we need to create pseudo bookmarks to work with.
tg = cf ? cf.tagName : "";
tg = tg.toLowerCase();
if(cf && tg && (tg == "button" || tg == "textarea" || tg == "input")){
if(sel.type && sel.type.toLowerCase() == "none"){
return {
isCollapsed: true,
mark: null
}
}else{
rg = sel.createRange();
return {
isCollapsed: rg.text && rg.text.length?false:true,
mark: {
range: rg,
pRange: true
}
};
}
}
bm = {};
//'IE' way for selections.
try{
// createRange() throws exception when dojo in iframe
// and nothing selected, see #9632
rg = sel.createRange();
bm.isCollapsed = !(sel.type == 'Text' ? rg.htmlText.length : rg.length);
}catch(e){
bm.isCollapsed = true;
return bm;
}
if(sel.type.toUpperCase() == 'CONTROL'){
if(rg.length){
bm.mark=[];
var i=0,len=rg.length;
while(i<len){
bm.mark.push(rg.item(i++));
}
}else{
bm.isCollapsed = true;
bm.mark = null;
}
}else{
bm.mark = rg.getBookmark();
}
}else{
console.warn("No idea how to store the current selection for this browser!");
}
return bm; // Object
};
this.moveToBookmark = function(/*Object*/ bookmark){
// summary:
// Moves current selection to a bookmark.
// bookmark:
// This should be a returned object from getBookmark().
// TODO: merge additional code from Editor._moveToBookmark into this method
var mark = bookmark.mark;
if(mark){
if(doc.getSelection){
// W3C Range API (FF, WebKit, Opera, etc)
var sel = win.getSelection();
if(sel && sel.removeAllRanges){
if(mark.pRange){
var n = mark.node;
n.selectionStart = mark.start;
n.selectionEnd = mark.end;
}else{
sel.removeAllRanges();
sel.addRange(mark);
}
}else{
console.warn("No idea how to restore selection for this browser!");
}
}else if(doc.selection && mark){
//'IE' way.
var rg;
if(mark.pRange){
rg = mark.range;
}else if(lang.isArray(mark)){
rg = doc.body.createControlRange();
//rg.addElement does not have call/apply method, so can not call it directly
//rg is not available in "range.addElement(item)", so can't use that either
array.forEach(mark, function(n){
rg.addElement(n);
});
}else{
rg = doc.body.createTextRange();
rg.moveToBookmark(mark);
}
rg.select();
}
}
};
this.isCollapsed = function(){
// summary:
// Returns true if there is no text selected
return this.getBookmark().isCollapsed;
};
};
// singleton on the main window
var selection = new SelectionManager(window);
// hook for editor to use class
selection.SelectionManager = SelectionManager;
return selection;
});
|