D
dhtml
Just took a peek at the jQuery forums. One of latest posts:
http://forum.jquery.com/topic/jquery-and-textnodes
| I've recently stumbled upon a weird issue - for a given ajax
| response I would create HTML element and insert it into DOM,
| however weirdly, my first textNode would be dropped. So I did
| the test on newer version of jQuery, and here's mine
| observation:
| [...]
| $(document).ready(function() {
| console.log($("A<div>B</div>C<div>D</div>E"));
| });
| Expected result:
|
| [<TextNode textContent="A">, div, <TextNode textContent="C">,
| div, <TextNode textContent="E">]
|
| Received result:
|
| [div, <TextNode textContent="C">, div]
The last text node is also dropped. This is the result of the method
overloading approach and the `quickExpr` regex. Both of which still
persist to this day.
// A simple way to check for HTML strings or ID strings
// (both of which we optimize for)
quickExpr = /^(?:[^<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/,
The first part of that expression, `[^<]*` says "0 or more characters
that aren't `<`". So in essence, they deliberately drop text nodes.
We've covered the problems with method overloading in numerous
threads). All of the proposed solutions recommend strange hacks to get
around this design.
$("<jQfix/>"+response+"<jQfix/>").filter(":not(jQfix)");
Another comment tries to justify jQuery's behavior:
"... jQuery in my opinion was very smart by simply removing those in
such cases."
Another poster states:
| instead of doing:
|
| $(myhtml).appendTo(targetDiv);
|
|
| do this:
|
| $(targetDiv).append(myhtml);
And realizing that jQuery has two different ways of doing essentially
the same thing, and they they work differently. The second approach
goes through jQuery's long `domManip` function and then
`buildFragment`.
I've also told on many occasions that complicated methods lead to
trouble.
Function `buildFragment` goes through `clean`, which tries to either
parse the HTML or use innerHTML and then attempting to normalize some
of that, also calling `findInputs` which despite its name, returns
undefined. It begins like so:
| jQuery.buildFragment = function( args, nodes, scripts ) {
| var fragment, cacheable, cacheresults, doc;
|
|
| // nodes may contain either an explicit document object,
| // a jQuery collection or context object.
| // If nodes[0] contains a valid object to assign to doc
There is more method overloading in a newly introduced function.
Method `buildFragment` is new. So they're adding new functionality and
relying on the same old mistakes. How do you describe the psychology
that goes into that thinking? I'd call it insanity. And look just a
bit down in `buildFragment`, they realize getting bit by problems of
method overloading:
| // Ensure that an attr object doesn't incorrectly stand in as
| // a document object
| // Chrome and Firefox seem to allow this to occur and will
| // throw exception
| // Fixes #8950
| if ( !doc.createDocumentFragment ) {
| doc = document;
| }
What that comment means by "attr object" -- and it is horribly
misleading -- is that if an object is passed in to create attributes,
as in `jQuery('<input type="hidden" />', { 'class': 'test' });` that
the initial call to jQuery will end up calling `buildFragment` and
then passing it `{ 'class': 'test' }` as attributes and since
`{ 'class': 'test' }` doesn't have an owner document, the whole thing
falls apart. Now why would that happen, you might wonder. Sure, it is
complicated and if anyone got lost reading that, it is understandable;
the strategy and source code are overly complicated.
jQuery is presented as a script that will help simplify browser
scripting and smooth over the differences between browsers. Instead,
it adds a lot of unneeded complexity that has, does, and will continue
to cause problems. It is the tool of choice for the the uninformed.
Instead, if one learns javascript and browsers then one can reasonably
conclude that:
1) jQuery isn't needed
2) jQuery adds complexity that causes problems
Creating something whose complexity exceeds that of the original
problem will undoubtedly create more problems than is projected to
solve. And that is exactly what jQuery does. It is a shortcut to
failure.
If the goal is to build high-quality web apps, then instead of using
jQuery, learn the DOM, learn how browsers work, and learn to program
using concise, well-named methods.
Cornford on overloading:
http://groups.google.com/group/comp...4127e8/def848b5f197f41cQ#msg_9a67fd2073ce031f
Myself on J Resig's "banning" comp.lang.javascript:
groups.google.com/group/comp.lang.javascript/msg/71999734c4116c52
COrnford, on attitude towards learning:
http://groups.google.com/group/comp...290dd1b/ac8801bfec8e670f#msg_6ca46c969b8d899a
http://forum.jquery.com/topic/jquery-and-textnodes
| I've recently stumbled upon a weird issue - for a given ajax
| response I would create HTML element and insert it into DOM,
| however weirdly, my first textNode would be dropped. So I did
| the test on newer version of jQuery, and here's mine
| observation:
| [...]
| $(document).ready(function() {
| console.log($("A<div>B</div>C<div>D</div>E"));
| });
| Expected result:
|
| [<TextNode textContent="A">, div, <TextNode textContent="C">,
| div, <TextNode textContent="E">]
|
| Received result:
|
| [div, <TextNode textContent="C">, div]
The last text node is also dropped. This is the result of the method
overloading approach and the `quickExpr` regex. Both of which still
persist to this day.
// A simple way to check for HTML strings or ID strings
// (both of which we optimize for)
quickExpr = /^(?:[^<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/,
The first part of that expression, `[^<]*` says "0 or more characters
that aren't `<`". So in essence, they deliberately drop text nodes.
We've covered the problems with method overloading in numerous
threads). All of the proposed solutions recommend strange hacks to get
around this design.
$("<jQfix/>"+response+"<jQfix/>").filter(":not(jQfix)");
Another comment tries to justify jQuery's behavior:
"... jQuery in my opinion was very smart by simply removing those in
such cases."
Another poster states:
| instead of doing:
|
| $(myhtml).appendTo(targetDiv);
|
|
| do this:
|
| $(targetDiv).append(myhtml);
And realizing that jQuery has two different ways of doing essentially
the same thing, and they they work differently. The second approach
goes through jQuery's long `domManip` function and then
`buildFragment`.
I've also told on many occasions that complicated methods lead to
trouble.
Function `buildFragment` goes through `clean`, which tries to either
parse the HTML or use innerHTML and then attempting to normalize some
of that, also calling `findInputs` which despite its name, returns
undefined. It begins like so:
| jQuery.buildFragment = function( args, nodes, scripts ) {
| var fragment, cacheable, cacheresults, doc;
|
|
| // nodes may contain either an explicit document object,
| // a jQuery collection or context object.
| // If nodes[0] contains a valid object to assign to doc
There is more method overloading in a newly introduced function.
Method `buildFragment` is new. So they're adding new functionality and
relying on the same old mistakes. How do you describe the psychology
that goes into that thinking? I'd call it insanity. And look just a
bit down in `buildFragment`, they realize getting bit by problems of
method overloading:
| // Ensure that an attr object doesn't incorrectly stand in as
| // a document object
| // Chrome and Firefox seem to allow this to occur and will
| // throw exception
| // Fixes #8950
| if ( !doc.createDocumentFragment ) {
| doc = document;
| }
What that comment means by "attr object" -- and it is horribly
misleading -- is that if an object is passed in to create attributes,
as in `jQuery('<input type="hidden" />', { 'class': 'test' });` that
the initial call to jQuery will end up calling `buildFragment` and
then passing it `{ 'class': 'test' }` as attributes and since
`{ 'class': 'test' }` doesn't have an owner document, the whole thing
falls apart. Now why would that happen, you might wonder. Sure, it is
complicated and if anyone got lost reading that, it is understandable;
the strategy and source code are overly complicated.
jQuery is presented as a script that will help simplify browser
scripting and smooth over the differences between browsers. Instead,
it adds a lot of unneeded complexity that has, does, and will continue
to cause problems. It is the tool of choice for the the uninformed.
Instead, if one learns javascript and browsers then one can reasonably
conclude that:
1) jQuery isn't needed
2) jQuery adds complexity that causes problems
Creating something whose complexity exceeds that of the original
problem will undoubtedly create more problems than is projected to
solve. And that is exactly what jQuery does. It is a shortcut to
failure.
If the goal is to build high-quality web apps, then instead of using
jQuery, learn the DOM, learn how browsers work, and learn to program
using concise, well-named methods.
Cornford on overloading:
http://groups.google.com/group/comp...4127e8/def848b5f197f41cQ#msg_9a67fd2073ce031f
Myself on J Resig's "banning" comp.lang.javascript:
groups.google.com/group/comp.lang.javascript/msg/71999734c4116c52
COrnford, on attitude towards learning:
http://groups.google.com/group/comp...290dd1b/ac8801bfec8e670f#msg_6ca46c969b8d899a