Comparing version 2.1.3 to 2.1.7
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(factory()); | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(factory()); | ||
}(this, (function () { 'use strict'; | ||
function shave(target, maxHeight) { | ||
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
function shave(target, maxHeight) { | ||
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
if (!maxHeight) throw Error('maxHeight is required'); | ||
var els = typeof target === 'string' ? document.querySelectorAll(target) : target; | ||
if (!els) return; | ||
if (!maxHeight) throw Error('maxHeight is required'); | ||
var els = typeof target === 'string' ? document.querySelectorAll(target) : target; | ||
if (!els) return; | ||
var character = opts.character || '…'; | ||
var classname = opts.classname || 'js-shave'; | ||
var spaces = opts.spaces || true; | ||
var charHtml = '<span class="js-shave-char">' + character + '</span>'; | ||
var character = opts.character || '…'; | ||
var classname = opts.classname || 'js-shave'; | ||
var spaces = opts.spaces || true; | ||
var charHtml = '<span class="js-shave-char">' + character + '</span>'; | ||
if (!('length' in els)) els = [els]; | ||
for (var i = 0; i < els.length; i += 1) { | ||
var el = els[i]; | ||
var styles = el.style; | ||
var span = el.querySelector('.' + classname); | ||
var textProp = el.textContent === undefined ? 'innerText' : 'textContent'; | ||
if (!('length' in els)) els = [els]; | ||
for (var i = 0; i < els.length; i += 1) { | ||
var el = els[i]; | ||
var styles = el.style; | ||
var span = el.querySelector('.' + classname); | ||
var textProp = el.textContent === undefined ? 'innerText' : 'textContent'; | ||
// If element text has already been shaved | ||
if (span) { | ||
// Remove the ellipsis to recapture the original text | ||
el.removeChild(el.querySelector('.js-shave-char')); | ||
el[textProp] = el[textProp]; // nuke span, recombine text | ||
} | ||
// If element text has already been shaved | ||
if (span) { | ||
// Remove the ellipsis to recapture the original text | ||
el.removeChild(el.querySelector('.js-shave-char')); | ||
el[textProp] = el[textProp]; // nuke span, recombine text | ||
} | ||
var fullText = el[textProp]; | ||
var words = spaces ? fullText : fullText.split(' '); | ||
var fullText = el[textProp]; | ||
var words = spaces ? fullText : fullText.split(' '); | ||
// If 0 or 1 words, we're done | ||
if (words.length < 2) continue; | ||
// If 0 or 1 words, we're done | ||
if (words.length < 2) continue; | ||
// Temporarily remove any CSS height for text height calculation | ||
var heightStyle = styles.height; | ||
styles.height = 'auto'; | ||
var maxHeightStyle = styles.maxHeight; | ||
styles.maxHeight = 'none'; | ||
// Temporarily remove any CSS height for text height calculation | ||
var heightStyle = styles.height; | ||
styles.height = 'auto'; | ||
var maxHeightStyle = styles.maxHeight; | ||
styles.maxHeight = 'none'; | ||
// If already short enough, we're done | ||
if (el.offsetHeight <= maxHeight) { | ||
styles.height = heightStyle; | ||
styles.maxHeight = maxHeightStyle; | ||
continue; | ||
} | ||
// If already short enough, we're done | ||
if (el.offsetHeight <= maxHeight) { | ||
styles.height = heightStyle; | ||
styles.maxHeight = maxHeightStyle; | ||
continue; | ||
} | ||
// Binary search for number of words which can fit in allotted height | ||
var max = words.length - 1; | ||
var min = 0; | ||
var pivot = void 0; | ||
while (min < max) { | ||
pivot = min + max + 1 >> 1; // eslint-disable-line no-bitwise | ||
el[textProp] = spaces ? words.slice(0, pivot) : words.slice(0, pivot).join(' '); | ||
// Binary search for number of words which can fit in allotted height | ||
var max = words.length - 1; | ||
var min = 0; | ||
var pivot = void 0; | ||
while (min < max) { | ||
pivot = min + max + 1 >> 1; // eslint-disable-line no-bitwise | ||
el[textProp] = spaces ? words.slice(0, pivot) : words.slice(0, pivot).join(' '); | ||
el.insertAdjacentHTML('beforeend', charHtml); | ||
if (el.offsetHeight > maxHeight) max = spaces ? pivot - 2 : pivot - 1;else min = pivot; | ||
} | ||
el[textProp] = spaces ? words.slice(0, max) : words.slice(0, max).join(' '); | ||
el.insertAdjacentHTML('beforeend', charHtml); | ||
if (el.offsetHeight > maxHeight) max = spaces ? pivot - 2 : pivot - 1;else min = pivot; | ||
} | ||
var diff = spaces ? words.slice(max) : words.slice(max).join(' '); | ||
el[textProp] = spaces ? words.slice(0, max) : words.slice(0, max).join(' '); | ||
el.insertAdjacentHTML('beforeend', charHtml); | ||
var diff = spaces ? words.slice(max) : words.slice(max).join(' '); | ||
el.insertAdjacentHTML('beforeend', '<span class="' + classname + '" style="display:none;">' + diff + '</span>'); | ||
el.insertAdjacentHTML('beforeend', '<span class="' + classname + '" style="display:none;">' + diff + '</span>'); | ||
styles.height = heightStyle; | ||
styles.maxHeight = maxHeightStyle; | ||
styles.height = heightStyle; | ||
styles.maxHeight = maxHeightStyle; | ||
} | ||
} | ||
} | ||
/* global window */ | ||
if (typeof window !== 'undefined') { | ||
var plugin = window.$ || window.jQuery || window.Zepto; | ||
if (plugin) { | ||
plugin.fn.shave = function shavePlugin(maxHeight, opts) { | ||
shave(this, maxHeight, opts); | ||
return this; | ||
}; | ||
/* global window */ | ||
if (typeof window !== 'undefined') { | ||
var plugin = window.$ || window.jQuery || window.Zepto; | ||
if (plugin) { | ||
plugin.fn.shave = function shavePlugin(maxHeight, opts) { | ||
shave(this, maxHeight, opts); | ||
return this; | ||
}; | ||
} | ||
} | ||
} | ||
}))); |
/** | ||
* shave - Shave is a javascript plugin that truncates multi-line text within a html element based on set max height | ||
* @version v2.1.3 | ||
* @version v2.1.7 | ||
* @link https://github.com/dollarshaveclub/shave#readme | ||
* @author Jeff Wainwright <jjwainwright2@gmail.com> (jeffry.in) | ||
* @license MIT */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t():"function"==typeof define&&define.amd?define(t):t()}(0,function(){"use strict";function e(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if(!t)throw Error("maxHeight is required");var i="string"==typeof e?document.querySelectorAll(e):e;if(i){var o=n.character||"…",r=n.classname||"js-shave",s=n.spaces||!0,a='<span class="js-shave-char">'+o+"</span>";"length"in i||(i=[i]);for(var h=0;h<i.length;h+=1){var f=i[h],c=f.style,d=f.querySelector("."+r),l=void 0===f.textContent?"innerText":"textContent";d&&(f.removeChild(f.querySelector(".js-shave-char")),f[l]=f[l]);var v=f[l],g=s?v:v.split(" ");if(!(g.length<2)){var u=c.height;c.height="auto";var p=c.maxHeight;if(c.maxHeight="none",f.offsetHeight<=t)c.height=u,c.maxHeight=p;else{for(var y=g.length-1,j=0,m=void 0;j<y;)m=j+y+1>>1,f[l]=s?g.slice(0,m):g.slice(0,m).join(" "),f.insertAdjacentHTML("beforeend",a),f.offsetHeight>t?y=s?m-2:m-1:j=m;f[l]=s?g.slice(0,y):g.slice(0,y).join(" "),f.insertAdjacentHTML("beforeend",a);var H=s?g.slice(y):g.slice(y).join(" ");f.insertAdjacentHTML("beforeend",'<span class="'+r+'" style="display:none;">'+H+"</span>"),c.height=u,c.maxHeight=p}}}}}if("undefined"!=typeof window){var t=window.$||window.jQuery||window.Zepto;t&&(t.fn.shave=function(t,n){return e(this,t,n),this})}}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t():"function"==typeof define&&define.amd?define(t):t()}(0,function(){"use strict";if("undefined"!=typeof window){var e=window.$||window.jQuery||window.Zepto;e&&(e.fn.shave=function(e,t){return function(e,t){var n=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};if(!t)throw Error("maxHeight is required");var i="string"==typeof e?document.querySelectorAll(e):e;if(i){var o=n.character||"…",r=n.classname||"js-shave",s=n.spaces||!0,a='<span class="js-shave-char">'+o+"</span>";"length"in i||(i=[i]);for(var h=0;h<i.length;h+=1){var f=i[h],c=f.style,d=f.querySelector("."+r),l=void 0===f.textContent?"innerText":"textContent";d&&(f.removeChild(f.querySelector(".js-shave-char")),f[l]=f[l]);var v=f[l],g=s?v:v.split(" ");if(!(g.length<2)){var u=c.height;c.height="auto";var p=c.maxHeight;if(c.maxHeight="none",f.offsetHeight<=t)c.height=u,c.maxHeight=p;else{for(var y=g.length-1,j=0,m=void 0;j<y;)m=j+y+1>>1,f[l]=s?g.slice(0,m):g.slice(0,m).join(" "),f.insertAdjacentHTML("beforeend",a),f.offsetHeight>t?y=s?m-2:m-1:j=m;f[l]=s?g.slice(0,y):g.slice(0,y).join(" "),f.insertAdjacentHTML("beforeend",a);var H=s?g.slice(y):g.slice(y).join(" ");f.insertAdjacentHTML("beforeend",'<span class="'+r+'" style="display:none;">'+H+"</span>"),c.height=u,c.maxHeight=p}}}}}(this,e,t),this})}}); |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global.shave = factory()); | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global.shave = factory()); | ||
}(this, (function () { 'use strict'; | ||
function shave(target, maxHeight) { | ||
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
function shave(target, maxHeight) { | ||
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
if (!maxHeight) throw Error('maxHeight is required'); | ||
var els = typeof target === 'string' ? document.querySelectorAll(target) : target; | ||
if (!els) return; | ||
if (!maxHeight) throw Error('maxHeight is required'); | ||
var els = typeof target === 'string' ? document.querySelectorAll(target) : target; | ||
if (!els) return; | ||
var character = opts.character || '…'; | ||
var classname = opts.classname || 'js-shave'; | ||
var spaces = opts.spaces || true; | ||
var charHtml = '<span class="js-shave-char">' + character + '</span>'; | ||
var character = opts.character || '…'; | ||
var classname = opts.classname || 'js-shave'; | ||
var spaces = opts.spaces || true; | ||
var charHtml = '<span class="js-shave-char">' + character + '</span>'; | ||
if (!('length' in els)) els = [els]; | ||
for (var i = 0; i < els.length; i += 1) { | ||
var el = els[i]; | ||
var styles = el.style; | ||
var span = el.querySelector('.' + classname); | ||
var textProp = el.textContent === undefined ? 'innerText' : 'textContent'; | ||
if (!('length' in els)) els = [els]; | ||
for (var i = 0; i < els.length; i += 1) { | ||
var el = els[i]; | ||
var styles = el.style; | ||
var span = el.querySelector('.' + classname); | ||
var textProp = el.textContent === undefined ? 'innerText' : 'textContent'; | ||
// If element text has already been shaved | ||
if (span) { | ||
// Remove the ellipsis to recapture the original text | ||
el.removeChild(el.querySelector('.js-shave-char')); | ||
el[textProp] = el[textProp]; // nuke span, recombine text | ||
} | ||
// If element text has already been shaved | ||
if (span) { | ||
// Remove the ellipsis to recapture the original text | ||
el.removeChild(el.querySelector('.js-shave-char')); | ||
el[textProp] = el[textProp]; // nuke span, recombine text | ||
} | ||
var fullText = el[textProp]; | ||
var words = spaces ? fullText : fullText.split(' '); | ||
var fullText = el[textProp]; | ||
var words = spaces ? fullText : fullText.split(' '); | ||
// If 0 or 1 words, we're done | ||
if (words.length < 2) continue; | ||
// If 0 or 1 words, we're done | ||
if (words.length < 2) continue; | ||
// Temporarily remove any CSS height for text height calculation | ||
var heightStyle = styles.height; | ||
styles.height = 'auto'; | ||
var maxHeightStyle = styles.maxHeight; | ||
styles.maxHeight = 'none'; | ||
// Temporarily remove any CSS height for text height calculation | ||
var heightStyle = styles.height; | ||
styles.height = 'auto'; | ||
var maxHeightStyle = styles.maxHeight; | ||
styles.maxHeight = 'none'; | ||
// If already short enough, we're done | ||
if (el.offsetHeight <= maxHeight) { | ||
styles.height = heightStyle; | ||
styles.maxHeight = maxHeightStyle; | ||
continue; | ||
} | ||
// If already short enough, we're done | ||
if (el.offsetHeight <= maxHeight) { | ||
styles.height = heightStyle; | ||
styles.maxHeight = maxHeightStyle; | ||
continue; | ||
} | ||
// Binary search for number of words which can fit in allotted height | ||
var max = words.length - 1; | ||
var min = 0; | ||
var pivot = void 0; | ||
while (min < max) { | ||
pivot = min + max + 1 >> 1; // eslint-disable-line no-bitwise | ||
el[textProp] = spaces ? words.slice(0, pivot) : words.slice(0, pivot).join(' '); | ||
// Binary search for number of words which can fit in allotted height | ||
var max = words.length - 1; | ||
var min = 0; | ||
var pivot = void 0; | ||
while (min < max) { | ||
pivot = min + max + 1 >> 1; // eslint-disable-line no-bitwise | ||
el[textProp] = spaces ? words.slice(0, pivot) : words.slice(0, pivot).join(' '); | ||
el.insertAdjacentHTML('beforeend', charHtml); | ||
if (el.offsetHeight > maxHeight) max = spaces ? pivot - 2 : pivot - 1;else min = pivot; | ||
} | ||
el[textProp] = spaces ? words.slice(0, max) : words.slice(0, max).join(' '); | ||
el.insertAdjacentHTML('beforeend', charHtml); | ||
if (el.offsetHeight > maxHeight) max = spaces ? pivot - 2 : pivot - 1;else min = pivot; | ||
} | ||
var diff = spaces ? words.slice(max) : words.slice(max).join(' '); | ||
el[textProp] = spaces ? words.slice(0, max) : words.slice(0, max).join(' '); | ||
el.insertAdjacentHTML('beforeend', charHtml); | ||
var diff = spaces ? words.slice(max) : words.slice(max).join(' '); | ||
el.insertAdjacentHTML('beforeend', '<span class="' + classname + '" style="display:none;">' + diff + '</span>'); | ||
el.insertAdjacentHTML('beforeend', '<span class="' + classname + '" style="display:none;">' + diff + '</span>'); | ||
styles.height = heightStyle; | ||
styles.maxHeight = maxHeightStyle; | ||
styles.height = heightStyle; | ||
styles.maxHeight = maxHeightStyle; | ||
} | ||
} | ||
} | ||
return shave; | ||
return shave; | ||
}))); |
/** | ||
* shave - Shave is a javascript plugin that truncates multi-line text within a html element based on set max height | ||
* @version v2.1.3 | ||
* @version v2.1.7 | ||
* @link https://github.com/dollarshaveclub/shave#readme | ||
* @author Jeff Wainwright <jjwainwright2@gmail.com> (jeffry.in) | ||
* @license MIT */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.shave=t()}(this,function(){"use strict";function e(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if(!t)throw Error("maxHeight is required");var i="string"==typeof e?document.querySelectorAll(e):e;if(i){var r=n.character||"…",s=n.classname||"js-shave",a=n.spaces||!0,o='<span class="js-shave-char">'+r+"</span>";"length"in i||(i=[i]);for(var h=0;h<i.length;h+=1){var c=i[h],l=c.style,f=c.querySelector("."+s),d=void 0===c.textContent?"innerText":"textContent";f&&(c.removeChild(c.querySelector(".js-shave-char")),c[d]=c[d]);var v=c[d],g=a?v:v.split(" ");if(!(g.length<2)){var u=l.height;l.height="auto";var p=l.maxHeight;if(l.maxHeight="none",c.offsetHeight<=t)l.height=u,l.maxHeight=p;else{for(var m=g.length-1,j=0,x=void 0;j<m;)x=j+m+1>>1,c[d]=a?g.slice(0,x):g.slice(0,x).join(" "),c.insertAdjacentHTML("beforeend",o),c.offsetHeight>t?m=a?x-2:x-1:j=x;c[d]=a?g.slice(0,m):g.slice(0,m).join(" "),c.insertAdjacentHTML("beforeend",o);var y=a?g.slice(m):g.slice(m).join(" ");c.insertAdjacentHTML("beforeend",'<span class="'+s+'" style="display:none;">'+y+"</span>"),l.height=u,l.maxHeight=p}}}}}return e}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.shave=t()}(this,function(){"use strict";return function(e,t){var n=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};if(!t)throw Error("maxHeight is required");var i="string"==typeof e?document.querySelectorAll(e):e;if(i){var r=n.character||"…",s=n.classname||"js-shave",a=n.spaces||!0,o='<span class="js-shave-char">'+r+"</span>";"length"in i||(i=[i]);for(var h=0;h<i.length;h+=1){var c=i[h],l=c.style,f=c.querySelector("."+s),d=void 0===c.textContent?"innerText":"textContent";f&&(c.removeChild(c.querySelector(".js-shave-char")),c[d]=c[d]);var v=c[d],g=a?v:v.split(" ");if(!(g.length<2)){var u=l.height;l.height="auto";var p=l.maxHeight;if(l.maxHeight="none",c.offsetHeight<=t)l.height=u,l.maxHeight=p;else{for(var m=g.length-1,j=0,x=void 0;j<m;)x=j+m+1>>1,c[d]=a?g.slice(0,x):g.slice(0,x).join(" "),c.insertAdjacentHTML("beforeend",o),c.offsetHeight>t?m=a?x-2:x-1:j=x;c[d]=a?g.slice(0,m):g.slice(0,m).join(" "),c.insertAdjacentHTML("beforeend",o);var y=a?g.slice(m):g.slice(m).join(" ");c.insertAdjacentHTML("beforeend",'<span class="'+s+'" style="display:none;">'+y+"</span>"),l.height=u,l.maxHeight=p}}}}}}); |
{ | ||
"name": "shave", | ||
"version": "2.1.3", | ||
"version": "2.1.7", | ||
"description": "Shave is a javascript plugin that truncates multi-line text within a html element based on set max height", | ||
@@ -12,3 +12,6 @@ "main": "dist/shave.js", | ||
"scripts": { | ||
"eslint": "eslint . --fix", | ||
"eslint:ci": "eslint .", | ||
"test": "gulp test", | ||
"test:es-check": "es-check es5 dist/shave.min.js dist/shave.js dist/jquery.shave.js dist/jquery.shave.min.js", | ||
"rollup": "rollup -c --environment entry:shave,format:umd && rollup -c --environment entry:shave,format:es && rollup -c --environment entry:jquery.shave", | ||
@@ -47,19 +50,20 @@ "build": "npm run rollup && gulp", | ||
"bower": "^1.7.9", | ||
"debug": "^2.6.8", | ||
"eslint": "^3.19.0", | ||
"eslint-config-airbnb": "14.1.0", | ||
"debug": "^3.1.0", | ||
"es-check": "^2.0.0", | ||
"eslint": "^4.17.0", | ||
"eslint-config-airbnb": "16.1.0", | ||
"eslint-plugin-import": "^2.7.0", | ||
"gulp": "^3.9.1", | ||
"gulp-header": "^1.8.8", | ||
"gulp-qunit": "^1.5.0", | ||
"gulp-header": "^2.0.1", | ||
"gulp-qunit": "^2.0.1", | ||
"gulp-rename": "^1.2.2", | ||
"gulp-uglify": "^2.0.0", | ||
"node-qunit-phantomjs": "^1.5.0", | ||
"rollup": "^0.41.6", | ||
"rollup-plugin-babel": "^2.7.1", | ||
"rollup-plugin-commonjs": "^8.0.2", | ||
"rollup-plugin-eslint": "^3.0.0", | ||
"rollup-plugin-node-resolve": "^2.1.1", | ||
"rollup-plugin-uglify": "^1.0.2" | ||
"gulp-uglify": "^3.0.0", | ||
"node-qunit-phantomjs": "^2.0.0", | ||
"rollup": "^0.60.0", | ||
"rollup-plugin-babel": "^3.0.3", | ||
"rollup-plugin-commonjs": "^9.0.0", | ||
"rollup-plugin-eslint": "^4.0.0", | ||
"rollup-plugin-node-resolve": "^3.0.3", | ||
"rollup-plugin-uglify": "^4.0.0" | ||
} | ||
} |
@@ -128,2 +128,2 @@ <p align="center"> | ||
Created and maintained by [Jeff Wainwright](https://github.com/yowainwright) with [Dollar Shave Club Engineering](https://github.com/dollarshaveclub). | ||
[Created](https://github.com/yowainwright/truncated.js) and maintained by [Jeff Wainwright](https://github.com/yowainwright) with [Dollar Shave Club Engineering](https://github.com/dollarshaveclub). |
@@ -60,4 +60,6 @@ export default function shave(target, maxHeight, opts = {}) { | ||
el.insertAdjacentHTML('beforeend', | ||
`<span class="${classname}" style="display:none;">${diff}</span>`) | ||
el.insertAdjacentHTML( | ||
'beforeend', | ||
`<span class="${classname}" style="display:none;">${diff}</span>`, | ||
) | ||
@@ -64,0 +66,0 @@ styles.height = heightStyle |
22238
266
21