iepng.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. var EvPNG = {
  2. ns: 'EvPNG',
  3. imgSize: {},
  4. createVmlNameSpace: function() {
  5. if (document.namespaces && !document.namespaces[this.ns]) {
  6. document.namespaces.add(this.ns, 'urn:schemas-microsoft-com:vml')
  7. }
  8. if (window.attachEvent) {
  9. window.attachEvent('onbeforeunload',
  10. function() {
  11. EvPNG = null
  12. })
  13. }
  14. },
  15. createVmlStyleSheet: function() {
  16. var a = document.createElement('style');
  17. document.documentElement.firstChild.insertBefore(a, document.documentElement.firstChild.firstChild);
  18. var b = a.styleSheet;
  19. b.addRule(this.ns + '\\:*', '{behavior:url(#default#VML)}');
  20. b.addRule(this.ns + '\\:shape', 'position:absolute;');
  21. b.addRule('img.' + this.ns + '_sizeFinder', 'behavior:none; border:none; position:absolute; z-index:-1; top:-10000px; visibility:hidden;');
  22. this.styleSheet = b
  23. },
  24. readPropertyChange: function() {
  25. var a = event.srcElement;
  26. if (event.propertyName.search('background') != -1 || event.propertyName.search('border') != -1) {
  27. EvPNG.applyVML(a)
  28. }
  29. if (event.propertyName == 'style.display') {
  30. var b = (a.currentStyle.display == 'none') ? 'none': 'block';
  31. for (var v in a.vml) {
  32. a.vml[v].shape.style.display = b
  33. }
  34. }
  35. if (event.propertyName.search('filter') != -1) {
  36. EvPNG.vmlOpacity(a)
  37. }
  38. },
  39. vmlOpacity: function(a) {
  40. if (a.currentStyle.filter.search('lpha') != -1) {
  41. var b = a.currentStyle.filter;
  42. b = parseInt(b.substring(b.lastIndexOf('=') + 1, b.lastIndexOf(')')), 10) / 100;
  43. a.vml.color.shape.style.filter = a.currentStyle.filter;
  44. a.vml.image.fill.opacity = b
  45. }
  46. },
  47. handlePseudoHover: function(a) {
  48. setTimeout(function() {
  49. EvPNG.applyVML(a)
  50. },
  51. 1)
  52. },
  53. fix: function(a) {
  54. var b = a.split(',');
  55. for (var i = 0; i < b.length; i++) {
  56. this.styleSheet.addRule(b[i], 'behavior:expression(EvPNG.fixPng(this))')
  57. }
  58. },
  59. applyVML: function(a) {
  60. a.runtimeStyle.cssText = '';
  61. this.vmlFill(a);
  62. this.vmlOffsets(a);
  63. this.vmlOpacity(a);
  64. if (a.isImg) {
  65. this.copyImageBorders(a)
  66. }
  67. },
  68. attachHandlers: function(b) {
  69. var c = this;
  70. var d = {
  71. resize: 'vmlOffsets',
  72. move: 'vmlOffsets'
  73. };
  74. if (b.nodeName == 'A') {
  75. var e = {
  76. mouseleave: 'handlePseudoHover',
  77. mouseenter: 'handlePseudoHover',
  78. focus: 'handlePseudoHover',
  79. blur: 'handlePseudoHover'
  80. };
  81. for (var a in e) {
  82. d[a] = e[a]
  83. }
  84. }
  85. for (var h in d) {
  86. b.attachEvent('on' + h,
  87. function() {
  88. c[d[h]](b)
  89. })
  90. }
  91. b.attachEvent('onpropertychange', this.readPropertyChange)
  92. },
  93. giveLayout: function(a) {
  94. a.style.zoom = 1;
  95. if (a.currentStyle.position == 'static') {
  96. a.style.position = 'relative'
  97. }
  98. },
  99. copyImageBorders: function(a) {
  100. var b = {
  101. 'borderStyle': true,
  102. 'borderWidth': true,
  103. 'borderColor': true
  104. };
  105. for (var s in b) {
  106. a.vml.color.shape.style[s] = a.currentStyle[s]
  107. }
  108. },
  109. vmlFill: function(a) {
  110. if (!a.currentStyle) {
  111. return
  112. } else {
  113. var b = a.currentStyle
  114. }
  115. for (var v in a.vml) {
  116. a.vml[v].shape.style.zIndex = b.zIndex
  117. }
  118. a.runtimeStyle.backgroundColor = '';
  119. a.runtimeStyle.backgroundImage = '';
  120. var c = (b.backgroundColor == 'transparent');
  121. var d = true;
  122. if (b.backgroundImage != 'none' || a.isImg) {
  123. if (!a.isImg) {
  124. a.vmlBg = b.backgroundImage;
  125. a.vmlBg = a.vmlBg.substr(5, a.vmlBg.lastIndexOf('")') - 5)
  126. } else {
  127. a.vmlBg = a.src
  128. }
  129. var e = this;
  130. if (!e.imgSize[a.vmlBg]) {
  131. var f = document.createElement('img');
  132. e.imgSize[a.vmlBg] = f;
  133. f.className = e.ns + '_sizeFinder';
  134. f.runtimeStyle.cssText = 'behavior:none; position:absolute; left:-10000px; top:-10000px; border:none;';
  135. f.attachEvent('onload',
  136. function() {
  137. this.width = this.offsetWidth;
  138. this.height = this.offsetHeight;
  139. e.vmlOffsets(a)
  140. });
  141. f.src = a.vmlBg;
  142. f.removeAttribute('width');
  143. f.removeAttribute('height');
  144. document.body.insertBefore(f, document.body.firstChild)
  145. }
  146. a.vml.image.fill.src = a.vmlBg;
  147. d = false
  148. }
  149. a.vml.image.fill.on = !d;
  150. a.vml.image.fill.color = 'none';
  151. a.vml.color.shape.style.backgroundColor = b.backgroundColor;
  152. a.runtimeStyle.backgroundImage = 'none';
  153. a.runtimeStyle.backgroundColor = 'transparent'
  154. },
  155. vmlOffsets: function(e) {
  156. var f = e.currentStyle;
  157. var g = {
  158. 'W': e.clientWidth + 1,
  159. 'H': e.clientHeight + 1,
  160. 'w': this.imgSize[e.vmlBg].width,
  161. 'h': this.imgSize[e.vmlBg].height,
  162. 'L': e.offsetLeft,
  163. 'T': e.offsetTop,
  164. 'bLW': e.clientLeft,
  165. 'bTW': e.clientTop
  166. };
  167. var i = (g.L + g.bLW == 1) ? 1: 0;
  168. var j = function(a, l, t, w, h, o) {
  169. a.coordsize = w + ',' + h;
  170. a.coordorigin = o + ',' + o;
  171. a.path = 'm0,0l' + w + ',0l' + w + ',' + h + 'l0,' + h + ' xe';
  172. a.style.width = w + 'px';
  173. a.style.height = h + 'px';
  174. a.style.left = l + 'px';
  175. a.style.top = t + 'px'
  176. };
  177. j(e.vml.color.shape, (g.L + (e.isImg ? 0: g.bLW)), (g.T + (e.isImg ? 0: g.bTW)), (g.W - 1), (g.H - 1), 0);
  178. j(e.vml.image.shape, (g.L + g.bLW), (g.T + g.bTW), (g.W), (g.H), 1);
  179. var k = {
  180. 'X': 0,
  181. 'Y': 0
  182. };
  183. var m = function(a, b) {
  184. var c = true;
  185. switch (b) {
  186. case 'left':
  187. case 'top':
  188. k[a] = 0;
  189. break;
  190. case 'center':
  191. k[a] = .5;
  192. break;
  193. case 'right':
  194. case 'bottom':
  195. k[a] = 1;
  196. break;
  197. default:
  198. if (b.search('%') != -1) {
  199. k[a] = parseInt(b) * .01
  200. } else {
  201. c = false
  202. }
  203. }
  204. var d = (a == 'X');
  205. k[a] = Math.ceil(c ? ((g[d ? 'W': 'H'] * k[a]) - (g[d ? 'w': 'h'] * k[a])) : parseInt(b));
  206. if (k[a] == 0) {
  207. k[a]++
  208. }
  209. };
  210. for (var b in k) {
  211. m(b, f['backgroundPosition' + b])
  212. }
  213. e.vml.image.fill.position = (k.X / g.W) + ',' + (k.Y / g.H);
  214. var n = f.backgroundRepeat;
  215. var p = {
  216. 'T': 1,
  217. 'R': g.W + i,
  218. 'B': g.H,
  219. 'L': 1 + i
  220. };
  221. var q = {
  222. 'X': {
  223. 'b1': 'L',
  224. 'b2': 'R',
  225. 'd': 'W'
  226. },
  227. 'Y': {
  228. 'b1': 'T',
  229. 'b2': 'B',
  230. 'd': 'H'
  231. }
  232. };
  233. if (n != 'repeat') {
  234. var c = {
  235. 'T': (k.Y),
  236. 'R': (k.X + g.w),
  237. 'B': (k.Y + g.h),
  238. 'L': (k.X)
  239. };
  240. if (n.search('repeat-') != -1) {
  241. var v = n.split('repeat-')[1].toUpperCase();
  242. c[q[v].b1] = 1;
  243. c[q[v].b2] = g[q[v].d]
  244. }
  245. if (c.B > g.H) {
  246. c.B = g.H
  247. }
  248. e.vml.image.shape.style.clip = 'rect(' + c.T + 'px ' + (c.R + i) + 'px ' + c.B + 'px ' + (c.L + i) + 'px)'
  249. } else {
  250. e.vml.image.shape.style.clip = 'rect(' + p.T + 'px ' + p.R + 'px ' + p.B + 'px ' + p.L + 'px)'
  251. }
  252. },
  253. fixPng: function(a) {
  254. a.style.behavior = 'none';
  255. if (a.nodeName == 'BODY' || a.nodeName == 'TD' || a.nodeName == 'TR') {
  256. return
  257. }
  258. a.isImg = false;
  259. if (a.nodeName == 'IMG') {
  260. if (a.src.toLowerCase().search(/\.png$/) != -1) {
  261. a.isImg = true;
  262. a.style.visibility = 'hidden'
  263. } else {
  264. return
  265. }
  266. } else if (a.currentStyle.backgroundImage.toLowerCase().search('.png') == -1) {
  267. return
  268. }
  269. var b = EvPNG;
  270. a.vml = {
  271. color: {},
  272. image: {}
  273. };
  274. var c = {
  275. shape: {},
  276. fill: {}
  277. };
  278. for (var r in a.vml) {
  279. for (var e in c) {
  280. var d = b.ns + ':' + e;
  281. a.vml[r][e] = document.createElement(d)
  282. }
  283. a.vml[r].shape.stroked = false;
  284. a.vml[r].shape.appendChild(a.vml[r].fill);
  285. a.parentNode.insertBefore(a.vml[r].shape, a)
  286. }
  287. a.vml.image.shape.fillcolor = 'none';
  288. a.vml.image.fill.type = 'tile';
  289. a.vml.color.fill.on = false;
  290. b.attachHandlers(a);
  291. b.giveLayout(a);
  292. b.giveLayout(a.offsetParent);
  293. b.applyVML(a)
  294. }
  295. };
  296. try {
  297. document.execCommand("BackgroundImageCache", false, true)
  298. } catch(r) {}
  299. EvPNG.createVmlNameSpace();
  300. EvPNG.createVmlStyleSheet();