-
Notifications
You must be signed in to change notification settings - Fork 24
/
jquery.ba-htmldoc.js
89 lines (69 loc) · 2.95 KB
/
jquery.ba-htmldoc.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
/*!
* jQuery htmlDoc "fixer" - v0.2pre - 12/15/2010
* http://benalman.com/projects/jquery-misc-plugins/
*
* Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
(function($){
var // RegExp that matches opening and closing HTML, HEAD, BODY tags.
// $1 = slash, $2 = tag name, $3 = attributes
rtag = /<(\/?)(html|head|body)(\s+[^>]*)?>/ig,
// Unique id prefix for selecting placeholder elements.
prefix = 'hd' + +new Date();
$.htmlDoc = function( str ) {
var // A collection of "intended" elements that can't be rendered
// cross-browser with .innerHTML, for which placeholders must be
// swapped.
elems = $([]),
// Input HTML string, parsed to include placeholder DIVs.
parsed,
// A node under which a temporary DOM tree can be constructed.
root;
// Replace HTML, HEAD, BODY tags with DIV placeholders.
parsed = str.replace( rtag, function( tag, slash, name, attrs ) {
var // Current intended / placeholder element index.
len = elems.length,
// Temporary object in which to hold attributes.
obj = {};
// If this is an opening tag...
if ( !slash ) {
// Add an element of this name into the collection of elements. Note
// that if a string of attributes is added at this point, it fails.
elems = elems.add( '<' + name + '/>' );
// If the original tag had attributes, create a temporary div with
// those attributes. Then, copy each attribute from the temporary div
// over to the temporary object.
if ( attrs ) {
$.each( $( '<div' + attrs + '/>' )[0].attributes, function(i,v){
obj[ v.name ] = v.value;
});
}
// Set the attributes of the intended object based on the attributes
// copied in the previous step.
elems.eq( len ).attr( obj );
}
// A placeholder div with a unique id replaces the intended element's
// tag in the parsed HTML string.
return '<' + slash + 'div'
+ ( slash ? '' : ' id="' + prefix + len + '"' ) + '>';
});
// If placeholder elements were necessary...
if ( elems.length ) {
// Create the root node and append the parsed, place-held HTML.
root = $('<div/>').html( parsed );
// Replace each placeholder element with its intended element.
$.each( elems, function(i,v){
var elem = root.find( '#' + prefix + i ).before( elems[i] );
elems.eq(i).html( elem.contents() );
elem.remove();
});
// Return the topmost intended element(s), sans text nodes.
return root.children();
}
// No placeholder elements were necessary, so just return a normal
// jQuery-parsed HTML string.
return $(str);
};
})(jQuery);