Element.cleanWhitespace not recursive?

12 views
Skip to first unread message

sclaflin

unread,
May 9, 2011, 9:12:43 AM5/9/11
to Prototype & script.aculo.us
I ran into this recently - I thought that in the past it had been
recursive.

But, the code I see looks like:

cleanWhitespace: function(element) {
element = $(element);
var node = element.firstChild;
while (node) {
var nextNode = node.nextSibling;
if (node.nodeType == 3 && !/\S/.test(node.nodeValue))
element.removeChild(node);
node = nextNode;
}
return element;
},

1. I don't see any reason for the element = $(element) line, other
than possibly for the return value;
it isn't used within the function
2. adding the marked line seems to make it work the way I want it to.

cleanWhitespace: function(element) {
//element = $(element);
var node = element.firstChild;
while (node) {
var nextNode = node.nextSibling;
if (node.nodeType == 3 && !/\S/.test(node.nodeValue))
element.removeChild(node);
if (node.nodeType == 1) Element.cleanWhitespace(node); //
Added -----------------!
node = nextNode;
}
return element;
},

Bertilo Wennergren

unread,
May 10, 2011, 3:47:47 AM5/10/11
to prototype-s...@googlegroups.com
On Mon, May 9, 2011 at 15:12, sclaflin <st...@steveclaflin.com> wrote:

> 1. I don't see any reason for the element = $(element) line, other
> than possibly for the return value;

It's probably there to cater for MSIE.

--
Bertilo Wennergren
bert...@gmail.com http://bertilow.com

T.J. Crowder

unread,
May 10, 2011, 7:22:23 AM5/10/11
to Prototype & script.aculo.us
On May 10, 8:47 am, Bertilo Wennergren <berti...@gmail.com> wrote:
> On Mon, May 9, 2011 at 15:12, sclaflin <st...@steveclaflin.com> wrote:
> > 1. I don't see any reason for the element = $(element) line, other
> > than possibly for the return value;
>
> It's probably there to cater for MSIE.
>
> --
> Bertilo Wennergren
> berti...@gmail.comhttp://bertilow.com

And because you can do `Element.cleanWhitespace("myElementID");`
--
T.J. Crowder
Independent Software Engineer
tj / crowder software / com
www / crowder software / com

sclaflin

unread,
May 10, 2011, 8:02:50 AM5/10/11
to Prototype & script.aculo.us
Ahhh, right. I didn't think of that because I had assumed there was
some sort of typeof element == 'string' test for that case.

Victor

unread,
Sep 2, 2011, 5:37:51 AM9/2/11
to prototype-s...@googlegroups.com
cleanWhitespace should not be recursive, at least for inline elements. E.g. for HTML fragment <b><i>the</i> <i>space</i></b> user will see output thespace (single word) instead of the space (two distinct space-separated words), which isn't correct.
Reply all
Reply to author
Forward
0 new messages