Commit ffbedf02 authored by David Serduke's avatar David Serduke

Fixed #1039 and #1733 by going through the core API and making them text node...

Fixed #1039 and #1733 by going through the core API and making them text node and comment node safe.
parent 12ef255f
...@@ -251,13 +251,15 @@ jQuery.fn = jQuery.prototype = { ...@@ -251,13 +251,15 @@ jQuery.fn = jQuery.prototype = {
append: function() { append: function() {
return this.domManip(arguments, true, false, function(elem){ return this.domManip(arguments, true, false, function(elem){
this.appendChild( elem ); if (this.nodeType == 1)
this.appendChild( elem );
}); });
}, },
prepend: function() { prepend: function() {
return this.domManip(arguments, true, true, function(elem){ return this.domManip(arguments, true, true, function(elem){
this.insertBefore( elem, this.firstChild ); if (this.nodeType == 1)
this.insertBefore( elem, this.firstChild );
}); });
}, },
...@@ -402,6 +404,9 @@ jQuery.fn = jQuery.prototype = { ...@@ -402,6 +404,9 @@ jQuery.fn = jQuery.prototype = {
} else } else
return this.each(function(){ return this.each(function(){
if ( this.nodeType != 1 )
return;
if ( value.constructor == Array && /radio|checkbox/.test( this.type ) ) if ( value.constructor == Array && /radio|checkbox/.test( this.type ) )
this.checked = (jQuery.inArray(this.value, value) >= 0 || this.checked = (jQuery.inArray(this.value, value) >= 0 ||
jQuery.inArray(this.name, value) >= 0); jQuery.inArray(this.name, value) >= 0);
...@@ -722,18 +727,19 @@ jQuery.extend({ ...@@ -722,18 +727,19 @@ jQuery.extend({
// internal only, use addClass("class") // internal only, use addClass("class")
add: function( elem, classNames ) { add: function( elem, classNames ) {
jQuery.each((classNames || "").split(/\s+/), function(i, className){ jQuery.each((classNames || "").split(/\s+/), function(i, className){
if ( !jQuery.className.has( elem.className, className ) ) if ( elem.nodeType == 1 && !jQuery.className.has( elem.className, className ) )
elem.className += (elem.className ? " " : "") + className; elem.className += (elem.className ? " " : "") + className;
}); });
}, },
// internal only, use removeClass("class") // internal only, use removeClass("class")
remove: function( elem, classNames ) { remove: function( elem, classNames ) {
elem.className = classNames != undefined ? if (elem.nodeType == 1)
jQuery.grep(elem.className.split(/\s+/), function(className){ elem.className = classNames != undefined ?
return !jQuery.className.has( classNames, className ); jQuery.grep(elem.className.split(/\s+/), function(className){
}).join(" ") : return !jQuery.className.has( classNames, className );
""; }).join(" ") :
"";
}, },
// internal only, use is(".class") // internal only, use is(".class")
...@@ -1014,6 +1020,10 @@ jQuery.extend({ ...@@ -1014,6 +1020,10 @@ jQuery.extend({
}, },
attr: function( elem, name, value ) { attr: function( elem, name, value ) {
// don't set attributes on text and comment nodes
if (!elem || elem.nodeType == 3 || elem.nodeType == 8)
return undefined;
var fix = jQuery.isXMLDoc( elem ) ? var fix = jQuery.isXMLDoc( elem ) ?
{} : {} :
jQuery.props; jQuery.props;
...@@ -1267,7 +1277,8 @@ jQuery.each({ ...@@ -1267,7 +1277,8 @@ jQuery.each({
jQuery.each({ jQuery.each({
removeAttr: function( name ) { removeAttr: function( name ) {
jQuery.attr( this, name, "" ); jQuery.attr( this, name, "" );
this.removeAttribute( name ); if (this.nodeType == 1)
this.removeAttribute( name );
}, },
addClass: function( classNames ) { addClass: function( classNames ) {
......
...@@ -8,6 +8,9 @@ jQuery.event = { ...@@ -8,6 +8,9 @@ jQuery.event = {
// Bind an event to an element // Bind an event to an element
// Original by Dean Edwards // Original by Dean Edwards
add: function(element, type, handler, data) { add: function(element, type, handler, data) {
if ( element.nodeType == 3 || element.nodeType == 8 )
return;
// For whatever reason, IE has trouble passing the window object // For whatever reason, IE has trouble passing the window object
// around, causing it to be cloned in the process // around, causing it to be cloned in the process
if ( jQuery.browser.msie && element.setInterval != undefined ) if ( jQuery.browser.msie && element.setInterval != undefined )
...@@ -83,6 +86,10 @@ jQuery.event = { ...@@ -83,6 +86,10 @@ jQuery.event = {
// Detach an event or set of events from an element // Detach an event or set of events from an element
remove: function(element, type, handler) { remove: function(element, type, handler) {
// don't do events on text and comment nodes
if ( element.nodeType == 3 || element.nodeType == 8 )
return;
var events = jQuery.data(element, "events"), ret, index; var events = jQuery.data(element, "events"), ret, index;
// Namespaced event handlers // Namespaced event handlers
...@@ -147,6 +154,10 @@ jQuery.event = { ...@@ -147,6 +154,10 @@ jQuery.event = {
// Handle triggering a single element // Handle triggering a single element
} else { } else {
// don't do events on text and comment nodes
if ( element.nodeType == 3 || element.nodeType == 8 )
return;
var val, ret, fn = jQuery.isFunction( element[ type ] || null ), var val, ret, fn = jQuery.isFunction( element[ type ] || null ),
// Check to see if we need to provide a fake event, or not // Check to see if we need to provide a fake event, or not
event = !data[0] || !data[0].preventDefault; event = !data[0] || !data[0].preventDefault;
...@@ -273,7 +284,7 @@ jQuery.event = { ...@@ -273,7 +284,7 @@ jQuery.event = {
if ( event.pageX == null && event.clientX != null ) { if ( event.pageX == null && event.clientX != null ) {
var doc = document.documentElement, body = document.body; var doc = document.documentElement, body = document.body;
event.pageX = event.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc.clientLeft || 0); event.pageX = event.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc.clientLeft || 0);
event.pageY = event.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc.clientLeft || 0); event.pageY = event.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc.clientLeft || 0);
} }
// Add which for key events // Add which for key events
...@@ -437,7 +448,7 @@ function bindReady(){ ...@@ -437,7 +448,7 @@ function bindReady(){
// If Safari or IE is used // If Safari or IE is used
// Continually check to see if the document is ready // Continually check to see if the document is ready
if (jQuery.browser.msie || jQuery.browser.safari ) (function(){ if (jQuery.browser.msie || jQuery.browser.safari ) (function(){
try { try {
// If IE is used, use the trick by Diego Perini // If IE is used, use the trick by Diego Perini
// http://javascript.nwbox.com/IEContentLoaded/ // http://javascript.nwbox.com/IEContentLoaded/
......
...@@ -69,6 +69,9 @@ jQuery.fn.extend({ ...@@ -69,6 +69,9 @@ jQuery.fn.extend({
var optall = jQuery.speed(speed, easing, callback); var optall = jQuery.speed(speed, easing, callback);
return this[ optall.queue === false ? "each" : "queue" ](function(){ return this[ optall.queue === false ? "each" : "queue" ](function(){
if ( this.nodeType != 1)
return false;
var opt = jQuery.extend({}, optall); var opt = jQuery.extend({}, optall);
var hidden = jQuery(this).is(":hidden"), self = this; var hidden = jQuery(this).is(":hidden"), self = this;
...@@ -135,6 +138,9 @@ jQuery.fn.extend({ ...@@ -135,6 +138,9 @@ jQuery.fn.extend({
return queue( this[0], type ); return queue( this[0], type );
return this.each(function(){ return this.each(function(){
if ( this.nodeType != 1)
return;
if ( fn.constructor == Array ) if ( fn.constructor == Array )
queue(this, type, fn); queue(this, type, fn);
else { else {
......
...@@ -96,9 +96,9 @@ jQuery.extend({ ...@@ -96,9 +96,9 @@ jQuery.extend({
if ( typeof t != "string" ) if ( typeof t != "string" )
return [ t ]; return [ t ];
// Make sure that the context is a DOM Element // check to make sure context is a DOM element or a document
if ( context && !context.nodeType ) if ( context && context.nodeType != 1 && context.nodeType != 9)
context = null; return [ ];
// Set the correct context (if none is provided) // Set the correct context (if none is provided)
context = context || document; context = context || document;
......
...@@ -171,6 +171,7 @@ Z</textarea> ...@@ -171,6 +171,7 @@ Z</textarea>
<input type="checkbox" name="checkedtestcheckboxes" /> <input type="checkbox" name="checkedtestcheckboxes" />
</div> </div>
</form> </form>
<div id="nonnodes"><span>hi</span> there <!-- mon ami --></div>
</div> </div>
</div> </div>
</dl> </dl>
......
This diff is collapsed.
module("event"); module("event");
test("bind()", function() { test("bind()", function() {
expect(15); expect(16);
var handler = function(event) { var handler = function(event) {
ok( event.data, "bind() with data, check passed data exists" ); ok( event.data, "bind() with data, check passed data exists" );
...@@ -67,6 +67,11 @@ test("bind()", function() { ...@@ -67,6 +67,11 @@ test("bind()", function() {
// Trigger the remaining fn (1) // Trigger the remaining fn (1)
$("#firstp").trigger("click"); $("#firstp").trigger("click");
// using contents will get comments regular, text, and comment nodes
$("#nonnodes").contents().bind("tester", function () {
equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
}).trigger("tester");
}); });
test("click()", function() { test("click()", function() {
......
module("fx"); module("fx");
test("animate(Hash, Object, Function)", function() { test("animate(Hash, Object, Function)", function() {
expect(1); expect(3);
stop(); stop();
var hash = {opacity: 'show'}; var hash = {opacity: 'show'};
var hashCopy = $.extend({}, hash); var hashCopy = $.extend({}, hash);
$('#foo').animate(hash, 0, function() { $('#foo').animate(hash, 0, function() {
ok( hash.opacity == hashCopy.opacity, 'Check if animate changed the hash parameter' ); ok( hash.opacity == hashCopy.opacity, 'Check if animate changed the hash parameter' );
});
// using contents will get comments regular, text, and comment nodes
$("#nonnodes").contents().animate({paddingLeft:"5px"}, 100, function () {
equals(this.nodeType, 1, "Check node,textnode,comment animate just does real nodes" );
equals($(this).css("paddingLeft"), "5px", "Check node,textnode,comment animate just does real nodes" );
start(); start();
}); });
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment