jquery.sceditor.bbcode.js 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  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. 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. 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)\]/g,
  437. atribsRegex = /(\S+)=((?:(?:(["'])(?:\\\3|[^\3])*?\3))|(?:[^'"\s]+))/g;
  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('author').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. if(element.attr('target') !== undefined)
  937. return '[url=' + decodeURI(element.attr('href')) + ']' + content + '[/url]';
  938. else
  939. return '[iurl=' + decodeURI(element.attr('href')) + ']' + content + '[/iurl]';
  940. },
  941. html: function(element, attrs, content) {
  942. if(typeof attrs.defaultAttr === "undefined" || attrs.defaultAttr.length === 0)
  943. attrs.defaultAttr = content;
  944. return '<a target="_blank" href="' + encodeURI(attrs.defaultAttr) + '">' + content + '</a>';
  945. }
  946. },
  947. iurl: {
  948. html: function(element, attrs, content) {
  949. if(typeof attrs.defaultAttr === "undefined" || attrs.defaultAttr.length === 0)
  950. attrs.defaultAttr = content;
  951. return '<a href="' + encodeURI(attrs.defaultAttr) + '">' + content + '</a>';
  952. }
  953. },
  954. ftp: {
  955. html: function(element, attrs, content) {
  956. if(typeof attrs.defaultAttr === "undefined" || attrs.defaultAttr.length === 0)
  957. attrs.defaultAttr = content;
  958. return '<a target="_blank" href="' + encodeURI(attrs.defaultAttr) + '">' + content + '</a>';
  959. }
  960. },
  961. // END_COMMAND
  962. // START_COMMAND: E-mail
  963. email: {
  964. html: function(element, attrs, content) {
  965. if(typeof attrs.defaultattr === "undefined")
  966. attrs.defaultattr = content;
  967. return '<a href="mailto:' + attrs.defaultattr + '">' + content + '</a>';
  968. }
  969. },
  970. // END_COMMAND
  971. // START_COMMAND: Quote
  972. quote: {
  973. tags: {
  974. blockquote: null
  975. },
  976. isBlock: true,
  977. format: function(element, content) {
  978. var author = '';
  979. var date = '';
  980. var link = '';
  981. if ($(element).children("cite:first").length === 1)
  982. {
  983. author = $(element).children("cite:first").find("author").text();
  984. date = $(element).children("cite:first").find("date").attr('timestamp');
  985. link = $(element).children("cite:first").find("quote_link").text();
  986. $(element).attr({'author': author.php_htmlspecialchars(), 'date': date, 'link': link});
  987. if (author != '')
  988. author = ' author=' + author;
  989. if (date != '')
  990. date = ' date=' + date;
  991. if (link != '')
  992. link = ' link=' + link;
  993. content = '';
  994. $(element).children("cite:first").remove();
  995. var preserve_newline = $(element).children().last().is("br") && $(element).children().last().last().is("br");
  996. content = this.elementToBbcode($(element)) + (preserve_newline ? "\n" : '');
  997. }
  998. else
  999. {
  1000. if ($(element).attr('author') != undefined)
  1001. author = ' author=' + $(element).attr('author').php_unhtmlspecialchars();
  1002. if ($(element).attr('date') != undefined)
  1003. date = ' date=' + $(element).attr('date');
  1004. if ($(element).attr('link') != undefined)
  1005. link = ' link=' + $(element).attr('link');
  1006. }
  1007. return '[quote' + author + date + link + ']' + content + '[/quote]';
  1008. },
  1009. attrs: function () {
  1010. return ['author', 'date', 'link'];
  1011. },
  1012. html: function(element, attrs, content) {
  1013. var author = '';
  1014. var sDate = '';
  1015. var link = '';
  1016. if(typeof attrs.author !== "undefined")
  1017. author = bbc_quote_from + ': <author>' + attrs.author + '</author>';
  1018. // Links could be in the form: link=topic=71.msg201#msg201 that would fool javascript, so we need a workaround
  1019. // Probably no more necessary
  1020. for (var key in attrs)
  1021. {
  1022. if (key.substr(0, 4) == 'link' && attrs.hasOwnProperty(key))
  1023. {
  1024. var possible_url = key.length > 4 ? key.substr(5) + '=' + attrs[key] : attrs[key];
  1025. link = possible_url.substr(0, 7) == 'http://' ? possible_url : smf_scripturl + '?' + possible_url;
  1026. author = author == '' ? '<a href="' + link + '">' + bbc_quote_from + ': <author src=">' + link + '</author></a>' : '<a href="' + link + '">' + author + '</a>';
  1027. link = '<quote_link style="display:none">' + possible_url + '</quote_link>';
  1028. }
  1029. }
  1030. if(typeof attrs.date !== "undefined")
  1031. {
  1032. var date = new Date(attrs.date * 1000);
  1033. sDate = date;
  1034. }
  1035. if (author == '' && sDate == '')
  1036. author = bbc_quote;
  1037. else
  1038. author += ' ';
  1039. content = '<blockquote><cite>' + author + bbc_search_on + ' ' + '<date timestamp="' + attrs.date + '">' + sDate + '</date>' + link + '</cite>' + content + '</blockquote>';
  1040. return content;
  1041. }
  1042. },
  1043. // END_COMMAND
  1044. // START_COMMAND: Code
  1045. code: {
  1046. tags: {
  1047. code: null
  1048. },
  1049. isBlock: true,
  1050. format: function(element, content) {
  1051. if ($(element[0]).hasClass('php'))
  1052. return '[php]' + content.replace('&#91;', '[') + '[/php]';
  1053. var from = '';
  1054. if ($(element).children("cite:first").length === 1)
  1055. {
  1056. from = $(element).children("cite:first").text();
  1057. $(element).attr({'from': from.php_htmlspecialchars()});
  1058. from = '=' + from;
  1059. content = '';
  1060. $(element).children("cite:first").remove();
  1061. content = this.elementToBbcode($(element));
  1062. }
  1063. else
  1064. {
  1065. if ($(element).attr('from') != undefined)
  1066. {
  1067. from = '=' + $(element).attr('from').php_unhtmlspecialchars();
  1068. }
  1069. }
  1070. return '[code' + from + ']' + content.replace('&#91;', '[') + '[/code]';
  1071. },
  1072. html: function(element, attrs, content) {
  1073. var from = '';
  1074. if(typeof attrs.defaultAttr !== "undefined")
  1075. from = '<cite>' + attrs.defaultAttr + '</cite>';
  1076. return '<code>' + from + content.replace('[', '&#91;') + '</code>'
  1077. }
  1078. },
  1079. php: {
  1080. isBlock: true,
  1081. format: "[php]{0}[/php]",
  1082. html: '<code class="php">{0}</code>'
  1083. },
  1084. // END_COMMAND
  1085. // START_COMMAND: Left
  1086. left: {
  1087. styles: {
  1088. "text-align": ["left", "-webkit-left", "-moz-left", "-khtml-left"]
  1089. },
  1090. isBlock: true,
  1091. format: "[left]{0}[/left]",
  1092. html: '<div align="left">{0}</div>'
  1093. },
  1094. // END_COMMAND
  1095. // START_COMMAND: Centre
  1096. center: {
  1097. styles: {
  1098. "text-align": ["center", "-webkit-center", "-moz-center", "-khtml-center"]
  1099. },
  1100. isBlock: true,
  1101. format: "[center]{0}[/center]",
  1102. html: '<div align="center">{0}</div>'
  1103. },
  1104. // END_COMMAND
  1105. // START_COMMAND: Right
  1106. right: {
  1107. styles: {
  1108. "text-align": ["right", "-webkit-right", "-moz-right", "-khtml-right"]
  1109. },
  1110. isBlock: true,
  1111. format: "[right]{0}[/right]",
  1112. html: '<div align="right">{0}</div>'
  1113. },
  1114. // END_COMMAND
  1115. // START_COMMAND: Justify
  1116. justify: {
  1117. styles: {
  1118. "text-align": ["justify", "-webkit-justify", "-moz-justify", "-khtml-justify"]
  1119. },
  1120. isBlock: true,
  1121. format: "[justify]{0}[/justify]",
  1122. html: '<div align="justify">{0}</div>'
  1123. },
  1124. // END_COMMAND
  1125. // START_COMMAND: YouTube
  1126. youtube: {
  1127. allowsEmpty: true,
  1128. tags: {
  1129. iframe: {
  1130. 'data-youtube-id': null
  1131. }
  1132. },
  1133. format: function(element, content) {
  1134. if(!element.attr('data-youtube-id'))
  1135. return content;
  1136. return '[youtube]' + element.attr('data-youtube-id') + '[/youtube]';
  1137. },
  1138. html: '<iframe width="560" height="315" src="http://www.youtube.com/embed/{0}?wmode=opaque' +
  1139. '" data-youtube-id="{0}" frameborder="0" allowfullscreen></iframe>'
  1140. },
  1141. // END_COMMAND
  1142. // START_COMMAND: Rtl
  1143. rtl: {
  1144. styles: {
  1145. "direction": ["rtl"]
  1146. },
  1147. format: "[rtl]{0}[/rtl]",
  1148. html: '<div style="direction: rtl">{0}</div>'
  1149. },
  1150. // END_COMMAND
  1151. // START_COMMAND: Ltr
  1152. ltr: {
  1153. styles: {
  1154. "direction": ["ltr"]
  1155. },
  1156. format: "[ltr]{0}[/ltr]",
  1157. html: '<div style="direction: ltr">{0}</div>'
  1158. },
  1159. // END_COMMAND
  1160. abbr: {
  1161. tags: {
  1162. abbr: {
  1163. title: null
  1164. }
  1165. },
  1166. format: function(element, content) {
  1167. return '[abbr=' + element.attr('title') + ']' + content + '[/abbr]';
  1168. },
  1169. html: function(element, attrs, content) {
  1170. if(typeof attrs.defaultAttr === "undefined" || attrs.defaultAttr.length === 0)
  1171. return content;
  1172. return '<abbr title="' + attrs.defaultAttr + '">' + content + '</abbr>';
  1173. }
  1174. },
  1175. acronym: {
  1176. tags: {
  1177. acronym: {
  1178. title: null
  1179. }
  1180. },
  1181. format: function(element, content) {
  1182. return '[acronym=' + element.attr('title') + ']' + content + '[/acronym]';
  1183. },
  1184. html: function(element, attrs, content) {
  1185. if(typeof attrs.defaultAttr === "undefined" || attrs.defaultAttr.length === 0)
  1186. return content;
  1187. return '<acronym title="' + attrs.defaultAttr + '">' + content + '</acronym>';
  1188. }
  1189. },
  1190. bdo: {
  1191. tags: {
  1192. bdo: {
  1193. dir: null
  1194. }
  1195. },
  1196. format: function(element, content) {
  1197. return '[bdo=' + element.attr('dir') + ']' + content + '[/bdo]';
  1198. },
  1199. html: function(element, attrs, content) {
  1200. if(typeof attrs.defaultAttr === "undefined" || attrs.defaultAttr.length === 0)
  1201. return content;
  1202. if (attrs.defaultAttr != 'rtl' && attrs.defaultAttr != 'ltr')
  1203. return '[bdo=' + attrs.defaultAttr + ']' + content + '[/bdo]';
  1204. return '<bdo dir="' + attrs.defaultAttr + '">' + content + '</bdo>';
  1205. }
  1206. },
  1207. tt: {
  1208. tags: {
  1209. tt: null
  1210. },
  1211. format: "[tt]{0}[/tt]",
  1212. html: '<tt>{0}</tt>'
  1213. },
  1214. pre: {
  1215. tags: {
  1216. pre: null
  1217. },
  1218. isBlock: true,
  1219. format: "[pre]{0}[/pre]",
  1220. html: "<pre>{0}</pre>\n"
  1221. },
  1222. move: {
  1223. tags: {
  1224. marquee: null
  1225. },
  1226. format: "[move]{0}[/move]",
  1227. html: '<marquee>{0}</marquee>'
  1228. },
  1229. // this is here so that commands above can be removed
  1230. // without having to remove the , after the last one.
  1231. // Needed for IE.
  1232. ignore: {}
  1233. };
  1234. /**
  1235. * Static BBCode helper class
  1236. * @class command
  1237. * @name jQuery.sceditorBBCodePlugin.bbcode
  1238. */
  1239. $.sceditorBBCodePlugin.bbcode =
  1240. /** @lends jQuery.sceditorBBCodePlugin.bbcode */
  1241. {
  1242. /**
  1243. * Gets a BBCode
  1244. *
  1245. * @param {String} name
  1246. * @return {Object|null}
  1247. * @since v1.3.5
  1248. */
  1249. get: function(name) {
  1250. return $.sceditorBBCodePlugin.bbcodes[name] || null;
  1251. },
  1252. /**
  1253. * <p>Adds a BBCode to the parser or updates an exisiting
  1254. * BBCode if a BBCode with the specified name already exists.</p>
  1255. *
  1256. * @param {String} name
  1257. * @param {Object} bbcode
  1258. * @return {this|false} Returns false if name or bbcode is false
  1259. * @since v1.3.5
  1260. */
  1261. set: function(name, bbcode) {
  1262. if(!name || !bbcode)
  1263. return false;
  1264. // merge any existing command properties
  1265. bbcode = $.extend($.sceditorBBCodePlugin.bbcodes[name] || {}, bbcode);
  1266. bbcode.remove = function() { $.sceditorBBCodePlugin.bbcode.remove(name); };
  1267. $.sceditorBBCodePlugin.bbcodes[name] = bbcode;
  1268. return this;
  1269. },
  1270. /**
  1271. * Removes a BBCode
  1272. *
  1273. * @param {String} name
  1274. * @return {this}
  1275. * @since v1.3.5
  1276. */
  1277. remove: function(name) {
  1278. if($.sceditorBBCodePlugin.bbcodes[name])
  1279. delete $.sceditorBBCodePlugin.bbcodes[name];
  1280. return this;
  1281. }
  1282. };
  1283. /**
  1284. * Checks if a command with the specified name exists
  1285. *
  1286. * @param string name
  1287. * @return bool
  1288. * @deprecated Since v1.3.5
  1289. * @memberOf jQuery.sceditorBBCodePlugin
  1290. */
  1291. $.sceditorBBCodePlugin.commandExists = function(name) {
  1292. return !!$.sceditorBBCodePlugin.bbcode.get(name);
  1293. };
  1294. /**
  1295. * Adds/updates a BBCode.
  1296. *
  1297. * @param String name The BBCode name
  1298. * @param Object tags Any html tags this bbcode applies to, i.e. strong for [b]
  1299. * @param Object styles Any style properties this applies to, i.e. font-weight for [b]
  1300. * @param String|Function format Function or string to convert the element into BBCode
  1301. * @param String|Function html String or function to format the BBCode back into HTML.
  1302. * @param bool allowsEmpty If this BBCodes is allowed to be empty, e.g. [b][/b]
  1303. * @return Bool
  1304. * @deprecated Since v1.3.5
  1305. * @memberOf jQuery.sceditorBBCodePlugin
  1306. */
  1307. $.sceditorBBCodePlugin.setCommand = function(name, tags, styles, format, html, allowsEmpty, isBlock) {
  1308. return $.sceditorBBCodePlugin.bbcode.set(name,
  1309. {
  1310. tags: tags || {},
  1311. styles: styles || {},
  1312. allowsEmpty: allowsEmpty,
  1313. isBlock: isBlock,
  1314. format: format,
  1315. html: html
  1316. });
  1317. };
  1318. $.fn.sceditorBBCodePlugin = function(options) {
  1319. if((!options || !options.runWithoutWysiwygSupport) && !$.sceditor.isWysiwygSupported())
  1320. return;
  1321. return this.each(function() {
  1322. (new $.sceditorBBCodePlugin(this, options));
  1323. });
  1324. };
  1325. })(jQuery);