selection.js revision 7fa32c729b6acad78e9f5f6e940984fc78f07a9b
0fdefaa9ca017edfb76b736c825b34186f33045aTripp * Wraps some common Selection/Range functionality into a simple object
0fdefaa9ca017edfb76b736c825b34186f33045aTripp * @module editor
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @submodule selection
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Wraps some common Selection/Range functionality into a simple object
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @class Selection
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @for Selection
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @constructor
a89ad754cce3cfc8aee71760e10217b54020360dTripp //TODO This shouldn't be there, Y.Node doesn't normalize getting textnode content.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp this.isCollapsed = (sel.compareEndPoints('StartToEnd', sel)) ? false : true;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp this.anchorNode = this.focusNode = Y.one(sel.parentElement());
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp ieNode = Y.config.doc.elementFromPoint(domEvent.clientX, domEvent.clientY);
a89ad754cce3cfc8aee71760e10217b54020360dTripp //This causes IE to not allow a selection on a doubleclick
a89ad754cce3cfc8aee71760e10217b54020360dTripp //rng.select(nodes[i]);
a89ad754cce3cfc8aee71760e10217b54020360dTripp this.anchorNode = this.focusNode = Y.Selection.resolve(ieNode);
a89ad754cce3cfc8aee71760e10217b54020360dTripp this.anchorOffset = this.focusOffset = (this.anchorNode.nodeValue) ? this.anchorNode.nodeValue.length : 0 ;
a89ad754cce3cfc8aee71760e10217b54020360dTripp this.anchorTextNode = this.focusTextNode = Y.one(ieNode);
a89ad754cce3cfc8aee71760e10217b54020360dTripp //var self = this;
7947db4b7d8682ea81598e3a4283e659a8103be6Tripp //debugger;
a89ad754cce3cfc8aee71760e10217b54020360dTripp this.anchorNode = Y.Selection.resolve(sel.anchorNode);
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 * @method filter
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp //This is for IE
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (raw.getAttribute('style').toLowerCase() === 'font-family: ') {
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');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp //This is for IE
c093c1aed867e18aa4778708592e1ceb45d18cffTripp if (n.getAttribute('style').toLowerCase() === 'font-family: ') {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp n.removeAttribute('style');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp el.setAttribute('style', 'border: 1px solid #ccc; line-height: 0; font-size: 0;margin-top: 5px; margin-bottom: 5px;');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp cssString += k + ' { font-family: ' + v.replace(/"/gi, '') + '; }';
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp Y.StyleSheet(cssString, 'editor');
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 Y.Selection.prototype._swap(baseNodes.item(k), newTag);
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 if (blocks) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp Y.Selection.filterBlocks();
c093c1aed867e18aa4778708592e1ceb45d18cffTripp var endTime = (new Date()).getTime();
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Method attempts to replace all "orphined" text nodes in the main body by wrapping them with a <p>. Called from filter.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @method filterBlocks
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 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 if (doit) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (!wrapped) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp wrapped = [];
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp wrapped.push(childs[i]);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp wrapped = Y.Selection._wrapBlock(wrapped);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp wrapped = Y.Selection._wrapBlock(wrapped);
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);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp single.each(function(p) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp var html = p.get('innerHTML');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (html === '') {
cec703a844d9691646231634fe709f4ea41d278aTripp p.remove();
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 if (d.get('childNodes').size() == 1) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (d.ancestor('p')) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp d.replace(d.get('firstChild'));
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp spans = Y.all('.Apple-style-span, .apple-style-span');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp spans.each(function(s) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp s.setAttribute('style', '');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp var endTime = (new Date()).getTime();
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Regular Expression to determine if a string has a character in it
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property REG_CHAR
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp Y.Selection.REG_CHAR = /[a-zA-Z-0-9_]/gi;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Regular Expression to determine if a string has a non-character in it
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @property REG_NON
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp Y.Selection.REG_NON = /[\s\S|\n|\t]/gi;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Wraps an array of elements in a Block level tag
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @method _wrapBlock
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 for (i = 1; i < wrapped.length; i++) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp newChild.append(wrapped[i]);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp firstChild.replace(newChild);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp newChild.prepend(firstChild);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp return false;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Undoes what filter does enough to return the HTML from the Editor, then re-applies the filter.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @method unfilter
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @return {String} The filtered HTML
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp Y.Selection.unfilter = function() {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp var nodes = Y.all('body [class]'),
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp html = '', nons, ids;
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');
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp nons = Y.all('.yui-non');
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp nons.each(function(n) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (n.get('innerHTML') === '') {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp n.remove();
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp n.removeClass('yui-non');
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');
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp html = Y.one('body').get('innerHTML');
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 return html;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Resolve a node from the selection object and return a Node instance
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @method resolve
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @param {HTMLElement} n The HTMLElement to resolve. Might be a TextNode, gives parentNode.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @return {Node} The Resolved node
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp Y.Selection.resolve = function(n) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (n && n.nodeType === 3) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp n = n.parentNode;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp return Y.one(n);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Returns the innerHTML of a node with all HTML tags removed.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @method getText
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @param {Node} node The Node instance to remove the HTML from
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @return {String} The string of text
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) {
a89ad754cce3cfc8aee71760e10217b54020360dTripp * The selector to use when looking for Nodes to cache the value of: [style],font[face]
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @property ALL
a89ad754cce3cfc8aee71760e10217b54020360dTripp Y.Selection.ALL = '[style],font[face]';
a89ad754cce3cfc8aee71760e10217b54020360dTripp * RegExp used to strip HTML tags from a string
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @property STRIP_HTML
a89ad754cce3cfc8aee71760e10217b54020360dTripp Y.Selection.STRIP_HTML = /<\S[^><]*>/g;
a89ad754cce3cfc8aee71760e10217b54020360dTripp * The selector to use when looking for block level items.
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @property BLOCKS
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp Y.Selection.BLOCKS = 'p,div,ul,ol,table,style';
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * The temporary fontname applied to a selection to retrieve their values: yui-tmp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property TMP
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp Y.Selection.TMP = 'yui-tmp';
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * The default tag to use when creating elements: span
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property DEFAULT_TAG
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp Y.Selection.DEFAULT_TAG = 'span';
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * The id of the outer cursor wrapper
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property DEFAULT_TAG
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp Y.Selection.CURID = 'yui-cursor';
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * The id used to wrap the inner space of the cursor position
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @property CUR_WRAPID
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp Y.Selection.CUR_WRAPID = 'yui-cursor-wrapper';
e188eaea1f0e60d808be8a4f6ccdbbd3c9b5039bTripp * The default HTML used to focus the cursor..
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property CURSOR
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp Y.Selection.CURSOR = '<span id="' + Y.Selection.CURID + '"><span id="' + Y.Selection.CUR_WRAPID + '"> </span></span>';
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Called from Editor keydown to remove the "extra" space before the cursor.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method cleanCursor
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp Y.Selection.cleanCursor = function() {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp var cur = Y.config.doc.getElementById(Y.Selection.CUR_WRAPID);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp cur.id = '';
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (cur.innerHTML == ' ' || cur.innerHTML == '<br>') {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (cur.parentNode) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp cur.parentNode.removeChild(cur);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp var cur = Y.one('#' + Y.Selection.CUR_WRAPID);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (cur && cur.get('innerHTML') == ' ') {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp cur.remove();
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp Y.Selection.prototype = {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Range text value
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @property text
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @type String
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp text: null,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Flag to show if the range is collapsed or not
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property isCollapsed
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @type Boolean
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp isCollapsed: null,
a89ad754cce3cfc8aee71760e10217b54020360dTripp * A Node instance of the parentNode of the anchorNode of the range
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property anchorNode
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @type Node
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp anchorNode: null,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * The offset from the range object
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property anchorOffset
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @type Number
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp anchorOffset: null,
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * A Node instance of the actual textNode of the range.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @property anchorTextNode
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @type Node
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp anchorTextNode: null,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * A Node instance of the parentNode of the focusNode of the range
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property focusNode
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @type Node
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp focusNode: null,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * The offset from the range object
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property focusOffset
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @type Number
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp focusOffset: null,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * A Node instance of the actual textNode of the range.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property focusTextNode
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @type Node
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp focusTextNode: null,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * The actual Selection/Range object
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property _selection
b79c07ef87dd1a48a03fc33a91c37d04f3addae2Tripp _selection: null,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Wrap an element, with another element
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 _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);
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * Swap an element, with another element
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 _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 * 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 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 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');
9eaaa502227248d304ac9170902697d02158c1d9Tripp if (!n.test('body')) {
9eaaa502227248d304ac9170902697d02158c1d9Tripp items.push(Y.Node.getDOMNode(nodes.item(k)));
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp return Y.all(items);
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 insertContent: function(html) {
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp return this.insertAtCursor(html, this.anchorTextNode, this.anchorOffset, true);
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.
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 if (node && node.test('body')) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp b = Y.Node.create('<span></span>');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp node.append(b);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if (range.pasteHTML) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp newNode = Y.Node.create(html);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp } catch (e) {}
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp inHTML = Y.one('#rte-insert');
e1d9c39d1eb1062e5c99cf8994b981636d8841ccTripp if (inHTML) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp inHTML.set('id', '');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp inHTML.replace(newNode);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp return newNode;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp Y.on('available', function() {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp inHTML.set('id', '');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp inHTML.replace(newNode);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp }, '#rte-insert');
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 txt = Y.one(Y.config.doc.createTextNode(inHTML.substr(0, offset)));
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp txt2 = Y.one(Y.config.doc.createTextNode(inHTML.substr(offset)));
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;
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 if (node.get('nodeType') === 3) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp node = node.get('parentNode');
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp newNode = Y.Node.create(html);
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp node.insert(newNode, 'before');
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp return newNode;
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.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp wrapContent: function(tag) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp tag = (tag) ? tag : Y.Selection.DEFAULT_TAG;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (!this.isCollapsed) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp var items = this.getSelected(),
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp changed = [], range, last, first, range2;
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));
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp changed.push(this._wrap(items.item(k), tag));
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);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp range.moveToElementText(Y.Node.getDOMNode(first));
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp range2 = this.createRange();
if (Y.config.doc.selection) {
return Y.config.doc.selection.createRange();
return Y.config.doc.createRange();