selection.js revision 76ca635d61eb3f9fb7c9d788a44fa8b1690aa138
/**
* @class Selection
* @constructor
* @module editor
* @submodule selection
*/
//TODO This shouldn't be there, Y.Node doesn't normalize getting textnode content.
var textContent = 'textContent',
INNER_HTML = 'innerHTML',
FONT_FAMILY = 'fontFamily';
textContent = 'nodeValue';
}
}
this._selection = sel;
if (this.isCollapsed) {
if (domEvent) {
}
if (!ieNode) {
//This causes IE to not allow a selection on a doubleclick
//rng.select(nodes[i]);
if (!ieNode) {
}
}
}
}
if (ieNode) {
if (ieNode.firstChild) {
}
if (ieNode.firstChild) {
}
}
}
moved = 0;
if (comp) {
//We are not at the beginning of the selection.
//Setting the move to something large, may need to increase it later
}
}
} else {
//This helps IE deal with a selection and nodeChange events
if (n && n.get('id')) {
} else if (n) {
n = n.get('childNodes');
}
}
}
//var self = this;
//debugger;
} else {
}
} else {
} else {
this.text = '';
}
}
};
/**
* Utility method to remove dead font-family styles from an element.
* @static
* @method removeFontFamily
*/
Y.Selection.removeFontFamily = function(n) {
n.removeAttribute('face');
if (s === '' || (s == 'font-family: ')) {
n.removeAttribute('style');
}
n.setAttribute('style', s);
}
};
/**
* Performs a prefilter on all nodes in the editor. Looks for nodes with a style: fontFamily or font face
* It then creates a dynamic class assigns it and removed the property. This is so that we don't lose
* the fontFamily when selecting nodes.
* @static
* @method filter
*/
ls;
var startTime1 = (new Date()).getTime();
}
/*
if (n.getStyle(FONT_FAMILY)) {
classNames['.' + n._yuid] = n.getStyle(FONT_FAMILY);
n.addClass(n._yuid);
n.removeAttribute('face');
n.setStyle(FONT_FAMILY, '');
if (n.getAttribute('style') === '') {
n.removeAttribute('style');
}
//This is for IE
if (n.getAttribute('style').toLowerCase() === 'font-family: ') {
n.removeAttribute('style');
}
}
*/
});
if (hr.parentNode) {
}
//Had to move to inline style. writes for ie's < 8. They don't render el.setAttribute('style');
s.border = '1px solid #ccc';
s.lineHeight = '0';
s.height = '0';
s.fontSize = '0';
s.marginTop = '5px';
s.marginBottom = '5px';
s.marginLeft = '0px';
s.marginRight = '0px';
s.padding = '0';
});
}
Y.each(classNames, function(v, k) {
});
Y.StyleSheet(cssString, 'editor');
//Not sure about this one?
baseNodes.each(function(n, k) {
var t = n.get('tagName').toLowerCase(),
newTag = 'i';
if (t === 'strong') {
newTag = 'b';
}
Y.Selection.prototype._swap(baseNodes.item(k), newTag);
});
ls = Y.all('ol,ul');
ls.each(function(v, k) {
var lis = v.all('li');
if (!lis.size()) {
v.remove();
}
});
if (blocks) {
Y.Selection.filterBlocks();
}
var endTime = (new Date()).getTime();
};
/**
* Method attempts to replace all "orphined" text nodes in the main body by wrapping them with a <p>. Called from filter.
* @static
* @method filterBlocks
*/
Y.Selection.filterBlocks = function() {
var startTime = (new Date()).getTime();
var childs = Y.config.doc.body.childNodes, i, node, wrapped = false, doit = true,
sel, single, br, divs, spans, c, s;
if (childs) {
for (i = 0; i < childs.length; i++) {
node = Y.one(childs[i]);
if (!node.test(Y.Selection.BLOCKS)) {
doit = true;
if (childs[i].nodeType == 3) {
c = childs[i][textContent].match(Y.Selection.REG_CHAR);
s = childs[i][textContent].match(Y.Selection.REG_NON);
if (c === null && s) {
doit = false;
}
}
if (doit) {
if (!wrapped) {
wrapped = [];
}
wrapped.push(childs[i]);
}
} else {
wrapped = Y.Selection._wrapBlock(wrapped);
}
}
wrapped = Y.Selection._wrapBlock(wrapped);
}
single = Y.all(Y.Selection.DEFAULT_BLOCK_TAG);
if (single.size() === 1) {
br = single.item(0).all('br');
if (br.size() === 1) {
if (!br.item(0).test('.yui-cursor')) {
br.item(0).remove();
}
var html = single.item(0).get('innerHTML');
if (html === '' || html === ' ') {
single.set('innerHTML', Y.Selection.CURSOR);
sel = new Y.Selection();
sel.focusCursor(true, true);
}
if (br.item(0).test('.yui-cursor') && Y.UA.ie) {
br.item(0).remove();
}
}
} else {
single.each(function(p) {
var html = p.get('innerHTML');
if (html === '') {
p.remove();
}
});
}
if (!Y.UA.ie) {
/*
divs = Y.all('div, p');
divs.each(function(d) {
if (d.hasClass('yui-non')) {
return;
}
var html = d.get('innerHTML');
if (html === '') {
d.remove();
} else {
if (d.get('childNodes').size() == 1) {
if (d.ancestor('p')) {
d.replace(d.get('firstChild'));
}
}
}
});*/
/** Removed this, as it was causing Pasting to be funky in Safari
spans = Y.all('.Apple-style-span, .apple-style-span');
spans.each(function(s) {
s.setAttribute('style', '');
});
*/
}
var endTime = (new Date()).getTime();
};
/**
* Regular Expression used to find dead font-family styles
* @static
* @property REG_FONTFAMILY
*/
Y.Selection.REG_FONTFAMILY = /font-family: ;/;
/**
* Regular Expression to determine if a string has a character in it
* @static
* @property REG_CHAR
*/
Y.Selection.REG_CHAR = /[a-zA-Z-0-9_!@#\$%\^&*\(\)-=_+\[\]\\{}|;':",.\/<>\?]/gi;
/**
* Regular Expression to determine if a string has a non-character in it
* @static
* @property REG_NON
*/
/**
* Regular Expression to remove all HTML from a string
* @static
* @property REG_NOHTML
*/
Y.Selection.REG_NOHTML = /<\S[^><]*>/g;
/**
* Wraps an array of elements in a Block level tag
* @static
* @private
* @method _wrapBlock
*/
if (wrapped) {
var newChild = Y.Node.create('<' + Y.Selection.DEFAULT_BLOCK_TAG + '></' + Y.Selection.DEFAULT_BLOCK_TAG + '>'),
}
}
return false;
};
/**
* Undoes what filter does enough to return the HTML from the Editor, then re-applies the filter.
* @static
* @method unfilter
* @return {String} The filtered HTML
*/
//One of ours
n.removeClass(n._yuid);
n.removeAttribute('class');
}
}
});
n.remove();
} else {
}
});
n.removeAttribute('id');
n.removeAttribute('_yuid');
}
});
if (body) {
}
/*
nodes.each(function(n) {
n.addClass(n._yuid);
n.setStyle(FONT_FAMILY, '');
if (n.getAttribute('style') === '') {
n.removeAttribute('style');
}
});
*/
return html;
};
/**
* Resolve a node from the selection object and return a Node instance
* @static
* @method resolve
* @param {HTMLElement} n The HTMLElement to resolve. Might be a TextNode, gives parentNode.
* @return {Node} The Resolved node
*/
if (n && n.nodeType === 3) {
//Throw a error accessing the parentNode of a stranded text node.
//In the case of Ctrl+Z (Undo)
try {
n = n.parentNode;
} catch (re) {
n = 'body';
}
}
return Y.one(n);
};
/**
* Returns the innerHTML of a node with all HTML tags removed.
* @static
* @method getText
* @param {Node} node The Node instance to remove the HTML from
* @return {String} The string of text
*/
//Clean out the cursor subs to see if the Node is empty
return txt;
};
//Y.Selection.DEFAULT_BLOCK_TAG = 'div';
/**
* The selector to use when looking for Nodes to cache the value of: [style],font[face]
* @static
* @property ALL
*/
/**
* The selector to use when looking for block level items.
* @static
* @property BLOCKS
*/
/**
* The temporary fontname applied to a selection to retrieve their values: yui-tmp
* @static
* @property TMP
*/
/**
* The default tag to use when creating elements: span
* @static
* @property DEFAULT_TAG
*/
/**
* The id of the outer cursor wrapper
* @static
* @property DEFAULT_TAG
*/
/**
* The id used to wrap the inner space of the cursor position
* @static
* @property CUR_WRAPID
*/
/**
* The default HTML used to focus the cursor..
* @static
* @property CURSOR
*/
};
/**
* Called from Editor keydown to remove the "extra" space before the cursor.
* @static
* @method cleanCursor
*/
Y.Selection.cleanCursor = function() {
if (c.size()) {
b.remove();
} else {
if (html !== '') {
b.remove();
}
}
});
}
/*
var cur = Y.all('#' + Y.Selection.CUR_WRAPID);
if (cur.size()) {
cur.each(function(c) {
var html = c.get('innerHTML');
if (html == ' ' || html == '<br>') {
if (c.previous() || c.next()) {
c.remove();
}
}
});
}
*/
};
/**
* Range text value
* @property text
* @type String
*/
text: null,
/**
* Flag to show if the range is collapsed or not
* @property isCollapsed
* @type Boolean
*/
isCollapsed: null,
/**
* A Node instance of the parentNode of the anchorNode of the range
* @property anchorNode
* @type Node
*/
anchorNode: null,
/**
* The offset from the range object
* @property anchorOffset
* @type Number
*/
anchorOffset: null,
/**
* A Node instance of the actual textNode of the range.
* @property anchorTextNode
* @type Node
*/
anchorTextNode: null,
/**
* A Node instance of the parentNode of the focusNode of the range
* @property focusNode
* @type Node
*/
focusNode: null,
/**
* The offset from the range object
* @property focusOffset
* @type Number
*/
focusOffset: null,
/**
* A Node instance of the actual textNode of the range.
* @property focusTextNode
* @type Node
*/
focusTextNode: null,
/**
* @property _selection
* @private
*/
_selection: null,
/**
* Wrap an element, with another element
* @private
* @method _wrap
* @param {HTMLElement} n The node to wrap
* @param {String} tag The tag to use when creating the new element.
* @return {HTMLElement} The wrapped node
*/
},
/**
* Swap an element, with another element
* @private
* @method _swap
* @param {HTMLElement} n The node to swap
* @param {String} tag The tag to use when creating the new element.
* @return {HTMLElement} The new node
*/
},
/**
* Get all the nodes in the current selection. This method will actually perform a filter first.
* Then it calls doc.execCommand('fontname', null, 'yui-tmp') to touch all nodes in the selection.
* The it compiles a list of all nodes affected by the execCommand and builds a NodeList to return.
* @method getSelected
* @return {NodeList} A NodeList of all items in the selection.
*/
getSelected: function() {
items = [];
Y.Selection.removeFontFamily(n);
if (!n.test('body')) {
}
}
});
},
/**
* Insert HTML at the current cursor position and return a Node instance of the newly inserted element.
* @method insertContent
* @param {String} html The HTML to insert.
* @return {Node} The inserted Node.
*/
insertContent: function(html) {
},
/**
* 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.
* @method insertAtCursor
* @param {String} html The HTML to insert.
* @param {Node} node The text node to break when inserting.
* @param {Number} offset The left offset of the text node to break and insert the new content.
* @param {Boolean} collapse Should the range be collapsed after insertion. default: false
* @return {Node} The inserted Node.
*/
var cur = Y.Node.create('<' + Y.Selection.DEFAULT_TAG + ' class="yui-non"></' + Y.Selection.DEFAULT_TAG + '>'),
node = b;
}
/**
* For some strange reason, range.pasteHTML fails if the node is a textNode and
* the offset is 0. (The cursor is at the beginning of the line)
* It will always insert the new content at position 1 instead of
* position 0. Here we test for that case and do it the hard way.
*/
if (range.moveToElementText) {
}
//Move the cursor after the new node
} else {
try {
} catch (e) {}
if (inHTML) {
if (range.moveToElementText) {
}
return newNode;
} else {
Y.on('available', function() {
if (range.moveToElementText) {
}
}, '#rte-insert');
}
}
} else {
//TODO using Y.Node.create here throws warnings & strips first white space character
//txt = Y.one(Y.Node.create(inHTML.substr(0, offset)));
//txt2 = Y.one(Y.Node.create(inHTML.substr(offset)));
if (offset > 0) {
newNode = b;
}
//if (txt2 && txt2.get('length')) {
if (txt2) {
}
} else {
}
} else {
} else {
}
}
}
}
}
return newNode;
},
/**
* Get all elements inside a selection and wrap them with a new element and return a NodeList of all elements touched.
* @method wrapContent
* @param {String} tag The tag to wrap all selected items with.
* @return {NodeList} A NodeList of all items in the selection.
*/
wrapContent: function(tag) {
if (!this.isCollapsed) {
var items = this.getSelected(),
if (t === 'font') {
} else {
}
}, this);
range = this.createRange();
if (this._selection.removeAllRanges) {
this._selection.removeAllRanges();
} else {
if (range.moveToElementText) {
range2 = this.createRange();
}
}
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.
*/
if (range.getBookmark) {
} else {
node = this.anchorTextNode;
}
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() {
} else {
}
},
/**
* 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}
*/
if (!node) {
return;
}
var range = this.createRange();
if (range.selectNode) {
this._selection.removeAllRanges();
if (collapse) {
try {
} catch (err) {
}
}
} else {
}
try {
} catch(e) {}
if (collapse) {
}
}
return this;
},
/**
* Put a placeholder in the DOM at the current cursor position.
* @method setCursor
* @return {Node}
*/
setCursor: function() {
this.removeCursor(false);
},
/**
* Get the placeholder in the DOM at the current cursor position.
* @method getCursor
* @return {Node}
*/
getCursor: function() {
},
/**
* 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) {
if (cur) {
if (keep) {
} else {
}
}
return cur;
},
/**
* Gets a stored cursor and focuses it for editing, must be called sometime after setCursor
* @method focusCursor
* @return {Node}
*/
if (collapse !== false) {
collapse = true;
}
if (end !== false) {
end = true;
}
var cur = this.removeCursor(true);
if (cur) {
}, this);
}
},
/**
* Generic toString for logging.
* @method toString
* @return {String}
*/
toString: function() {
return 'Selection Object';
}
};