Comparing version 2.1.7 to 2.2.0
@@ -30,3 +30,4 @@ (function (global, factory) { | ||
el.removeChild(el.querySelector('.js-shave-char')); | ||
el[textProp] = el[textProp]; // nuke span, recombine text | ||
el[textProp] = el[textProp]; // eslint-disable-line | ||
// nuke span, recombine text | ||
} | ||
@@ -33,0 +34,0 @@ |
/** | ||
* shave - Shave is a javascript plugin that truncates multi-line text within a html element based on set max height | ||
* @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";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})}}); | ||
shave - Shave is a javascript plugin that truncates multi-line text within a html element based on set max height | ||
@version v2.2.0 | ||
@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";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||"…",a=n.classname||"js-shave",r=n.spaces||!0,s='<span class="js-shave-char">'.concat(o,"</span>");"length"in i||(i=[i]);for(var c=0;c<i.length;c+=1){var h=i[c],f=h.style,d=h.querySelector(".".concat(a)),l=void 0===h.textContent?"innerText":"textContent";d&&(h.removeChild(h.querySelector(".js-shave-char")),h[l]=h[l]);var v=h[l],g=r?v:v.split(" ");if(!(g.length<2)){var u=f.height;f.height="auto";var p=f.maxHeight;if(f.maxHeight="none",h.offsetHeight<=t)f.height=u,f.maxHeight=p;else{for(var y=g.length-1,j=0,m=void 0;j<y;)m=j+y+1>>1,h[l]=r?g.slice(0,m):g.slice(0,m).join(" "),h.insertAdjacentHTML("beforeend",s),h.offsetHeight>t?y=r?m-2:m-1:j=m;h[l]=r?g.slice(0,y):g.slice(0,y).join(" "),h.insertAdjacentHTML("beforeend",s);var H=r?g.slice(y):g.slice(y).join(" ");h.insertAdjacentHTML("beforeend",'<span class="'.concat(a,'" style="display:none;">').concat(H,"</span>")),f.height=u,f.maxHeight=p}}}}}(this,e,t),this})}}); |
@@ -0,40 +1,42 @@ | ||
/** | ||
shave - Shave is a javascript plugin that truncates multi-line text within a html element based on set max height | ||
@version v2.2.0 | ||
@link https://github.com/dollarshaveclub/shave#readme | ||
@author Jeff Wainwright <jjwainwright2@gmail.com> (jeffry.in) | ||
@license MIT | ||
**/ | ||
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; | ||
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 charHtml = "<span class=\"js-shave-char\">".concat(character, "</span>"); | ||
if (!('length' in els)) els = [els]; | ||
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'; | ||
var span = el.querySelector(".".concat(classname)); | ||
var textProp = el.textContent === undefined ? 'innerText' : 'textContent'; // If element text has already been shaved | ||
// 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 | ||
el[textProp] = el[textProp]; // eslint-disable-line | ||
// nuke span, recombine text | ||
} | ||
var fullText = el[textProp]; | ||
var words = spaces ? fullText : fullText.split(' '); | ||
var words = spaces ? fullText : fullText.split(' '); // If 0 or 1 words, we're done | ||
// If 0 or 1 words, we're done | ||
if (words.length < 2) continue; | ||
if (words.length < 2) continue; // Temporarily remove any CSS height for text height calculation | ||
// Temporarily remove any CSS height for text height calculation | ||
var heightStyle = styles.height; | ||
styles.height = 'auto'; | ||
var maxHeightStyle = styles.maxHeight; | ||
styles.maxHeight = 'none'; | ||
styles.maxHeight = 'none'; // If already short enough, we're done | ||
// If already short enough, we're done | ||
if (el.offsetHeight <= maxHeight) { | ||
@@ -44,10 +46,12 @@ styles.height = heightStyle; | ||
continue; | ||
} | ||
} // Binary search for number of words which can fit in allotted height | ||
// 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(' '); | ||
@@ -61,5 +65,3 @@ 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=\"".concat(classname, "\" style=\"display:none;\">").concat(diff, "</span>")); | ||
styles.height = heightStyle; | ||
@@ -66,0 +68,0 @@ styles.maxHeight = maxHeightStyle; |
@@ -0,1 +1,8 @@ | ||
/** | ||
shave - Shave is a javascript plugin that truncates multi-line text within a html element based on set max height | ||
@version v2.2.0 | ||
@link https://github.com/dollarshaveclub/shave#readme | ||
@author Jeff Wainwright <jjwainwright2@gmail.com> (jeffry.in) | ||
@license MIT | ||
**/ | ||
(function (global, factory) { | ||
@@ -9,39 +16,34 @@ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
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; | ||
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 charHtml = "<span class=\"js-shave-char\">".concat(character, "</span>"); | ||
if (!('length' in els)) els = [els]; | ||
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'; | ||
var span = el.querySelector(".".concat(classname)); | ||
var textProp = el.textContent === undefined ? 'innerText' : 'textContent'; // If element text has already been shaved | ||
// 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 | ||
el[textProp] = el[textProp]; // eslint-disable-line | ||
// nuke span, recombine text | ||
} | ||
var fullText = el[textProp]; | ||
var words = spaces ? fullText : fullText.split(' '); | ||
var words = spaces ? fullText : fullText.split(' '); // If 0 or 1 words, we're done | ||
// If 0 or 1 words, we're done | ||
if (words.length < 2) continue; | ||
if (words.length < 2) continue; // Temporarily remove any CSS height for text height calculation | ||
// Temporarily remove any CSS height for text height calculation | ||
var heightStyle = styles.height; | ||
styles.height = 'auto'; | ||
var maxHeightStyle = styles.maxHeight; | ||
styles.maxHeight = 'none'; | ||
styles.maxHeight = 'none'; // If already short enough, we're done | ||
// If already short enough, we're done | ||
if (el.offsetHeight <= maxHeight) { | ||
@@ -51,10 +53,12 @@ styles.height = heightStyle; | ||
continue; | ||
} | ||
} // Binary search for number of words which can fit in allotted height | ||
// 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(' '); | ||
@@ -68,5 +72,3 @@ 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=\"".concat(classname, "\" style=\"display:none;\">").concat(diff, "</span>")); | ||
styles.height = heightStyle; | ||
@@ -73,0 +75,0 @@ styles.maxHeight = maxHeightStyle; |
/** | ||
* shave - Shave is a javascript plugin that truncates multi-line text within a html element based on set max height | ||
* @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";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}}}}}}); | ||
shave - Shave is a javascript plugin that truncates multi-line text within a html element based on set max height | ||
@version v2.2.0 | ||
@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";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 a=n.character||"…",o=n.classname||"js-shave",r=n.spaces||!0,s='<span class="js-shave-char">'.concat(a,"</span>");"length"in i||(i=[i]);for(var c=0;c<i.length;c+=1){var h=i[c],l=h.style,f=h.querySelector(".".concat(o)),d=void 0===h.textContent?"innerText":"textContent";f&&(h.removeChild(h.querySelector(".js-shave-char")),h[d]=h[d]);var v=h[d],g=r?v:v.split(" ");if(!(g.length<2)){var u=l.height;l.height="auto";var p=l.maxHeight;if(l.maxHeight="none",h.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,h[d]=r?g.slice(0,x):g.slice(0,x).join(" "),h.insertAdjacentHTML("beforeend",s),h.offsetHeight>t?m=r?x-2:x-1:j=x;h[d]=r?g.slice(0,m):g.slice(0,m).join(" "),h.insertAdjacentHTML("beforeend",s);var y=r?g.slice(m):g.slice(m).join(" ");h.insertAdjacentHTML("beforeend",'<span class="'.concat(o,'" style="display:none;">').concat(y,"</span>")),l.height=u,l.maxHeight=p}}}}}}); |
{ | ||
"name": "shave", | ||
"version": "2.1.7", | ||
"version": "2.2.0", | ||
"description": "Shave is a javascript plugin that truncates multi-line text within a html element based on set max height", | ||
@@ -14,6 +14,6 @@ "main": "dist/shave.js", | ||
"eslint:ci": "eslint .", | ||
"test": "gulp test", | ||
"test": "npm run test:acceptance && npm run test:es-check", | ||
"test:acceptance": "node ./scripts/acceptance.js --coverage", | ||
"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", | ||
"build": "npm run rollup && gulp", | ||
"build": "rollup --config rollup.config.js", | ||
"postpublish": "git tag $npm_package_version && git push origin --tags" | ||
@@ -46,24 +46,13 @@ }, | ||
"devDependencies": { | ||
"babel-core": "^6.25.0", | ||
"babel-preset-es2015": "^6.24.1", | ||
"babel-preset-es2015-rollup": "^3.0.0", | ||
"bower": "^1.7.9", | ||
"debug": "^3.1.0", | ||
"@babel/core": "^7.0.0-beta.44", | ||
"@babel/preset-env": "^7.0.0-beta.40", | ||
"babel-core": "^7.0.0-bridge.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": "^2.0.1", | ||
"gulp-qunit": "^2.0.1", | ||
"gulp-rename": "^1.2.2", | ||
"gulp-uglify": "^3.0.0", | ||
"eslint": "^5.0.0", | ||
"eslint-config-dollarshaveclub": "^3.1.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": "^0.61.2", | ||
"rollup-plugin-babel": "^4.0.0-beta.0", | ||
"rollup-plugin-uglify": "^4.0.0" | ||
} | ||
} |
@@ -7,3 +7,3 @@ /* global window */ | ||
if (plugin) { | ||
plugin.fn.shave = function shavePlugin(maxHeight, opts) { | ||
plugin.fn.shave = function shavePlugin (maxHeight, opts) { | ||
shave(this, maxHeight, opts) | ||
@@ -10,0 +10,0 @@ return this |
@@ -1,2 +0,2 @@ | ||
export default function shave(target, maxHeight, opts = {}) { | ||
export default function shave (target, maxHeight, opts = {}) { | ||
if (!maxHeight) throw Error('maxHeight is required') | ||
@@ -22,3 +22,4 @@ let els = (typeof target === 'string') ? document.querySelectorAll(target) : target | ||
el.removeChild(el.querySelector('.js-shave-char')) | ||
el[textProp] = el[textProp] // nuke span, recombine text | ||
el[textProp] = el[textProp] // eslint-disable-line | ||
// nuke span, recombine text | ||
} | ||
@@ -25,0 +26,0 @@ |
22608
10
287