root / snf-astakos-app / astakos / im / static / im / js / html5shiv-printshiv.js @ 0f4a8a68
History | View | Annotate | Download (8.7 kB)
1 |
/*! HTML5 Shiv v3 | @jon_neal @afarkas @rem | MIT/GPL2 Licensed */ |
---|---|
2 |
(function (win, doc) {
|
3 |
// feature detection: whether the browser supports unknown elements
|
4 |
var supportsUnknownElements = (function (a) { |
5 |
a.innerHTML = '<x-element></x-element>';
|
6 |
return a.childNodes.length === 1; |
7 |
})(doc.createElement('a'));
|
8 |
|
9 |
// feature detection: whether the browser supports default html5 styles
|
10 |
var supportsHtml5Styles = (function (nav, docEl, compStyle) { |
11 |
docEl.appendChild(nav); |
12 |
return (compStyle = (compStyle ? compStyle(nav) : nav.currentStyle).display) && docEl.removeChild(nav) && compStyle === 'block'; |
13 |
})(doc.createElement('nav'), doc.documentElement, win.getComputedStyle);
|
14 |
|
15 |
// html5 global so that more elements can be shived and also so that existing shiving can be detected on iframes
|
16 |
// more elements can be added and shived with the following code: html5.elements.push('element-name'); shivDocument(document);
|
17 |
var html5 = {
|
18 |
// a list of html5 elements
|
19 |
elements: 'abbr article aside audio bdi canvas data datalist details figcaption figure footer header hgroup mark meter nav output progress section summary time video'.split(' '), |
20 |
|
21 |
// the shiv function
|
22 |
shivDocument: function (scopeDocument) { |
23 |
scopeDocument = scopeDocument || doc; |
24 |
|
25 |
// test if the document has already been shived
|
26 |
if (scopeDocument.documentShived) {
|
27 |
return;
|
28 |
} |
29 |
scopeDocument.documentShived = true;
|
30 |
|
31 |
// set local variables
|
32 |
var
|
33 |
documentCreateElement = scopeDocument.createElement, |
34 |
documentCreateDocumentFragment = scopeDocument.createDocumentFragment, |
35 |
documentHead = scopeDocument.getElementsByTagName('head')[0], |
36 |
documentCreateElementReplaceFunction = function (m) { |
37 |
documentCreateElement(m); |
38 |
}; |
39 |
|
40 |
// shiv for unknown elements
|
41 |
if (!supportsUnknownElements) {
|
42 |
// shiv the document
|
43 |
html5.elements.join(' ').replace(/\w+/g, documentCreateElementReplaceFunction); |
44 |
|
45 |
// shiv document create element function
|
46 |
scopeDocument.createElement = function (nodeName) { |
47 |
var element = documentCreateElement(nodeName);
|
48 |
if (element.canHaveChildren){
|
49 |
html5.shivDocument(element.document); |
50 |
} |
51 |
return element;
|
52 |
}; |
53 |
|
54 |
// shiv document create element function
|
55 |
scopeDocument.createDocumentFragment = function () { |
56 |
return html5.shivDocument(documentCreateDocumentFragment());
|
57 |
}; |
58 |
} |
59 |
|
60 |
// shiv for default html5 styles
|
61 |
if (!supportsHtml5Styles && documentHead) {
|
62 |
var div = documentCreateElement('div'); |
63 |
div.innerHTML = ['x<style>',
|
64 |
'article,aside,details,figcaption,figure,footer,header,hgroup,nav,section{display:block}', // Corrects block display not defined in IE6/7/8/9 |
65 |
'audio{display:none}', // Corrects audio display not defined in IE6/7/8/9 |
66 |
'canvas,video{display:inline-block;*display:inline;*zoom:1}', // Corrects canvas and video display not defined in IE6/7/8/9 (audio[controls] in IE7) |
67 |
'[hidden]{display:none}audio[controls]{display:inline-block;*display:inline;*zoom:1}', // Corrects 'hidden' attribute and audio[controls] display not present in IE7/8/9 |
68 |
'mark{background:#FF0;color:#000}', // Addresses styling not present in IE6/7/8/9 |
69 |
'</style>'].join(''); |
70 |
documentHead.insertBefore(div.lastChild, documentHead.firstChild); |
71 |
} |
72 |
|
73 |
// return document (for potential chaining)
|
74 |
return scopeDocument;
|
75 |
} |
76 |
}; |
77 |
|
78 |
// shiv the document
|
79 |
html5.shivDocument(doc); |
80 |
|
81 |
win.html5 = html5; |
82 |
|
83 |
// ie print shiv
|
84 |
if (supportsUnknownElements || !win.attachEvent){return;} |
85 |
|
86 |
// replaces an element with a namespace-shived clone (eg. header element becomes shiv:header element)
|
87 |
function namespaceShivElement(element) { |
88 |
var elementClone, a, l, i;
|
89 |
if (doc.documentMode > 7) { |
90 |
elementClone = doc.createElement('font');
|
91 |
elementClone.setAttribute('data-html5shiv', element.nodeName.toLowerCase());
|
92 |
} |
93 |
else {
|
94 |
elementClone = doc.createElement('shiv:' + element.nodeName);
|
95 |
} |
96 |
while (element.firstChild) {
|
97 |
elementClone.appendChild(element.childNodes[0]);
|
98 |
} |
99 |
for (a = element.attributes, l = a.length, i = 0; i < l; ++i) { |
100 |
if (a[i].specified) {
|
101 |
elementClone.setAttribute(a[i].nodeName, a[i].nodeValue); |
102 |
} |
103 |
} |
104 |
elementClone.style.cssText = element.style.cssText; |
105 |
element.parentNode.replaceChild(elementClone, element); |
106 |
elementClone.originalElement = element; |
107 |
} |
108 |
|
109 |
// restores an element from a namespace-shived clone (eg. shiv:header element becomes header element)
|
110 |
function unNamespaceShivElement(element) { |
111 |
var originalElement = element.originalElement;
|
112 |
while (element.childNodes.length) {
|
113 |
originalElement.appendChild(element.childNodes[0]);
|
114 |
} |
115 |
element.parentNode.replaceChild(originalElement, element); |
116 |
} |
117 |
|
118 |
// get style sheet list css text
|
119 |
function getStyleSheetListCssText(styleSheetList, mediaType) { |
120 |
// set media type
|
121 |
mediaType = mediaType || 'all';
|
122 |
|
123 |
// set local variables
|
124 |
var
|
125 |
i = -1,
|
126 |
cssTextArr = [], |
127 |
styleSheetListLength = styleSheetList.length, |
128 |
styleSheet, |
129 |
styleSheetMediaType; |
130 |
|
131 |
// loop through style sheets
|
132 |
while (++i < styleSheetListLength) {
|
133 |
// get style sheet
|
134 |
styleSheet = styleSheetList[i]; |
135 |
|
136 |
// get style sheet media type
|
137 |
styleSheetMediaType = styleSheet.media || mediaType; |
138 |
|
139 |
// skip a disabled or non-print style sheet
|
140 |
if (styleSheet.disabled || !(/print|all/.test(styleSheetMediaType))) { |
141 |
continue;
|
142 |
} |
143 |
|
144 |
// push style sheet css text
|
145 |
cssTextArr.push(getStyleSheetListCssText(styleSheet.imports, styleSheetMediaType), styleSheet.cssText); |
146 |
} |
147 |
|
148 |
// return css text
|
149 |
return cssTextArr.join(''); |
150 |
} |
151 |
|
152 |
// shiv css text (eg. header {} becomes shiv\:header {})
|
153 |
function shivCssText (cssText) { |
154 |
// set local variables
|
155 |
var
|
156 |
elementsRegExp = new RegExp('(^|[\\s,{}])(' + win.html5.elements.join('|') + ')', 'gi'), |
157 |
cssTextSplit = cssText.split('{'),
|
158 |
cssTextSplitLength = cssTextSplit.length, |
159 |
i = -1;
|
160 |
|
161 |
// shiv css text
|
162 |
while (++i < cssTextSplitLength) {
|
163 |
cssTextSplit[i] = cssTextSplit[i].split('}');
|
164 |
if (doc.documentMode > 7) { |
165 |
cssTextSplit[i][cssTextSplit[i].length - 1] = cssTextSplit[i][cssTextSplit[i].length - 1].replace(elementsRegExp, '$1font[data-html5shiv="$2"]'); |
166 |
} |
167 |
else {
|
168 |
cssTextSplit[i][cssTextSplit[i].length - 1] = cssTextSplit[i][cssTextSplit[i].length - 1].replace(elementsRegExp, '$1shiv\\:$2'); |
169 |
} |
170 |
cssTextSplit[i] = cssTextSplit[i].join('}');
|
171 |
} |
172 |
|
173 |
// return shived css text
|
174 |
return cssTextSplit.join('{'); |
175 |
} |
176 |
|
177 |
// the before print function
|
178 |
win.attachEvent( |
179 |
'onbeforeprint',
|
180 |
function () {
|
181 |
// test for scenarios where shiving is unnecessary or unavailable
|
182 |
if (win.html5.supportsXElement || !doc.namespaces) {
|
183 |
return;
|
184 |
} |
185 |
|
186 |
// add the shiv namespace
|
187 |
if (!doc.namespaces.shiv) {
|
188 |
doc.namespaces.add('shiv');
|
189 |
} |
190 |
|
191 |
// set local variables
|
192 |
var
|
193 |
i = -1,
|
194 |
elementsRegExp = new RegExp('^(' + win.html5.elements.join('|') + ')$', 'i'), |
195 |
nodeList = doc.getElementsByTagName('*'),
|
196 |
nodeListLength = nodeList.length, |
197 |
element, |
198 |
// sorts style and link files and returns their stylesheets
|
199 |
shivedCSS = shivCssText(getStyleSheetListCssText((function (s, l) {
|
200 |
var arr = [], i = s.length;
|
201 |
while (i) {
|
202 |
arr.unshift(s[--i]); |
203 |
} |
204 |
i = l.length; |
205 |
while (i) {
|
206 |
arr.unshift(l[--i]); |
207 |
} |
208 |
arr.sort(function (a, b) {
|
209 |
return (a.sourceIndex - b.sourceIndex);
|
210 |
}); |
211 |
i = arr.length; |
212 |
while (i) {
|
213 |
arr[--i] = arr[i].styleSheet; |
214 |
} |
215 |
return arr;
|
216 |
})(doc.getElementsByTagName('style'), doc.getElementsByTagName('link')))); |
217 |
|
218 |
// loop through document elements
|
219 |
while (++i < nodeListLength) {
|
220 |
// get element
|
221 |
element = nodeList[i]; |
222 |
|
223 |
// clone matching elements as shiv namespaced
|
224 |
if (elementsRegExp.test(element.nodeName)) {
|
225 |
namespaceShivElement(element); |
226 |
} |
227 |
} |
228 |
|
229 |
// set new shived css text
|
230 |
doc.appendChild(doc._shivedStyleSheet = doc.createElement('style')).styleSheet.cssText = shivedCSS;
|
231 |
} |
232 |
); |
233 |
|
234 |
// the after print function
|
235 |
win.attachEvent( |
236 |
'onafterprint',
|
237 |
function() {
|
238 |
// test for scenarios where shiving is unnecessary
|
239 |
if (win.html5.supportsXElement || !doc.namespaces) {
|
240 |
return;
|
241 |
} |
242 |
|
243 |
// set local variables
|
244 |
var
|
245 |
i = -1,
|
246 |
nodeList = doc.getElementsByTagName('*'),
|
247 |
nodeListLength = nodeList.length, |
248 |
element; |
249 |
|
250 |
// loop through document elements
|
251 |
while (++i < nodeListLength) {
|
252 |
// get element
|
253 |
element = nodeList[i]; |
254 |
|
255 |
// restore original elements
|
256 |
if (element.originalElement) {
|
257 |
unNamespaceShivElement(element); |
258 |
} |
259 |
} |
260 |
|
261 |
// cut new shived css text
|
262 |
if (doc._shivedStyleSheet) {
|
263 |
doc._shivedStyleSheet.parentNode.removeChild(doc._shivedStyleSheet); |
264 |
} |
265 |
} |
266 |
); |
267 |
})(this, document);
|