jquery.sceditor.bbcode.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  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) {
  356. $.sceditor.dom.removeWhiteSpace(domBody[0]);
  357. return $.trim(base.elementToBbcode(domBody));
  358. };
  359. /**
  360. * Converts a HTML dom element to BBCode starting from
  361. * the innermost element and working backwards
  362. *
  363. * @private
  364. * @param HtmlElement element The element to convert to BBCode
  365. * @param array vChildren Valid child tags allowed
  366. * @return string BBCode
  367. * @memberOf jQuery.sceditorBBCodePlugin.prototype
  368. */
  369. base.elementToBbcode = function($element) {
  370. return (function toBBCode(node, vChildren) {
  371. var ret = '';
  372. $.sceditor.dom.traverse(node, function(node) {
  373. var $node = $(node),
  374. curTag = '',
  375. tag = node.nodeName.toLowerCase(),
  376. vChild = validChildren[tag],
  377. isValidChild = true;
  378. if(typeof vChildren === 'object')
  379. {
  380. isValidChild = $.inArray(tag, vChildren) > -1;
  381. // if this tag is one of the parents allowed children
  382. // then set this tags allowed children to whatever it allows,
  383. // otherwise set to what the parent allows
  384. if(!isValidChild)
  385. vChild = vChildren;
  386. }
  387. // 3 is text element
  388. if(node.nodeType !== 3)
  389. {
  390. // skip ignored elments
  391. if($node.hasClass("sceditor-ignore"))
  392. return;
  393. // don't loop inside iframes
  394. if(tag !== 'iframe')
  395. curTag = toBBCode(node, vChild);
  396. if(isValidChild)
  397. {
  398. // code tags should skip most styles
  399. if(!$node.is('code'))
  400. {
  401. // handle inline bbcodes
  402. curTag = handleStyles($node, curTag);
  403. curTag = handleTags($node, curTag);
  404. // handle blocklevel bbcodes
  405. curTag = handleStyles($node, curTag, true);
  406. }
  407. ret += handleTags($node, curTag, true);
  408. }
  409. else
  410. ret += curTag;
  411. }
  412. else if(node.wholeText && (!node.previousSibling || node.previousSibling.nodeType !== 3))
  413. {
  414. if($(node).parents('code').length === 0)
  415. ret += node.wholeText.replace(/ +/g, " ");
  416. else
  417. ret += node.wholeText;
  418. }
  419. else if(!node.wholeText)
  420. ret += node.nodeValue;
  421. }, false, true);
  422. return ret;
  423. }($element.get(0)));
  424. };
  425. /**
  426. * Converts BBCode to HTML
  427. *
  428. * @param {String} text
  429. * @param {Bool} isFragment
  430. * @return {String} HTML
  431. * @memberOf jQuery.sceditorBBCodePlugin.prototype
  432. */
  433. base.getTextHandler = function(text, isFragment) {
  434. var oldText, replaceBBCodeFunc,
  435. // Previous bbcodeRegex = /\[([^\[\s=]*?)(?:([\s=][^\[]*?))?\]((?:[\s\S(?!=\[\\\1)](?!\[\1))*?)\[\/(\1)\]/g,
  436. bbcodeRegex = /\[([^\[\s=]+)(?:([\s=][^\[\]]+))?\]((?:[\s\S](?!\[\1))*?)\[\/(\1)\]/gi,
  437. atribsRegex = /(\S+)=((?:(?:(["'])(?:\\\3|[^\3])*?\3))|(?:[^'"\s]+))/gi;
  438. replaceBBCodeFunc = function(str, bbcode, attrs, content)
  439. {
  440. var attrsMap = {},
  441. matches;
  442. bbcode = bbcode.toLowerCase();
  443. if(attrs)
  444. {
  445. attrs = $.trim(attrs);
  446. // if only one attribute then remove the = from the start and strip any quotes
  447. if((attrs.charAt(0) === "=" && (attrs.split("=").length - 1) <= 1) || bbcode === 'url')
  448. attrsMap.defaultattr = base.stripQuotes(attrs.substr(1));
  449. else
  450. {
  451. if(attrs.charAt(0) === "=")
  452. attrs = "defaultattr" + attrs;
  453. if (typeof base.bbcodes[bbcode].attrs == 'function')
  454. {
  455. var declaredAttrs = base.bbcodes[bbcode].attrs();
  456. var attrArray = new Array;
  457. var compatArray = new Array;
  458. for (var i = 0; i < declaredAttrs.length; i++)
  459. {
  460. var attrPos = attrs.indexOf(declaredAttrs[i]);
  461. if (attrPos != -1)
  462. {
  463. attrArray[attrPos] = [declaredAttrs[i], attrPos + declaredAttrs[i].length + 1];
  464. }
  465. }
  466. for (var attrElem in attrArray)
  467. compatArray.push(attrArray[attrElem]);
  468. for (var i = 0; i < compatArray.length; i++)
  469. {
  470. if (typeof compatArray[i+1] != 'undefined')
  471. attrsMap[compatArray[i][0].toLowerCase()] = attrs.substr(compatArray[i][1], attrs.indexOf(compatArray[i+1][0]) - compatArray[i][1]).trim();
  472. else
  473. attrsMap[compatArray[i][0].toLowerCase()] = attrs.substr(compatArray[i][1], attrs.length);
  474. }
  475. }
  476. else
  477. while((matches = atribsRegex.exec(attrs)))
  478. attrsMap[matches[1].toLowerCase()] = base.stripQuotes(matches[2]);
  479. }
  480. }
  481. if(!base.bbcodes[bbcode])
  482. return str;
  483. if($.isFunction(base.bbcodes[bbcode].html))
  484. return base.bbcodes[bbcode].html.call(base, bbcode, attrsMap, content);
  485. else
  486. return formatString(base.bbcodes[bbcode].html, content);
  487. };
  488. text = text.replace(/&/g, "&amp;")
  489. .replace(/</g, "&lt;")
  490. .replace(/>/g, "&gt;")
  491. .replace(/\r/g, "")
  492. .replace(/(\[\/?(?:left|center|right|justify|align|rtl|ltr)\])\n/g, "$1")
  493. .replace(/\n/g, "<br />");
  494. while(text !== oldText)
  495. {
  496. oldText = text;
  497. text = text.replace(bbcodeRegex, replaceBBCodeFunc);
  498. }
  499. // As hr is the only bbcode not to have a start and end tag it's
  500. // just being replace here instead of adding support for it above.
  501. text = text.replace(/\[hr\]/gi, "<hr>")
  502. .replace(/\[\*\]/gi, "<li>");
  503. // replace multi-spaces which are not inside tags with a non-breaking space
  504. // to preserve them. Otherwise they will just be converted to 1!
  505. text = text.replace(/ {2}(?=([^<\>]*?<|[^<\>]*?$))/g, " &nbsp;");
  506. return wrapInDivs(text, isFragment);
  507. };
  508. /**
  509. * Wraps divs around inline HTML. Needed for IE
  510. *
  511. * @param string html
  512. * @return string HTML
  513. * @private
  514. */
  515. wrapInDivs = function(html, excludeFirstLast)
  516. {
  517. var d = document,
  518. inlineFrag = d.createDocumentFragment(),
  519. outputDiv = d.createElement('div'),
  520. tmpDiv = d.createElement('div'),
  521. div, node, next, nodeName;
  522. $(tmpDiv).hide().appendTo(d.body);
  523. tmpDiv.innerHTML = html;
  524. node = tmpDiv.firstChild;
  525. while(node)
  526. {
  527. next = node.nextSibling;
  528. nodeName = node.nodeName.toLowerCase();
  529. if((node.nodeType === 1 && !$.sceditor.dom.isInline(node)) || nodeName === "br")
  530. {
  531. if(inlineFrag.childNodes.length > 0 || nodeName === "br")
  532. {
  533. div = d.createElement('div');
  534. div.appendChild(inlineFrag);
  535. // Putting BR in a div in IE9 causes it to do a double line break,
  536. // as much as I hate browser UA sniffing, to do feature detection would
  537. // be more code than it's worth for this specific bug.
  538. if(nodeName === "br" && !$.sceditor.ie)
  539. div.appendChild(d.createElement('br'));
  540. // If it's an empty DIV and in compatibility mode is below IE8 then
  541. // we must add a non-breaking space to the div otherwise the div
  542. // will be collapsed. Adding a BR works but when you press enter
  543. // to make a newline it suddenly gose back to the normal IE div
  544. // behaviour and creates two lines, one for the newline and one
  545. // for the BR. I'm sure there must be a better fix but I've yet to
  546. // find one.
  547. // Cannot do zoom: 1; or set a height on the div to fix it as that
  548. // causes resize handles to be added to the div when it's clicked on/
  549. if(!div.childNodes.length && (d.documentMode && d.documentMode < 8 || $.sceditor.ie < 8))
  550. div.appendChild(d.createTextNode('\u00a0'));
  551. outputDiv.appendChild(div);
  552. inlineFrag = d.createDocumentFragment();
  553. }
  554. if(nodeName !== "br")
  555. outputDiv.appendChild(node);
  556. }
  557. else
  558. inlineFrag.appendChild(node);
  559. node = next;
  560. }
  561. if(inlineFrag.childNodes.length > 0)
  562. {
  563. div = d.createElement('div');
  564. div.appendChild(inlineFrag);
  565. outputDiv.appendChild(div);
  566. }
  567. // needed for paste, the first shouldn't be wrapped in a div
  568. if(excludeFirstLast)
  569. {
  570. node = outputDiv.firstChild;
  571. if(node && node.nodeName.toLowerCase() === "div")
  572. {
  573. while((next = node.firstChild))
  574. outputDiv.insertBefore(next, node);
  575. if($.sceditor.ie >= 9)
  576. outputDiv.insertBefore(d.createElement('br'), node);
  577. outputDiv.removeChild(node);
  578. }
  579. node = outputDiv.lastChild;
  580. if(node && node.nodeName.toLowerCase() === "div")
  581. {
  582. while((next = node.firstChild))
  583. outputDiv.insertBefore(next, node);
  584. if($.sceditor.ie >= 9)
  585. outputDiv.insertBefore(d.createElement('br'), node);
  586. outputDiv.removeChild(node);
  587. }
  588. }
  589. $(tmpDiv).remove();
  590. return outputDiv.innerHTML;
  591. };
  592. init();
  593. };
  594. $.sceditorBBCodePlugin.bbcodes = {
  595. // START_COMMAND: Bold
  596. b: {
  597. tags: {
  598. b: null,
  599. strong: null
  600. },
  601. styles: {
  602. // 401 is for FF 3.5
  603. "font-weight": ["bold", "bolder", "401", "700", "800", "900"]
  604. },
  605. format: "[b]{0}[/b]",
  606. html: '<strong>{0}</strong>'
  607. },
  608. // END_COMMAND
  609. // START_COMMAND: Italic
  610. i: {
  611. tags: {
  612. i: null,
  613. em: null
  614. },
  615. styles: {
  616. "font-style": ["italic", "oblique"]
  617. },
  618. format: "[i]{0}[/i]",
  619. html: '<em>{0}</em>'
  620. },
  621. // END_COMMAND
  622. // START_COMMAND: Underline
  623. u: {
  624. tags: {
  625. u: null
  626. },
  627. styles: {
  628. "text-decoration": ["underline"]
  629. },
  630. format: "[u]{0}[/u]",
  631. html: '<u>{0}</u>'
  632. },
  633. // END_COMMAND
  634. // START_COMMAND: Strikethrough
  635. s: {
  636. tags: {
  637. s: null,
  638. strike: null
  639. },
  640. styles: {
  641. "text-decoration": ["line-through"]
  642. },
  643. format: "[s]{0}[/s]",
  644. html: '<s>{0}</s>'
  645. },
  646. // END_COMMAND
  647. // START_COMMAND: Subscript
  648. sub: {
  649. tags: {
  650. sub: null
  651. },
  652. format: "[sub]{0}[/sub]",
  653. html: '<sub>{0}</sub>'
  654. },
  655. // END_COMMAND
  656. // START_COMMAND: Superscript
  657. sup: {
  658. tags: {
  659. sup: null
  660. },
  661. format: "[sup]{0}[/sup]",
  662. html: '<sup>{0}</sup>'
  663. },
  664. // END_COMMAND
  665. // START_COMMAND: Font
  666. font: {
  667. tags: {
  668. font: {
  669. face: null
  670. }
  671. },
  672. styles: {
  673. "font-family": null
  674. },
  675. format: function(element, content) {
  676. if(element[0].nodeName.toLowerCase() === "font" && element.attr('face'))
  677. return '[font=' + this.stripQuotes(element.attr('face')) + ']' + content + '[/font]';
  678. return '[font=' + this.stripQuotes(element.css('font-family')) + ']' + content + '[/font]';
  679. },
  680. html: function(element, attrs, content) {
  681. return '<font face="' + attrs.defaultattr + '">' + content + '</font>';
  682. }
  683. },
  684. // END_COMMAND
  685. // START_COMMAND: Size
  686. size: {
  687. tags: {
  688. font: {
  689. size: null
  690. }
  691. },
  692. styles: {
  693. "font-size": null
  694. },
  695. format: function(element, content) {
  696. var fontSize = element.css('fontSize'),
  697. size = 1;
  698. if(element.attr('size'))
  699. size = element.attr('size');
  700. // Most browsers return px value but IE returns 1-7
  701. else if(fontSize.indexOf("px") > -1) {
  702. // convert size to an int
  703. fontSize = fontSize.replace("px", "") - 0;
  704. if(fontSize > 12)
  705. size = 2;
  706. if(fontSize > 15)
  707. size = 3;
  708. if(fontSize > 17)
  709. size = 4;
  710. if(fontSize > 23)
  711. size = 5;
  712. if(fontSize > 31)
  713. size = 6;
  714. if(fontSize > 47)
  715. size = 7;
  716. }
  717. else
  718. size = fontSize;
  719. return '[size=' + size + ']' + content + '[/size]';
  720. },
  721. html: function(element, attrs, content) {
  722. return '<font size="' + attrs.defaultattr + '">' + content + '</font>';
  723. }
  724. },
  725. // END_COMMAND
  726. // START_COMMAND: Color
  727. color: {
  728. tags: {
  729. font: {
  730. color: null
  731. }
  732. },
  733. styles: {
  734. color: null
  735. },
  736. format: function(element, content) {
  737. /**
  738. * Converts CSS rgb value into hex
  739. * @private
  740. * @return string Hex color
  741. */
  742. var rgbToHex = function(rgbStr) {
  743. var m;
  744. function toHex(n) {
  745. n = parseInt(n,10);
  746. if(isNaN(n))
  747. return "00";
  748. n = Math.max(0,Math.min(n,255)).toString(16);
  749. return n.length<2 ? '0'+n : n;
  750. }
  751. // rgb(n,n,n);
  752. if((m = rgbStr.match(/rgb\((\d+),\s*?(\d+),\s*?(\d+)\)/i)))
  753. return '#' + toHex(m[1]) + toHex(m[2]-0) + toHex(m[3]-0);
  754. // expand shorthand
  755. if((m = rgbStr.match(/#([0-f])([0-f])([0-f])\s*?$/i)))
  756. return '#' + m[1] + m[1] + m[2] + m[2] + m[3] + m[3];
  757. return rgbStr;
  758. };
  759. var color = element.css('color');
  760. if(element[0].nodeName.toLowerCase() === "font" && element.attr('color'))
  761. color = element.attr('color');
  762. color = rgbToHex(color);
  763. return '[color=' + color + ']' + content + '[/color]';
  764. },
  765. html: function(element, attrs, content) {
  766. return '<font color="' + attrs.defaultattr + '">' + content + '</font>';
  767. }
  768. },
  769. black: {
  770. html: '<font color="black">{0}</font>'
  771. },
  772. blue: {
  773. html: '<font color="blue">{0}</font>'
  774. },
  775. green: {
  776. html: '<font color="green">{0}</font>'
  777. },
  778. red: {
  779. html: '<font color="red">{0}</font>'
  780. },
  781. white: {
  782. html: '<font color="white">{0}</font>'
  783. },
  784. // END_COMMAND
  785. // START_COMMAND: Lists
  786. list: {
  787. isBlock: true,
  788. html: function(element, attrs, content) {
  789. var style = '';
  790. var code = 'ul';
  791. if (attrs.type)
  792. style = ' style="list-style-type: ' + attrs.type + '"';
  793. return '<' + code + style + '>' + content + '</' + code + '>';
  794. }
  795. },
  796. ul: {
  797. tags: {
  798. ul: null
  799. },
  800. isBlock: true,
  801. format: function(element, content) {
  802. if ($(element[0]).css('list-style-type') == 'disc')
  803. return '[list]' + content + '[/list]';
  804. else
  805. return '[list type=' + $(element[0]).css('list-style-type') + ']' + content + '[/list]';
  806. },
  807. html: '<ul>{0}</ul>'
  808. },
  809. ol: {
  810. tags: {
  811. ol: null
  812. },
  813. isBlock: true,
  814. format: '[list type=decimal]{0}[/list]',
  815. html: '<ol>{0}</ol>'
  816. },
  817. li: {
  818. tags: {
  819. li: null
  820. },
  821. format: "[li]{0}[/li]",
  822. html: '<li>{0}</li>'
  823. },
  824. "*": {
  825. html: '<li>{0}</li>'
  826. },
  827. // END_COMMAND
  828. // START_COMMAND: Table
  829. table: {
  830. tags: {
  831. table: null
  832. },
  833. format: "[table]{0}[/table]",
  834. html: '<table>{0}</table>'
  835. },
  836. tr: {
  837. tags: {
  838. tr: null
  839. },
  840. format: "[tr]{0}[/tr]",
  841. html: '<tr>{0}</tr>'
  842. },
  843. th: {
  844. tags: {
  845. th: null
  846. },
  847. isBlock: true,
  848. format: "[th]{0}[/th]",
  849. html: '<th>{0}</th>'
  850. },
  851. td: {
  852. tags: {
  853. td: null
  854. },
  855. isBlock: true,
  856. format: "[td]{0}[/td]",
  857. html: '<td>{0}<br class="sceditor-ignore" /></td>'
  858. },
  859. // END_COMMAND
  860. // START_COMMAND: Emoticons
  861. emoticon: {
  862. allowsEmpty: true,
  863. tags: {
  864. img: {
  865. src: null,
  866. "data-sceditor-emoticon": null
  867. }
  868. },
  869. format: function(element, content) {
  870. if (element.attr('data-sceditor-emoticon') == '')
  871. return content;
  872. return element.attr('data-sceditor-emoticon') + content;
  873. },
  874. html: '{0}'
  875. },
  876. // END_COMMAND
  877. // START_COMMAND: Horizontal Rule
  878. horizontalrule: {
  879. allowsEmpty: true,
  880. tags: {
  881. hr: null
  882. },
  883. format: "[hr]{0}",
  884. html: "<hr />"
  885. },
  886. // END_COMMAND
  887. // START_COMMAND: Image
  888. img: {
  889. allowsEmpty: true,
  890. tags: {
  891. img: {
  892. src: null
  893. }
  894. },
  895. format: function(element, content) {
  896. var attribs = '',
  897. style = function(name) {
  898. return element.style ? element.style[name] : null;
  899. };
  900. // check if this is an emoticon image
  901. if(typeof element.attr('data-sceditor-emoticon') !== "undefined")
  902. return content;
  903. var width = ' width=' + $(element).width();
  904. var height = ' height=' + $(element).height();
  905. var alt = $(element).attr('alt') != undefined ? ' alt=' + $(element).attr('alt').php_unhtmlspecialchars() : '';
  906. return '[img' + width + height + alt + ']' + element.attr('src') + '[/img]';
  907. },
  908. attrs: function () {
  909. return ['alt', 'width', 'height'];
  910. },
  911. html: function(element, attrs, content) {
  912. var attribs = "", parts;
  913. // handle [img width=340 height=240]url[/img]
  914. if(typeof attrs.width !== "undefined")
  915. attribs += ' width="' + attrs.width + '"';
  916. if(typeof attrs.height !== "undefined")
  917. attribs += ' height="' + attrs.height + '"';
  918. if(typeof attrs.alt !== "undefined")
  919. attribs += ' alt="' + attrs.alt + '"';
  920. return '<img ' + attribs + ' src="' + content + '" />';
  921. }
  922. },
  923. // END_COMMAND
  924. // START_COMMAND: URL
  925. url: {
  926. allowsEmpty: true,
  927. tags: {
  928. a: {
  929. href: null
  930. }
  931. },
  932. format: function(element, content) {
  933. // make sure this link is not an e-mail, if it is return e-mail BBCode
  934. if(element.attr('href').substr(0, 7) === 'mailto:')
  935. return '[email=' + element.attr('href').substr(7) + ']' + content + '[/email]';
  936. // make sure this link is not an ftp, if it is return ftp BBCode
  937. else if(element.attr('href').substr(0, 3) === 'ftp')
  938. return '[ftp=' + element.attr('href') + ']' + content + '[/ftp]';
  939. if(element.attr('target') !== undefined)
  940. return '[url=' + decodeURI(element.attr('href')) + ']' + content + '[/url]';
  941. else
  942. return '[iurl=' + decodeURI(element.attr('href')) + ']' + content + '[/iurl]';
  943. },
  944. html: function(element, attrs, content) {
  945. if(typeof attrs.defaultattr === "undefined" || attrs.defaultattr.length === 0)
  946. attrs.defaultattr = content;
  947. return '<a target="_blank" href="' + encodeURI(attrs.defaultattr) + '">' + content + '</a>';
  948. }
  949. },
  950. iurl: {
  951. html: function(element, attrs, content) {
  952. if(typeof attrs.defaultattr === "undefined" || attrs.defaultattr.length === 0)
  953. attrs.defaultattr = content;
  954. return '<a href="' + encodeURI(attrs.defaultattr) + '">' + content + '</a>';
  955. }
  956. },
  957. ftp: {
  958. html: function(element, attrs, content) {
  959. if(typeof attrs.defaultattr === "undefined" || attrs.defaultattr.length === 0)
  960. attrs.defaultattr = content;
  961. return '<a target="_blank" href="' + encodeURI(attrs.defaultattr) + '">' + content + '</a>';
  962. }
  963. },
  964. // END_COMMAND
  965. // START_COMMAND: E-mail
  966. email: {
  967. html: function(element, attrs, content) {
  968. if(typeof attrs.defaultattr === "undefined")
  969. attrs.defaultattr = content;
  970. return '<a href="mailto:' + attrs.defaultattr + '">' + content + '</a>';
  971. }
  972. },
  973. // END_COMMAND
  974. // START_COMMAND: Quote
  975. quote: {
  976. tags: {
  977. blockquote: null,
  978. cite: null
  979. },
  980. isBlock: true,
  981. format: function(element, content) {
  982. var author = '';
  983. var date = '';
  984. var link = '';
  985. if (element[0].tagName.toLowerCase() == 'cite')
  986. return '';
  987. if ($(element).attr('author'))
  988. author = ' author=' + $(element).attr('author').php_unhtmlspecialchars();
  989. if ($(element).attr('date'))
  990. date = ' date=' + $(element).attr('date');
  991. if ($(element).attr('link'))
  992. link = ' link=' + $(element).attr('link');
  993. return '[quote' + author + date + link + ']' + content + '[/quote]';
  994. },
  995. attrs: function () {
  996. return ['author', 'date', 'link'];
  997. },
  998. html: function(element, attrs, content) {
  999. var attr_author = '', author = '';
  1000. var attr_date = '', sDate = '';
  1001. var attr_link = '', link = '';
  1002. if(typeof attrs.author !== "undefined" && attrs.author)
  1003. {
  1004. attr_author = attrs.author;
  1005. author = bbc_quote_from + ': ' + attr_author;
  1006. }
  1007. // Links could be in the form: link=topic=71.msg201#msg201 that would fool javascript, so we need a workaround
  1008. // Probably no more necessary
  1009. for (var key in attrs)
  1010. {
  1011. if (key.substr(0, 4) == 'link' && attrs.hasOwnProperty(key))
  1012. {
  1013. var attr_link = key.length > 4 ? key.substr(5) + '=' + attrs[key] : attrs[key];
  1014. link = attr_link.substr(0, 7) == 'http://' ? attr_link : smf_scripturl + '?' + attr_link;
  1015. author = author == '' ? '<a href="' + link + '">' + bbc_quote_from + ': ' + link + '</a>' : '<a href="' + link + '">' + author + '</a>';
  1016. }
  1017. }
  1018. if(typeof attrs.date !== "undefined" && attrs.date)
  1019. {
  1020. attr_date = attrs.date;
  1021. sDate = '<date timestamp="' + attr_date + '">' + new Date(attrs.date * 1000) + '</date>';
  1022. }
  1023. if (author == '' && sDate == '')
  1024. author = bbc_quote;
  1025. else
  1026. author += ' ' + bbc_search_on;
  1027. content = '<blockquote author="' + attr_author + '" date="' + attr_date + '" link="' + attr_link + '"><cite>' + author + ' ' + sDate + '</cite>' + content + '</blockquote>';
  1028. return content;
  1029. }
  1030. },
  1031. // END_COMMAND
  1032. // START_COMMAND: Code
  1033. code: {
  1034. tags: {
  1035. code: null
  1036. },
  1037. isBlock: true,
  1038. format: function(element, content) {
  1039. if ($(element[0]).hasClass('php'))
  1040. return '[php]' + content.replace('&#91;', '[') + '[/php]';
  1041. var from = '';
  1042. if ($(element).children("cite:first").length === 1)
  1043. {
  1044. from = $(element).children("cite:first").text();
  1045. $(element).attr({'from': from.php_htmlspecialchars()});
  1046. from = '=' + from;
  1047. content = '';
  1048. $(element).children("cite:first").remove();
  1049. content = this.elementToBbcode($(element));
  1050. }
  1051. else
  1052. {
  1053. if ($(element).attr('from') != undefined)
  1054. {
  1055. from = '=' + $(element).attr('from').php_unhtmlspecialchars();
  1056. }
  1057. }
  1058. return '[code' + from + ']' + content.replace('&#91;', '[') + '[/code]';
  1059. },
  1060. html: function(element, attrs, content) {
  1061. var from = '';
  1062. if(typeof attrs.defaultattr !== "undefined")
  1063. from = '<cite>' + attrs.defaultattr + '</cite>';
  1064. return '<code>' + from + content.replace('[', '&#91;') + '</code>'
  1065. }
  1066. },
  1067. php: {
  1068. isBlock: true,
  1069. format: "[php]{0}[/php]",
  1070. html: '<code class="php">{0}</code>'
  1071. },
  1072. // END_COMMAND
  1073. // START_COMMAND: Left
  1074. left: {
  1075. styles: {
  1076. "text-align": ["left", "-webkit-left", "-moz-left", "-khtml-left"]
  1077. },
  1078. isBlock: true,
  1079. format: "[left]{0}[/left]",
  1080. html: '<div align="left">{0}</div>'
  1081. },
  1082. // END_COMMAND
  1083. // START_COMMAND: Centre
  1084. center: {
  1085. styles: {
  1086. "text-align": ["center", "-webkit-center", "-moz-center", "-khtml-center"]
  1087. },
  1088. isBlock: true,
  1089. format: "[center]{0}[/center]",
  1090. html: '<div align="center">{0}</div>'
  1091. },
  1092. // END_COMMAND
  1093. // START_COMMAND: Right
  1094. right: {
  1095. styles: {
  1096. "text-align": ["right", "-webkit-right", "-moz-right", "-khtml-right"]
  1097. },
  1098. isBlock: true,
  1099. format: "[right]{0}[/right]",
  1100. html: '<div align="right">{0}</div>'
  1101. },
  1102. // END_COMMAND
  1103. // START_COMMAND: Justify
  1104. justify: {
  1105. styles: {
  1106. "text-align": ["justify", "-webkit-justify", "-moz-justify", "-khtml-justify"]
  1107. },
  1108. isBlock: true,
  1109. format: "[justify]{0}[/justify]",
  1110. html: '<div align="justify">{0}</div>'
  1111. },
  1112. // END_COMMAND
  1113. // START_COMMAND: YouTube
  1114. youtube: {
  1115. allowsEmpty: true,
  1116. tags: {
  1117. iframe: {
  1118. 'data-youtube-id': null
  1119. }
  1120. },
  1121. format: function(element, content) {
  1122. if(!element.attr('data-youtube-id'))
  1123. return content;
  1124. return '[youtube]' + element.attr('data-youtube-id') + '[/youtube]';
  1125. },
  1126. html: '<iframe width="560" height="315" src="http://www.youtube.com/embed/{0}?wmode=opaque' +
  1127. '" data-youtube-id="{0}" frameborder="0" allowfullscreen></iframe>'
  1128. },
  1129. // END_COMMAND
  1130. // START_COMMAND: Rtl
  1131. rtl: {
  1132. styles: {
  1133. "direction": ["rtl"]
  1134. },
  1135. format: "[rtl]{0}[/rtl]",
  1136. html: '<div style="direction: rtl">{0}</div>'
  1137. },
  1138. // END_COMMAND
  1139. // START_COMMAND: Ltr
  1140. ltr: {
  1141. styles: {
  1142. "direction": ["ltr"]
  1143. },
  1144. format: "[ltr]{0}[/ltr]",
  1145. html: '<div style="direction: ltr">{0}</div>'
  1146. },
  1147. // END_COMMAND
  1148. abbr: {
  1149. tags: {
  1150. abbr: {
  1151. title: null
  1152. }
  1153. },
  1154. format: function(element, content) {
  1155. return '[abbr=' + element.attr('title') + ']' + content + '[/abbr]';
  1156. },
  1157. html: function(element, attrs, content) {
  1158. if(typeof attrs.defaultattr === "undefined" || attrs.defaultattr.length === 0)
  1159. return content;
  1160. return '<abbr title="' + attrs.defaultattr + '">' + content + '</abbr>';
  1161. }
  1162. },
  1163. acronym: {
  1164. tags: {
  1165. acronym: {
  1166. title: null
  1167. }
  1168. },
  1169. format: function(element, content) {
  1170. return '[acronym=' + element.attr('title') + ']' + content + '[/acronym]';
  1171. },
  1172. html: function(element, attrs, content) {
  1173. if(typeof attrs.defaultattr === "undefined" || attrs.defaultattr.length === 0)
  1174. return content;
  1175. return '<acronym title="' + attrs.defaultattr + '">' + content + '</acronym>';
  1176. }
  1177. },
  1178. bdo: {
  1179. tags: {
  1180. bdo: {
  1181. dir: null
  1182. }
  1183. },
  1184. format: function(element, content) {
  1185. return '[bdo=' + element.attr('dir') + ']' + content + '[/bdo]';
  1186. },
  1187. html: function(element, attrs, content) {
  1188. if(typeof attrs.defaultattr === "undefined" || attrs.defaultattr.length === 0)
  1189. return content;
  1190. if (attrs.defaultattr != 'rtl' && attrs.defaultattr != 'ltr')
  1191. return '[bdo=' + attrs.defaultattr + ']' + content + '[/bdo]';
  1192. return '<bdo dir="' + attrs.defaultattr + '">' + content + '</bdo>';
  1193. }
  1194. },
  1195. tt: {
  1196. tags: {
  1197. tt: null
  1198. },
  1199. format: "[tt]{0}[/tt]",
  1200. html: '<tt>{0}</tt>'
  1201. },
  1202. pre: {
  1203. tags: {
  1204. pre: null
  1205. },
  1206. isBlock: true,
  1207. format: "[pre]{0}[/pre]",
  1208. html: "<pre>{0}</pre>\n"
  1209. },
  1210. move: {
  1211. tags: {
  1212. marquee: null
  1213. },
  1214. format: "[move]{0}[/move]",
  1215. html: '<marquee>{0}</marquee>'
  1216. },
  1217. // this is here so that commands above can be removed
  1218. // without having to remove the , after the last one.
  1219. // Needed for IE.
  1220. ignore: {}
  1221. };
  1222. /**
  1223. * Static BBCode helper class
  1224. * @class command
  1225. * @name jQuery.sceditorBBCodePlugin.bbcode
  1226. */
  1227. $.sceditorBBCodePlugin.bbcode =
  1228. /** @lends jQuery.sceditorBBCodePlugin.bbcode */
  1229. {
  1230. /**
  1231. * Gets a BBCode
  1232. *
  1233. * @param {String} name
  1234. * @return {Object|null}
  1235. * @since v1.3.5
  1236. */
  1237. get: function(name) {
  1238. return $.sceditorBBCodePlugin.bbcodes[name] || null;
  1239. },
  1240. /**
  1241. * <p>Adds a BBCode to the parser or updates an exisiting
  1242. * BBCode if a BBCode with the specified name already exists.</p>
  1243. *
  1244. * @param {String} name
  1245. * @param {Object} bbcode
  1246. * @return {this|false} Returns false if name or bbcode is false
  1247. * @since v1.3.5
  1248. */
  1249. set: function(name, bbcode) {
  1250. if(!name || !bbcode)
  1251. return false;
  1252. // merge any existing command properties
  1253. bbcode = $.extend($.sceditorBBCodePlugin.bbcodes[name] || {}, bbcode);
  1254. bbcode.remove = function() { $.sceditorBBCodePlugin.bbcode.remove(name); };
  1255. $.sceditorBBCodePlugin.bbcodes[name] = bbcode;
  1256. return this;
  1257. },
  1258. /**
  1259. * Removes a BBCode
  1260. *
  1261. * @param {String} name
  1262. * @return {this}
  1263. * @since v1.3.5
  1264. */
  1265. remove: function(name) {
  1266. if($.sceditorBBCodePlugin.bbcodes[name])
  1267. delete $.sceditorBBCodePlugin.bbcodes[name];
  1268. return this;
  1269. }
  1270. };
  1271. /**
  1272. * Checks if a command with the specified name exists
  1273. *
  1274. * @param string name
  1275. * @return bool
  1276. * @deprecated Since v1.3.5
  1277. * @memberOf jQuery.sceditorBBCodePlugin
  1278. */
  1279. $.sceditorBBCodePlugin.commandExists = function(name) {
  1280. return !!$.sceditorBBCodePlugin.bbcode.get(name);
  1281. };
  1282. /**
  1283. * Adds/updates a BBCode.
  1284. *
  1285. * @param String name The BBCode name
  1286. * @param Object tags Any html tags this bbcode applies to, i.e. strong for [b]
  1287. * @param Object styles Any style properties this applies to, i.e. font-weight for [b]
  1288. * @param String|Function format Function or string to convert the element into BBCode
  1289. * @param String|Function html String or function to format the BBCode back into HTML.
  1290. * @param bool allowsEmpty If this BBCodes is allowed to be empty, e.g. [b][/b]
  1291. * @return Bool
  1292. * @deprecated Since v1.3.5
  1293. * @memberOf jQuery.sceditorBBCodePlugin
  1294. */
  1295. $.sceditorBBCodePlugin.setCommand = function(name, tags, styles, format, html, allowsEmpty, isBlock) {
  1296. return $.sceditorBBCodePlugin.bbcode.set(name,
  1297. {
  1298. tags: tags || {},
  1299. styles: styles || {},
  1300. allowsEmpty: allowsEmpty,
  1301. isBlock: isBlock,
  1302. format: format,
  1303. html: html
  1304. });
  1305. };
  1306. $.fn.sceditorBBCodePlugin = function(options) {
  1307. if((!options || !options.runWithoutWysiwygSupport) && !$.sceditor.isWysiwygSupported())
  1308. return;
  1309. return this.each(function() {
  1310. (new $.sceditorBBCodePlugin(this, options));
  1311. });
  1312. };
  1313. })(jQuery);