Skip to content

Instantly share code, notes, and snippets.

@curtisharvey
Created August 13, 2010 08:33
Show Gist options
  • Save curtisharvey/522546 to your computer and use it in GitHub Desktop.
Save curtisharvey/522546 to your computer and use it in GitHub Desktop.
diff --git a/src/editor/js/selection.js b/src/editor/js/selection.js
index a046f0a..448cbf8 100644
--- a/src/editor/js/selection.js
+++ b/src/editor/js/selection.js
@@ -143,34 +143,20 @@
* @method filterBlocks
*/
Y.Selection.filterBlocks = function() {
- var childs = Y.config.doc.body.childNodes, i, node, wrapped = false, doit = true,
- sel, single, br, divs, spans, c, s;
+ var childs = Y.all(Y.config.doc.body.childNodes), wrapped = [],
+ sel, single, br, divs, spans;
- if (childs) {
- for (i = 0; i < childs.length; i++) {
- node = Y.one(childs[i]);
+ childs.each(function(node) {
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]);
+ if (node.get('nodeType') === 3 && /\S/.test(node.get('text'))) {
+ wrapped.push(node);
}
} else {
- wrapped = Y.Selection._wrapBlock(wrapped);
- }
- }
- wrapped = Y.Selection._wrapBlock(wrapped);
+ Y.Selection._wrapBlock(wrapped);
}
+ });
+ Y.Selection._wrapBlock(wrapped);
+
single = Y.all('p');
if (single.size() === 1) {
Y.log('Only One Paragragh, focus it..', 'info', 'selection');
@@ -219,38 +205,23 @@
};
/**
- * 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
- */
- Y.Selection.REG_NON = /[\s\S|\n|\t]/gi;
-
-
- /**
* Wraps an array of elements in a Block level tag
* @static
* @private
* @method _wrapBlock
+ * @param to_wrap {Array} an array of nodes to wrap
+ * @return {void} and to_wrap is emptied
*/
- Y.Selection._wrapBlock = function(wrapped) {
- if (wrapped) {
- var newChild = Y.Node.create('<p></p>'),
- firstChild = Y.one(wrapped[0]), i;
-
- for (i = 1; i < wrapped.length; i++) {
- newChild.append(wrapped[i]);
+ Y.Selection._wrapBlock = function(to_wrap) {
+ if (to_wrap && to_wrap.length) {
+ var wrapper = Y.Node.create('<p></p>'),
+ placeholder = Y.one(to_wrap.shift());
+ while (to_wrap.length) {
+ wrapper.append(to_wrap.shift());
}
- firstChild.replace(newChild);
- newChild.prepend(firstChild);
+ placeholder.replace(wrapper);
+ wrapper.prepend(placeholder);
}
- return false;
};
/**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment