Comparing version 2.0.2 to 2.0.3
@@ -7,8 +7,8 @@ (function (global, factory) { | ||
/* global document */ | ||
function shaveEl(target, maxHeight) { | ||
function shave(target, maxHeight) { | ||
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var el = target; | ||
var styles = el.style; | ||
if (!maxHeight) throw Error('maxHeight is required'); | ||
var els = typeof target === 'string' ? document.querySelectorAll(target) : target; | ||
var character = opts.character || '…'; | ||
@@ -18,59 +18,55 @@ var classname = opts.classname || 'js-shave'; | ||
var charHtml = '<span class="js-shave-char">' + character + '</span>'; | ||
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 (!('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 fullText = el[textProp]; | ||
var words = spaces ? fullText.split(' ') : fullText; | ||
// 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 0 or 1 words, we're done | ||
if (words.length < 2) return; | ||
var fullText = el[textProp]; | ||
var words = spaces ? fullText.split(' ') : fullText; | ||
// Temporarily remove any CSS height for text height calculation | ||
var heightStyle = styles.height; | ||
styles.height = 'auto'; | ||
var maxHeightStyle = styles.maxHeight; | ||
styles.maxHeight = 'none'; | ||
// If 0 or 1 words, we're done | ||
if (words.length < 2) continue; | ||
// If already short enough, we're done | ||
if (el.offsetHeight <= maxHeight) { | ||
styles.height = heightStyle; | ||
styles.maxHeight = maxHeightStyle; | ||
return; | ||
} | ||
// Temporarily remove any CSS height for text height calculation | ||
var heightStyle = styles.height; | ||
styles.height = 'auto'; | ||
var maxHeightStyle = styles.maxHeight; | ||
styles.maxHeight = 'none'; | ||
// 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).join(' ') : words.slice(0, pivot); | ||
el.insertAdjacentHTML('beforeend', charHtml); | ||
if (el.offsetHeight > maxHeight) max = spaces ? pivot - 1 : pivot - 2;else min = pivot; | ||
} | ||
// If already short enough, we're done | ||
if (el.offsetHeight <= maxHeight) { | ||
styles.height = heightStyle; | ||
styles.maxHeight = maxHeightStyle; | ||
continue; | ||
} | ||
el[textProp] = spaces ? words.slice(0, max).join(' ') : words.slice(0, max); | ||
el.insertAdjacentHTML('beforeend', charHtml); | ||
var diff = spaces ? words.slice(max).join(' ') : words.slice(max); | ||
// 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).join(' ') : words.slice(0, pivot); | ||
el.insertAdjacentHTML('beforeend', charHtml); | ||
if (el.offsetHeight > maxHeight) max = spaces ? pivot - 1 : pivot - 2;else min = pivot; | ||
} | ||
el.insertAdjacentHTML('beforeend', '<span class="' + classname + '" style="display:none;">' + diff + '</span>'); | ||
el[textProp] = spaces ? words.slice(0, max).join(' ') : words.slice(0, max); | ||
el.insertAdjacentHTML('beforeend', charHtml); | ||
var diff = spaces ? words.slice(max).join(' ') : words.slice(max); | ||
styles.height = heightStyle; | ||
styles.maxHeight = maxHeightStyle; | ||
} | ||
el.insertAdjacentHTML('beforeend', '<span class="' + classname + '" style="display:none;">' + diff + '</span>'); | ||
function shave(target, maxHeight, opts) { | ||
if (!maxHeight) throw Error('maxHeight is required'); | ||
var els = typeof target === 'string' ? document.querySelectorAll(target) : target; | ||
if (!('length' in els)) els = [els]; | ||
for (var i = 0; i < els.length; i += 1) { | ||
var el = els[i]; | ||
shaveEl(el, maxHeight, opts); | ||
styles.height = heightStyle; | ||
styles.maxHeight = maxHeightStyle; | ||
} | ||
@@ -77,0 +73,0 @@ } |
/** | ||
* shave - Shave is a javascript plugin that truncates multi-line text within a html element based on set max height | ||
* @version v2.0.2 | ||
* @version v2.0.3 | ||
* @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]:{},i=e,o=i.style,r=n.character||"…",s=n.classname||"js-shave",a=n.spaces||!1,h='<span class="js-shave-char">'+r+"</span>",f=i.querySelector("."+s),c=void 0===i.textContent?"innerText":"textContent";f&&(i.removeChild(i.querySelector(".js-shave-char")),i[c]=i[c]);var d=i[c],l=a?d.split(" "):d;if(!(l.length<2)){var u=o.height;o.height="auto";var v=o.maxHeight;if(o.maxHeight="none",i.offsetHeight<=t)return o.height=u,void(o.maxHeight=v);for(var g=l.length-1,p=0,y=void 0;p<g;)y=p+g+1>>1,i[c]=a?l.slice(0,y).join(" "):l.slice(0,y),i.insertAdjacentHTML("beforeend",h),i.offsetHeight>t?g=a?y-1:y-2:p=y;i[c]=a?l.slice(0,g).join(" "):l.slice(0,g),i.insertAdjacentHTML("beforeend",h);var j=a?l.slice(g).join(" "):l.slice(g);i.insertAdjacentHTML("beforeend",'<span class="'+s+'" style="display:none;">'+j+"</span>"),o.height=u,o.maxHeight=v}}function t(t,n,i){if(!n)throw Error("maxHeight is required");var o="string"==typeof t?document.querySelectorAll(t):t;"length"in o||(o=[o]);for(var r=0;r<o.length;r+=1){e(o[r],n,i)}}if("undefined"!=typeof window){var n=window.$||window.jQuery||window.Zepto;n&&(n.fn.shave=function(e,n){return t(this,e,n),this})}}); | ||
!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,o=n.character||"…",r=n.classname||"js-shave",s=n.spaces||!1,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.split(" "):v;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).join(" "):g.slice(0,m),f.insertAdjacentHTML("beforeend",a),f.offsetHeight>t?y=s?m-1:m-2:j=m;f[l]=s?g.slice(0,y).join(" "):g.slice(0,y),f.insertAdjacentHTML("beforeend",a);var H=s?g.slice(y).join(" "):g.slice(y);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})}}); |
@@ -7,8 +7,8 @@ (function (global, factory) { | ||
/* global document */ | ||
function shaveEl(target, maxHeight) { | ||
function shave(target, maxHeight) { | ||
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var el = target; | ||
var styles = el.style; | ||
if (!maxHeight) throw Error('maxHeight is required'); | ||
var els = typeof target === 'string' ? document.querySelectorAll(target) : target; | ||
var character = opts.character || '…'; | ||
@@ -18,59 +18,55 @@ var classname = opts.classname || 'js-shave'; | ||
var charHtml = '<span class="js-shave-char">' + character + '</span>'; | ||
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 (!('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 fullText = el[textProp]; | ||
var words = spaces ? fullText.split(' ') : fullText; | ||
// 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 0 or 1 words, we're done | ||
if (words.length < 2) return; | ||
var fullText = el[textProp]; | ||
var words = spaces ? fullText.split(' ') : fullText; | ||
// Temporarily remove any CSS height for text height calculation | ||
var heightStyle = styles.height; | ||
styles.height = 'auto'; | ||
var maxHeightStyle = styles.maxHeight; | ||
styles.maxHeight = 'none'; | ||
// If 0 or 1 words, we're done | ||
if (words.length < 2) continue; | ||
// If already short enough, we're done | ||
if (el.offsetHeight <= maxHeight) { | ||
styles.height = heightStyle; | ||
styles.maxHeight = maxHeightStyle; | ||
return; | ||
} | ||
// Temporarily remove any CSS height for text height calculation | ||
var heightStyle = styles.height; | ||
styles.height = 'auto'; | ||
var maxHeightStyle = styles.maxHeight; | ||
styles.maxHeight = 'none'; | ||
// 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).join(' ') : words.slice(0, pivot); | ||
el.insertAdjacentHTML('beforeend', charHtml); | ||
if (el.offsetHeight > maxHeight) max = spaces ? pivot - 1 : pivot - 2;else min = pivot; | ||
} | ||
// If already short enough, we're done | ||
if (el.offsetHeight <= maxHeight) { | ||
styles.height = heightStyle; | ||
styles.maxHeight = maxHeightStyle; | ||
continue; | ||
} | ||
el[textProp] = spaces ? words.slice(0, max).join(' ') : words.slice(0, max); | ||
el.insertAdjacentHTML('beforeend', charHtml); | ||
var diff = spaces ? words.slice(max).join(' ') : words.slice(max); | ||
// 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).join(' ') : words.slice(0, pivot); | ||
el.insertAdjacentHTML('beforeend', charHtml); | ||
if (el.offsetHeight > maxHeight) max = spaces ? pivot - 1 : pivot - 2;else min = pivot; | ||
} | ||
el.insertAdjacentHTML('beforeend', '<span class="' + classname + '" style="display:none;">' + diff + '</span>'); | ||
el[textProp] = spaces ? words.slice(0, max).join(' ') : words.slice(0, max); | ||
el.insertAdjacentHTML('beforeend', charHtml); | ||
var diff = spaces ? words.slice(max).join(' ') : words.slice(max); | ||
styles.height = heightStyle; | ||
styles.maxHeight = maxHeightStyle; | ||
} | ||
el.insertAdjacentHTML('beforeend', '<span class="' + classname + '" style="display:none;">' + diff + '</span>'); | ||
function shave(target, maxHeight, opts) { | ||
if (!maxHeight) throw Error('maxHeight is required'); | ||
var els = typeof target === 'string' ? document.querySelectorAll(target) : target; | ||
if (!('length' in els)) els = [els]; | ||
for (var i = 0; i < els.length; i += 1) { | ||
var el = els[i]; | ||
shaveEl(el, maxHeight, opts); | ||
styles.height = heightStyle; | ||
styles.maxHeight = maxHeightStyle; | ||
} | ||
@@ -77,0 +73,0 @@ } |
/** | ||
* shave - Shave is a javascript plugin that truncates multi-line text within a html element based on set max height | ||
* @version v2.0.2 | ||
* @version v2.0.3 | ||
* @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]:{},i=e,r=i.style,s=n.character||"…",o=n.classname||"js-shave",a=n.spaces||!1,h='<span class="js-shave-char">'+s+"</span>",c=i.querySelector("."+o),l=void 0===i.textContent?"innerText":"textContent";c&&(i.removeChild(i.querySelector(".js-shave-char")),i[l]=i[l]);var f=i[l],d=a?f.split(" "):f;if(!(d.length<2)){var g=r.height;r.height="auto";var u=r.maxHeight;if(r.maxHeight="none",i.offsetHeight<=t)return r.height=g,void(r.maxHeight=u);for(var v=d.length-1,p=0,m=void 0;p<v;)m=p+v+1>>1,i[l]=a?d.slice(0,m).join(" "):d.slice(0,m),i.insertAdjacentHTML("beforeend",h),i.offsetHeight>t?v=a?m-1:m-2:p=m;i[l]=a?d.slice(0,v).join(" "):d.slice(0,v),i.insertAdjacentHTML("beforeend",h);var j=a?d.slice(v).join(" "):d.slice(v);i.insertAdjacentHTML("beforeend",'<span class="'+o+'" style="display:none;">'+j+"</span>"),r.height=g,r.maxHeight=u}}function t(t,n,i){if(!n)throw Error("maxHeight is required");var r="string"==typeof t?document.querySelectorAll(t):t;"length"in r||(r=[r]);for(var s=0;s<r.length;s+=1){e(r[s],n,i)}}return t}); | ||
!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,s=n.character||"…",r=n.classname||"js-shave",o=n.spaces||!1,a='<span class="js-shave-char">'+s+"</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("."+r),d=void 0===c.textContent?"innerText":"textContent";f&&(c.removeChild(c.querySelector(".js-shave-char")),c[d]=c[d]);var g=c[d],v=o?g.split(" "):g;if(!(v.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=v.length-1,j=0,x=void 0;j<m;)x=j+m+1>>1,c[d]=o?v.slice(0,x).join(" "):v.slice(0,x),c.insertAdjacentHTML("beforeend",a),c.offsetHeight>t?m=o?x-1:x-2:j=x;c[d]=o?v.slice(0,m).join(" "):v.slice(0,m),c.insertAdjacentHTML("beforeend",a);var y=o?v.slice(m).join(" "):v.slice(m);c.insertAdjacentHTML("beforeend",'<span class="'+r+'" style="display:none;">'+y+"</span>"),l.height=u,l.maxHeight=p}}}}return e}); |
{ | ||
"name": "shave", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "Shave is a javascript plugin that truncates multi-line text within a html element based on set max height", | ||
@@ -5,0 +5,0 @@ "main": "dist/shave.js", |
102
src/shave.js
@@ -1,5 +0,5 @@ | ||
/* global document */ | ||
function shaveEl(target, maxHeight, opts = {}) { | ||
const el = target | ||
const styles = el.style | ||
export default function shave(target, maxHeight, opts = {}) { | ||
if (!maxHeight) throw Error('maxHeight is required') | ||
let els = (typeof target === 'string') ? document.querySelectorAll(target) : target | ||
const character = opts.character || '…' | ||
@@ -9,62 +9,58 @@ const classname = opts.classname || 'js-shave' | ||
const charHtml = `<span class="js-shave-char">${character}</span>` | ||
const span = el.querySelector(`.${classname}`) | ||
const 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 (!('length' in els)) els = [els] | ||
for (let i = 0; i < els.length; i += 1) { | ||
const el = els[i] | ||
const styles = el.style | ||
const span = el.querySelector(`.${classname}`) | ||
const textProp = el.textContent === undefined ? 'innerText' : 'textContent' | ||
const fullText = el[textProp] | ||
const words = spaces ? fullText.split(' ') : fullText | ||
// 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 0 or 1 words, we're done | ||
if (words.length < 2) return | ||
const fullText = el[textProp] | ||
const words = spaces ? fullText.split(' ') : fullText | ||
// Temporarily remove any CSS height for text height calculation | ||
const heightStyle = styles.height | ||
styles.height = 'auto' | ||
const maxHeightStyle = styles.maxHeight | ||
styles.maxHeight = 'none' | ||
// If 0 or 1 words, we're done | ||
if (words.length < 2) continue | ||
// If already short enough, we're done | ||
if (el.offsetHeight <= maxHeight) { | ||
styles.height = heightStyle | ||
styles.maxHeight = maxHeightStyle | ||
return | ||
} | ||
// Temporarily remove any CSS height for text height calculation | ||
const heightStyle = styles.height | ||
styles.height = 'auto' | ||
const maxHeightStyle = styles.maxHeight | ||
styles.maxHeight = 'none' | ||
// Binary search for number of words which can fit in allotted height | ||
let max = words.length - 1 | ||
let min = 0 | ||
let pivot | ||
while (min < max) { | ||
pivot = (min + max + 1) >> 1 // eslint-disable-line no-bitwise | ||
el[textProp] = spaces ? words.slice(0, pivot).join(' ') : words.slice(0, pivot) | ||
el.insertAdjacentHTML('beforeend', charHtml) | ||
if (el.offsetHeight > maxHeight) max = spaces ? pivot - 1 : pivot - 2 | ||
else min = pivot | ||
} | ||
// If already short enough, we're done | ||
if (el.offsetHeight <= maxHeight) { | ||
styles.height = heightStyle | ||
styles.maxHeight = maxHeightStyle | ||
continue | ||
} | ||
el[textProp] = spaces ? words.slice(0, max).join(' ') : words.slice(0, max) | ||
el.insertAdjacentHTML('beforeend', charHtml) | ||
const diff = spaces ? words.slice(max).join(' ') : words.slice(max) | ||
// Binary search for number of words which can fit in allotted height | ||
let max = words.length - 1 | ||
let min = 0 | ||
let pivot | ||
while (min < max) { | ||
pivot = (min + max + 1) >> 1 // eslint-disable-line no-bitwise | ||
el[textProp] = spaces ? words.slice(0, pivot).join(' ') : words.slice(0, pivot) | ||
el.insertAdjacentHTML('beforeend', charHtml) | ||
if (el.offsetHeight > maxHeight) max = spaces ? pivot - 1 : pivot - 2 | ||
else min = pivot | ||
} | ||
el.insertAdjacentHTML('beforeend', | ||
`<span class="${classname}" style="display:none;">${diff}</span>`) | ||
el[textProp] = spaces ? words.slice(0, max).join(' ') : words.slice(0, max) | ||
el.insertAdjacentHTML('beforeend', charHtml) | ||
const diff = spaces ? words.slice(max).join(' ') : words.slice(max) | ||
styles.height = heightStyle | ||
styles.maxHeight = maxHeightStyle | ||
} | ||
el.insertAdjacentHTML('beforeend', | ||
`<span class="${classname}" style="display:none;">${diff}</span>`) | ||
export default function shave(target, maxHeight, opts) { | ||
if (!maxHeight) throw Error('maxHeight is required') | ||
let els = (typeof target === 'string') ? document.querySelectorAll(target) : target | ||
if (!('length' in els)) els = [els] | ||
for (let i = 0; i < els.length; i += 1) { | ||
const el = els[i] | ||
shaveEl(el, maxHeight, opts) | ||
styles.height = heightStyle | ||
styles.maxHeight = maxHeightStyle | ||
} | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
18799
206