Skip to content

Commit

Permalink
Merge branch 'hotfix/0.1.4' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
pencilpix committed Apr 17, 2017
2 parents 5f95f4c + 8665850 commit 04a852c
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 11 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ellipsis",
"description": "ellipsis text depending on number of lines or number of chars",
"version": "0.1.3",
"version": "0.1.4",
"main": "./dist/jquery.ellipsis.min.js",
"authors": [
"Mohamed Hassan"
Expand Down
18 changes: 15 additions & 3 deletions dist/jquery.ellipsis.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/jquery.ellipsis.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jquery-ellipsis",
"version": "0.1.3",
"version": "0.1.4",
"description": "ellipsis text depending on number of lines or number of chars",
"private": false,
"main": "./dist/jquery.ellipsis.min.js",
Expand Down
25 changes: 20 additions & 5 deletions src/jquery.ellipsis.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
'use strict';
// define the plugin name and the default options
const PLUGIN_NAME = 'ellipsis';
const VERSION = '0.1.3';
const VERSION = '0.1.4';


/**
Expand Down Expand Up @@ -74,7 +74,8 @@
result = this._excerptTillChar(this.options.count);
} else if(this.options.type === 'lines') {
charsNo = this._getTotalCharsInLines(this.options.count);
result = this._excerptTillChar(charsNo);
if(charsNo)
result = this._excerptTillChar(charsNo);
}


Expand Down Expand Up @@ -122,7 +123,10 @@
if(this.options.type === 'lines' && number >= 3)
number -= 3;

return this.element.html(this.text.slice(0, number) + '...')
if(number >= this.text.length)
return null;

return this.element.html(this.text.slice(0, number) + '...');
}


Expand Down Expand Up @@ -155,16 +159,27 @@
height = linesNo * $charSpan.height();


if(count >= this.text.length){
$charSpan.remove();
return null;
}

$charSpan.text(this.text.slice(0, count));
$charSpan.css('max-width', this.element.width());

while($charSpan.height() <= height) {
$charSpan.text(this.text.slice(0, $charSpan.text().length + 1));
while($charSpan.height() <= height &&
this.text.length >= $charSpan.text().length + 1) {
$charSpan.text(this.text.slice(0, $charSpan.text().length + 1));
}

count = $charSpan.text().length;

$charSpan.remove();

if(count >= this.text.length){
return null;
}

if(count && typeof count === 'number') count --;
return count;
}
Expand Down
33 changes: 33 additions & 0 deletions test/specs/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,39 @@ sunt in culpa qui officia deserunt mollit anim id est laborum.`
$('#paragraph').ellipsis({type: 'lines', count: 3});
expect($('#paragraph').height()).toEqual(expectedHeight);
});


it('should not excerpt the text if it\'s less than 2 lines', () => {
let $p = $('#paragraph');
let expectedText = '';
let expectedHeight = 0;
$p.append('<span class="height-span">x</span>');
expectedHeight = $('.height-span').height();
$('.height-span').remove();

$('#paragraph').text(text.slice(0, 16));
expectedText = text.slice(0, 16)

$('#paragraph').ellipsis({type: 'lines', count: 2});
expect($('#paragraph').height()).toEqual(expectedHeight);
expect($('#paragraph').text()).toEqual(expectedText);
});

it('should not excerpt the text if text length less than dedicated lines count chars', () => {
let $p = $('#paragraph');
let expectedText = '';
let expectedHeight = 0;
$p.append('<span class="height-span">x</span>');
expectedHeight = $('.height-span').height() * 2;
$('.height-span').remove();

$('#paragraph').text(text.slice(0, 34));
expectedText = text.slice(0, 34)

$('#paragraph').ellipsis({type: 'lines', count: 2});
expect($('#paragraph').height()).toEqual(expectedHeight);
expect($('#paragraph').text()).toEqual(expectedText);
});
});


Expand Down

0 comments on commit 04a852c

Please sign in to comment.