ueditor.parse.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. var defaultOption ={
  169. liiconpath : 'http://bs.baidu.com/listicon/',
  170. listDefaultPaddingLeft : '20',
  171. 'highlightJsUrl':'',
  172. 'highlightCssUrl':'',
  173. customRule:function(){}
  174. };
  175. if(opt){
  176. for(var p in opt){
  177. defaultOption[p] = opt[p]
  178. }
  179. }
  180. domReady(function(){
  181. var contents;
  182. if(document.querySelectorAll){
  183. contents = document.querySelectorAll(selector)
  184. }else{
  185. if(/^#/.test(selector)){
  186. contents = [document.getElementById(selector.replace(/^#/,''))]
  187. }else if(/^\./.test(selector)){
  188. var contents = [];
  189. _each(document.getElementsByTagName('*'),function(node){
  190. if(node.className && new RegExp('\\b' + selector.replace(/^\./,'') + '\\b','i').test(node.className)){
  191. contents.push(node)
  192. }
  193. })
  194. }else{
  195. contents = document.getElementsByTagName(selector)
  196. }
  197. }
  198. _each(contents,function(content){
  199. if(content.tagName.toLowerCase() == 'textarea'){
  200. var tmpNode = document.createElement('div');
  201. if(/^#/.test(selector)){
  202. tmpNode.id = selector.replace(/^#/,'')
  203. }else if(/^\./.test(selector)){
  204. tmpNode.className = selector.replace(/^\./,'')
  205. }
  206. content.parentNode.insertBefore(tmpNode,content);
  207. tmpNode.innerHTML = content.value;
  208. content.parentNode.removeChild(content);
  209. content = tmpNode;
  210. }
  211. function fillNode(nodes){
  212. _each(nodes,function(node){
  213. if(!node.firstChild){
  214. node.innerHTML = '&nbsp;'
  215. }
  216. })
  217. }
  218. function checkList(nodes){
  219. var customCss = [],
  220. customStyle = {
  221. 'cn' : 'cn-1-',
  222. 'cn1' : 'cn-2-',
  223. 'cn2' : 'cn-3-',
  224. 'num' : 'num-1-',
  225. 'num1' : 'num-2-',
  226. 'num2' : 'num-3-',
  227. 'dash' : 'dash',
  228. 'dot' : 'dot'
  229. };
  230. _each(nodes,function(list){
  231. if(list.className && /custom_/i.test(list.className)){
  232. var listStyle = list.className.match(/custom_(\w+)/)[1];
  233. if(listStyle == 'dash' || listStyle == 'dot'){
  234. pushItem(customCss,selector +' li.list-' + customStyle[listStyle] + '{background-image:url(' + defaultOption.liiconpath +customStyle[listStyle]+'.gif)}');
  235. pushItem(customCss,selector +' ul.custom_'+listStyle+'{list-style:none;} '+ selector +' ul.custom_'+listStyle+' li{background-position:0 3px;background-repeat:no-repeat}');
  236. }else{
  237. var index = 1;
  238. _each(list.childNodes,function(li){
  239. if(li.tagName == 'LI'){
  240. pushItem(customCss,selector + ' li.list-' + customStyle[listStyle] + index + '{background-image:url(' + defaultOption.liiconpath + 'list-'+customStyle[listStyle] +index + '.gif)}');
  241. index++;
  242. }
  243. });
  244. pushItem(customCss,selector + ' ol.custom_'+listStyle+'{list-style:none;}'+selector+' ol.custom_'+listStyle+' li{background-position:0 3px;background-repeat:no-repeat}');
  245. }
  246. switch(listStyle){
  247. case 'cn':
  248. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-1{padding-left:25px}');
  249. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-2{padding-left:40px}');
  250. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-3{padding-left:55px}');
  251. break;
  252. case 'cn1':
  253. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-1{padding-left:30px}');
  254. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-2{padding-left:40px}');
  255. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-3{padding-left:55px}');
  256. break;
  257. case 'cn2':
  258. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-1{padding-left:40px}');
  259. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-2{padding-left:55px}');
  260. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-3{padding-left:68px}');
  261. break;
  262. case 'num':
  263. case 'num1':
  264. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-1{padding-left:25px}');
  265. break;
  266. case 'num2':
  267. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-1{padding-left:35px}');
  268. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-2{padding-left:40px}');
  269. break;
  270. case 'dash':
  271. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft{padding-left:35px}');
  272. break;
  273. case 'dot':
  274. pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft{padding-left:20px}');
  275. }
  276. }
  277. });
  278. customCss.push(selector +' .list-paddingleft-1{padding-left:0}');
  279. customCss.push(selector +' .list-paddingleft-2{padding-left:'+defaultOption.listDefaultPaddingLeft+'px}');
  280. customCss.push(selector +' .list-paddingleft-3{padding-left:'+defaultOption.listDefaultPaddingLeft*2+'px}');
  281. cssRule('list', selector +' ol,'+selector +' ul{margin:0;padding:0;}li{clear:both;}'+customCss.join('\n'), document);
  282. }
  283. var needParseTagName = {
  284. 'table' : function(){
  285. cssRule('table',
  286. selector +' table.noBorderTable td,'+selector+' table.noBorderTable th,'+selector+' table.noBorderTable caption{border:1px dashed #ddd !important}' +
  287. selector +' table{margin-bottom:10px;border-collapse:collapse;display:table;}' +
  288. selector +' td,'+selector+' th{ background:white; padding: 5px 10px;border: 1px solid #DDD;}' +
  289. selector +' caption{border:1px dashed #DDD;border-bottom:0;padding:3px;text-align:center;}' +
  290. selector +' th{border-top:2px solid #BBB;background:#F7F7F7;}' +
  291. selector +' td p{margin:0;padding:0;}',
  292. document);
  293. },
  294. 'ol' : checkList,
  295. 'ul' : checkList,
  296. 'pre': function(nodes){
  297. if(typeof XRegExp == "undefined"){
  298. loadFile(document,{
  299. id : "syntaxhighlighter_js",
  300. src : defaultOption.highlightJsUrl,
  301. tag : "script",
  302. type : "text/javascript",
  303. defer : "defer"
  304. },function(){
  305. _each(nodes,function(pi){
  306. if(pi && /brush/i.test(pi.className)){
  307. SyntaxHighlighter.highlight(pi);
  308. // var tables = document.getElementsByTagName('table');
  309. // for(var t= 0,ti;ti=tables[t++];){
  310. // if(/SyntaxHighlighter/i.test(ti.className)){
  311. // var tds = ti.getElementsByTagName('td');
  312. // for(var i=0,li,ri;li=tds[0].childNodes[i];i++){
  313. // ri = tds[1].firstChild.childNodes[i];
  314. // if(ri){
  315. // ri.style.height = li.style.height = ri.offsetHeight + 'px';
  316. // }
  317. // }
  318. // }
  319. // }
  320. }
  321. });
  322. });
  323. }
  324. if(!document.getElementById("syntaxhighlighter_css")){
  325. loadFile(document,{
  326. id : "syntaxhighlighter_css",
  327. tag : "link",
  328. rel : "stylesheet",
  329. type : "text/css",
  330. href : defaultOption.highlightCssUrl
  331. });
  332. }
  333. },
  334. 'td':fillNode,
  335. 'th':fillNode,
  336. 'caption':fillNode
  337. };
  338. for(var tag in needParseTagName){
  339. var nodes = content.getElementsByTagName(tag);
  340. if(nodes.length){
  341. needParseTagName[tag](nodes)
  342. }
  343. }
  344. defaultOption.customRule(content);
  345. });
  346. })
  347. }