var span = document.getElementById("text");
var countSpan = document.getElementById("count");
var orig = span.innerText;
var vis = orig;
while(vis.length && span.scrollWidth > parseFloat(window.getComputedStyle(span).width)
{
vis = vis.slice(0,-1);
span.innerText = vis + "....";
}
span.innerText = orig;
var visibleText = vis;
var truncatedText = orig.replace(new RegExp("^" + vis.replace(/[.*+?^${}()|[\]\\]/g, '\\$&")), "");
var visibleCleanLessText;
var visibleCleanMoreText;
var truncatedCleanLessText;
var truncatedCleanMoreText;
if (truncatedText === "" || truncatedText.substr(0, 1) == ":")
{
visibleCleanLessText = vis;
visibleCleanMoreText = vis;
truncatedCleanLessText = truncatedText.substr(1);
truncatedCleanMoreText = truncatedCleanLessText;
}
else
{
var m = vis.match(/:([^:]*)$/);
visibleCleanLessText = m ? vis.replace(/:[^:]*$/, "") : "";
visibleCleanMoreText = vis + (truncatedText.match(/^([^:]*):/) || ["",""])[1];
truncatedCleanMoreText = (m ? m[1] : vis) + truncatedText;
truncatedCleanLessText = truncatedText.replace(/^[^:]*:/, "");
}
var truncatedCount = truncatedCleanMoreText === "" ? 0 : truncatedCleanMoreText.split(":").length;
if (truncatedCount)
{
countSpan.innerText = "+" + truncatedCount;
span.title = orig;
}
console.log("orig ", orig);
console.log("visible ", vis);
console.log("truncated ", truncatedText);
console.log("visibleCleanLess ", visibleCleanLessText);
console.log("truncatedCleanMore", truncatedCleanMoreText);
console.log("visibleCleanMore ", visibleCleanMoreText);
console.log("truncatedCleanLess", truncatedCleanLessText);
div
{
vertical-align: middle;
}
.truncate
{
width: 300px;
display: inline-block;
overflow: hidden;
text-overflow: ellipsis;
vertical-align: middle;
}
.count:empty
{
display: none;
}
.count
{
display: inline-block;
position: relative;
font-family: monospace;
font-size: 90%;
left: -0.5em;
border: 1px solid black;
border-radius: 0.3em;
padding: 0.1em;
}
<div>
<span id="text" class="truncate"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="32575f535b5e0372574a535f425e571c515d5f">[email protected]</a>;<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="46232b272f2a7406233e272b362a236825292b">[email protected]</a>;<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e6838b878f8ad5a6839e878b968a83c885898b">[email protected]</a>;<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fa9f979b9396ceba9f829b978a969fd4999597">[email protected]</a></span>
<span id="count" class="count"></span>
</div>