Comparing version 2.0.0 to 2.0.2
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global.jquery = global.jquery || {}, global.jquery.shave = factory()); | ||
(factory()); | ||
}(this, (function () { 'use strict'; | ||
/* global document, window */ | ||
function shaver(target, maxHeight, opts) { | ||
if (!maxHeight) throw Error('maxHeight is required'); | ||
/* global document */ | ||
function shaveEl(target, maxHeight) { | ||
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var el = target; | ||
var character = opts && opts.character || '…'; | ||
var classname = opts && opts.classname || 'js-shave'; | ||
var spaces = true; | ||
if (opts && opts.spaces === false) spaces = false; | ||
var styles = el.style; | ||
var character = opts.character || '…'; | ||
var classname = opts.classname || 'js-shave'; | ||
var spaces = opts.spaces || false; | ||
var charHtml = '<span class="js-shave-char">' + character + '</span>'; | ||
@@ -33,11 +34,11 @@ var span = el.querySelector('.' + classname); | ||
// Temporarily remove any CSS height for text height calculation | ||
var heightStyle = el.style.height; | ||
el.style.height = 'auto'; | ||
var maxHeightStyle = el.style.maxHeight; | ||
el.style.maxHeight = 'none'; | ||
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) { | ||
el.style.height = heightStyle; | ||
el.style.maxHeight = maxHeightStyle; | ||
styles.height = heightStyle; | ||
styles.maxHeight = maxHeightStyle; | ||
return; | ||
@@ -63,8 +64,8 @@ } | ||
el.style.height = heightStyle; | ||
el.style.maxHeight = maxHeightStyle; | ||
styles.height = heightStyle; | ||
styles.maxHeight = maxHeightStyle; | ||
} | ||
/* global document, window */ | ||
function shave(target, maxHeight, opts) { | ||
if (!maxHeight) throw Error('maxHeight is required'); | ||
var els = typeof target === 'string' ? document.querySelectorAll(target) : target; | ||
@@ -74,6 +75,7 @@ if (!('length' in els)) els = [els]; | ||
var el = els[i]; | ||
shaver(el, maxHeight, opts); | ||
shaveEl(el, maxHeight, opts); | ||
} | ||
} | ||
/* global window */ | ||
if (typeof window !== 'undefined') { | ||
@@ -89,4 +91,2 @@ var plugin = window.$ || window.jQuery || window.Zepto; | ||
return shave; | ||
}))); |
@@ -7,10 +7,11 @@ (function (global, factory) { | ||
/* global document, window */ | ||
function shaver(target, maxHeight, opts) { | ||
if (!maxHeight) throw Error('maxHeight is required'); | ||
/* global document */ | ||
function shaveEl(target, maxHeight) { | ||
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var el = target; | ||
var character = opts && opts.character || '…'; | ||
var classname = opts && opts.classname || 'js-shave'; | ||
var spaces = true; | ||
if (opts && opts.spaces === false) spaces = false; | ||
var styles = el.style; | ||
var character = opts.character || '…'; | ||
var classname = opts.classname || 'js-shave'; | ||
var spaces = opts.spaces || false; | ||
var charHtml = '<span class="js-shave-char">' + character + '</span>'; | ||
@@ -34,11 +35,11 @@ var span = el.querySelector('.' + classname); | ||
// Temporarily remove any CSS height for text height calculation | ||
var heightStyle = el.style.height; | ||
el.style.height = 'auto'; | ||
var maxHeightStyle = el.style.maxHeight; | ||
el.style.maxHeight = 'none'; | ||
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) { | ||
el.style.height = heightStyle; | ||
el.style.maxHeight = maxHeightStyle; | ||
styles.height = heightStyle; | ||
styles.maxHeight = maxHeightStyle; | ||
return; | ||
@@ -64,12 +65,13 @@ } | ||
el.style.height = heightStyle; | ||
el.style.maxHeight = maxHeightStyle; | ||
styles.height = heightStyle; | ||
styles.maxHeight = maxHeightStyle; | ||
} | ||
/* global document, window */ | ||
function shave(target, maxHeight, opts) { | ||
var els = document.querySelectorAll(target); | ||
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]; | ||
shaver(el, maxHeight, opts); | ||
shaveEl(el, maxHeight, opts); | ||
} | ||
@@ -76,0 +78,0 @@ } |
/** | ||
* shave - Shave is a javascript plugin that truncates multi-line text within a html element based on set max height | ||
* @version v2.0.0 | ||
* @version v2.0.2 | ||
* @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,n){if(!t)throw Error("maxHeight is required");var s=e,i=n&&n.character||"…",r=n&&n.classname||"js-shave",o=!0;n&&n.spaces===!1&&(o=!1);var a='<span class="js-shave-char">'+i+"</span>",l=s.querySelector("."+r),c=void 0===s.textContent?"innerText":"textContent";l&&(s.removeChild(s.querySelector(".js-shave-char")),s[c]=s[c]);var h=s[c],f=o?h.split(" "):h;if(!(f.length<2)){var d=s.style.height;s.style.height="auto";var u=s.style.maxHeight;if(s.style.maxHeight="none",s.offsetHeight<=t)return s.style.height=d,void(s.style.maxHeight=u);for(var v=f.length-1,y=0,g=void 0;y<v;)g=y+v+1>>1,s[c]=o?f.slice(0,g).join(" "):f.slice(0,g),s.insertAdjacentHTML("beforeend",a),s.offsetHeight>t?v=o?g-1:g-2:y=g;s[c]=o?f.slice(0,v).join(" "):f.slice(0,v),s.insertAdjacentHTML("beforeend",a);var p=o?f.slice(v).join(" "):f.slice(v);s.insertAdjacentHTML("beforeend",'<span class="'+r+'" style="display:none;">'+p+"</span>"),s.style.height=d,s.style.maxHeight=u}}function t(t,n,s){for(var i=document.querySelectorAll(t),r=0;r<i.length;r+=1){e(i[r],n,s)}}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]:{},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}); |
{ | ||
"name": "shave", | ||
"version": "2.0.0", | ||
"version": "2.0.2", | ||
"description": "Shave is a javascript plugin that truncates multi-line text within a html element based on set max height", | ||
@@ -41,22 +41,23 @@ "main": "dist/shave.js", | ||
"devDependencies": { | ||
"babel-core": "^6.14.0", | ||
"babel-preset-es2015": "^6.14.0", | ||
"babel-core": "^6.25.0", | ||
"babel-preset-es2015": "^6.24.1", | ||
"babel-preset-es2015-rollup": "^3.0.0", | ||
"bower": "^1.7.9", | ||
"debug": "^2.2.0", | ||
"eslint": "^3.4.0", | ||
"debug": "^2.6.8", | ||
"eslint": "^3.19.0", | ||
"eslint-config-airbnb": "14.1.0", | ||
"eslint-plugin-import": "^2.7.0", | ||
"gulp": "^3.9.1", | ||
"gulp-header": "^1.8.8", | ||
"gulp-qunit": "^1.5.0", | ||
"gulp-rename": "^1.2.2", | ||
"gulp-uglify": "^2.0.0", | ||
"node-qunit-phantomjs": "^1.5.0", | ||
"rollup": "^0.41.6", | ||
"rollup-plugin-babel": "^2.6.1", | ||
"rollup-plugin-babel": "^2.7.1", | ||
"rollup-plugin-commonjs": "^8.0.2", | ||
"rollup-plugin-eslint": "^3.0.0", | ||
"rollup-plugin-node-resolve": "^2.0.0", | ||
"rollup-plugin-uglify": "^1.0.1", | ||
"gulp-qunit": "^1.5.0", | ||
"node-qunit-phantomjs": "^1.5.0" | ||
"rollup-plugin-node-resolve": "^2.1.1", | ||
"rollup-plugin-uglify": "^1.0.2" | ||
} | ||
} |
@@ -1,21 +0,12 @@ | ||
/* global document, window */ | ||
import shaver from './shaver'; | ||
/* global window */ | ||
import shave from './shave' | ||
export default function shave(target, maxHeight, opts) { | ||
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]; | ||
shaver(el, maxHeight, opts); | ||
} | ||
} | ||
if (typeof window !== 'undefined') { | ||
const plugin = window.$ || window.jQuery || window.Zepto; | ||
const plugin = window.$ || window.jQuery || window.Zepto | ||
if (plugin) { | ||
plugin.fn.shave = function shavePlugin(maxHeight, opts) { | ||
shave(this, maxHeight, opts); | ||
return this; | ||
}; | ||
shave(this, maxHeight, opts) | ||
return this | ||
} | ||
} | ||
} |
@@ -1,10 +0,69 @@ | ||
/* global document, window */ | ||
import shaver from './shaver'; | ||
/* global document */ | ||
function shaveEl(target, maxHeight, opts = {}) { | ||
const el = target | ||
const styles = el.style | ||
const character = opts.character || '…' | ||
const classname = opts.classname || 'js-shave' | ||
const spaces = opts.spaces || false | ||
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 | ||
} | ||
const fullText = el[textProp] | ||
const words = spaces ? fullText.split(' ') : fullText | ||
// If 0 or 1 words, we're done | ||
if (words.length < 2) return | ||
// Temporarily remove any CSS height for text height calculation | ||
const heightStyle = styles.height | ||
styles.height = 'auto' | ||
const maxHeightStyle = styles.maxHeight | ||
styles.maxHeight = 'none' | ||
// If already short enough, we're done | ||
if (el.offsetHeight <= maxHeight) { | ||
styles.height = heightStyle | ||
styles.maxHeight = maxHeightStyle | ||
return | ||
} | ||
// 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[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) | ||
el.insertAdjacentHTML('beforeend', | ||
`<span class="${classname}" style="display:none;">${diff}</span>`) | ||
styles.height = heightStyle | ||
styles.maxHeight = maxHeightStyle | ||
} | ||
export default function shave(target, maxHeight, opts) { | ||
const els = document.querySelectorAll(target); | ||
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]; | ||
shaver(el, maxHeight, opts); | ||
const el = els[i] | ||
shaveEl(el, maxHeight, opts) | ||
} | ||
} |
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
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
18963
20
221