-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
163 lines (130 loc) · 4.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
'use strict';
/**
* @param {HTMLElement} element
* @param {string} property
* @return {string}
*/
function getStyleValue(element, property) {
return window.getComputedStyle(element, null).getPropertyValue(property);
}
/**
* @param {HTMLElement} element
* @return {number}
*/
function getLineHeight(element) {
var lineHeight = getStyleValue(element, 'line-height');
if (lineHeight === 'normal') {
return parseInt(getStyleValue(element, 'font-size'), 10) * 1.25;
}
return parseFloat(lineHeight);
}
/**
* @param {HTMLElement} element
*/
function setupLineHeight(element) {
for (var index = 0; index < element.childNodes.length; index++) {
var childNode = element.childNodes[index];
if (childNode.nodeType === 1) {
childNode.inlineCssText = childNode.style.cssText;
childNode.style.lineHeight = '100%';
}
}
}
/**
* @param {HTMLElement} element
*/
function removeLineHeight(element) {
for (var index = 0; index < element.childNodes.length; index++) {
var childNode = element.childNodes[index];
if (childNode.nodeType === 1) {
childNode.removeAttribute('style');
if (childNode.inlineCssText) {
childNode.style.cssText = childNode.inlineCssText;
}
}
}
}
/**
* @param {HTMLElement} textNode
* @param {HTMLElement} rootElement
* @param {number} maxHeight
* @return {boolean}
*/
function truncateTextNode(textNode, rootElement, maxHeight) {
var textContent = textNode.textContent;
textNode.textContent = '';
if (rootElement.clientHeight > maxHeight) return false;
var chunks = textContent.split(' ');
while (chunks.pop()) {
textNode.textContent = chunks.join(' ');
if (rootElement.clientHeight <= maxHeight) {
textNode.textContent = textContent;
break;
}
textContent = textNode.textContent;
}
var length = textContent.length;
while (length > 2) {
textContent = textContent.substring(0, --length);
textNode.textContent = textContent + '…';
if (rootElement.clientHeight <= maxHeight) return true;
}
return false;
}
/**
* @param {HTMLElement} element
* @param {HTMLElement} rootElement
* @param {number} maxHeight
* @param {number} lineHeight
* @return {boolean}
*/
function truncateElementNode(element, rootElement, maxHeight, lineHeight) {
var childNodes = element.childNodes;
var length = childNodes.length - 1;
while (length > -1) {
var childNode = childNodes[length--];
var func = childNode.nodeType === 1 ? truncateElementNode : truncateTextNode;
if (func(childNode, rootElement, maxHeight, lineHeight)) return true;
element.removeChild(childNode);
}
return false;
}
/**
* @param {HTMLElement} rootElement
* @param {number} lineCount
*/
function truncate(rootElement, lineCount) {
var lineHeight = getLineHeight(rootElement);
var maxHeight = Math.round(lineHeight * lineCount);
if (rootElement.clientHeight <= maxHeight) return;
setupLineHeight(rootElement);
truncateElementNode(rootElement, rootElement, maxHeight, lineHeight);
removeLineHeight(rootElement);
}
/**
* @param {HTMLElement} rootElement
* @param {number} lineCount
*/
function native(rootElement, lineCount) {
rootElement.style.overflow = 'hidden';
rootElement.style.textOverflow = 'ellipsis';
rootElement.style.webkitBoxOrient = 'vertical';
rootElement.style.display = '-webkit-box';
rootElement.style.webkitLineClamp = lineCount;
}
/**
* @param {HTMLElement} element
* @param {number} lineCount
*/
function webkitLineClamp(element, lineCount) {
if (!lineCount) return;
(typeof element.style.webkitLineClamp === 'undefined' ? truncate : native)(element, lineCount);
}
(function wrapper(root, factory) {
if (typeof exports === 'object') {
module.exports = factory;
module.exports.webkitLineClamp = factory;
} else {
root.webkitLineClamp = factory;
}
}(this, webkitLineClamp));