Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split on Word Boundary Functionality #56

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Settings
* **tooltip** _(Default: `true`)_ When true, the `title` attribute of the targeted HTML element will be set to the original, untruncated string. Valid values include `true` and `false`.
* **width** _(Default: `'auto'`)_ The width, in characters, of the desired text. When set to `'auto'`, trunk8 will maximize the amount of text without spilling over.
* **parseHTML** _(Default: `'false'`)_ When true, parse and save html structure and restore structure in the truncated text.
* **splitOn** _(Default: `'false'`)_ Takes and allow splitting on a regular expression. More commonly used to truncate to a whitespace. Takes into account the `side` option and trims only that side.
* **onTruncate** _(Callback)_: Called after truncation is completed.

Public Methods
Expand Down
25 changes: 24 additions & 1 deletion demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@
</select>
</label>
</div>

<div>
<label>
Split On

<select id="spliton">
<option value="false" selected>false (no splitting turned on)</option>
<option value="\s">\s</option>
<option value="[aeiou]">[aeiou] (vowels)</option>
</select>
</label>
</div>

<div>
<pre id="settings"></pre>
Expand Down Expand Up @@ -147,7 +159,18 @@
$('#txt').trunk8({fill: this.value});
updateSettings($('#txt').trunk8('getSettings'));
});

$('#spliton').change(function () {
if(this.value === 'false')
value = false;
else
value = this.value;

$('#txt').trunk8({splitOn: value});
updateSettings($('#txt').trunk8('getSettings'));
});

});
</script>
</body>
</html>
</html>
44 changes: 44 additions & 0 deletions trunk8.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
side = settings.side,
fill = settings.fill,
parseHTML = settings.parseHTML,
splitOn = settings.splitOn,
line_height = utils.getLineHeight(this) * settings.lines,
str = data.original_text,
length = str.length,
Expand Down Expand Up @@ -178,6 +179,10 @@

bite = utils.eatStr(str, side, length - bite_size, fill);

if(splitOn) {
bite = utils.eatFromLastOccurance(bite, splitOn, side, fill);
}

if (parseHTML && htmlObject) {
bite = rebuildHtmlFromBite(bite, htmlObject, fill);
}
Expand Down Expand Up @@ -211,6 +216,10 @@

bite = utils.eatStr(str, side, bite_size, fill);

if(splitOn) {
bite = utils.eatFromLastOccurance(bite, splitOn, side, fill);
}

this.html(bite);

if (settings.tooltip) {
Expand Down Expand Up @@ -351,7 +360,41 @@
$(elem).html(html).css({ 'float': floats, 'position': pos }).unwrap();

return line_height;
},

eatFromLastOccurance: function (str, splitOn, side, fill) {
/* Executes the following algorithm to support splitting on a regex:
* 1. determine which side to trim
* 2. remove the filled text
* 3. iterate on substrings checking for regex to split on
* 4. trim the text to that point
* 5. add the fill text back
*/
switch (side) {
case SIDES.right:
str = str.replace(new RegExp(fill + '$'), '');
splitOn = new RegExp(splitOn + '$');
while (str.length > 0 && !str.match(splitOn)) {
str = str.slice(0, -1);
}
str = str + fill;
break;

case SIDES.left:
str = str.replace(new RegExp('^' + fill), '');
splitOn = new RegExp('^' + splitOn);
while (str.length > 0 && !str.match(splitOn)) {
str = str.slice(1);
}
str = fill + str;
break;

default:
break;
}

return str;
}
};

utils.eatStr.cache = {};
Expand Down Expand Up @@ -379,6 +422,7 @@
tooltip: true,
width: WIDTH.auto,
parseHTML: false,
splitOn: false,
onTruncate: function () {}
};
})(jQuery);