fckplugin.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * FCKeditor - The text editor for Internet - http://www.fckeditor.net
  3. * Copyright (C) 2003-2010 Frederico Caldeira Knabben
  4. *
  5. * == BEGIN LICENSE ==
  6. *
  7. * Licensed under the terms of any of the following licenses at your
  8. * choice:
  9. *
  10. * - GNU General Public License Version 2 or later (the "GPL")
  11. * http://www.gnu.org/licenses/gpl.html
  12. *
  13. * - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
  14. * http://www.gnu.org/licenses/lgpl.html
  15. *
  16. * - Mozilla Public License Version 1.1 or later (the "MPL")
  17. * http://www.mozilla.org/MPL/MPL-1.1.html
  18. *
  19. * == END LICENSE ==
  20. *
  21. * This is a sample implementation for a custom Data Processor for basic BBCode.
  22. */
  23. FCK.DataProcessor =
  24. {
  25. /*
  26. * Returns a string representing the HTML format of "data". The returned
  27. * value will be loaded in the editor.
  28. * The HTML must be from <html> to </html>, eventually including
  29. * the DOCTYPE.
  30. * @param {String} data The data to be converted in the
  31. * DataProcessor specific format.
  32. */
  33. ConvertToHtml : function( data )
  34. {
  35. // Convert < and > to their HTML entities.
  36. data = data.replace( /</g, '&lt;' ) ;
  37. data = data.replace( />/g, '&gt;' ) ;
  38. // Convert line breaks to <br>.
  39. data = data.replace( /(?:\r\n|\n|\r)/g, '<br>' ) ;
  40. // [url]
  41. data = data.replace( /\[url\](.+?)\[\/url]/gi, '<a href="$1">$1</a>' ) ;
  42. data = data.replace( /\[url\=([^\]]+)](.+?)\[\/url]/gi, '<a href="$1">$2</a>' ) ;
  43. // [b]
  44. data = data.replace( /\[b\](.+?)\[\/b]/gi, '<b>$1</b>' ) ;
  45. // [i]
  46. data = data.replace( /\[i\](.+?)\[\/i]/gi, '<i>$1</i>' ) ;
  47. // [u]
  48. data = data.replace( /\[u\](.+?)\[\/u]/gi, '<u>$1</u>' ) ;
  49. return '<html><head><title></title></head><body>' + data + '</body></html>' ;
  50. },
  51. /*
  52. * Converts a DOM (sub-)tree to a string in the data format.
  53. * @param {Object} rootNode The node that contains the DOM tree to be
  54. * converted to the data format.
  55. * @param {Boolean} excludeRoot Indicates that the root node must not
  56. * be included in the conversion, only its children.
  57. * @param {Boolean} format Indicates that the data must be formatted
  58. * for human reading. Not all Data Processors may provide it.
  59. */
  60. ConvertToDataFormat : function( rootNode, excludeRoot, ignoreIfEmptyParagraph, format )
  61. {
  62. var data = rootNode.innerHTML ;
  63. // Convert <br> to line breaks.
  64. data = data.replace( /<br(?=[ \/>]).*?>/gi, '\r\n') ;
  65. // [url]
  66. data = data.replace( /<a .*?href=(["'])(.+?)\1.*?>(.+?)<\/a>/gi, '[url=$2]$3[/url]') ;
  67. // [b]
  68. data = data.replace( /<(?:b|strong)>/gi, '[b]') ;
  69. data = data.replace( /<\/(?:b|strong)>/gi, '[/b]') ;
  70. // [i]
  71. data = data.replace( /<(?:i|em)>/gi, '[i]') ;
  72. data = data.replace( /<\/(?:i|em)>/gi, '[/i]') ;
  73. // [u]
  74. data = data.replace( /<u>/gi, '[u]') ;
  75. data = data.replace( /<\/u>/gi, '[/u]') ;
  76. // Remove remaining tags.
  77. data = data.replace( /<[^>]+>/g, '') ;
  78. return data ;
  79. },
  80. /*
  81. * Makes any necessary changes to a piece of HTML for insertion in the
  82. * editor selection position.
  83. * @param {String} html The HTML to be fixed.
  84. */
  85. FixHtml : function( html )
  86. {
  87. return html ;
  88. }
  89. } ;
  90. // This Data Processor doesn't support <p>, so let's use <br>.
  91. FCKConfig.EnterMode = 'br' ;
  92. // To avoid pasting invalid markup (which is discarded in any case), let's
  93. // force pasting to plain text.
  94. FCKConfig.ForcePasteAsPlainText = true ;
  95. // Rename the "Source" buttom to "BBCode".
  96. FCKToolbarItems.RegisterItem( 'Source', new FCKToolbarButton( 'Source', 'BBCode', null, FCK_TOOLBARITEM_ICONTEXT, true, true, 1 ) ) ;
  97. // Let's enforce the toolbar to the limits of this Data Processor. A custom
  98. // toolbar set may be defined in the configuration file with more or less entries.
  99. FCKConfig.ToolbarSets["Default"] = [
  100. ['Source'],
  101. ['Bold','Italic','Underline','-','Link','Unlink'],
  102. ['About']
  103. ] ;