-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.htmlclean.js
executable file
·180 lines (177 loc) · 5.67 KB
/
jquery.htmlclean.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**************************************************************************
Name: HTML cleaner for the BIUS Editor for jQuery
Copyright: (c) 2011, Rodney Giles <rodney@rodneygiles.net>
License: Creative Commons Attribution v3.0
Version: 0.11
***************************************************************************/
(function($) {
// Private Variables and Methods
function __regex(str, reg, fn) {
if(!str)
return '';
var result = [];
var lastidx = reg.lastIndex;
var re;
while((re = reg.exec(str)) != null){
var idx = re.index;
var args = re.concat(idx, str);
result.push(
str.slice(lastidx,idx),
fn.apply(null,args).toString()
);
if(!reg.global){
lastidx += RegExp.lastMatch.length;
break
}else{
lastidx = reg.lastIndex;
}
}
result.push(str.slice(lastidx));
return result.join('')
};
// Namespace Default Function
$.htmlCleaner = function(html, fn) {
if(!fn) switch(true) {
case $.browser.msie: fn = 'msie'; break;
case $.browser.mozilla: fn = 'mozilla'; break;
case $.browser.safari: fn = 'safari'; break;
case $.browser.opera: fn = 'opera'; break;
case $.browser.chrome: fn = 'chrome'; break;
}
if(window.console && window.console.log)
console.log({cleaner: fn, html: html});
if(typeof fn == 'string')
fn = $.htmlCleaner[fn];
if(typeof fn == 'function')
return fn.call($.htmlCleaner, html);
return html;
};
// Public Variables and Methods
$.extend($.htmlCleaner, {
msie: function(html) {
html = $.htmlCleaner(html, 'lowercase');
return html;
},
mozilla: function(html) {
html = $.htmlCleaner(html, 'div_align');
html = $.htmlCleaner(html, 'hr_mozilla');
html = $.htmlCleaner(html, 'lowercase');
html = $.htmlCleaner(html, 'single_tags');
return html;
},
chrome: function(html) {
html = $.htmlCleaner(html, 'webkit_style');
html = $.htmlCleaner(html, 'webkit_indent');
html = $.htmlCleaner(html, 'blockquote_p');
html = $.htmlCleaner(html, 'single_tags');
html = $.htmlCleaner(html, 'block_style');
html = $.htmlCleaner(html, 'p_safari');
return html;
},
safari: function(html) {
html = $.htmlCleaner(html, 'webkit_style');
html = $.htmlCleaner(html, 'webkit_indent');
html = $.htmlCleaner(html, 'blockquote_p');
html = $.htmlCleaner(html, 'single_tags');
html = $.htmlCleaner(html, 'block_style');
html = $.htmlCleaner(html, 'p_safari');
return html;
},
opera: function(html) {
html = $.htmlCleaner(html, 'blockquote_p');
html = $.htmlCleaner(html, 'hr_block');
html = $.htmlCleaner(html, 'lowercase');
html = $.htmlCleaner(html, 'single_tags');
//html = $.htmlCleaner(html, 'p_opera');
return html;
},
webkit_style: function(html) {
return $('<div>' + html + '</div>').find('.Apple-style-span').each(function() {
var style = $(this).attr('style');
var doc = $.htmlCleaner($(this).html(), 'webkit');
if(style.indexOf('bold') >= 0) doc = '<b>' + doc + '</b>';
if(style.indexOf('italic') >= 0) doc = '<i>' + doc + '</i>';
if(style.indexOf('underline') >= 0) doc = '<u>' + doc + '</u>';
if(style.indexOf('line-through') >= 0) doc = '<strike>' + doc + '</strike>';
$(this).before(doc).remove();
}).end().html();
},
webkit_indent: function(html) {
return $('<div>' + html + '</div>').find('.webkit-indent-blockquote').each(function() {
var align = $(this).css('text-align');
align = align? ' align="' + align + '"' : '';
var doc = '<blockquote' + align + '>' + $(this).html() + '</blockquote>';
$(this).before(doc).remove();
}).end().html();
},
div_align: function(html) {
return $('<div>' + html + '</div>').find('div[@align]').each(function() {
$(this).contents().attr('align', $(this).attr('align')).insertBefore(this);
$(this).remove();
}).end().html();
},
blockquote_p: function(html) {
return $('<div>' + html + '</div>').find('blockquote').each(function() {
$(this).html('<p>' + $(this).html() + '</p>');
}).end().html();
},
hr_block: function(html) {
return $('<div>' + html + '</div>').find('hr').each(function() {
var p = $(this).parent();
var block = p.is('p, blockquote');
alert(p.html());
alert(p[0]);
var first = block && this == p.contents()[0];
var last = block && this == p.contents(':last')[0];
if(first) p.before(this);
if(last) p.after(this);
if(first || last) p.parent().html(this.hr_block(p.parent().html()));
}).end().html();
},
lowercase: function(html) {
return __regex(html, /<(\/)?(\w+)([^>]+)?>/g, function(_, sl, tag, args) {
if(!sl) sl = '';
args = __regex(args, / (\w+)="/g, function(_, arg) {
return ' ' + arg.toLowerCase() + '="';
});
return '<' + sl + tag.toLowerCase()+ args + '>';
});
},
hr_mozilla: function(html) {
return __regex(html, /<hr size="2" width="100%">/g, function(_, sl, tag, args) {
return '<hr />';
});
},
p_safari: function(html) {
return __regex(html, /<p><\/p>/g, function(_) {
return '';
});
},
p_opera: function(html) {
return __regex(html, /<p><(blockquote|ul|ol)/g, function(_, tag) {
return '<' + tag;
});
},
p_block: function(html) {
return __regex(html, /<p><\/p>/g, function(_) {
return '';
});
},
single_tags: function(html) {
return __regex(html, /<((hr|img|input|br)([^\/>]*))>/g, function(_, all, tag, args) {
return '<' + all + ' />';
});
},
block_style: function(html) {
return __regex(html, /<(p|ol|ul|address|blockquote) style="text-align: (\w+);">/g, function(_, tag, dir) {
return '<' + tag + ' align="' + dir + '">';
});
}
});
// Private Variables and Methods
$.fn.extend({
htmlCleaner: function() {
$.htmlCleaner($(this).html());
}
});
})(jQuery);