Skip to content

Commit bf9fd17

Browse files
committed
fixed bug where jquery would crash if a text node is appended to a <table> adamwulf#134
1 parent 46e402e commit bf9fd17

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

src/jquery.columnizer.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@
4848
options.width = defaults.width;
4949
}
5050
}
51+
52+
/**
53+
* appending a text node to a <table> will
54+
* cause a jquery crash.
55+
* so wrap all append() calls and revert to
56+
* a simple appendChild() in case it fails
57+
*/
58+
function appendSafe($target, $elem){
59+
try{
60+
$target.append($elem);
61+
}catch(e){
62+
$target[0].appendChild($elem[0]);
63+
}
64+
}
5165

5266
return this.each(function() {
5367
var $inBox = options.target ? $(options.target) : $(this);
@@ -64,7 +78,7 @@
6478

6579
var adjustment = 0;
6680

67-
$cache.append($(this).contents().clone(true));
81+
appendSafe($cache, $(this).contents().clone(true));
6882

6983
// images loading after dom load
7084
// can screw up the column heights,
@@ -78,7 +92,7 @@
7892
var func = function($inBox,$cache){ return function(){
7993
if(!$inBox.data("firstImageLoaded")){
8094
$inBox.data("firstImageLoaded", "true");
81-
$inBox.empty().append($cache.children().clone(true));
95+
appendSafe($inBox.empty(), $cache.children().clone(true));
8296
$inBox.columnize(options);
8397
}
8498
};
@@ -149,7 +163,7 @@
149163
// our column is on a column break, so just end here
150164
return;
151165
}
152-
$putInHere.append(node);
166+
appendSafe($putInHere, $(node));
153167
}
154168
if($putInHere[0].childNodes.length === 0) return;
155169

@@ -184,7 +198,7 @@
184198
columnText = oText;
185199
}
186200
latestTextNode = document.createTextNode(columnText);
187-
$putInHere.append(latestTextNode);
201+
appendSafe($putInHere, $(latestTextNode));
188202

189203
if(oText.length > counter2 && indexOfSpace != -1){
190204
oText = oText.substring(indexOfSpace);
@@ -207,7 +221,7 @@
207221
if($pullOutHere.contents().length){
208222
$pullOutHere.prepend($item);
209223
}else{
210-
$pullOutHere.append($item);
224+
appendSafe($pullOutHere, $item);
211225
}
212226

213227
return $item[0].nodeType == 3;
@@ -244,14 +258,14 @@
244258
//
245259
// ok, we have a columnbreak, so add it into
246260
// the column and exit
247-
$putInHere.append($clone);
261+
appendSafe($putInHere, $clone);
248262
$cloneMe.remove();
249263
}else if (manualBreaks){
250264
// keep adding until we hit a manual break
251-
$putInHere.append($clone);
265+
appendSafe($putInHere, $clone);
252266
$cloneMe.remove();
253267
}else if($clone.get(0).nodeType == 1 && !$clone.hasClass(prefixTheClassName("dontend"))){
254-
$putInHere.append($clone);
268+
appendSafe($putInHere, $clone);
255269
if($clone.is("img") && $parentColumn.height() < targetHeight + 20){
256270
//
257271
// we can't split an img in half, so just add it
@@ -351,7 +365,7 @@
351365
overflow.innerHTML = html;
352366

353367
}else{
354-
$col.append($destroyable.contents());
368+
appendSafe($col, $destroyable.contents());
355369
}
356370
$inBox.data("columnizing", false);
357371

@@ -414,7 +428,7 @@
414428
$inBox.empty();
415429
$inBox.append($("<div style='width:" + (Math.floor(100 / numCols))+ "%; float: " + options.columnFloat + ";'></div>")); //"
416430
$col = $inBox.children(":last");
417-
$col.append($cache.clone());
431+
appendSafe($col, $cache.clone());
418432
maxHeight = $col.height();
419433
$inBox.empty();
420434

0 commit comments

Comments
 (0)