7e7a2f26368775889782d9d21ccab80c2b7c281b.svn-base 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. //by zhanyi
  2. function uParse(selector,opt){
  3. var ie = !!window.ActiveXObject,
  4. cssRule = ie ? function(key,style,doc){
  5. var indexList,index;
  6. doc = doc || document;
  7. if(doc.indexList){
  8. indexList = doc.indexList;
  9. }else{
  10. indexList = doc.indexList = {};
  11. }
  12. var sheetStyle;
  13. if(!indexList[key]){
  14. if(style === undefined){
  15. return ''
  16. }
  17. sheetStyle = doc.createStyleSheet('',index = doc.styleSheets.length);
  18. indexList[key] = index;
  19. }else{
  20. sheetStyle = doc.styleSheets[indexList[key]];
  21. }
  22. if(style === undefined){
  23. return sheetStyle.cssText
  24. }
  25. sheetStyle.cssText = sheetStyle.cssText + '\n' + (style || '')
  26. } : function(key,style,doc){
  27. doc = doc || document;
  28. var head = doc.getElementsByTagName('head')[0],node;
  29. if(!(node = doc.getElementById(key))){
  30. if(style === undefined){
  31. return ''
  32. }
  33. node = doc.createElement('style');
  34. node.id = key;
  35. head.appendChild(node)
  36. }
  37. if(style === undefined){
  38. return node.innerHTML
  39. }
  40. if(style !== ''){
  41. node.innerHTML = node.innerHTML + '\n' + style;
  42. }else{
  43. head.removeChild(node)
  44. }
  45. },
  46. domReady = function (onready) {
  47. var doc = window.document;
  48. if (doc.readyState === "complete") {
  49. onready();
  50. }else{
  51. if (ie) {
  52. (function () {
  53. if (doc.isReady) return;
  54. try {
  55. doc.documentElement.doScroll("left");
  56. } catch (error) {
  57. setTimeout(arguments.callee, 0);
  58. return;
  59. }
  60. onready();
  61. })();
  62. window.attachEvent('onload', function(){
  63. onready()
  64. });
  65. } else {
  66. doc.addEventListener("DOMContentLoaded", function () {
  67. doc.removeEventListener("DOMContentLoaded", arguments.callee, false);
  68. onready();
  69. }, false);
  70. window.addEventListener('load', function(){onready()}, false);
  71. }
  72. }
  73. },
  74. _each = function(obj, iterator, context) {
  75. if (obj == null) return;
  76. if (obj.length === +obj.length) {
  77. for (var i = 0, l = obj.length; i < l; i++) {
  78. if(iterator.call(context, obj[i], i, obj) === false)
  79. return false;
  80. }
  81. } else {
  82. for (var key in obj) {
  83. if (obj.hasOwnProperty(key)) {
  84. if(iterator.call(context, obj[key], key, obj) === false)
  85. return false;
  86. }
  87. }
  88. }
  89. },
  90. inArray = function(arr,item){
  91. var index = -1;
  92. _each(arr,function(v,i){
  93. if(v === item){
  94. index = i;
  95. return false;
  96. }
  97. });
  98. return index;
  99. },
  100. pushItem = function(arr,item){
  101. if(inArray(arr,item)==-1){
  102. arr.push(item)
  103. }
  104. },
  105. loadFile = function () {
  106. var tmpList = [];
  107. function getItem(doc,obj){
  108. try{
  109. for(var i= 0,ci;ci=tmpList[i++];){
  110. if(ci.doc === doc && ci.url == (obj.src || obj.href)){
  111. return ci;
  112. }
  113. }
  114. }catch(e){
  115. return null;
  116. }
  117. }
  118. return function (doc, obj, fn) {
  119. var item = getItem(doc,obj);
  120. if (item) {
  121. if(item.ready){
  122. fn && fn();
  123. }else{
  124. item.funs.push(fn)
  125. }
  126. return;
  127. }
  128. tmpList.push({
  129. doc:doc,
  130. url:obj.src||obj.href,
  131. funs:[fn]
  132. });
  133. if (!doc.body) {
  134. var html = [];
  135. for(var p in obj){
  136. if(p == 'tag')continue;
  137. html.push(p + '="' + obj[p] + '"')
  138. }
  139. doc.write('<' + obj.tag + ' ' + html.join(' ') + ' ></'+obj.tag+'>');
  140. return;
  141. }
  142. if (obj.id && doc.getElementById(obj.id)) {
  143. return;
  144. }
  145. var element = doc.createElement(obj.tag);
  146. delete obj.tag;
  147. for (var p in obj) {
  148. element.setAttribute(p, obj[p]);
  149. }
  150. element.onload = element.onreadystatechange = function () {
  151. if (!this.readyState || /loaded|complete/.test(this.readyState)) {
  152. item = getItem(doc,obj);
  153. if (item.funs.length > 0) {
  154. item.ready = 1;
  155. for (var fi; fi = item.funs.pop();) {
  156. fi();
  157. }
  158. }
  159. element.onload = element.onreadystatechange = null;
  160. }
  161. };
  162. element.onerror = function(){
  163. throw Error('The load '+(obj.href||obj.src)+' fails,check the url')
  164. };
  165. doc.getElementsByTagName("head")[0].appendChild(element);
  166. }
  167. }();
  168. //Ĭ�ϵ�������Ŀ
  169. var defaultOption ={
  170. liiconpath : 'http://bs.baidu.com/listicon/',
  171. listDefaultPaddingLeft : '20',
  172. 'highlightJsUrl':'',
  173. 'highlightCssUrl':'',
  174. customRule:function(){}
  175. };
  176. if(opt){
  177. for(var p in opt){
  178. defaultOption[p] = opt[p]
  179. }
  180. }
  181. domReady(function(){
  182. var contents;
  183. if(document.querySelectorAll){
  184. contents = document.querySelectorAll(selector)
  185. }else{
  186. if(/^#/.test(selector)){
  187. contents = [document.getElementById(selector.replace(/^#/,''))]
  188. }else if(/^\./.test(selector)){
  189. var contents = [];
  190. _each(document.getElementsByTagName('*'),function(node){
  191. if(node.className && new RegExp('\\b' + selector.replace(/^\./,'') + '\\b','i').test(node.className)){
  192. contents.push(node)
  193. }
  194. })
  195. }else{
  196. contents = document.getElementsByTagName(selector)
  197. }
  198. }
  199. _each(contents,function(content){
  200. if(content.tagName.toLowerCase() == 'textarea'){
  201. var tmpNode = document.createElement('div');
  202. if(/^#/.test(selector)){
  203. tmpNode.id = selector.replace(/^#/,'')
  204. }else if(/^\./.test(selector)){
  205. tmpNode.className = selector.replace(/^\./,'')
  206. }
  207. content.parentNode.insertBefore(tmpNode,content);
  208. tmpNode.innerHTML = content.value;
  209. content.parentNode.removeChild(content);
  210. content = tmpNode;
  211. }
  212. function fillNode(nodes){
  213. _each(nodes,function(node){
  214. if(!node.firstChild){
  215. node.innerHTML = '&nbsp;'
  216. }
  217. })
  218. }
  219. function checkList(nodes){
  220. var customCss = [],
  221. customStyle = {
  222. 'cn' : 'cn-1-',
  223. 'cn1' : 'cn-2-',
  224. 'cn2' : 'cn-3-',
  225. 'num' : 'num-1-',
  226. 'num1' : 'num-2-',
  227. 'num2' : 'num-3-',
  228. 'dash' : 'dash',
  229. 'dot' : 'dot'
  230. };
  231. _each(nodes,function(list){
  232. if(list.className && /custom_/i.test(list.className)){
  233. var listStyle = list.className.match(/custom_(\w+)/)[1];
  234. if(listStyle == 'dash' || listStyle == 'dot'){
  235. pushItem(customCss,selector +' li.list-' + customStyle[listStyle] + '{background-image:url(' + defaultOption.liiconpath +customStyle[listStyle]+'.gif)}');
  236. pushItem(customCss,selector +' ul.custom_'+listStyle+'{list-style:none;} '+ selector +' ul.custom_'+listStyle+' li{background-position:0 3px;background-repeat:no-repeat}');
  237. }else{
  238. var index = 1;
  239. _each(list.childNodes,function(li){
  240. if(li.tagName == 'LI'){
  241. pushItem(customCss,selector + ' li.list-' + customStyle[listStyle] + index + '{background-image:url(' + defaultOption.liiconpath + 'list-'+customStyle[listStyle] +index + '.gif)}');
  242. index++;
  243. }
  244. });
  245. pushItem(customCss,selector + ' ol.custom_'+listStyle+'{list-style:none;}'+selector+' ol.custom_'+listStyle+' li{background-position:0 3px;background-repeat:no-repeat}');
  246. }
  247. switch(listStyle){
  248. case 'cn':
  249. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-1{padding-left:25px}');
  250. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-2{padding-left:40px}');
  251. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-3{padding-left:55px}');
  252. break;
  253. case 'cn1':
  254. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-1{padding-left:30px}');
  255. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-2{padding-left:40px}');
  256. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-3{padding-left:55px}');
  257. break;
  258. case 'cn2':
  259. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-1{padding-left:40px}');
  260. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-2{padding-left:55px}');
  261. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-3{padding-left:68px}');
  262. break;
  263. case 'num':
  264. case 'num1':
  265. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-1{padding-left:25px}');
  266. break;
  267. case 'num2':
  268. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-1{padding-left:35px}');
  269. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-2{padding-left:40px}');
  270. break;
  271. case 'dash':
  272. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft{padding-left:35px}');
  273. break;
  274. case 'dot':
  275. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft{padding-left:20px}');
  276. }
  277. }
  278. });
  279. customCss.push(selector +' .list-paddingleft-1{padding-left:0}');
  280. customCss.push(selector +' .list-paddingleft-2{padding-left:'+defaultOption.listDefaultPaddingLeft+'px}');
  281. customCss.push(selector +' .list-paddingleft-3{padding-left:'+defaultOption.listDefaultPaddingLeft*2+'px}');
  282. //�����Ȼ����Զ�Ӧ��ʽ����ֹ�����
  283. cssRule('list', selector +' ol,'+selector +' ul{margin:0;padding:0;}li{clear:both;}'+customCss.join('\n'), document);
  284. }
  285. //��������
  286. var needParseTagName = {
  287. 'table' : function(){
  288. cssRule('table',
  289. selector +' table.noBorderTable td,'+selector+' table.noBorderTable th,'+selector+' table.noBorderTable caption{border:1px dashed #ddd !important}' +
  290. //����ı���Ĭ����ʽ
  291. selector +' table{margin-bottom:10px;border-collapse:collapse;display:table;}' +
  292. selector +' td,'+selector+' th{ background:white; padding: 5px 10px;border: 1px solid #DDD;}' +
  293. selector +' caption{border:1px dashed #DDD;border-bottom:0;padding:3px;text-align:center;}' +
  294. selector +' th{border-top:2px solid #BBB;background:#F7F7F7;}' +
  295. selector +' td p{margin:0;padding:0;}',
  296. document);
  297. },
  298. 'ol' : checkList,
  299. 'ul' : checkList,
  300. 'pre': function(nodes){
  301. if(typeof XRegExp == "undefined"){
  302. loadFile(document,{
  303. id : "syntaxhighlighter_js",
  304. src : defaultOption.highlightJsUrl,
  305. tag : "script",
  306. type : "text/javascript",
  307. defer : "defer"
  308. },function(){
  309. _each(nodes,function(pi){
  310. if(/brush/i.test(pi.className)){
  311. SyntaxHighlighter.highlight(pi);
  312. var tables = document.getElementsByTagName('table');
  313. for(var t= 0,ti;ti=tables[t++];){
  314. if(/SyntaxHighlighter/i.test(ti.className)){
  315. var tds = ti.getElementsByTagName('td');
  316. for(var i=0,li,ri;li=tds[0].childNodes[i];i++){
  317. ri = tds[1].firstChild.childNodes[i];
  318. if(ri){
  319. ri.style.height = li.style.height = ri.offsetHeight + 'px';
  320. }
  321. }
  322. }
  323. }
  324. }
  325. });
  326. });
  327. }
  328. if(!document.getElementById("syntaxhighlighter_css")){
  329. loadFile(document,{
  330. id : "syntaxhighlighter_css",
  331. tag : "link",
  332. rel : "stylesheet",
  333. type : "text/css",
  334. href : defaultOption.highlightCssUrl
  335. });
  336. }
  337. },
  338. 'td':fillNode,
  339. 'th':fillNode,
  340. 'caption':fillNode
  341. };
  342. //�Ȳ���Ĭ�ϵ�����
  343. for(var tag in needParseTagName){
  344. var nodes = content.getElementsByTagName(tag);
  345. if(nodes.length){
  346. needParseTagName[tag](nodes)
  347. }
  348. }
  349. defaultOption.customRule(content);
  350. });
  351. })
  352. }