1
1
/*
2
- HTML Clean for jQuery
3
- Anthony Johnston
4
- http://www.antix.co.uk
5
-
6
- version 1.4.0
2
+ HTML Clean for jQuery
3
+ Anthony Johnston
4
+ http://www.antix.co.uk
7
5
8
- $Revision: 99 $
6
+ version 1.4.0
9
7
10
- requires jQuery http://jquery.com
8
+ $Revision: 99 $
11
9
12
- Use and distibution http://www.opensource.org/licenses/bsd-license.php
10
+ requires jQuery http://jquery.com
13
11
14
- 2010-04-02 allowedTags/removeTags added (white/black list) thanks to David Wartian (Dwartian)
15
- 2010-06-30 replaceStyles added for replacement of bold, italic, super and sub styles on a tag
16
- 2012-04-30 allowedAttributes added, an array of attributed allowed on the elements
17
- 2013-02-25 now will push non-inline elements up the stack if nested in an inline element
18
- 2013-02-25 comment element support added, removed by default, see AllowComments in options
19
- */
12
+ Use and distibution http://www.opensource.org/licenses/bsd-license.php
13
+
14
+ 2010-04-02 allowedTags/removeTags added (white/black list) thanks to David Wartian (Dwartian)
15
+ 2010-06-30 replaceStyles added for replacement of bold, italic, super and sub styles on a tag
16
+ 2012-04-30 allowedAttributes added, an array of attributed allowed on the elements
17
+ 2013-02-25 now will push non-inline elements up the stack if nested in an inline element
18
+ 2013-02-25 comment element support added, removed by default, see AllowComments in options
19
+ */
20
20
( function ( $ ) {
21
21
$ . fn . htmlClean = function ( options ) {
22
22
// iterate and html clean each matched element
@@ -61,7 +61,7 @@ Use and distibution http://www.opensource.org/licenses/bsd-license.php
61
61
if ( text . length > 0 ) {
62
62
var child = container . children [ container . children . length - 1 ] ;
63
63
if ( container . children . length > 0
64
- && isText ( child = container . children [ container . children . length - 1 ] ) ) {
64
+ && isText ( child = container . children [ container . children . length - 1 ] ) ) {
65
65
// merge text
66
66
container . children [ container . children . length - 1 ] = child . concat ( text ) ;
67
67
} else {
@@ -107,8 +107,8 @@ Use and distibution http://www.opensource.org/licenses/bsd-license.php
107
107
}
108
108
109
109
if ( tag . allowedAttributes != null
110
- && ( tag . allowedAttributes . length == 0
111
- || $ . inArray ( attrMatch [ 1 ] , tag . allowedAttributes ) > - 1 ) ) {
110
+ && ( tag . allowedAttributes . length == 0
111
+ || $ . inArray ( attrMatch [ 1 ] , tag . allowedAttributes ) > - 1 ) ) {
112
112
element . attributes . push ( new Attribute ( attrMatch [ 1 ] , attrMatch [ 2 ] ) ) ;
113
113
}
114
114
}
@@ -123,7 +123,7 @@ Use and distibution http://www.opensource.org/licenses/bsd-license.php
123
123
for ( var tagIndex = 0 ; tagIndex < options . replace [ repIndex ] [ 0 ] . length ; tagIndex ++ ) {
124
124
var byName = typeof ( options . replace [ repIndex ] [ 0 ] [ tagIndex ] ) == "string" ;
125
125
if ( ( byName && options . replace [ repIndex ] [ 0 ] [ tagIndex ] == tag . name )
126
- || ( ! byName && options . replace [ repIndex ] [ 0 ] [ tagIndex ] . test ( tagMatch ) ) ) {
126
+ || ( ! byName && options . replace [ repIndex ] [ 0 ] [ tagIndex ] . test ( tagMatch ) ) ) {
127
127
128
128
// set the name to the replacement
129
129
tag . rename ( options . replace [ repIndex ] [ 1 ] ) ;
@@ -142,7 +142,7 @@ Use and distibution http://www.opensource.org/licenses/bsd-license.php
142
142
container = stack [ stack . length - 1 ] ;
143
143
}
144
144
} else if ( container . tag . disallowNest && tag . disallowNest
145
- && ! tag . requiredParent ) {
145
+ && ! tag . requiredParent ) {
146
146
add = false ;
147
147
} else if ( tag . requiredParent ) {
148
148
if ( add = popToTagName ( stack , tag . requiredParent ) ) {
@@ -198,7 +198,7 @@ Use and distibution http://www.opensource.org/licenses/bsd-license.php
198
198
format : false ,
199
199
// format indent to start on
200
200
formatIndent : 0 ,
201
- // tags to replace, and what to replace with, tag name or regex to match the tag and attributes
201
+ // tags to replace, and what to replace with, tag name or regex to match the tag and attributes
202
202
replace : [
203
203
[ [ "b" , "big" ] , "strong" ] ,
204
204
[ [ "i" ] , "em" ]
@@ -237,8 +237,8 @@ Use and distibution http://www.opensource.org/licenses/bsd-license.php
237
237
// don't render if not in allowedTags or in removeTags
238
238
var renderTag
239
239
= element . tag . render
240
- && ( options . allowedTags . length == 0 || $ . inArray ( element . tag . name , options . allowedTags ) > - 1 )
241
- && ( options . removeTags . length == 0 || $ . inArray ( element . tag . name , options . removeTags ) == - 1 ) ;
240
+ && ( options . allowedTags . length == 0 || $ . inArray ( element . tag . name , options . allowedTags ) > - 1 )
241
+ && ( options . removeTags . length == 0 || $ . inArray ( element . tag . name , options . removeTags ) == - 1 ) ;
242
242
243
243
if ( ! element . isRoot && renderTag ) {
244
244
@@ -251,16 +251,16 @@ Use and distibution http://www.opensource.org/licenses/bsd-license.php
251
251
var value = m [ 2 ] ;
252
252
var valueQuote = m [ 1 ] || "'" ;
253
253
254
- // check for classes allowed
254
+ // check for classes allowed
255
255
if ( this . name == "class" && options . allowedClasses . length > 0 ) {
256
256
value =
257
- $ . grep ( value . split ( " " ) , function ( c ) {
258
- return $ . grep ( options . allowedClasses , function ( a ) {
259
- return a == c
260
- || ( a [ 0 ] == c && ( a . length == 1 || $ . inArray ( element . tag . name , a [ 1 ] ) > - 1 ) ) ;
261
- } ) . length > 0 ;
262
- } )
263
- . join ( " " ) ;
257
+ $ . grep ( value . split ( " " ) , function ( c ) {
258
+ return $ . grep ( options . allowedClasses , function ( a ) {
259
+ return a == c
260
+ || ( a [ 0 ] == c && ( a . length == 1 || $ . inArray ( element . tag . name , a [ 1 ] ) > - 1 ) ) ;
261
+ } ) . length > 0 ;
262
+ } )
263
+ . join ( " " ) ;
264
264
}
265
265
266
266
if ( value != null && ( value . length > 0 || $ . inArray ( this . name , element . tag . requiredAttributes ) > - 1 ) ) {
@@ -276,7 +276,7 @@ Use and distibution http://www.opensource.org/licenses/bsd-license.php
276
276
}
277
277
278
278
if ( element . tag . isSelfClosing ) {
279
- // self closing
279
+ // self closing
280
280
if ( renderTag ) output . push ( " />" ) ;
281
281
empty = false ;
282
282
} else if ( element . tag . isNonClosing ) {
@@ -301,7 +301,7 @@ Use and distibution http://www.opensource.org/licenses/bsd-license.php
301
301
var text = $ . htmlClean . trim ( textClean ( isText ( child ) ? child : child . childrenToString ( ) ) ) ;
302
302
if ( isInline ( child ) ) {
303
303
if ( i > 0 && text . length > 0
304
- && ( startsWithWhitespace ( child ) || endsWithWhitespace ( element . children [ i - 1 ] ) ) ) {
304
+ && ( startsWithWhitespace ( child ) || endsWithWhitespace ( element . children [ i - 1 ] ) ) ) {
305
305
outputChildren . push ( " " ) ;
306
306
}
307
307
}
@@ -365,7 +365,7 @@ Use and distibution http://www.opensource.org/licenses/bsd-license.php
365
365
if ( test ( element ) ) {
366
366
return true ;
367
367
} else if ( stack . length - index > 0
368
- && pop ( stack , test , index + 1 ) ) {
368
+ && pop ( stack , test , index + 1 ) ) {
369
369
stack . pop ( ) ;
370
370
return true ;
371
371
}
@@ -444,9 +444,9 @@ Use and distibution http://www.opensource.org/licenses/bsd-license.php
444
444
for ( var i = 0 ; i < options . allowedAttributes . length ; i ++ ) {
445
445
var attrName = options . allowedAttributes [ i ] [ 0 ] ;
446
446
if ( (
447
- options . allowedAttributes [ i ] . length == 1
447
+ options . allowedAttributes [ i ] . length == 1
448
448
|| $ . inArray ( this . name , options . allowedAttributes [ i ] [ 1 ] ) > - 1
449
- ) && $ . inArray ( attrName , cacheItem ) == - 1 ) {
449
+ ) && $ . inArray ( attrName , cacheItem ) == - 1 ) {
450
450
cacheItem . push ( attrName ) ;
451
451
}
452
452
}
@@ -537,48 +537,48 @@ Use and distibution http://www.opensource.org/licenses/bsd-license.php
537
537
"tbody" , [ "table" ] ,
538
538
"tfoot" , [ "table" ] ,
539
539
"param" , [ "object" ]
540
- ] ;
540
+ ] ;
541
541
var tagProtect = [ "script" , "style" , "pre" , "code" ] ;
542
542
// tags which self close e.g. <br />
543
543
var tagSelfClosing = [ "area" , "base" , "br" , "col" , "command" , "embed" , "hr" , "img" , "input" , "keygen" , "link" , "meta" , "param" , "source" , "track" , "wbr" ] ;
544
544
// tags which do not close
545
545
var tagNonClosing = [ "!doctype" , "?xml" ] ;
546
546
// attributes allowed on tags
547
547
var tagAttributes = [
548
- [ "class" ] , // default, for all tags not mentioned
549
- "?xml" , [ ] ,
550
- "!doctype" , [ ] ,
551
- "a" , [ "accesskey" , "class" , "href" , "name" , "title" , "rel" , "rev" , "type" , "tabindex" ] ,
552
- "abbr" , [ "class" , "title" ] ,
553
- "acronym" , [ "class" , "title" ] ,
554
- "blockquote" , [ "cite" , "class" ] ,
555
- "button" , [ "class" , "disabled" , "name" , "type" , "value" ] ,
556
- "del" , [ "cite" , "class" , "datetime" ] ,
557
- "form" , [ "accept" , "action" , "class" , "enctype" , "method" , "name" ] ,
558
- "iframe" , [ "class" , "height" , "name" , "sandbox" , "seamless" , "src" , "srcdoc" , "width" ] ,
559
- "input" , [ "accept" , "accesskey" , "alt" , "checked" , "class" , "disabled" , "ismap" , "maxlength" , "name" , "size" , "readonly" , "src" , "tabindex" , "type" , "usemap" , "value" ] ,
560
- "img" , [ "alt" , "class" , "height" , "src" , "width" ] ,
561
- "ins" , [ "cite" , "class" , "datetime" ] ,
562
- "label" , [ "accesskey" , "class" , "for" ] ,
563
- "legend" , [ "accesskey" , "class" ] ,
564
- "link" , [ "href" , "rel" , "type" ] ,
565
- "meta" , [ "content" , "http-equiv" , "name" , "scheme" , "charset" ] ,
566
- "map" , [ "name" ] ,
567
- "optgroup" , [ "class" , "disabled" , "label" ] ,
568
- "option" , [ "class" , "disabled" , "label" , "selected" , "value" ] ,
569
- "q" , [ "class" , "cite" ] ,
570
- "script" , [ "src" , "type" ] ,
571
- "select" , [ "class" , "disabled" , "multiple" , "name" , "size" , "tabindex" ] ,
572
- "style" , [ "type" ] ,
573
- "table" , [ "class" , "summary" ] ,
574
- "th" , [ "class" , "colspan" , "rowspan" ] ,
575
- "td" , [ "class" , "colspan" , "rowspan" ] ,
576
- "textarea" , [ "accesskey" , "class" , "cols" , "disabled" , "name" , "readonly" , "rows" , "tabindex" ] ,
577
- "param" , [ "name" , "value" ] ,
578
- "embed" , [ "height" , "src" , "type" , "width" ]
579
- ] ;
548
+ [ "class" ] , // default, for all tags not mentioned
549
+ "?xml" , [ ] ,
550
+ "!doctype" , [ ] ,
551
+ "a" , [ "accesskey" , "class" , "href" , "name" , "title" , "rel" , "rev" , "type" , "tabindex" ] ,
552
+ "abbr" , [ "class" , "title" ] ,
553
+ "acronym" , [ "class" , "title" ] ,
554
+ "blockquote" , [ "cite" , "class" ] ,
555
+ "button" , [ "class" , "disabled" , "name" , "type" , "value" ] ,
556
+ "del" , [ "cite" , "class" , "datetime" ] ,
557
+ "form" , [ "accept" , "action" , "class" , "enctype" , "method" , "name" ] ,
558
+ "iframe" , [ "class" , "height" , "name" , "sandbox" , "seamless" , "src" , "srcdoc" , "width" ] ,
559
+ "input" , [ "accept" , "accesskey" , "alt" , "checked" , "class" , "disabled" , "ismap" , "maxlength" , "name" , "size" , "readonly" , "src" , "tabindex" , "type" , "usemap" , "value" ] ,
560
+ "img" , [ "alt" , "class" , "height" , "src" , "width" ] ,
561
+ "ins" , [ "cite" , "class" , "datetime" ] ,
562
+ "label" , [ "accesskey" , "class" , "for" ] ,
563
+ "legend" , [ "accesskey" , "class" ] ,
564
+ "link" , [ "href" , "rel" , "type" ] ,
565
+ "meta" , [ "content" , "http-equiv" , "name" , "scheme" , "charset" ] ,
566
+ "map" , [ "name" ] ,
567
+ "optgroup" , [ "class" , "disabled" , "label" ] ,
568
+ "option" , [ "class" , "disabled" , "label" , "selected" , "value" ] ,
569
+ "q" , [ "class" , "cite" ] ,
570
+ "script" , [ "src" , "type" ] ,
571
+ "select" , [ "class" , "disabled" , "multiple" , "name" , "size" , "tabindex" ] ,
572
+ "style" , [ "type" ] ,
573
+ "table" , [ "class" , "summary" ] ,
574
+ "th" , [ "class" , "colspan" , "rowspan" ] ,
575
+ "td" , [ "class" , "colspan" , "rowspan" ] ,
576
+ "textarea" , [ "accesskey" , "class" , "cols" , "disabled" , "name" , "readonly" , "rows" , "tabindex" ] ,
577
+ "param" , [ "name" , "value" ] ,
578
+ "embed" , [ "height" , "src" , "type" , "width" ]
579
+ ] ;
580
580
var tagAttributesRequired = [ [ ] , "img" , [ "alt" ] ] ;
581
581
// white space chars
582
- var whitespace = [ " " , " " , "\t" , "\n" , "\r" , "\f" ] ;
582
+ var whitespace = [ " " , " " , "\t" , "\n" , "\r" , "\f" ] ;
583
583
584
584
} ) ( jQuery ) ;
0 commit comments