This repository has been archived by the owner. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
jquery.fancyletter.js
150 lines (124 loc) · 3.57 KB
/
jquery.fancyletter.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
/*!
* jQuery Fancy Letter Plugin v1.1
*
* Date: Sun Feb 06 20:51:59 2011 EST
* Requires: jQuery v1.2.6+
*
* Copyright 2011, Karl Swedberg
* Dual licensed under the MIT and GPL licenses (just like jQuery):
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* Contributor: Matt Wiebe (::first-letter spec semi-conformance)
*
*
*/
(function($) {
$.fn.fancyletter = function(options) {
this.each(function() {
var text, firstHtml, firstLetter, firstltr, nodeInfo,
node = this,
$this = $(this),
opts = $.extend({}, $.fn.fancyletter.defaults, options || {}, $.metadata ? $this.metadata() : $.meta ? $this.data() : {}),
re = new RegExp(opts.characters);
nodeInfo = getNodeValue(node, opts);
if ( !nodeInfo.firstChar ) {
return;
}
text = nodeInfo.val;
node = nodeInfo.node;
firstLetter = nodeInfo.firstChar;
firstltr = firstLetter.toLowerCase();
if ( re.test(firstLetter) ) {
var $span = $('<span></span>');
node.nodeValue = text.slice( nodeInfo.slicePoint );
$span.html(nodeInfo.pl + firstLetter + nodeInfo.pr);
$span.addClass(opts.commonClass + ' ' + opts.ltrClassPrefix + firstltr);
$span.prependTo(this);
}
});
function buildPunctuationSpan( puncSide, puncMark, opts ) {
var puncSet = opts['punctuation' + puncSide];
return [
'<span class="',
opts.punctuationClass,
' ',
opts.ltrClassPrefix + puncSet[ puncMark ],
'">',
puncMark,
'</span>'
].join('');
}
function getPunctuation( val, opts ) {
var pl = '',
pr = '',
slicePoint = 0,
firstChar = val.charAt( slicePoint ),
puncRet = {};
while ( (firstChar in opts.punctuationLeft) ) {
slicePoint++;
pl += buildPunctuationSpan( 'Left', firstChar, opts );
firstChar = val.charAt( slicePoint );
}
puncRet.pl = pl;
puncRet.firstChar = firstChar;
slicePoint++;
firstChar = val.charAt( slicePoint );
while ( (firstChar in opts.punctuationRight) ) {
slicePoint++;
pr += buildPunctuationSpan( 'Right', firstChar, opts );
firstChar = val.charAt( slicePoint );
}
puncRet.pr = pr;
puncRet.slicePoint = slicePoint;
return puncRet;
}
function getNodeValue(node, opts) {
var startNode = node,
startChild = node.firstChild,
val = '',
ret = {};
if (!node ) {
return ret;
}
while (node.childNodes.length) {
node = node.firstChild;
}
val = node.nodeValue && node.nodeValue.replace(/^\s\s*/,'') || '';
if ( val === '' && startChild && startChild.nextSibling ) {
return getNodeValue(startChild.nextSibling, opts);
} else {
ret = getPunctuation( val, opts );
ret.node = node;
ret.val = val;
return ret;
}
}
return this;
};
$.fn.fancyletter.defaults = {
commonClass: 'fancy-letter',
ltrClassPrefix: 'ltr-',
characters: '[a-zA-Z0-9]',
groupPunctuation: true,
punctuationClass: 'punct',
punctuationLeft: {
'"': 'dquo',
"'": 'squo',
'“': 'ldquo',
'‘': 'lsquo',
'«': 'laquo',
'‹': 'lsaquo',
'(': 'lparen'
},
punctuationRight: {
'"': 'dquo',
"'": 'squo',
'”': 'rdquo',
'’': 'rsquo',
'»': 'raquo',
'›': 'rsaquo',
')': 'rparen'
}
};
})(jQuery);