      1 /**
      2  * RichDom for Webkit
      3  */
      4 xq.RichDomWebkit = Class.create(xq.RichDomW3, {
      5 	makePlaceHolder: function() {
      6 		var holder = this.createElement("BR");
      7 		holder.className = "webkit-block-placeholder";
      8 		return holder;
      9 	},
     10
     11 	makePlaceHolderString: function() {
     12 		return '<br class="webkit-block-placeholder" />';
     13 	},
     14
     15 	makeEmptyParagraph: function() {
     16 		return this.createElementFromHtml('<p><br class="webkit-block-placeholder" /></p>');
     17 	},
     18
     19 	isPlaceHolder: function(node) {
     20 		return node.nodeName == "BR" && node.className == "webkit-block-placeholder";
     21 	},
     22
     23 	rng: function() {
     24 		var sel = this.sel();
     25 		var rng = this.doc.createRange();
     26 		if (!this._rng ||
     27 			this._anchorNode != sel.anchorNode ||
     28 			this._anchorOffset != sel.anchorOffset ||
     29 			this._focusNode != sel.focusNode ||
     30 			this._focusOffset != sel.focusOffset ) {
     31
     32 			if (sel.type != 'None') {
     33 				rng.setStart(sel.anchorNode, sel.anchorOffset);
     34 				rng.setEnd(sel.focusNode, sel.focusOffset);
     35 			}
     36 			this._anchorNode = sel.anchorNode;
     37 			this._anchorOffset = sel.anchorOffset;
     38 			this._focusNode = sel.focusNode;
     39 			this._focusOffset = sel.focusOffset;
     40 			this._rng = rng;
     41 		}
     42 		return this._rng;
     43 	},
     44
     45 	selectElement: function(element, entireElement) {
     46 		if(!element) throw "[element] is null";
     47 		if(element.nodeType != 1) throw "[element] is not an element";
     48
     49 		var rng = this.rng();
     50 		if(entireElement) {
     51 			rng.selectNode(element);
     52 		} else {
     53 			rng.selectNodeContents(element);
     54 		}
     55 		this._setSelectionByRange(rng);
     56 	},
     57
     58 	deleteSelection: function() {
     59 		this.rng().deleteContents();
     60 	},
     61
     62 	collapseSelection: function(toStart) {
     63 		var rng = this.rng();
     64 		rng.collapse(toStart);
     65 		this._setSelectionByRange(rng);
     66 	},
     67
     68 	getSelectionAsHtml: function() {
     69 		var container = this.createElement("div");
     70 		var rng = this.rng();
     71 		var contents = this.rng().cloneContents();
     72 		if(contents) container.appendChild(contents);
     73 		return container.innerHTML;
     74 	},
     75
     76 	_setSelectionByRange: function(rng) {
     77 		var sel = this.sel();
     78 		sel.setBaseAndExtent(rng.startContainer, rng.startOffset, rng.endContainer, rng.endOffset);
     79 		this._anchorNode = sel.anchorNode;
     80 		this._anchorOffset = sel.anchorOffset;
     81 		this._focusNode = sel.focusNode;
     82 		this._focusOffset = sel.focusOffset;
     83 	}
     84 });