ueditor.parse.js 16 KB

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