forked from huang47/nodejs-html-truncate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
57 lines (50 loc) · 1.41 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="./dist/truncate.js"></script>
<style>
textarea { display: block; width: 400px; height: 200px; font-size: 24px; }
input { font-size: 24px; }
button { font-size: 24px; }
ul { list-style-type: none; }
</style>
<title>HTML Truncate</title>
</head>
<body>
<textarea>hello world</textarea>
<input id="length" type="text" value="4">
<button>click</button>
<div>
<h2>options</h2>
<ul>
<li>keep image tag <input type="checkbox" id="keepImageTag"></li>
<li>customize suffix <input type="text" id="suffix" value="..."></li>
</ul>
</div>
<div>
<h2>result</h2>
<p id="result"></p>
</div>
<script type="text/javascript">
var $ = document.querySelector.bind(document),
input = $('textarea'),
output = $('#result'),
keepImageTag = $('input[type="checkbox"]'),
suffix = $('#suffix');
function escapeHTML(string) {
return string.replace(/>/g,'>').replace(/</g,'<').replace(/"/g,'"');
}
function run() {
var length = $('#length').value,
options = {
keepImageTag: keepImageTag.checked,
ellipsis: suffix.value
}
output.innerHTML = escapeHTML(window.truncate(input.value, length, options));
}
var button = $('button');
button.onclick = run;
run();
</script>
</body>
</html>