jquery.sceditor.bbcode.js 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  1. /**
  2. * SCEditor BBCode Plugin
  3. * http://www.samclarke.com/2011/07/sceditor/
  4. *
  5. * Copyright (C) 2011-2012, Sam Clarke (samclarke.com)
  6. *
  7. * SCEditor is licensed under the MIT license:
  8. * http://www.opensource.org/licenses/mit-license.php
  9. *
  10. * @author Sam Clarke
  11. * @version 1.3.7
  12. * @requires jQuery
  13. */
  14. // ==ClosureCompiler==
  15. // @output_file_name jquery.sceditor.min.js
  16. // @compilation_level SIMPLE_OPTIMIZATIONS
  17. // ==/ClosureCompiler==
  18. /*jshint smarttabs: true, jquery: true, eqnull:true, curly: false */
  19. (function($) {
  20. 'use strict';
  21. /**
  22. * BBCode plugin for SCEditor
  23. *
  24. * @param {Element} el The textarea to be converted
  25. * @return {Object} options
  26. * @class sceditorBBCodePlugin
  27. * @name jQuery.sceditorBBCodePlugin
  28. */
  29. $.sceditorBBCodePlugin = function(element, options) {
  30. var base = this;
  31. /**
  32. * Private methods
  33. * @private
  34. */
  35. var init,
  36. buildBbcodeCache,
  37. handleStyles,
  38. handleTags,
  39. formatString,
  40. getStyle,
  41. wrapInDivs,
  42. isEmpty,
  43. mergeTextModeCommands;
  44. base.bbcodes = $.sceditorBBCodePlugin.bbcodes;
  45. /**
  46. * cache of all the tags pointing to their bbcodes to enable
  47. * faster lookup of which bbcode a tag should have
  48. * @private
  49. */
  50. var tagsToBbcodes = {};
  51. /**
  52. * Same as tagsToBbcodes but instead of HTML tags it's styles
  53. * @private
  54. */
  55. var stylesToBbcodes = {};
  56. /**
  57. * Allowed children of specific HTML tags. Empty array if no
  58. * children other than text nodes are allowed
  59. * @private
  60. */
  61. var validChildren = {
  62. list: ['li'],
  63. table: ['tr'],
  64. tr: ['td', 'th'],
  65. code: ['br', 'p', 'div'],
  66. youtube: []
  67. };
  68. /**
  69. * Initializer
  70. * @private
  71. * @name sceditorBBCodePlugin.init
  72. */
  73. init = function() {
  74. $.data(element, "sceditorbbcode", base);
  75. base.options = $.extend({}, $.sceditor.defaultOptions, options);
  76. // build the BBCode cache
  77. buildBbcodeCache();
  78. (new $.sceditor(element,
  79. $.extend({}, base.options, {
  80. getHtmlHandler: base.getHtmlHandler,
  81. getTextHandler: base.getTextHandler,
  82. commands: mergeTextModeCommands()
  83. })
  84. ));
  85. };
  86. mergeTextModeCommands = function() {
  87. var merge = {
  88. bold: { txtExec: ["[b]", "[/b]"] },
  89. italic: { txtExec: ["[i]", "[/i]"] },
  90. underline: { txtExec: ["[u]", "[/u]"] },
  91. strike: { txtExec: ["[s]", "[/s]"] },
  92. subscript: { txtExec: ["[sub]", "[/sub]"] },
  93. superscript: { txtExec: ["[sup]", "[/sup]"] },
  94. left: { txtExec: ["[left]", "[/left]"] },
  95. center: { txtExec: ["[center]", "[/center]"] },
  96. right: { txtExec: ["[right]", "[/right]"] },
  97. justify: { txtExec: ["[justify]", "[/justify]"] },
  98. ftp: { txtExec: ["[ftp]", "[/ftp]"] },
  99. tt: { txtExec: ["[tt]", "[/tt]"] },
  100. glow: { txtExec: ["[glow=red,2,300]", "[/glow]"] },
  101. shadow: { txtExec: ["[shadow=red,left]", "[/shadow]"] },
  102. pre: { txtExec: ["[pre]", "[/pre]"] },
  103. // @todo: check tooltip
  104. font: { txtExec: function(caller) {
  105. var editor = this;
  106. $.sceditor.command.get('font')._dropDown(
  107. editor,
  108. caller,
  109. function(fontName) {
  110. editor.insertText("[font="+fontName+"]", "[/font]");
  111. }
  112. );
  113. } },
  114. // @todo: check overlapping
  115. size: { txtExec: function(caller) {
  116. var editor = this;
  117. $.sceditor.command.get('size')._dropDown(
  118. editor,
  119. caller,
  120. function(fontSize) {
  121. editor.insertText("[size="+fontSize+"]", "[/size]");
  122. }
  123. );
  124. } },
  125. color: { txtExec: function(caller) {
  126. var editor = this;
  127. $.sceditor.command.get('color')._dropDown(
  128. editor,
  129. caller,
  130. function(color) {
  131. editor.insertText("[color="+color+"]", "[/color]");
  132. }
  133. );
  134. } },
  135. bulletlist: { txtExec: ["[list]\n[li]", "[/li]\n[li][/li]\n[/list]"] },
  136. orderedlist: { txtExec: ["[list type=decimal]\n[li]", "[/li]\n[li][/li]\n[/list]"] },
  137. table: { txtExec: ["[table]\n[tr]\n[td]", "[/td]\n[/tr]\n[/table]"] },
  138. horizontalrule: { txtExec: ["[hr]"] },
  139. code: { txtExec: ["[code]", "[/code]"] },
  140. image: { txtExec: function(caller, selected) {
  141. var url = prompt(this._("Enter the image URL:"), selected);
  142. if(url)
  143. this.insertText("[img]" + url + "[/img]");
  144. } },
  145. email: { txtExec: function(caller, selected) {
  146. var display = selected && selected.indexOf('@') > -1 ? null : selected,
  147. email = prompt(this._("Enter the e-mail address:"), (display ? '' : selected));
  148. if (email)
  149. {
  150. var text = prompt(this._("Enter the displayed text:"), display || email) || email;
  151. this.insertText("[email=" + email + "]" + text + "[/email]");
  152. }
  153. } },
  154. link: { txtExec: function(caller, selected) {
  155. var display = selected && selected.indexOf('http://') > -1 ? null : selected,
  156. url = prompt(this._("Enter URL:"), (display ? 'http://' : selected));
  157. if (url)
  158. {
  159. var text = prompt(this._("Enter the displayed text:"), display || url) || url;
  160. this.insertText("[url=" + url + "]" + text + "[/url]");
  161. }
  162. } },
  163. quote: { txtExec: ["[quote]", "[/quote]"] },
  164. youtube: { txtExec: function(caller) {
  165. var editor = this;
  166. $.sceditor.command.get('youtube')._dropDown(
  167. editor,
  168. caller,
  169. function(id) {
  170. editor.insertText("[youtube]" + id + "[/youtube]");
  171. }
  172. );
  173. } },
  174. rtl: { txtExec: ["[rtl]", "[/rtl]"] },
  175. ltr: { txtExec: ["[ltr]", "[/ltr]"] }
  176. };
  177. return $.extend(true, {}, merge, $.sceditor.commands);
  178. };
  179. /**
  180. * Populates tagsToBbcodes and stylesToBbcodes to enable faster lookups
  181. *
  182. * @private
  183. */
  184. buildBbcodeCache = function() {
  185. $.each(base.bbcodes, function(bbcode, info) {
  186. if(typeof base.bbcodes[bbcode].tags !== "undefined")
  187. $.each(base.bbcodes[bbcode].tags, function(tag, values) {
  188. var isBlock = !!base.bbcodes[bbcode].isBlock;
  189. tagsToBbcodes[tag] = (tagsToBbcodes[tag] || {});
  190. tagsToBbcodes[tag][isBlock] = (tagsToBbcodes[tag][isBlock] || {});
  191. tagsToBbcodes[tag][isBlock][bbcode] = values;
  192. });
  193. if(typeof base.bbcodes[bbcode].styles !== "undefined")
  194. $.each(base.bbcodes[bbcode].styles, function(style, values) {
  195. var isBlock = !!base.bbcodes[bbcode].isBlock;
  196. stylesToBbcodes[isBlock] = (stylesToBbcodes[isBlock] || {});
  197. stylesToBbcodes[isBlock][style] = (stylesToBbcodes[isBlock][style] || {});
  198. stylesToBbcodes[isBlock][style][bbcode] = values;
  199. });
  200. });
  201. };
  202. getStyle = function(element, property) {
  203. var name = $.camelCase(property),
  204. $elm, ret, dir;
  205. // add exception for align
  206. if("text-align" === property)
  207. {
  208. $elm = $(element);
  209. if($elm.parent().css(property) !== $elm.css(property) &&
  210. $elm.css('display') === "block" && !$elm.is('hr') && !$elm.is('th'))
  211. ret = $elm.css(property);
  212. // IE changes text-align to the same as direction so skip unless overried by user
  213. dir = element.style.direction;
  214. if(dir && ((/right/i.test(ret) && dir === 'rtl') || (/left/i.test(ret) && dir === 'ltr')))
  215. return null;
  216. return ret;
  217. }
  218. if(element.style)
  219. return element.style[name];
  220. return null;
  221. };
  222. isEmpty = function(element) {
  223. var childNodes = element.childNodes,
  224. i = childNodes.length;
  225. if(element.nodeValue)
  226. return false;
  227. if(childNodes.length === 0 || (childNodes.length === 1 && (/br/i.test(childNodes[0].nodeName) || isEmpty(childNodes[0]))))
  228. return true;
  229. while(i--)
  230. if(!isEmpty(childNodes[i]))
  231. return false;
  232. return true;
  233. };
  234. /**
  235. * Checks if any bbcode styles match the elements styles
  236. *
  237. * @private
  238. * @return string Content with any matching bbcode tags wrapped around it.
  239. * @Private
  240. */
  241. handleStyles = function(element, content, blockLevel) {
  242. var elementPropVal;
  243. // convert blockLevel to boolean
  244. blockLevel = !!blockLevel;
  245. if(!stylesToBbcodes[blockLevel])
  246. return content;
  247. $.each(stylesToBbcodes[blockLevel], function(property, bbcodes) {
  248. elementPropVal = getStyle(element[0], property);
  249. // if the parent has the same style use that instead of this one
  250. // so you dont end up with [i]parent[i]child[/i][/i]
  251. if(!elementPropVal || getStyle(element.parent()[0], property) === elementPropVal)
  252. return;
  253. $.each(bbcodes, function(bbcode, values) {
  254. if(!/\S|\u00A0/.test(content) && !base.bbcodes[bbcode].allowsEmpty && isEmpty(element[0]))
  255. return;
  256. if(!values || $.inArray(elementPropVal.toString(), values) > -1) {
  257. if($.isFunction(base.bbcodes[bbcode].format))
  258. content = base.bbcodes[bbcode].format.call(base, element, content);
  259. else
  260. content = formatString(base.bbcodes[bbcode].format, content);
  261. }
  262. });
  263. });
  264. return content;
  265. };
  266. /**
  267. * Handles a HTML tag and finds any matching bbcodes
  268. *
  269. * @private
  270. * @param jQuery element element The element to convert
  271. * @param string content The Tags text content
  272. * @param bool blockLevel If to convert block level tags
  273. * @return string Content with any matching bbcode tags wrapped around it.
  274. * @Private
  275. */
  276. handleTags = function(element, content, blockLevel) {
  277. var tag = element[0].nodeName.toLowerCase();
  278. // convert blockLevel to boolean
  279. blockLevel = !!blockLevel;
  280. if(tagsToBbcodes[tag] && tagsToBbcodes[tag][blockLevel]) {
  281. // loop all bbcodes for this tag
  282. $.each(tagsToBbcodes[tag][blockLevel], function(bbcode, bbcodeAttribs) {
  283. if(!/\S|\u00A0/.test(content) && !base.bbcodes[bbcode].allowsEmpty && isEmpty(element[0]))
  284. return;
  285. // if the bbcode requires any attributes then check this has
  286. // all needed
  287. if(bbcodeAttribs) {
  288. var runBbcode = false;
  289. // loop all the bbcode attribs
  290. $.each(bbcodeAttribs, function(attrib, values)
  291. {
  292. // if the element has the bbcodes attribute and the bbcode attribute
  293. // has values check one of the values matches
  294. if(!element.attr(attrib) || (values && $.inArray(element.attr(attrib), values) < 0))
  295. return;
  296. // break this loop as we have matched this bbcode
  297. runBbcode = true;
  298. return false;
  299. });
  300. if(!runBbcode)
  301. return;
  302. }
  303. if($.isFunction(base.bbcodes[bbcode].format))
  304. content = base.bbcodes[bbcode].format.call(base, element, content);
  305. else
  306. content = formatString(base.bbcodes[bbcode].format, content);
  307. });
  308. }
  309. // add newline after paragraph elements p and div (WebKit uses divs) and br tags
  310. if(blockLevel && /^(br|div|p)$/.test(tag))
  311. {
  312. // Only treat divs/p as a newline if their last child was not a new line.
  313. if(!(/^(div|p)$/i.test(tag) && element[0].lastChild && element[0].lastChild.nodeName.toLowerCase() === "br"))
  314. content += "\n";
  315. // needed for browsers that enter textnode then when return is pressed put the rest in a div, i.e.:
  316. // text<div>line 2</div>
  317. if("br" !== tag && !$.sceditor.dom.isInline(element[0].parentNode) && element[0].previousSibling &&
  318. element[0].previousSibling.nodeType === 3) {
  319. content = "\n" + content;
  320. }
  321. }
  322. return content;
  323. };
  324. /**
  325. * Formats a string in the format
  326. * {0}, {1}, {2}, ect. with the params provided
  327. * @private
  328. * @return string
  329. * @Private
  330. */
  331. formatString = function() {
  332. var args = arguments;
  333. return args[0].replace(/\{(\d+)\}/g, function(str, p1) {
  334. return typeof args[p1-0+1] !== "undefined" ?
  335. args[p1-0+1] :
  336. '{' + p1 + '}';
  337. });
  338. };
  339. /**
  340. * Removes any leading or trailing quotes ('")
  341. *
  342. * @return string
  343. * @memberOf jQuery.sceditorBBCodePlugin.prototype
  344. */
  345. base.stripQuotes = function(str) {
  346. return str.replace(/^(["'])(.*?)\1$/, "$2");
  347. };
  348. /**
  349. * Converts HTML to BBCode
  350. * @param string html Html string, this function ignores this, it works off domBody
  351. * @param HtmlElement domBody Editors dom body object to convert
  352. * @return string BBCode which has been converted from HTML
  353. * @memberOf jQuery.sceditorBBCodePlugin.prototype
  354. */
  355. base.getHtmlHandler = function(html, domBody, filter) {
  356. if (typeof filter == 'undefined')
  357. $.sceditor.dom.removeWhiteSpace(domBody[0]);
  358. return $.trim(base.elementToBbcode(domBody));
  359. };
  360. /**
  361. * Converts a HTML dom element to BBCode starting from
  362. * the innermost element and working backwards
  363. *
  364. * @private
  365. * @param HtmlElement element The element to convert to BBCode
  366. * @param array vChildren Valid child tags allowed
  367. * @return string BBCode
  368. * @memberOf jQuery.sceditorBBCodePlugin.prototype
  369. */
  370. base.elementToBbcode = function($element) {
  371. return (function toBBCode(node, vChildren) {
  372. var ret = '';
  373. $.sceditor.dom.traverse(node, function(node) {
  374. var $node = $(node),
  375. curTag = '',
  376. tag = node.nodeName.toLowerCase(),
  377. vChild = validChildren[tag],
  378. isValidChild = true;
  379. if(typeof vChildren === 'object')
  380. {
  381. isValidChild = $.inArray(tag, vChildren) > -1;
  382. // if this tag is one of the parents allowed children
  383. // then set this tags allowed children to whatever it allows,
  384. // otherwise set to what the parent allows
  385. if(!isValidChild)
  386. vChild = vChildren;
  387. }
  388. // 3 is text element
  389. if(node.nodeType !== 3)
  390. {
  391. // skip ignored elments
  392. if($node.hasClass("sceditor-ignore"))
  393. return;
  394. // don't loop inside iframes
  395. if(tag !== 'iframe')
  396. curTag = toBBCode(node, vChild);
  397. if(isValidChild)
  398. {
  399. // code tags should skip most styles
  400. if(!$node.is('code'))
  401. {
  402. // handle inline bbcodes
  403. curTag = handleStyles($node, curTag);
  404. curTag = handleTags($node, curTag);
  405. // handle blocklevel bbcodes
  406. curTag = handleStyles($node, curTag, true);
  407. }
  408. ret += handleTags($node, curTag, true);
  409. }
  410. else
  411. ret += curTag;
  412. }
  413. else if(node.wholeText && (!node.previousSibling || node.previousSibling.nodeType !== 3))
  414. {
  415. if($(node).parents('code').length === 0)
  416. ret += node.wholeText.replace(/ +/g, " ");
  417. else
  418. ret += node.wholeText;
  419. }
  420. else if(!node.wholeText)
  421. ret += node.nodeValue;
  422. }, false, true);
  423. return ret;
  424. }($element.get(0)));
  425. };
  426. /**
  427. * Converts BBCode to HTML
  428. *
  429. * @param {String} text
  430. * @param {Bool} isFragment
  431. * @return {String} HTML
  432. * @memberOf jQuery.sceditorBBCodePlugin.prototype
  433. */
  434. base.getTextHandler = function(text, isFragment) {
  435. var oldText, replaceBBCodeFunc,
  436. // Previous bbcodeRegex = /\[([^\[\s=]*?)(?:([\s=][^\[]*?))?\]((?:[\s\S(?!=\[\\\1)](?!\[\1))*?)\[\/(\1)\]/g,
  437. bbcodeRegex = /\[([^\[\s=]+)(?:([\s=][^\[\]]+))?\]((?:[\s\S](?!\[\1))*?)\[\/(\1)\]/gi,
  438. atribsRegex = /(\S+)=((?:(?:(["'])(?:\\\3|[^\3])*?\3))|(?:[^'"\s]+))/gi;
  439. replaceBBCodeFunc = function(str, bbcode, attrs, content)
  440. {
  441. var attrsMap = {},
  442. matches;
  443. bbcode = bbcode.toLowerCase();
  444. if(attrs)
  445. {
  446. attrs = $.trim(attrs);
  447. // if only one attribute then remove the = from the start and strip any quotes
  448. if((attrs.charAt(0) === "=" && (attrs.split("=").length - 1) <= 1) || bbcode === 'url')
  449. attrsMap.defaultattr = base.stripQuotes(attrs.substr(1));
  450. else
  451. {
  452. if(attrs.charAt(0) === "=")
  453. attrs = "defaultattr" + attrs;
  454. if (typeof base.bbcodes[bbcode].attrs == 'function')
  455. {
  456. var declaredAttrs = base.bbcodes[bbcode].attrs();
  457. var attrArray = new Array;
  458. var compatArray = new Array;
  459. for (var i = 0; i < declaredAttrs.length; i++)
  460. {
  461. var attrPos = attrs.indexOf(declaredAttrs[i]);
  462. if (attrPos != -1)
  463. {
  464. attrArray[attrPos] = [declaredAttrs[i], attrPos + declaredAttrs[i].length + 1];
  465. }
  466. }
  467. for (var attrElem in attrArray)
  468. compatArray.push(attrArray[attrElem]);
  469. for (var i = 0; i < compatArray.length; i++)
  470. {
  471. if (typeof compatArray[i+1] != 'undefined')
  472. attrsMap[compatArray[i][0].toLowerCase()] = attrs.substr(compatArray[i][1], attrs.indexOf(compatArray[i+1][0]) - compatArray[i][1]).trim();
  473. else
  474. attrsMap[compatArray[i][0].toLowerCase()] = attrs.substr(compatArray[i][1], attrs.length);
  475. }
  476. }
  477. else
  478. while((matches = atribsRegex.exec(attrs)))
  479. attrsMap[matches[1].toLowerCase()] = base.stripQuotes(matches[2]);
  480. }
  481. }
  482. if(!base.bbcodes[bbcode])
  483. return str;
  484. if($.isFunction(base.bbcodes[bbcode].html))
  485. return base.bbcodes[bbcode].html.call(base, bbcode, attrsMap, content);
  486. else
  487. return formatString(base.bbcodes[bbcode].html, content);
  488. };
  489. text = text.replace(/&/g, "&amp;")
  490. .replace(/</g, "&lt;")
  491. .replace(/>/g, "&gt;")
  492. .replace(/\r/g, "")
  493. .replace(/(\[\/?(?:left|center|right|justify|align|rtl|ltr)\])\n/g, "$1")
  494. .replace(/\n/g, "<br />");
  495. while(text !== oldText)
  496. {
  497. oldText = text;
  498. text = text.replace(bbcodeRegex, replaceBBCodeFunc);
  499. }
  500. // As hr is the only bbcode not to have a start and end tag it's
  501. // just being replace here instead of adding support for it above.
  502. text = text.replace(/\[hr\]/gi, "<hr>")
  503. .replace(/\[\*\]/gi, "<li>");
  504. // replace multi-spaces which are not inside tags with a non-breaking space
  505. // to preserve them. Otherwise they will just be converted to 1!
  506. text = text.replace(/ {2}(?=([^<\>]*?<|[^<\>]*?$))/g, " &nbsp;");
  507. return wrapInDivs(text, isFragment);
  508. };
  509. /**
  510. * Wraps divs around inline HTML. Needed for IE
  511. *
  512. * @param string html
  513. * @return string HTML
  514. * @private
  515. */
  516. wrapInDivs = function(html, excludeFirstLast)
  517. {
  518. var d = document,
  519. inlineFrag = d.createDocumentFragment(),
  520. outputDiv = d.createElement('div'),
  521. tmpDiv = d.createElement('div'),
  522. div, node, next, nodeName;
  523. $(tmpDiv).hide().appendTo(d.body);
  524. tmpDiv.innerHTML = html;
  525. node = tmpDiv.firstChild;
  526. while(node)
  527. {
  528. next = node.nextSibling;
  529. nodeName = node.nodeName.toLowerCase();
  530. if((node.nodeType === 1 && !$.sceditor.dom.isInline(node)) || nodeName === "br")
  531. {
  532. if(inlineFrag.childNodes.length > 0 || nodeName === "br")
  533. {
  534. div = d.createElement('div');
  535. div.appendChild(inlineFrag);
  536. // Putting BR in a div in IE9 causes it to do a double line break,
  537. // as much as I hate browser UA sniffing, to do feature detection would
  538. // be more code than it's worth for this specific bug.
  539. if(nodeName === "br" && !$.sceditor.ie)
  540. div.appendChild(d.createElement('br'));
  541. // If it's an empty DIV and in compatibility mode is below IE8 then
  542. // we must add a non-breaking space to the div otherwise the div
  543. // will be collapsed. Adding a BR works but when you press enter
  544. // to make a newline it suddenly gose back to the normal IE div
  545. // behaviour and creates two lines, one for the newline and one
  546. // for the BR. I'm sure there must be a better fix but I've yet to
  547. // find one.
  548. // Cannot do zoom: 1; or set a height on the div to fix it as that
  549. // causes resize handles to be added to the div when it's clicked on/
  550. if(!div.childNodes.length && (d.documentMode && d.documentMode < 8 || $.sceditor.ie < 8))
  551. div.appendChild(d.createTextNode('\u00a0'));
  552. outputDiv.appendChild(div);
  553. inlineFrag = d.createDocumentFragment();
  554. }
  555. if(nodeName !== "br")
  556. outputDiv.appendChild(node);
  557. }
  558. else
  559. inlineFrag.appendChild(node);
  560. node = next;
  561. }
  562. if(inlineFrag.childNodes.length > 0)
  563. {
  564. div = d.createElement('div');
  565. div.appendChild(inlineFrag);
  566. outputDiv.appendChild(div);
  567. }
  568. // needed for paste, the first shouldn't be wrapped in a div
  569. if(excludeFirstLast)
  570. {
  571. node = outputDiv.firstChild;
  572. if(node && node.nodeName.toLowerCase() === "div")
  573. {
  574. while((next = node.firstChild))
  575. outputDiv.insertBefore(next, node);
  576. if($.sceditor.ie >= 9)
  577. outputDiv.insertBefore(d.createElement('br'), node);
  578. outputDiv.removeChild(node);
  579. }
  580. node = outputDiv.lastChild;
  581. if(node && node.nodeName.toLowerCase() === "div")
  582. {
  583. while((next = node.firstChild))
  584. outputDiv.insertBefore(next, node);
  585. if($.sceditor.ie >= 9)
  586. outputDiv.insertBefore(d.createElement('br'), node);
  587. outputDiv.removeChild(node);
  588. }
  589. }
  590. $(tmpDiv).remove();
  591. return outputDiv.innerHTML;
  592. };
  593. init();
  594. };
  595. $.sceditorBBCodePlugin.bbcodes = {
  596. // START_COMMAND: Bold
  597. b: {
  598. tags: {
  599. b: null,
  600. strong: null
  601. },
  602. styles: {
  603. // 401 is for FF 3.5
  604. "font-weight": ["bold", "bolder", "401", "700", "800", "900"]
  605. },
  606. format: "[b]{0}[/b]",
  607. html: '<strong>{0}</strong>'
  608. },
  609. // END_COMMAND
  610. // START_COMMAND: Italic
  611. i: {
  612. tags: {
  613. i: null,
  614. em: null
  615. },
  616. styles: {
  617. "font-style": ["italic", "oblique"]
  618. },
  619. format: "[i]{0}[/i]",
  620. html: '<em>{0}</em>'
  621. },
  622. // END_COMMAND
  623. // START_COMMAND: Underline
  624. u: {
  625. tags: {
  626. u: null
  627. },
  628. styles: {
  629. "text-decoration": ["underline"]
  630. },
  631. format: "[u]{0}[/u]",
  632. html: '<u>{0}</u>'
  633. },
  634. // END_COMMAND
  635. // START_COMMAND: Strikethrough
  636. s: {
  637. tags: {
  638. s: null,
  639. strike: null
  640. },
  641. styles: {
  642. "text-decoration": ["line-through"]
  643. },
  644. format: "[s]{0}[/s]",
  645. html: '<s>{0}</s>'
  646. },
  647. // END_COMMAND
  648. // START_COMMAND: Subscript
  649. sub: {
  650. tags: {
  651. sub: null
  652. },
  653. format: "[sub]{0}[/sub]",
  654. html: '<sub>{0}</sub>'
  655. },
  656. // END_COMMAND
  657. // START_COMMAND: Superscript
  658. sup: {
  659. tags: {
  660. sup: null
  661. },
  662. format: "[sup]{0}[/sup]",
  663. html: '<sup>{0}</sup>'
  664. },
  665. // END_COMMAND
  666. // START_COMMAND: Font
  667. font: {
  668. tags: {
  669. font: {
  670. face: null
  671. }
  672. },
  673. styles: {
  674. "font-family": null
  675. },
  676. format: function(element, content) {
  677. if(element[0].nodeName.toLowerCase() === "font" && element.attr('face'))
  678. return '[font=' + this.stripQuotes(element.attr('face')) + ']' + content + '[/font]';
  679. return '[font=' + this.stripQuotes(element.css('font-family')) + ']' + content + '[/font]';
  680. },
  681. html: function(element, attrs, content) {
  682. return '<font face="' + attrs.defaultattr + '">' + content + '</font>';
  683. }
  684. },
  685. // END_COMMAND
  686. // START_COMMAND: Size
  687. size: {
  688. tags: {
  689. font: {
  690. size: null
  691. }
  692. },
  693. styles: {
  694. "font-size": null
  695. },
  696. format: function(element, content) {
  697. var fontSize = element.css('fontSize'),
  698. size = 1;
  699. if(element.attr('size'))
  700. size = element.attr('size');
  701. // Most browsers return px value but IE returns 1-7
  702. else if(fontSize.indexOf("px") > -1) {
  703. // convert size to an int
  704. fontSize = fontSize.replace("px", "") - 0;
  705. if(fontSize > 12)
  706. size = 2;
  707. if(fontSize > 15)
  708. size = 3;
  709. if(fontSize > 17)
  710. size = 4;
  711. if(fontSize > 23)
  712. size = 5;
  713. if(fontSize > 31)
  714. size = 6;
  715. if(fontSize > 47)
  716. size = 7;
  717. }
  718. else
  719. size = fontSize;
  720. return '[size=' + size + ']' + content + '[/size]';
  721. },
  722. html: function(element, attrs, content) {
  723. return '<font size="' + attrs.defaultattr + '">' + content + '</font>';
  724. }
  725. },
  726. // END_COMMAND
  727. // START_COMMAND: Color
  728. color: {
  729. tags: {
  730. font: {
  731. color: null
  732. }
  733. },
  734. styles: {
  735. color: null
  736. },
  737. format: function(element, content) {
  738. /**
  739. * Converts CSS rgb value into hex
  740. * @private
  741. * @return string Hex color
  742. */
  743. var rgbToHex = function(rgbStr) {
  744. var m;
  745. function toHex(n) {
  746. n = parseInt(n,10);
  747. if(isNaN(n))
  748. return "00";
  749. n = Math.max(0,Math.min(n,255)).toString(16);
  750. return n.length<2 ? '0'+n : n;
  751. }
  752. // rgb(n,n,n);
  753. if((m = rgbStr.match(/rgb\((\d+),\s*?(\d+),\s*?(\d+)\)/i)))
  754. return '#' + toHex(m[1]) + toHex(m[2]-0) + toHex(m[3]-0);
  755. // expand shorthand
  756. if((m = rgbStr.match(/#([0-f])([0-f])([0-f])\s*?$/i)))
  757. return '#' + m[1] + m[1] + m[2] + m[2] + m[3] + m[3];
  758. return rgbStr;
  759. };
  760. var color = element.css('color');
  761. if(element[0].nodeName.toLowerCase() === "font" && element.attr('color'))
  762. color = element.attr('color');
  763. color = rgbToHex(color);
  764. return '[color=' + color + ']' + content + '[/color]';
  765. },
  766. html: function(element, attrs, content) {
  767. return '<font color="' + attrs.defaultattr + '">' + content + '</font>';
  768. }
  769. },
  770. black: {
  771. html: '<font color="black">{0}</font>'
  772. },
  773. blue: {
  774. html: '<font color="blue">{0}</font>'
  775. },
  776. green: {
  777. html: '<font color="green">{0}</font>'
  778. },
  779. red: {
  780. html: '<font color="red">{0}</font>'
  781. },
  782. white: {
  783. html: '<font color="white">{0}</font>'
  784. },
  785. // END_COMMAND
  786. // START_COMMAND: Lists
  787. list: {
  788. isBlock: true,
  789. html: function(element, attrs, content) {
  790. var style = '';
  791. var code = 'ul';
  792. if (attrs.type)
  793. style = ' style="list-style-type: ' + attrs.type + '"';
  794. return '<' + code + style + '>' + content + '</' + code + '>';
  795. }
  796. },
  797. ul: {
  798. tags: {
  799. ul: null
  800. },
  801. isBlock: true,
  802. format: function(element, content) {
  803. if ($(element[0]).css('list-style-type') == 'disc')
  804. return '[list]' + content + '[/list]';
  805. else
  806. return '[list type=' + $(element[0]).css('list-style-type') + ']' + content + '[/list]';
  807. },
  808. html: '<ul>{0}</ul>'
  809. },
  810. ol: {
  811. tags: {
  812. ol: null
  813. },
  814. isBlock: true,
  815. format: '[list type=decimal]{0}[/list]',
  816. html: '<ol>{0}</ol>'
  817. },
  818. li: {
  819. tags: {
  820. li: null
  821. },
  822. format: "[li]{0}[/li]",
  823. html: '<li>{0}</li>'
  824. },
  825. "*": {
  826. html: '<li>{0}</li>'
  827. },
  828. // END_COMMAND
  829. // START_COMMAND: Table
  830. table: {
  831. tags: {
  832. table: null
  833. },
  834. format: "[table]{0}[/table]",
  835. html: '<table>{0}</table>'
  836. },
  837. tr: {
  838. tags: {
  839. tr: null
  840. },
  841. format: "[tr]{0}[/tr]",
  842. html: '<tr>{0}</tr>'
  843. },
  844. th: {
  845. tags: {
  846. th: null
  847. },
  848. isBlock: true,
  849. format: "[th]{0}[/th]",
  850. html: '<th>{0}</th>'
  851. },
  852. td: {
  853. tags: {
  854. td: null
  855. },
  856. isBlock: true,
  857. format: "[td]{0}[/td]",
  858. html: '<td>{0}<br class="sceditor-ignore" /></td>'
  859. },
  860. // END_COMMAND
  861. // START_COMMAND: Emoticons
  862. emoticon: {
  863. allowsEmpty: true,
  864. tags: {
  865. img: {
  866. src: null,
  867. "data-sceditor-emoticon": null
  868. }
  869. },
  870. format: function(element, content) {
  871. if (element.attr('data-sceditor-emoticon') == '')
  872. return content;
  873. return element.attr('data-sceditor-emoticon') + content;
  874. },
  875. html: '{0}'
  876. },
  877. // END_COMMAND
  878. // START_COMMAND: Horizontal Rule
  879. horizontalrule: {
  880. allowsEmpty: true,
  881. tags: {
  882. hr: null
  883. },
  884. format: "[hr]{0}",
  885. html: "<hr />"
  886. },
  887. // END_COMMAND
  888. // START_COMMAND: Image
  889. img: {
  890. allowsEmpty: true,
  891. tags: {
  892. img: {
  893. src: null
  894. }
  895. },
  896. format: function(element, content) {
  897. var attribs = '',
  898. style = function(name) {
  899. return element.style ? element.style[name] : null;
  900. };
  901. // check if this is an emoticon image
  902. if(typeof element.attr('data-sceditor-emoticon') !== "undefined")
  903. return content;
  904. var width = ' width=' + $(element).width();
  905. var height = ' height=' + $(element).height();
  906. var alt = $(element).attr('alt') != undefined ? ' alt=' + $(element).attr('alt').php_unhtmlspecialchars() : '';
  907. return '[img' + width + height + alt + ']' + element.attr('src') + '[/img]';
  908. },
  909. attrs: function () {
  910. return ['alt', 'width', 'height'];
  911. },
  912. html: function(element, attrs, content) {
  913. var attribs = "", parts;
  914. // handle [img width=340 height=240]url[/img]
  915. if(typeof attrs.width !== "undefined")
  916. attribs += ' width="' + attrs.width + '"';
  917. if(typeof attrs.height !== "undefined")
  918. attribs += ' height="' + attrs.height + '"';
  919. if(typeof attrs.alt !== "undefined")
  920. attribs += ' alt="' + attrs.alt + '"';
  921. return '<img ' + attribs + ' src="' + content + '" />';
  922. }
  923. },
  924. // END_COMMAND
  925. // START_COMMAND: URL
  926. url: {
  927. allowsEmpty: true,
  928. tags: {
  929. a: {
  930. href: null
  931. }
  932. },
  933. format: function(element, content) {
  934. // make sure this link is not an e-mail, if it is return e-mail BBCode
  935. if(element.attr('href').substr(0, 7) === 'mailto:')
  936. return '[email=' + element.attr('href').substr(7) + ']' + content + '[/email]';
  937. // make sure this link is not an ftp, if it is return ftp BBCode
  938. else if(element.attr('href').substr(0, 3) === 'ftp')
  939. return '[ftp=' + element.attr('href') + ']' + content + '[/ftp]';
  940. if(element.attr('target') !== undefined)
  941. return '[url=' + decodeURI(element.attr('href')) + ']' + content + '[/url]';
  942. else
  943. return '[iurl=' + decodeURI(element.attr('href')) + ']' + content + '[/iurl]';
  944. },
  945. html: function(element, attrs, content) {
  946. if(typeof attrs.defaultattr === "undefined" || attrs.defaultattr.length === 0)
  947. attrs.defaultattr = content;
  948. return '<a target="_blank" href="' + encodeURI(attrs.defaultattr) + '">' + content + '</a>';
  949. }
  950. },
  951. iurl: {
  952. html: function(element, attrs, content) {
  953. if(typeof attrs.defaultattr === "undefined" || attrs.defaultattr.length === 0)
  954. attrs.defaultattr = content;
  955. return '<a href="' + encodeURI(attrs.defaultattr) + '">' + content + '</a>';
  956. }
  957. },
  958. ftp: {
  959. html: function(element, attrs, content) {
  960. if(typeof attrs.defaultattr === "undefined" || attrs.defaultattr.length === 0)
  961. attrs.defaultattr = content;
  962. return '<a target="_blank" href="' + encodeURI(attrs.defaultattr) + '">' + content + '</a>';
  963. }
  964. },
  965. // END_COMMAND
  966. // START_COMMAND: E-mail
  967. email: {
  968. html: function(element, attrs, content) {
  969. if(typeof attrs.defaultattr === "undefined")
  970. attrs.defaultattr = content;
  971. return '<a href="mailto:' + attrs.defaultattr + '">' + content + '</a>';
  972. }
  973. },
  974. // END_COMMAND
  975. // START_COMMAND: Quote
  976. quote: {
  977. tags: {
  978. blockquote: null,
  979. cite: null
  980. },
  981. isBlock: true,
  982. format: function(element, content) {
  983. var author = '';
  984. var date = '';
  985. var link = '';
  986. if (element[0].tagName.toLowerCase() == 'cite')
  987. return '';
  988. if ($(element).attr('author'))
  989. author = ' author=' + $(element).attr('author').php_unhtmlspecialchars();
  990. if ($(element).attr('date'))
  991. date = ' date=' + $(element).attr('date');
  992. if ($(element).attr('link'))
  993. link = ' link=' + $(element).attr('link');
  994. return '[quote' + author + date + link + ']' + content + '[/quote]';
  995. },
  996. attrs: function () {
  997. return ['author', 'date', 'link'];
  998. },
  999. html: function(element, attrs, content) {
  1000. var attr_author = '', author = '';
  1001. var attr_date = '', sDate = '';
  1002. var attr_link = '', link = '';
  1003. if(typeof attrs.author !== "undefined" && attrs.author)
  1004. {
  1005. attr_author = attrs.author;
  1006. author = bbc_quote_from + ': ' + attr_author;
  1007. }
  1008. // Links could be in the form: link=topic=71.msg201#msg201 that would fool javascript, so we need a workaround
  1009. // Probably no more necessary
  1010. for (var key in attrs)
  1011. {
  1012. if (key.substr(0, 4) == 'link' && attrs.hasOwnProperty(key))
  1013. {
  1014. var attr_link = key.length > 4 ? key.substr(5) + '=' + attrs[key] : attrs[key];
  1015. link = attr_link.substr(0, 7) == 'http://' ? attr_link : smf_scripturl + '?' + attr_link;
  1016. author = author == '' ? '<a href="' + link + '">' + bbc_quote_from + ': ' + link + '</a>' : '<a href="' + link + '">' + author + '</a>';
  1017. }
  1018. }
  1019. if(typeof attrs.date !== "undefined" && attrs.date)
  1020. {
  1021. attr_date = attrs.date;
  1022. sDate = '<date timestamp="' + attr_date + '">' + new Date(attrs.date * 1000) + '</date>';
  1023. }
  1024. if (author == '' && sDate == '')
  1025. author = bbc_quote;
  1026. else
  1027. author += ' ' + bbc_search_on;
  1028. content = '<blockquote author="' + attr_author + '" date="' + attr_date + '" link="' + attr_link + '"><cite>' + author + ' ' + sDate + '</cite>' + content + '</blockquote>';
  1029. return content;
  1030. }
  1031. },
  1032. // END_COMMAND
  1033. // START_COMMAND: Code
  1034. code: {
  1035. tags: {
  1036. code: null
  1037. },
  1038. isBlock: true,
  1039. format: function(element, content) {
  1040. if ($(element[0]).hasClass('php'))
  1041. return '[php]' + content.replace('&#91;', '[') + '[/php]';
  1042. var from = '';
  1043. if ($(element).children("cite:first").length === 1)
  1044. {
  1045. from = $(element).children("cite:first").text();
  1046. $(element).attr({'from': from.php_htmlspecialchars()});
  1047. from = '=' + from;
  1048. content = '';
  1049. $(element).children("cite:first").remove();
  1050. content = this.elementToBbcode($(element));
  1051. }
  1052. else
  1053. {
  1054. if ($(element).attr('from') != undefined)
  1055. {
  1056. from = '=' + $(element).attr('from').php_unhtmlspecialchars();
  1057. }
  1058. }
  1059. return '[code' + from + ']' + content.replace('&#91;', '[') + '[/code]';
  1060. },
  1061. html: function(element, attrs, content) {
  1062. var from = '';
  1063. if(typeof attrs.defaultattr !== "undefined")
  1064. from = '<cite>' + attrs.defaultattr + '</cite>';
  1065. return '<code>' + from + content.replace('[', '&#91;') + '</code>'
  1066. }
  1067. },
  1068. php: {
  1069. isBlock: true,
  1070. format: "[php]{0}[/php]",
  1071. html: '<code class="php">{0}</code>'
  1072. },
  1073. // END_COMMAND
  1074. // START_COMMAND: Left
  1075. left: {
  1076. styles: {
  1077. "text-align": ["left", "-webkit-left", "-moz-left", "-khtml-left"]
  1078. },
  1079. isBlock: true,
  1080. format: "[left]{0}[/left]",
  1081. html: '<div align="left">{0}</div>'
  1082. },
  1083. // END_COMMAND
  1084. // START_COMMAND: Centre
  1085. center: {
  1086. styles: {
  1087. "text-align": ["center", "-webkit-center", "-moz-center", "-khtml-center"]
  1088. },
  1089. isBlock: true,
  1090. format: "[center]{0}[/center]",
  1091. html: '<div align="center">{0}</div>'
  1092. },
  1093. // END_COMMAND
  1094. // START_COMMAND: Right
  1095. right: {
  1096. styles: {
  1097. "text-align": ["right", "-webkit-right", "-moz-right", "-khtml-right"]
  1098. },
  1099. isBlock: true,
  1100. format: "[right]{0}[/right]",
  1101. html: '<div align="right">{0}</div>'
  1102. },
  1103. // END_COMMAND
  1104. // START_COMMAND: Justify
  1105. justify: {
  1106. styles: {
  1107. "text-align": ["justify", "-webkit-justify", "-moz-justify", "-khtml-justify"]
  1108. },
  1109. isBlock: true,
  1110. format: "[justify]{0}[/justify]",
  1111. html: '<div align="justify">{0}</div>'
  1112. },
  1113. // END_COMMAND
  1114. // START_COMMAND: YouTube
  1115. youtube: {
  1116. allowsEmpty: true,
  1117. tags: {
  1118. iframe: {
  1119. 'data-youtube-id': null
  1120. }
  1121. },
  1122. format: function(element, content) {
  1123. if(!element.attr('data-youtube-id'))
  1124. return content;
  1125. return '[youtube]' + element.attr('data-youtube-id') + '[/youtube]';
  1126. },
  1127. html: '<iframe width="560" height="315" src="http://www.youtube.com/embed/{0}?wmode=opaque' +
  1128. '" data-youtube-id="{0}" frameborder="0" allowfullscreen></iframe>'
  1129. },
  1130. // END_COMMAND
  1131. // START_COMMAND: Rtl
  1132. rtl: {
  1133. styles: {
  1134. "direction": ["rtl"]
  1135. },
  1136. format: "[rtl]{0}[/rtl]",
  1137. html: '<div style="direction: rtl">{0}</div>'
  1138. },
  1139. // END_COMMAND
  1140. // START_COMMAND: Ltr
  1141. ltr: {
  1142. styles: {
  1143. "direction": ["ltr"]
  1144. },
  1145. format: "[ltr]{0}[/ltr]",
  1146. html: '<div style="direction: ltr">{0}</div>'
  1147. },
  1148. // END_COMMAND
  1149. abbr: {
  1150. tags: {
  1151. abbr: {
  1152. title: null
  1153. }
  1154. },
  1155. format: function(element, content) {
  1156. return '[abbr=' + element.attr('title') + ']' + content + '[/abbr]';
  1157. },
  1158. html: function(element, attrs, content) {
  1159. if(typeof attrs.defaultattr === "undefined" || attrs.defaultattr.length === 0)
  1160. return content;
  1161. return '<abbr title="' + attrs.defaultattr + '">' + content + '</abbr>';
  1162. }
  1163. },
  1164. acronym: {
  1165. tags: {
  1166. acronym: {
  1167. title: null
  1168. }
  1169. },
  1170. format: function(element, content) {
  1171. return '[acronym=' + element.attr('title') + ']' + content + '[/acronym]';
  1172. },
  1173. html: function(element, attrs, content) {
  1174. if(typeof attrs.defaultattr === "undefined" || attrs.defaultattr.length === 0)
  1175. return content;
  1176. return '<acronym title="' + attrs.defaultattr + '">' + content + '</acronym>';
  1177. }
  1178. },
  1179. bdo: {
  1180. tags: {
  1181. bdo: {
  1182. dir: null
  1183. }
  1184. },
  1185. format: function(element, content) {
  1186. return '[bdo=' + element.attr('dir') + ']' + content + '[/bdo]';
  1187. },
  1188. html: function(element, attrs, content) {
  1189. if(typeof attrs.defaultattr === "undefined" || attrs.defaultattr.length === 0)
  1190. return content;
  1191. if (attrs.defaultattr != 'rtl' && attrs.defaultattr != 'ltr')
  1192. return '[bdo=' + attrs.defaultattr + ']' + content + '[/bdo]';
  1193. return '<bdo dir="' + attrs.defaultattr + '">' + content + '</bdo>';
  1194. }
  1195. },
  1196. tt: {
  1197. tags: {
  1198. tt: null
  1199. },
  1200. format: "[tt]{0}[/tt]",
  1201. html: '<tt>{0}</tt>'
  1202. },
  1203. pre: {
  1204. tags: {
  1205. pre: null
  1206. },
  1207. isBlock: true,
  1208. format: "[pre]{0}[/pre]",
  1209. html: "<pre>{0}</pre>\n"
  1210. },
  1211. move: {
  1212. tags: {
  1213. marquee: null
  1214. },
  1215. format: "[move]{0}[/move]",
  1216. html: '<marquee>{0}</marquee>'
  1217. },
  1218. // this is here so that commands above can be removed
  1219. // without having to remove the , after the last one.
  1220. // Needed for IE.
  1221. ignore: {}
  1222. };
  1223. /**
  1224. * Static BBCode helper class
  1225. * @class command
  1226. * @name jQuery.sceditorBBCodePlugin.bbcode
  1227. */
  1228. $.sceditorBBCodePlugin.bbcode =
  1229. /** @lends jQuery.sceditorBBCodePlugin.bbcode */
  1230. {
  1231. /**
  1232. * Gets a BBCode
  1233. *
  1234. * @param {String} name
  1235. * @return {Object|null}
  1236. * @since v1.3.5
  1237. */
  1238. get: function(name) {
  1239. return $.sceditorBBCodePlugin.bbcodes[name] || null;
  1240. },
  1241. /**
  1242. * <p>Adds a BBCode to the parser or updates an exisiting
  1243. * BBCode if a BBCode with the specified name already exists.</p>
  1244. *
  1245. * @param {String} name
  1246. * @param {Object} bbcode
  1247. * @return {this|false} Returns false if name or bbcode is false
  1248. * @since v1.3.5
  1249. */
  1250. set: function(name, bbcode) {
  1251. if(!name || !bbcode)
  1252. return false;
  1253. // merge any existing command properties
  1254. bbcode = $.extend($.sceditorBBCodePlugin.bbcodes[name] || {}, bbcode);
  1255. bbcode.remove = function() { $.sceditorBBCodePlugin.bbcode.remove(name); };
  1256. $.sceditorBBCodePlugin.bbcodes[name] = bbcode;
  1257. return this;
  1258. },
  1259. /**
  1260. * Removes a BBCode
  1261. *
  1262. * @param {String} name
  1263. * @return {this}
  1264. * @since v1.3.5
  1265. */
  1266. remove: function(name) {
  1267. if($.sceditorBBCodePlugin.bbcodes[name])
  1268. delete $.sceditorBBCodePlugin.bbcodes[name];
  1269. return this;
  1270. }
  1271. };
  1272. /**
  1273. * Checks if a command with the specified name exists
  1274. *
  1275. * @param string name
  1276. * @return bool
  1277. * @deprecated Since v1.3.5
  1278. * @memberOf jQuery.sceditorBBCodePlugin
  1279. */
  1280. $.sceditorBBCodePlugin.commandExists = function(name) {
  1281. return !!$.sceditorBBCodePlugin.bbcode.get(name);
  1282. };
  1283. /**
  1284. * Adds/updates a BBCode.
  1285. *
  1286. * @param String name The BBCode name
  1287. * @param Object tags Any html tags this bbcode applies to, i.e. strong for [b]
  1288. * @param Object styles Any style properties this applies to, i.e. font-weight for [b]
  1289. * @param String|Function format Function or string to convert the element into BBCode
  1290. * @param String|Function html String or function to format the BBCode back into HTML.
  1291. * @param bool allowsEmpty If this BBCodes is allowed to be empty, e.g. [b][/b]
  1292. * @return Bool
  1293. * @deprecated Since v1.3.5
  1294. * @memberOf jQuery.sceditorBBCodePlugin
  1295. */
  1296. $.sceditorBBCodePlugin.setCommand = function(name, tags, styles, format, html, allowsEmpty, isBlock) {
  1297. return $.sceditorBBCodePlugin.bbcode.set(name,
  1298. {
  1299. tags: tags || {},
  1300. styles: styles || {},
  1301. allowsEmpty: allowsEmpty,
  1302. isBlock: isBlock,
  1303. format: format,
  1304. html: html
  1305. });
  1306. };
  1307. $.fn.sceditorBBCodePlugin = function(options) {
  1308. if((!options || !options.runWithoutWysiwygSupport) && !$.sceditor.isWysiwygSupported())
  1309. return;
  1310. return this.each(function() {
  1311. (new $.sceditorBBCodePlugin(this, options));
  1312. });
  1313. };
  1314. })(jQuery);