selection.js revision e57824e27f47a76dae7b14c6ff1c7ed1186d16c1
341N/A //TODO This shouldn't be there, Y.Node doesn't normalize getting textnode content. 341N/A //This causes IE to not allow a selection on a doubleclick 341N/A //rng.select(nodes[i]); 341N/A //This helps IE deal with a selection and nodeChange events 341N/A * Performs a prefilter on all nodes in the editor. Looks for nodes with a style: fontFamily or font face 341N/A * It then creates a dynamic class assigns it and removed the property. This is so that we don't lose 341N/A * the fontFamily when selecting nodes. 341N/A Y.
log(
'Filtering nodes',
'info',
'selection');
341N/A //This was causing issues in IE 341N/A //raw.style[FONT_FAMILY] = 'inherit'; 341N/A if (n.getStyle(FONT_FAMILY)) { 341N/A classNames['.' + n._yuid] = n.getStyle(FONT_FAMILY); 341N/A n.removeAttribute('face'); 341N/A n.setStyle(FONT_FAMILY, ''); 341N/A if (n.getAttribute('style') === '') { 341N/A n.removeAttribute('style'); 341N/A if (n.getAttribute('style').toLowerCase() === 'font-family: ') { 341N/A n.removeAttribute('style'); 341N/A //Had to move to inline style. writes for ie's < 8. They don't render el.setAttribute('style'); 341N/A Y.StyleSheet(cssString, 'editor'); 341N/A //Not sure about this one? 341N/A baseNodes.each(function(n, k) { 341N/A var t = n.get('tagName').toLowerCase(), 341N/A Y.Selection.prototype._swap(baseNodes.item(k), newTag); 341N/A ls.each(function(v, k) { 341N/A Y.Selection.filterBlocks(); 341N/A var endTime = (new Date()).getTime(); 341N/A Y.log('Filter Timer: ' + (endTime - startTime) + 'ms', 'info', 'selection'); 341N/A * Method attempts to replace all "orphined" text nodes in the main body by wrapping them with a <p>. Called from filter. 341N/A Y.Selection.filterBlocks = function() { 341N/A var startTime = (new Date()).getTime(); 341N/A Y.log('RAW filter blocks', 'info', 'selection'); 341N/A var childs = Y.config.doc.body.childNodes, i, node, wrapped = false, doit = true, 341N/A sel, single, br, divs, spans, c, s; for (i = 0; i < childs.length; i++) { if (!node.test(Y.Selection.BLOCKS)) { if (childs[i].nodeType == 3) { c = childs[i][textContent].match(Y.Selection.REG_CHAR); s = childs[i][textContent].match(Y.Selection.REG_NON); wrapped = Y.Selection._wrapBlock(wrapped); wrapped = Y.Selection._wrapBlock(wrapped); single = Y.all(Y.Selection.DEFAULT_BLOCK_TAG); if (single.size() === 1) { Y.log('Only One default block tag (' + Y.Selection.DEFAULT_BLOCK_TAG + '), focus it..', 'info', 'selection'); br = single.item(0).all('br'); if (!br.item(0).test('.yui-cursor')) { var html = single.item(0).get('innerHTML'); if (html === '' || html === ' ') { Y.log('Paragraph empty, focusing cursor', 'info', 'selection'); single.set('innerHTML', Y.Selection.CURSOR); sel.focusCursor(true, true); single.each(function(p) { var html = p.get('innerHTML'); Y.log('Empty Paragraph Tag Found, Removing It', 'info', 'selection'); if (d.hasClass('yui-non')) { var html = d.get('innerHTML'); Y.log('Empty DIV/P Tag Found, Removing It', 'info', 'selection'); Y.log('DIVS/PS Count: ' + d.get('childNodes').size(), 'info', 'selection'); if (d.get('childNodes').size() == 1) { Y.log('This Div/P only has one Child Node', 'info', 'selection'); Y.log('This Div/P is a child of a paragraph, remove it..', 'info', 'selection'); 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'); Y.log('Apple Spans found: ' + spans.size(), 'info', 'selection'); s.setAttribute('style', ''); var endTime = (new Date()).getTime(); Y.log('FilterBlocks Timer: ' + (endTime - startTime) + 'ms', 'info', 'selection'); * Regular Expression to determine if a string has a character in it Y.Selection.REG_CHAR = /[a-zA-Z-0-9_!@#\$%\^&*\(\)-=_+\[\]\\{}|;':",.\/<>\?]/
gi;
* Regular Expression to determine if a string has a non-character in it * Regular Expression to remove all HTML from a string * Wraps an array of elements in a Block level tag * Undoes what filter does enough to return the HTML from the Editor, then re-applies the filter. * @return {String} The filtered HTML Y.
log(
'UnFiltering nodes',
'info',
'selection');
if (!n.
hasClass(
'yui-skip') && n.
get(
'innerHTML') ===
'') {
n.setStyle(FONT_FAMILY, ''); if (n.getAttribute('style') === '') { n.removeAttribute('style'); * Resolve a node from the selection object and return a Node instance * @param {HTMLElement} n The HTMLElement to resolve. Might be a TextNode, gives parentNode. * @return {Node} The Resolved node //Adding a try/catch here because in rare occasions IE will //Throw a error accessing the parentNode of a stranded text node. //In the case of Ctrl+Z (Undo) * Returns the innerHTML of a node with all HTML tags removed. * @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 //Y.Selection.DEFAULT_BLOCK_TAG = 'div'; * The selector to use when looking for Nodes to cache the value of: [style],font[face] * The selector to use when looking for block level items. * The temporary fontname applied to a selection to retrieve their values: yui-tmp * The default tag to use when creating elements: span * The id of the outer cursor wrapper * The id used to wrap the inner space of the cursor position * The default HTML used to focus the cursor.. Y.
log(
'Has Cursor: ' +
cur.
size(),
'info',
'selection');
* Called from Editor keydown to remove the "extra" space before the cursor. //Y.log('Cleaning Cursor', 'info', 'Selection'); var cur,
sel =
'br.yui-cursor';
var c = b.
get(
'parentNode.parentNode.childNodes'),
html;
var cur = Y.all('#' + Y.Selection.CUR_WRAPID); var html = c.get('innerHTML'); if (html == ' ' || html == '<br>') { if (c.previous() || c.next()) { * Flag to show if the range is collapsed or not * A Node instance of the parentNode of the anchorNode of the range * The offset from the range object * A Node instance of the actual textNode of the range. * @property anchorTextNode * A Node instance of the parentNode of the focusNode of the range * The offset from the range object * A Node instance of the actual textNode of the range. * @property focusTextNode * Wrap an element, with another element * @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 * @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. * @return {NodeList} A NodeList of all items in the selection. if (s ===
'' || (s.
toLowerCase() ==
'font-family: ')) {
* Insert HTML at the current cursor position and return a Node instance of the newly inserted element. * @param {String} html The HTML to insert. * @return {Node} The inserted Node. * 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. * @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. Y.
on(
'available',
function() {
//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 (txt2 && txt2.get('length')) { * Get all elements inside a selection and wrap them with a new element and return a NodeList of all elements touched. * @param {String} tag The tag to wrap all selected items with. * @return {NodeList} A NodeList of all items in the selection. Y.
log(
'Wrapping selection with: ' +
tag,
'info',
'selection');
Y.
log(
'Returning NodeList with (' +
changed.
size() +
') item(s)' ,
'info',
'selection');
Y.
log(
'Can not wrap a collapsed selection, use insertContent',
'error',
'selection');
* 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. * @param {String} se The string to search for. * @param {String} re The string of HTML to replace it with. * @return {Node} The node inserted. Y.
log(
'replacing (' +
se +
') with (' +
re +
')');
* Wrapper for the different range creation methods. * Select a Node (hilighting it). * @param {Node} node The node to select * @param {Boolean} collapse Should the range be collapsed after insertion. default: false Y.
log(
'Node passed to selectNode is null',
'error',
'selection');
* Put a placeholder in the DOM at the current cursor position. * Get the placeholder in the DOM at the current cursor position. * Remove the cursor placeholder from the DOM. * @param {Boolean} keep Setting this to true will keep the node, but remove the unique parts that make it the cursor. cur.
set(
'innerHTML',
'<br class="yui-cursor">');
* Gets a stored cursor and focuses it for editing, must be called sometime after setCursor * Generic toString for logging. return 'Selection Object';