Skip to content

Commit 72ba05a

Browse files
committed
fixed infinite loop situation / added test
1 parent ea37a65 commit 72ba05a

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

jquery.textWrap.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ function TextWrapper(element, settings) {
249249
if (makeLonger == undefined) makeLonger = pxOverlap < 0; // set makelonger if it hasn't been set
250250
guessLength = guessText.length;
251251
}
252-
while ((makeLonger && pxOverlap < 0 && guessText.length < wholeText.length) // repeat until edge found
253-
|| (!makeLonger && pxOverlap >= 0 && guessText.length > 0));
252+
while ((makeLonger && pxOverlap < 0 && guessText.trim().length < wholeText.trim().length) // repeat until edge found
253+
|| (!makeLonger && pxOverlap >= 0 && guessText.trim().length > 0));
254254

255255
if (makeLonger && pxOverlap >= 0) { // Drop back a breakpoint if longer
256256
guessText = prevText;

tests/jquery.textWrap.spec.js

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ describe("textWrap", function () {
6161
expect(model.staticWidthStack.length).toEqual(0);
6262
});
6363

64-
//this._tryAddBreak = tryAddBreak;
6564
it('tryAddBreak should break before and item too large to break, unless it is the beginning of a line', function () {
6665
var model = new TextWrapper.prototype.TextWrapperModel(item);
6766
var item = document.createElement('SPAN');
@@ -288,7 +287,7 @@ describe("textWrap", function () {
288287
expect(model.lastBreakIndex).toEqual(0);
289288
});
290289

291-
it('startSplitTextWith a short guess no possible break', function () {
290+
it('startSplitText With a short guess no possible break', function () {
292291
var item = document.createElement('SPAN');
293292
document.body.insertBefore(item, document.body.firstChild);
294293
var settings = new TextWrapper.prototype.TextWrapperSettings();
@@ -306,7 +305,26 @@ describe("textWrap", function () {
306305
document.body.removeChild(document.body.firstChild);
307306
});
308307

309-
it('startSplitTextWith a long guess no possible break', function () {
308+
it('startSplitText With a long guess and only possible break is leading space', function () {
309+
var item = document.createElement('SPAN');
310+
document.body.insertBefore(item, document.body.firstChild);
311+
var settings = new TextWrapper.prototype.TextWrapperSettings();
312+
settings.maxLines = 1;
313+
item.innerHTML = ' asdf';
314+
var model = new TextWrapper.prototype.TextWrapperModel(item, settings);
315+
model.children = w._getFlattenedChildren(item);
316+
model.splitIndex = 0;
317+
model.splitNode = model.children[model.splitIndex];
318+
model.splitNode.bounds = w._createRangeBounds(model.splitNode.node);
319+
model.elementBounds = new w._elementBounds(item, '1px');
320+
var result = w._startSplitText(model, ' asdf', 3);
321+
expect(item.firstChild.data).toEqual(' asdf');
322+
expect(result).toEqual(false);
323+
expect(model.splitIndex).toEqual(0);
324+
document.body.removeChild(document.body.firstChild);
325+
});
326+
327+
it('startSplitText With a long guess no possible break', function () {
310328
var item = document.createElement('SPAN');
311329
document.body.insertBefore(item, document.body.firstChild);
312330
var settings = new TextWrapper.prototype.TextWrapperSettings();
@@ -324,7 +342,7 @@ describe("textWrap", function () {
324342
document.body.removeChild(document.body.firstChild);
325343
});
326344

327-
it('startSplitTextWith a long guess and forcebreak on space default behavior', function () {
345+
it('startSplitText With a long guess and forcebreak on space default behavior', function () {
328346
var item = document.createElement('SPAN');
329347
document.body.insertBefore(item, document.body.firstChild);
330348
var settings = new TextWrapper.prototype.TextWrapperSettings();
@@ -345,7 +363,7 @@ describe("textWrap", function () {
345363
document.body.removeChild(document.body.firstChild);
346364
});
347365

348-
it('startSplitTextWith a short guess and forcebreak on space default behavior', function () {
366+
it('startSplitText With a short guess and forcebreak on space default behavior', function () {
349367
var item = document.createElement('SPAN');
350368
document.body.insertBefore(item, document.body.firstChild);
351369
var settings = new TextWrapper.prototype.TextWrapperSettings();
@@ -366,7 +384,7 @@ describe("textWrap", function () {
366384
document.body.removeChild(document.body.firstChild);
367385
});
368386

369-
it('startSplitTextWith a very long guess and break on space default behavior', function () {
387+
it('startSplitText With a very long guess and break on space default behavior', function () {
370388
var item = document.createElement('SPAN');
371389
document.body.insertBefore(item, document.body.firstChild);
372390
var settings = new TextWrapper.prototype.TextWrapperSettings();
@@ -386,7 +404,7 @@ describe("textWrap", function () {
386404
document.body.removeChild(document.body.firstChild);
387405
});
388406

389-
it('startSplitTextWith a long guess and break on space default behavior', function () {
407+
it('startSplitText With a long guess and break on space default behavior', function () {
390408
var item = document.createElement('SPAN');
391409
document.body.insertBefore(item, document.body.firstChild);
392410
var settings = new TextWrapper.prototype.TextWrapperSettings();
@@ -406,7 +424,7 @@ describe("textWrap", function () {
406424
document.body.removeChild(document.body.firstChild);
407425
});
408426

409-
it('startSplitTextWith a short guess and break on space default behavior', function () {
427+
it('startSplitText With a short guess and break on space default behavior', function () {
410428
var item = document.createElement('SPAN');
411429
document.body.insertBefore(item, document.body.firstChild);
412430
var settings = new TextWrapper.prototype.TextWrapperSettings();
@@ -426,7 +444,7 @@ describe("textWrap", function () {
426444
document.body.removeChild(document.body.firstChild);
427445
});
428446

429-
it('startSplitTextWith a very short guess and break on space default behavior', function () {
447+
it('startSplitText With a very short guess and break on space default behavior', function () {
430448
var item = document.createElement('SPAN');
431449
document.body.insertBefore(item, document.body.firstChild);
432450
var settings = new TextWrapper.prototype.TextWrapperSettings();
@@ -446,7 +464,7 @@ describe("textWrap", function () {
446464
document.body.removeChild(document.body.firstChild);
447465
});
448466

449-
it('startSplitTextWith a short guess and no break default behavior', function () {
467+
it('startSplitText With a short guess and no break default behavior', function () {
450468
var item = document.createElement('SPAN');
451469
document.body.insertBefore(item, document.body.firstChild);
452470
var settings = new TextWrapper.prototype.TextWrapperSettings();
@@ -463,7 +481,7 @@ describe("textWrap", function () {
463481
document.body.removeChild(document.body.firstChild);
464482
});
465483

466-
it('startSplitTextWith a short guess and no word break default behavior', function () {
484+
it('startSplitText With a short guess and no word break default behavior', function () {
467485
var item = document.createElement('SPAN');
468486
document.body.insertBefore(item, document.body.firstChild);
469487
var settings = new TextWrapper.prototype.TextWrapperSettings();

0 commit comments

Comments
 (0)