Skip to content

Commit

Permalink
upgrade to ie print protector 1.6.2. Even faster. Using unminified in…
Browse files Browse the repository at this point in the history
… the source, as it compresses better.
  • Loading branch information
paulirish committed Nov 4, 2010
1 parent 8e99638 commit 7e2703b
Showing 1 changed file with 80 additions and 2 deletions.
82 changes: 80 additions & 2 deletions modernizr.js
Original file line number Diff line number Diff line change
Expand Up @@ -878,8 +878,86 @@ window.Modernizr = (function(window,document,undefined){
if ( enableHTML5 && window.attachEvent && (function(){ var elem = document.createElement("div");
elem.innerHTML = "<elem></elem>";
return elem.childNodes.length !== 1; })()) {
// iepp v1.6 by @jon_neal : code.google.com/p/ie-print-protector
(function(f,l){var j="abbr|article|aside|audio|canvas|details|figcaption|figure|footer|header|hgroup|mark|meter|nav|output|progress|section|summary|time|video",n=j.split("|"),k=n.length,g=new RegExp("<(/*)("+j+")","gi"),h=new RegExp("\\b("+j+")\\b(?!.*[;}])","gi"),m=l.createDocumentFragment(),d=l.documentElement,i=d.firstChild,b=l.createElement("style"),e=l.createElement("body");b.media="all";function c(p){var o=-1;while(++o<k){p.createElement(n[o])}}c(l);c(m);function a(t,s){var r=t.length,q=-1,o,p=[];while(++q<r){o=t[q];s=o.media||s;p.push(a(o.imports,s));p.push(o.cssText)}return p.join("")}f.attachEvent("onbeforeprint",function(){var r=-1;while(++r<k){var o=l.getElementsByTagName(n[r]),q=o.length,p=-1;while(++p<q){if(o[p].className.indexOf("iepp_")<0){o[p].className+=" iepp_"+n[r]}}}i.insertBefore(b,i.firstChild);b.styleSheet.cssText=a(l.styleSheets,"all").replace(h,".iepp_$1");m.appendChild(l.body);d.appendChild(e);e.innerHTML=m.firstChild.innerHTML.replace(g,"<$1bdo")});f.attachEvent("onafterprint",function(){e.innerHTML="";d.removeChild(e);i.removeChild(b);d.appendChild(m.firstChild)})})(this,document);
// iepp v1.6.2 by @jon_neal : code.google.com/p/ie-print-protector
(function(win, doc) {
var elems = 'abbr|article|aside|audio|canvas|details|figcaption|figure|footer|header|hgroup|mark|meter|nav|output|progress|section|summary|time|video',
elemsArr = elems.split('|'),
elemsArrLen = elemsArr.length,
elemRegExp = new RegExp('(^|\\s)('+elems+')', 'gi'),
tagRegExp = new RegExp('<(\/*)('+elems+')', 'gi'),
ruleRegExp = new RegExp('(^|[^\\n]*?\\s)('+elems+')([^\\n]*)({[\\n\\w\\W]*?})', 'gi'),
docFrag = doc.createDocumentFragment(),
html = doc.documentElement,
head = html.firstChild,
bodyElem = doc.createElement('body'),
styleElem = doc.createElement('style'),
body;
function shim(doc) {
var a = -1;
while (++a < elemsArrLen)
// Use createElement so IE allows HTML5-named elements in a document
doc.createElement(elemsArr[a]);
}
function getCSS(styleSheetList, mediaType) {
var a = -1,
len = styleSheetList.length,
styleSheet,
cssTextArr = [];
while (++a < len) {
styleSheet = styleSheetList[a];
// Get css from all non-screen stylesheets and their imports
if ((mediaType = styleSheet.media || mediaType) != 'screen') cssTextArr.push(getCSS(styleSheet.imports, mediaType), styleSheet.cssText);
}
return cssTextArr.join('');
}
// Shim the document and iepp fragment
shim(doc);
shim(docFrag);
// Add iepp custom print style element
head.insertBefore(styleElem, head.firstChild);
styleElem.media = 'print';
win.attachEvent(
'onbeforeprint',
function() {
var a = -1,
cssText = getCSS(doc.styleSheets, 'all'),
cssTextArr = [],
rule;
body = body || doc.body;
// Get only rules which reference HTML5 elements by name
while ((rule = ruleRegExp.exec(cssText)) != null)
// Replace all html5 element references with iepp substitute classnames
cssTextArr.push((rule[1]+rule[2]+rule[3]).replace(elemRegExp, '$1.iepp_$2')+rule[4]);
// Write iepp custom print CSS
styleElem.styleSheet.cssText = cssTextArr.join('\n');
while (++a < elemsArrLen) {
var nodeList = doc.getElementsByTagName(elemsArr[a]),
nodeListLen = nodeList.length,
b = -1;
while (++b < nodeListLen)
if (nodeList[b].className.indexOf('iepp_') < 0)
// Append iepp substitute classnames to all html5 elements
nodeList[b].className += ' iepp_'+elemsArr[a];
}
docFrag.appendChild(body);
html.appendChild(bodyElem);
// Write iepp substitute print-safe document
bodyElem.className = body.className;
// Replace HTML5 elements with <font> which is print-safe and shouldn't conflict since it isn't part of html5
bodyElem.innerHTML = body.innerHTML.replace(tagRegExp, '<$1font');
}
);
win.attachEvent(
'onafterprint',
function() {
// Undo everything done in onbeforeprint
bodyElem.innerHTML = '';
html.removeChild(bodyElem);
html.appendChild(body);
styleElem.styleSheet.cssText = '';
}
);
})(window, document);
}

// Assign private properties to the return object with prefix
Expand Down

0 comments on commit 7e2703b

Please sign in to comment.