selection.js revision 7fa32c729b6acad78e9f5f6e940984fc78f07a9b
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTrippYUI.add('selection', function(Y) {
0fdefaa9ca017edfb76b736c825b34186f33045aTripp
0fdefaa9ca017edfb76b736c825b34186f33045aTripp /**
0fdefaa9ca017edfb76b736c825b34186f33045aTripp * Wraps some common Selection/Range functionality into a simple object
0fdefaa9ca017edfb76b736c825b34186f33045aTripp * @module editor
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @submodule selection
a75ebc38c1de401b679953a9b87bd323f0f48d02Tripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp /**
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Wraps some common Selection/Range functionality into a simple object
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @class Selection
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @for Selection
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @constructor
a89ad754cce3cfc8aee71760e10217b54020360dTripp */
a89ad754cce3cfc8aee71760e10217b54020360dTripp
a89ad754cce3cfc8aee71760e10217b54020360dTripp //TODO This shouldn't be there, Y.Node doesn't normalize getting textnode content.
a89ad754cce3cfc8aee71760e10217b54020360dTripp var textContent = 'textContent',
a89ad754cce3cfc8aee71760e10217b54020360dTripp INNER_HTML = 'innerHTML',
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp FONT_FAMILY = 'fontFamily';
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp if (Y.UA.ie) {
c093c1aed867e18aa4778708592e1ceb45d18cffTripp textContent = 'nodeValue';
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp }
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp
a89ad754cce3cfc8aee71760e10217b54020360dTripp Y.Selection = function(domEvent) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp var sel, par, ieNode, nodes, rng, i;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (Y.config.win.getSelection) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp sel = Y.config.win.getSelection();
c093c1aed867e18aa4778708592e1ceb45d18cffTripp } else if (Y.config.doc.selection) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp sel = Y.config.doc.selection.createRange();
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp this._selection = sel;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (sel.pasteHTML) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp this.isCollapsed = (sel.compareEndPoints('StartToEnd', sel)) ? false : true;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (this.isCollapsed) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp this.anchorNode = this.focusNode = Y.one(sel.parentElement());
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (domEvent) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp ieNode = Y.config.doc.elementFromPoint(domEvent.clientX, domEvent.clientY);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (!ieNode) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp par = sel.parentElement();
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp nodes = par.childNodes;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp rng = sel.duplicate();
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
a89ad754cce3cfc8aee71760e10217b54020360dTripp for (i = 0; i < nodes.length; i++) {
a89ad754cce3cfc8aee71760e10217b54020360dTripp //This causes IE to not allow a selection on a doubleclick
a89ad754cce3cfc8aee71760e10217b54020360dTripp //rng.select(nodes[i]);
a89ad754cce3cfc8aee71760e10217b54020360dTripp if (rng.inRange(sel)) {
c093c1aed867e18aa4778708592e1ceb45d18cffTripp ieNode = nodes[i];
a89ad754cce3cfc8aee71760e10217b54020360dTripp }
a89ad754cce3cfc8aee71760e10217b54020360dTripp }
a89ad754cce3cfc8aee71760e10217b54020360dTripp }
a89ad754cce3cfc8aee71760e10217b54020360dTripp
a89ad754cce3cfc8aee71760e10217b54020360dTripp this.ieNode = ieNode;
a89ad754cce3cfc8aee71760e10217b54020360dTripp
a89ad754cce3cfc8aee71760e10217b54020360dTripp if (ieNode) {
a89ad754cce3cfc8aee71760e10217b54020360dTripp if (ieNode.nodeType !== 3) {
a89ad754cce3cfc8aee71760e10217b54020360dTripp if (ieNode.firstChild) {
a89ad754cce3cfc8aee71760e10217b54020360dTripp ieNode = ieNode.firstChild;
a89ad754cce3cfc8aee71760e10217b54020360dTripp }
a89ad754cce3cfc8aee71760e10217b54020360dTripp }
a89ad754cce3cfc8aee71760e10217b54020360dTripp this.anchorNode = this.focusNode = Y.Selection.resolve(ieNode);
a89ad754cce3cfc8aee71760e10217b54020360dTripp
a89ad754cce3cfc8aee71760e10217b54020360dTripp this.anchorOffset = this.focusOffset = (this.anchorNode.nodeValue) ? this.anchorNode.nodeValue.length : 0 ;
c093c1aed867e18aa4778708592e1ceb45d18cffTripp
a89ad754cce3cfc8aee71760e10217b54020360dTripp this.anchorTextNode = this.focusTextNode = Y.one(ieNode);
a89ad754cce3cfc8aee71760e10217b54020360dTripp }
7947db4b7d8682ea81598e3a4283e659a8103be6Tripp
a89ad754cce3cfc8aee71760e10217b54020360dTripp
a89ad754cce3cfc8aee71760e10217b54020360dTripp }
a89ad754cce3cfc8aee71760e10217b54020360dTripp
a89ad754cce3cfc8aee71760e10217b54020360dTripp //var self = this;
7947db4b7d8682ea81598e3a4283e659a8103be6Tripp //debugger;
a89ad754cce3cfc8aee71760e10217b54020360dTripp } else {
a89ad754cce3cfc8aee71760e10217b54020360dTripp this.isCollapsed = sel.isCollapsed;
a89ad754cce3cfc8aee71760e10217b54020360dTripp this.anchorNode = Y.Selection.resolve(sel.anchorNode);
a89ad754cce3cfc8aee71760e10217b54020360dTripp this.focusNode = Y.Selection.resolve(sel.focusNode);
a89ad754cce3cfc8aee71760e10217b54020360dTripp this.anchorOffset = sel.anchorOffset;
a89ad754cce3cfc8aee71760e10217b54020360dTripp this.focusOffset = sel.focusOffset;
c093c1aed867e18aa4778708592e1ceb45d18cffTripp
a89ad754cce3cfc8aee71760e10217b54020360dTripp this.anchorTextNode = Y.one(sel.anchorNode);
a89ad754cce3cfc8aee71760e10217b54020360dTripp this.focusTextNode = Y.one(sel.focusNode);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
a89ad754cce3cfc8aee71760e10217b54020360dTripp if (Y.Lang.isString(sel.text)) {
a89ad754cce3cfc8aee71760e10217b54020360dTripp this.text = sel.text;
a89ad754cce3cfc8aee71760e10217b54020360dTripp } else {
a89ad754cce3cfc8aee71760e10217b54020360dTripp if (sel.toString) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp this.text = sel.toString();
a89ad754cce3cfc8aee71760e10217b54020360dTripp } else {
a89ad754cce3cfc8aee71760e10217b54020360dTripp this.text = '';
a89ad754cce3cfc8aee71760e10217b54020360dTripp }
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp }
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp };
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp
c093c1aed867e18aa4778708592e1ceb45d18cffTripp /**
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * Performs a prefilter on all nodes in the editor. Looks for nodes with a style: fontFamily or font face
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * It then creates a dynamic class assigns it and removed the property. This is so that we don't lose
a89ad754cce3cfc8aee71760e10217b54020360dTripp * the fontFamily when selecting nodes.
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @static
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @method filter
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp */
a89ad754cce3cfc8aee71760e10217b54020360dTripp Y.Selection.filter = function(blocks) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp var startTime = (new Date()).getTime();
a89ad754cce3cfc8aee71760e10217b54020360dTripp
a89ad754cce3cfc8aee71760e10217b54020360dTripp var nodes = Y.all(Y.Selection.ALL),
a89ad754cce3cfc8aee71760e10217b54020360dTripp baseNodes = Y.all('strong,em'),
a89ad754cce3cfc8aee71760e10217b54020360dTripp doc = Y.config.doc,
a89ad754cce3cfc8aee71760e10217b54020360dTripp hrs = doc.getElementsByTagName('hr'),
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp classNames = {}, cssString = '',
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp ls;
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp
c093c1aed867e18aa4778708592e1ceb45d18cffTripp console.log(hrs);
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp var startTime1 = (new Date()).getTime();
a89ad754cce3cfc8aee71760e10217b54020360dTripp nodes.each(function(n) {
a89ad754cce3cfc8aee71760e10217b54020360dTripp var raw = Y.Node.getDOMNode(n);
a89ad754cce3cfc8aee71760e10217b54020360dTripp if (raw.style[FONT_FAMILY]) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp classNames['.' + n._yuid] = raw.style[FONT_FAMILY];
a89ad754cce3cfc8aee71760e10217b54020360dTripp n.addClass(n._yuid);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp raw.style[FONT_FAMILY] = '';
a89ad754cce3cfc8aee71760e10217b54020360dTripp raw.removeAttribute('face');
a89ad754cce3cfc8aee71760e10217b54020360dTripp if (raw.getAttribute('style') === '') {
a89ad754cce3cfc8aee71760e10217b54020360dTripp raw.removeAttribute('style');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp //This is for IE
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (raw.getAttribute('style')) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (raw.getAttribute('style').toLowerCase() === 'font-family: ') {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp raw.removeAttribute('style');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c093c1aed867e18aa4778708592e1ceb45d18cffTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp /*
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (n.getStyle(FONT_FAMILY)) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp classNames['.' + n._yuid] = n.getStyle(FONT_FAMILY);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp n.addClass(n._yuid);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp n.removeAttribute('face');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp n.setStyle(FONT_FAMILY, '');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (n.getAttribute('style') === '') {
bf801d6851ecf7ed14742ef3639a077daecb5cf8Tripp n.removeAttribute('style');
a4d2446149b07f9e5c32947091dcbcf4d2eee765Tripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp //This is for IE
c093c1aed867e18aa4778708592e1ceb45d18cffTripp if (n.getAttribute('style').toLowerCase() === 'font-family: ') {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp n.removeAttribute('style');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp */
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp });
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp var endTime1 = (new Date()).getTime();
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp Y.each(hrs, function(hr) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp var el = doc.createElement('div');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp el.setAttribute('style', 'border: 1px solid #ccc; line-height: 0; font-size: 0;margin-top: 5px; margin-bottom: 5px;');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp el.setAttribute('readonly', true);
c09beb6ef319616edc832f6f6d137350d74096e0Tripp if (hr.parentNode) {
c09beb6ef319616edc832f6f6d137350d74096e0Tripp hr.parentNode.replaceChild(el, hr);
c09beb6ef319616edc832f6f6d137350d74096e0Tripp }
c09beb6ef319616edc832f6f6d137350d74096e0Tripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp });
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp Y.each(classNames, function(v, k) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp cssString += k + ' { font-family: ' + v.replace(/"/gi, '') + '; }';
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp });
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp Y.StyleSheet(cssString, 'editor');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
c093c1aed867e18aa4778708592e1ceb45d18cffTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp //Not sure about this one?
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp baseNodes.each(function(n, k) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp var t = n.get('tagName').toLowerCase(),
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp newTag = 'i';
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (t === 'strong') {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp newTag = 'b';
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp Y.Selection.prototype._swap(baseNodes.item(k), newTag);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp });
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp //Filter out all the empty UL/OL's
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp ls = Y.all('ol,ul');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp ls.each(function(v, k) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp var lis = v.all('li');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (!lis.size()) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp v.remove();
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp });
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (blocks) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp Y.Selection.filterBlocks();
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c093c1aed867e18aa4778708592e1ceb45d18cffTripp var endTime = (new Date()).getTime();
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp };
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp /**
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Method attempts to replace all "orphined" text nodes in the main body by wrapping them with a <p>. Called from filter.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @static
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @method filterBlocks
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp */
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp Y.Selection.filterBlocks = function() {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp var startTime = (new Date()).getTime();
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp var childs = Y.config.doc.body.childNodes, i, node, wrapped = false, doit = true,
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp sel, single, br, divs, spans, c, s;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (childs) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp for (i = 0; i < childs.length; i++) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp node = Y.one(childs[i]);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (!node.test(Y.Selection.BLOCKS)) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp doit = true;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (childs[i].nodeType == 3) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp c = childs[i][textContent].match(Y.Selection.REG_CHAR);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp s = childs[i][textContent].match(Y.Selection.REG_NON);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (c === null && s) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp doit = false;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
c093c1aed867e18aa4778708592e1ceb45d18cffTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (doit) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (!wrapped) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp wrapped = [];
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp wrapped.push(childs[i]);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp } else {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp wrapped = Y.Selection._wrapBlock(wrapped);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp wrapped = Y.Selection._wrapBlock(wrapped);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp single = Y.all('p');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (single.size() === 1) {
a89ad754cce3cfc8aee71760e10217b54020360dTripp br = single.item(0).all('br');
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if (br.size() === 1) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp br.item(0).remove();
6c2524a3f545f556615ff052e6676c5adb53c5d5Tripp var html = single.item(0).get('innerHTML');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (html == '' || html == ' ') {
cec703a844d9691646231634fe709f4ea41d278aTripp single.set('innerHTML', Y.Selection.CURSOR);
cec703a844d9691646231634fe709f4ea41d278aTripp sel = new Y.Selection();
cec703a844d9691646231634fe709f4ea41d278aTripp sel.focusCursor(true, true);
cec703a844d9691646231634fe709f4ea41d278aTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp } else {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp single.each(function(p) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp var html = p.get('innerHTML');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (html === '') {
cec703a844d9691646231634fe709f4ea41d278aTripp p.remove();
cec703a844d9691646231634fe709f4ea41d278aTripp }
cec703a844d9691646231634fe709f4ea41d278aTripp });
cec703a844d9691646231634fe709f4ea41d278aTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (!Y.UA.ie) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp divs = Y.all('div, p');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp divs.each(function(d) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp var html = d.get('innerHTML');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (html === '') {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp d.remove();
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp } else {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (d.get('childNodes').size() == 1) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (d.ancestor('p')) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp d.replace(d.get('firstChild'));
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp });
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp spans = Y.all('.Apple-style-span, .apple-style-span');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp spans.each(function(s) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp s.setAttribute('style', '');
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp });
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp var endTime = (new Date()).getTime();
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp };
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp /**
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Regular Expression to determine if a string has a character in it
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @static
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property REG_CHAR
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp Y.Selection.REG_CHAR = /[a-zA-Z-0-9_]/gi;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp /**
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Regular Expression to determine if a string has a non-character in it
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @static
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @property REG_NON
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp */
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp Y.Selection.REG_NON = /[\s\S|\n|\t]/gi;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp /**
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Wraps an array of elements in a Block level tag
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @static
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @private
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @method _wrapBlock
a89ad754cce3cfc8aee71760e10217b54020360dTripp */
a89ad754cce3cfc8aee71760e10217b54020360dTripp Y.Selection._wrapBlock = function(wrapped) {
a89ad754cce3cfc8aee71760e10217b54020360dTripp if (wrapped) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp var newChild = Y.Node.create('<p></p>'),
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp firstChild = Y.one(wrapped[0]), i;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp for (i = 1; i < wrapped.length; i++) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp newChild.append(wrapped[i]);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp firstChild.replace(newChild);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp newChild.prepend(firstChild);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp return false;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp };
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp /**
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Undoes what filter does enough to return the HTML from the Editor, then re-applies the filter.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @static
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @method unfilter
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @return {String} The filtered HTML
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp */
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp Y.Selection.unfilter = function() {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp var nodes = Y.all('body [class]'),
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp html = '', nons, ids;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp nodes.each(function(n) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (n.hasClass(n._yuid)) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp //One of ours
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp n.setStyle(FONT_FAMILY, n.getStyle(FONT_FAMILY));
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp n.removeClass(n._yuid);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (n.getAttribute('class') === '') {
117557654069e56f5003be755819b76fe0f77107Tripp n.removeAttribute('class');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp }
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp });
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp nons = Y.all('.yui-non');
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp nons.each(function(n) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (n.get('innerHTML') === '') {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp n.remove();
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp } else {
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp n.removeClass('yui-non');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp });
6c4ec9d420df654d019b936fd06bef6f769db4cbTripp
6c4ec9d420df654d019b936fd06bef6f769db4cbTripp ids = Y.all('body [id]');
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp ids.each(function(n) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (n.get('id').indexOf('yui_3_') === 0) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp n.removeAttribute('id');
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp n.removeAttribute('_yuid');
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp });
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp html = Y.one('body').get('innerHTML');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp nodes.each(function(n) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp n.addClass(n._yuid);
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp n.setStyle(FONT_FAMILY, '');
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if (n.getAttribute('style') === '') {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp n.removeAttribute('style');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp });
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp return html;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp };
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp /**
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Resolve a node from the selection object and return a Node instance
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @static
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @method resolve
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @param {HTMLElement} n The HTMLElement to resolve. Might be a TextNode, gives parentNode.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @return {Node} The Resolved node
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp */
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp Y.Selection.resolve = function(n) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (n && n.nodeType === 3) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp n = n.parentNode;
66ca16dd76367c074fe4df1dcf7b555489a9bf85Tripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp return Y.one(n);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp };
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp /**
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Returns the innerHTML of a node with all HTML tags removed.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @static
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @method getText
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @param {Node} node The Node instance to remove the HTML from
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @return {String} The string of text
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp */
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp Y.Selection.getText = function(node) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp var t = node.get('innerHTML').replace(Y.Selection.STRIP_HTML, ''),
117557654069e56f5003be755819b76fe0f77107Tripp c = t.match(Y.Selection.REG_CHAR),
117557654069e56f5003be755819b76fe0f77107Tripp s = t.match(Y.Selection.REG_NON);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (c === null && s) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp t = '';
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp return t;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp };
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
a89ad754cce3cfc8aee71760e10217b54020360dTripp /**
a89ad754cce3cfc8aee71760e10217b54020360dTripp * The selector to use when looking for Nodes to cache the value of: [style],font[face]
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @static
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @property ALL
a89ad754cce3cfc8aee71760e10217b54020360dTripp */
a89ad754cce3cfc8aee71760e10217b54020360dTripp Y.Selection.ALL = '[style],font[face]';
a89ad754cce3cfc8aee71760e10217b54020360dTripp
a89ad754cce3cfc8aee71760e10217b54020360dTripp /**
a89ad754cce3cfc8aee71760e10217b54020360dTripp * RegExp used to strip HTML tags from a string
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @static
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @property STRIP_HTML
a89ad754cce3cfc8aee71760e10217b54020360dTripp */
a89ad754cce3cfc8aee71760e10217b54020360dTripp Y.Selection.STRIP_HTML = /<\S[^><]*>/g;
a89ad754cce3cfc8aee71760e10217b54020360dTripp
a89ad754cce3cfc8aee71760e10217b54020360dTripp /**
a89ad754cce3cfc8aee71760e10217b54020360dTripp * The selector to use when looking for block level items.
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @static
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @property BLOCKS
a89ad754cce3cfc8aee71760e10217b54020360dTripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp Y.Selection.BLOCKS = 'p,div,ul,ol,table,style';
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp /**
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * The temporary fontname applied to a selection to retrieve their values: yui-tmp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @static
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property TMP
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp Y.Selection.TMP = 'yui-tmp';
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp /**
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * The default tag to use when creating elements: span
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @static
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property DEFAULT_TAG
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp Y.Selection.DEFAULT_TAG = 'span';
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp /**
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * The id of the outer cursor wrapper
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @static
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property DEFAULT_TAG
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp Y.Selection.CURID = 'yui-cursor';
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp /**
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * The id used to wrap the inner space of the cursor position
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @static
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @property CUR_WRAPID
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp Y.Selection.CUR_WRAPID = 'yui-cursor-wrapper';
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp /**
e188eaea1f0e60d808be8a4f6ccdbbd3c9b5039bTripp * The default HTML used to focus the cursor..
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @static
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property CURSOR
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp Y.Selection.CURSOR = '<span id="' + Y.Selection.CURID + '"><span id="' + Y.Selection.CUR_WRAPID + '">&nbsp;</span></span>';
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp /**
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Called from Editor keydown to remove the "extra" space before the cursor.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @static
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method cleanCursor
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp Y.Selection.cleanCursor = function() {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp var cur = Y.config.doc.getElementById(Y.Selection.CUR_WRAPID);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (cur) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp cur.id = '';
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (cur.innerHTML == '&nbsp;' || cur.innerHTML == '<br>') {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (cur.parentNode) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp cur.parentNode.removeChild(cur);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp /*
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp var cur = Y.one('#' + Y.Selection.CUR_WRAPID);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (cur && cur.get('innerHTML') == '&nbsp;') {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp cur.remove();
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp };
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp Y.Selection.prototype = {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp /**
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Range text value
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @property text
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @type String
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp text: null,
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp /**
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Flag to show if the range is collapsed or not
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property isCollapsed
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @type Boolean
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp */
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp isCollapsed: null,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp /**
a89ad754cce3cfc8aee71760e10217b54020360dTripp * A Node instance of the parentNode of the anchorNode of the range
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property anchorNode
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @type Node
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp anchorNode: null,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp /**
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * The offset from the range object
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property anchorOffset
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @type Number
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp anchorOffset: null,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp /**
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * A Node instance of the actual textNode of the range.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @property anchorTextNode
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @type Node
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp anchorTextNode: null,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp /**
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * A Node instance of the parentNode of the focusNode of the range
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property focusNode
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @type Node
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp focusNode: null,
b79c07ef87dd1a48a03fc33a91c37d04f3addae2Tripp /**
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * The offset from the range object
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property focusOffset
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @type Number
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp focusOffset: null,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp /**
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * A Node instance of the actual textNode of the range.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property focusTextNode
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @type Node
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp focusTextNode: null,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp /**
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * The actual Selection/Range object
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property _selection
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @private
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
b79c07ef87dd1a48a03fc33a91c37d04f3addae2Tripp _selection: null,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp /**
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Wrap an element, with another element
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @private
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method _wrap
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @param {HTMLElement} n The node to wrap
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @param {String} tag The tag to use when creating the new element.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @return {HTMLElement} The wrapped node
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp _wrap: function(n, tag) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp var tmp = Y.Node.create('<' + tag + '></' + tag + '>');
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp tmp.set(INNER_HTML, n.get(INNER_HTML));
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp n.set(INNER_HTML, '');
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp n.append(tmp);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp return Y.Node.getDOMNode(tmp);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp },
6c4ec9d420df654d019b936fd06bef6f769db4cbTripp /**
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * Swap an element, with another element
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * @private
cec703a844d9691646231634fe709f4ea41d278aTripp * @method _swap
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * @param {HTMLElement} n The node to swap
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * @param {String} tag The tag to use when creating the new element.
6c4ec9d420df654d019b936fd06bef6f769db4cbTripp * @return {HTMLElement} The new node
6c4ec9d420df654d019b936fd06bef6f769db4cbTripp */
6c4ec9d420df654d019b936fd06bef6f769db4cbTripp _swap: function(n, tag) {
6c4ec9d420df654d019b936fd06bef6f769db4cbTripp var tmp = Y.Node.create('<' + tag + '></' + tag + '>');
6c4ec9d420df654d019b936fd06bef6f769db4cbTripp tmp.set(INNER_HTML, n.get(INNER_HTML));
6c4ec9d420df654d019b936fd06bef6f769db4cbTripp n.replace(tmp, n);
6c4ec9d420df654d019b936fd06bef6f769db4cbTripp return Y.Node.getDOMNode(tmp);
6c4ec9d420df654d019b936fd06bef6f769db4cbTripp },
6c4ec9d420df654d019b936fd06bef6f769db4cbTripp /**
6c4ec9d420df654d019b936fd06bef6f769db4cbTripp * Get all the nodes in the current selection. This method will actually perform a filter first.
6c4ec9d420df654d019b936fd06bef6f769db4cbTripp * Then it calls doc.execCommand('fontname', null, 'yui-tmp') to touch all nodes in the selection.
6c4ec9d420df654d019b936fd06bef6f769db4cbTripp * The it compiles a list of all nodes affected by the execCommand and builds a NodeList to return.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method getSelected
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * @return {NodeList} A NodeList of all items in the selection.
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp */
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp getSelected: function() {
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp Y.Selection.filter();
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp Y.config.doc.execCommand('fontname', null, Y.Selection.TMP);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp var nodes = Y.all(Y.Selection.ALL),
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp items = [];
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp nodes.each(function(n, k) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if (n.getStyle(FONT_FAMILY, Y.Selection.TMP)) {
bbd1285cbb2183b7f89010412ad97ae1680b4b5eTripp n.setStyle(FONT_FAMILY, '');
bbd1285cbb2183b7f89010412ad97ae1680b4b5eTripp n.removeAttribute('face');
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if (n.getAttribute('style') === '') {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp n.removeAttribute('style');
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp }
9eaaa502227248d304ac9170902697d02158c1d9Tripp if (!n.test('body')) {
9eaaa502227248d304ac9170902697d02158c1d9Tripp items.push(Y.Node.getDOMNode(nodes.item(k)));
9eaaa502227248d304ac9170902697d02158c1d9Tripp }
9eaaa502227248d304ac9170902697d02158c1d9Tripp }
9eaaa502227248d304ac9170902697d02158c1d9Tripp });
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp return Y.all(items);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp },
4ef2f7e4cb7c7d255be077c47d542199f7bf8607Tripp /**
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Insert HTML at the current cursor position and return a Node instance of the newly inserted element.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @method insertContent
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @param {String} html The HTML to insert.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @return {Node} The inserted Node.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp */
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp insertContent: function(html) {
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp return this.insertAtCursor(html, this.anchorTextNode, this.anchorOffset, true);
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp },
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp /**
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * Insert HTML at the current cursor position, this method gives you control over the text node to insert into and the offset where to put it.
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * @method insertAtCursor
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * @param {String} html The HTML to insert.
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * @param {Node} node The text node to break when inserting.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @param {Number} offset The left offset of the text node to break and insert the new content.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @param {Boolean} collapse Should the range be collapsed after insertion. default: false
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @return {Node} The inserted Node.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp */
b79c07ef87dd1a48a03fc33a91c37d04f3addae2Tripp insertAtCursor: function(html, node, offset, collapse) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp var cur = Y.Node.create('<' + Y.Selection.DEFAULT_TAG + ' class="yui-non"></' + Y.Selection.DEFAULT_TAG + '>'),
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp inHTML, txt, txt2, newNode, range = this.createRange(), b;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if (node && node.test('body')) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp b = Y.Node.create('<span></span>');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp node.append(b);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp node = b;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
0fdefaa9ca017edfb76b736c825b34186f33045aTripp
0fdefaa9ca017edfb76b736c825b34186f33045aTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if (range.pasteHTML) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp newNode = Y.Node.create(html);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp try {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp range.pasteHTML('<span id="rte-insert"></span>');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp } catch (e) {}
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp inHTML = Y.one('#rte-insert');
e1d9c39d1eb1062e5c99cf8994b981636d8841ccTripp if (inHTML) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp inHTML.set('id', '');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp inHTML.replace(newNode);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp return newNode;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp } else {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp Y.on('available', function() {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp inHTML.set('id', '');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp inHTML.replace(newNode);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }, '#rte-insert');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp } else {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp //TODO using Y.Node.create here throws warnings & strips first white space character
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp //txt = Y.one(Y.Node.create(inHTML.substr(0, offset)));
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp //txt2 = Y.one(Y.Node.create(inHTML.substr(offset)));
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (offset > 0) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp inHTML = node.get(textContent);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp txt = Y.one(Y.config.doc.createTextNode(inHTML.substr(0, offset)));
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp txt2 = Y.one(Y.config.doc.createTextNode(inHTML.substr(offset)));
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp node.replace(txt, node);
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp newNode = Y.Node.create(html);
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp if (newNode.get('nodeType') === 11) {
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp b = Y.Node.create('<span></span>');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp b.append(newNode);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp newNode = b;
e1d9c39d1eb1062e5c99cf8994b981636d8841ccTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp txt.insert(newNode, 'after');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp //if (txt2 && txt2.get('length')) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (txt2) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp newNode.insert(cur, 'after');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp cur.insert(txt2, 'after');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp this.selectNode(cur, collapse);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp } else {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (node.get('nodeType') === 3) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp node = node.get('parentNode');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp newNode = Y.Node.create(html);
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp node.insert(newNode, 'before');
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp }
e0caea9528bfbb244d27129aa9dea5aebc07fc18Tripp }
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp return newNode;
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp },
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp /**
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Get all elements inside a selection and wrap them with a new element and return a NodeList of all elements touched.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method wrapContent
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @param {String} tag The tag to wrap all selected items with.
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @return {NodeList} A NodeList of all items in the selection.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp wrapContent: function(tag) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp tag = (tag) ? tag : Y.Selection.DEFAULT_TAG;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (!this.isCollapsed) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp var items = this.getSelected(),
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp changed = [], range, last, first, range2;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp items.each(function(n, k) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp var t = n.get('tagName').toLowerCase();
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (t === 'font') {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp changed.push(this._swap(items.item(k), tag));
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp } else {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp changed.push(this._wrap(items.item(k), tag));
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }, this);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp range = this.createRange();
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp first = changed[0];
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp last = changed[changed.length - 1];
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (this._selection.removeAllRanges) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp range.setStart(changed[0], 0);
7947db4b7d8682ea81598e3a4283e659a8103be6Tripp range.setEnd(last, last.childNodes.length);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp this._selection.removeAllRanges();
a89ad754cce3cfc8aee71760e10217b54020360dTripp this._selection.addRange(range);
a89ad754cce3cfc8aee71760e10217b54020360dTripp } else {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp range.moveToElementText(Y.Node.getDOMNode(first));
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp range2 = this.createRange();
range2.moveToElementText(Y.Node.getDOMNode(last));
range.setEndPoint('EndToEnd', range2);
range.select();
}
changed = Y.all(changed);
return changed;
} else {
return Y.all([]);
}
},
/**
* Find and replace a string inside a text node and replace it with HTML focusing the node after
* to allow you to continue to type.
* @method replace
* @param {String} se The string to search for.
* @param {String} re The string of HTML to replace it with.
* @return {Node} The node inserted.
*/
replace: function(se,re) {
var range = this.createRange(), node, txt, index, newNode;
if (range.getBookmark) {
index = range.getBookmark();
txt = this.anchorNode.get('innerHTML').replace(se, re);
this.anchorNode.set('innerHTML', txt);
range.moveToBookmark(index);
newNode = Y.one(range.parentElement());
} else {
node = this.anchorTextNode;
txt = node.get(textContent);
index = txt.indexOf(se);
txt = txt.replace(se, '');
node.set(textContent, txt);
newNode = this.insertAtCursor(re, node, index, true);
}
return newNode;
},
/**
* Destroy the range.
* @method remove
* @chainable
* @return {Y.Selection}
*/
remove: function() {
this._selection.removeAllRanges();
return this;
},
/**
* Wrapper for the different range creation methods.
* @method createRange
* @return {RangeObject}
*/
createRange: function() {
if (Y.config.doc.selection) {
return Y.config.doc.selection.createRange();
} else {
return Y.config.doc.createRange();
}
},
/**
* Select a Node (hilighting it).
* @method selectNode
* @param {Node} node The node to select
* @param {Boolean} collapse Should the range be collapsed after insertion. default: false
* @chainable
* @return {Y.Selection}
*/
selectNode: function(node, collapse, end) {
end = end || 0;
node = Y.Node.getDOMNode(node);
var range = this.createRange();
if (range.selectNode) {
range.selectNode(node);
this._selection.removeAllRanges();
this._selection.addRange(range);
if (collapse) {
try {
this._selection.collapse(node, end);
} catch (err) {
this._selection.collapse(node, 0);
}
}
} else {
if (node.nodeType === 3) {
node = node.parentNode;
}
try {
range.moveToElementText(node);
} catch(e) {}
if (collapse) {
range.collapse(((end) ? false : true));
}
range.select();
}
return this;
},
/**
* Put a placeholder in the DOM at the current cursor position.
* @method setCursor
* @return {Node}
*/
setCursor: function() {
return this.insertContent(Y.Selection.CURSOR);
},
/**
* Get the placeholder in the DOM at the current cursor position.
* @method getCursor
* @return {Node}
*/
getCursor: function() {
return Y.one('#' + Y.Selection.CURID);
},
/**
* Remove the cursor placeholder from the DOM.
* @method removeCursor
* @param {Boolean} keep Setting this to true will keep the node, but remove the unique parts that make it the cursor.
* @return {Node}
*/
removeCursor: function(keep) {
var cur = this.getCursor();
if (cur) {
if (keep) {
cur.removeAttribute('id');
cur.set('innerHTML', '<span id="' + Y.Selection.CUR_WRAPID + '">&nbsp;</span>');
} else {
cur.remove();
}
}
return cur;
},
/**
* Gets a stored cursor and focuses it for editing, must be called sometime after setCursor
* @method focusCursor
* @return {Node}
*/
focusCursor: function(collapse, end) {
if (collapse !== false) {
collapse = true;
}
if (end !== false) {
end = true;
}
var cur = this.removeCursor(true);
if (cur) {
this.selectNode(cur, collapse, end);
}
},
/**
* Generic toString for logging.
* @method toString
* @return {String}
*/
toString: function() {
return 'Selection Object';
}
};
}, '@VERSION@' ,{requires:['node'], skinnable:false});