outputhtml.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <!DOCTYPE html>
  2. <!--
  3. Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
  4. For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  5. -->
  6. <html lang="en">
  7. <head>
  8. <meta charset="utf-8">
  9. <title>HTML Compliant Output &mdash; CKEditor Sample</title>
  10. <script src="../../../ckeditor.js"></script>
  11. <script src="../../../samples/old/sample.js"></script>
  12. <link href="../../../samples/old/sample.css" rel="stylesheet">
  13. <meta name="ckeditor-sample-required-plugins" content="sourcearea">
  14. <meta name="ckeditor-sample-name" content="Output HTML">
  15. <meta name="ckeditor-sample-group" content="Advanced Samples">
  16. <meta name="ckeditor-sample-description" content="Configuring CKEditor to produce legacy HTML 4 code.">
  17. <meta name="description" content="Try the latest sample of CKEditor 4 and learn more about customizing your WYSIWYG editor with endless possibilities.">
  18. </head>
  19. <body>
  20. <h1 class="samples">
  21. <a href="../../../samples/old/index.html">CKEditor Samples</a> &raquo; Producing HTML Compliant Output
  22. </h1>
  23. <div class="warning deprecated">
  24. This sample is not maintained anymore. Check out the <a href="https://ckeditor.com/docs/ckeditor4/latest/examples/index.html">brand new samples in CKEditor Examples</a>.
  25. </div>
  26. <div class="description">
  27. <p>
  28. This sample shows how to configure CKEditor to output valid
  29. <a class="samples" href="http://www.w3.org/TR/html401/">HTML 4.01</a> code.
  30. Traditional HTML elements like <code>&lt;b&gt;</code>,
  31. <code>&lt;i&gt;</code>, and <code>&lt;font&gt;</code> are used in place of
  32. <code>&lt;strong&gt;</code>, <code>&lt;em&gt;</code>, and CSS styles.
  33. </p>
  34. <p>
  35. To add a CKEditor instance outputting legacy HTML 4.01 code, load the editor using a standard
  36. JavaScript call, and define CKEditor features to use the HTML compliant elements and attributes.
  37. </p>
  38. <p>
  39. A snippet of the configuration code can be seen below; check the source of this page for
  40. full definition:
  41. </p>
  42. <pre class="samples">
  43. CKEDITOR.replace( '<em>textarea_id</em>', {
  44. coreStyles_bold: { element: 'b' },
  45. coreStyles_italic: { element: 'i' },
  46. fontSize_style: {
  47. element: 'font',
  48. attributes: { 'size': '#(size)' }
  49. }
  50. ...
  51. });</pre>
  52. </div>
  53. <form action="../../../samples/sample_posteddata.php" method="post">
  54. <p>
  55. <label for="editor1">
  56. Editor 1:
  57. </label>
  58. <textarea cols="80" id="editor1" name="editor1" rows="10">&lt;p&gt;This is some &lt;b&gt;sample text&lt;/b&gt;. You are using &lt;a href="https://ckeditor.com/"&gt;CKEditor&lt;/a&gt;.&lt;/p&gt;</textarea>
  59. <script>
  60. CKEDITOR.replace( 'editor1', {
  61. /*
  62. * Ensure that htmlwriter plugin, which is required for this sample, is loaded.
  63. */
  64. extraPlugins: 'htmlwriter',
  65. /*
  66. * Style sheet for the contents
  67. */
  68. contentsCss: 'body {color:#000; background-color#:FFF;}',
  69. /*
  70. * Simple HTML5 doctype
  71. */
  72. docType: '<!DOCTYPE HTML>',
  73. /*
  74. * Allowed content rules which beside limiting allowed HTML
  75. * will also take care of transforming styles to attributes
  76. * (currently only for img - see transformation rules defined below).
  77. *
  78. * Read more: https://ckeditor.com/docs/ckeditor4/latest/guide/dev_advanced_content_filter.html
  79. */
  80. allowedContent:
  81. 'h1 h2 h3 p pre[align]; ' +
  82. 'blockquote code kbd samp var del ins cite q b i u strike ul ol li hr table tbody tr td th caption; ' +
  83. 'img[!src,alt,align,width,height]; font[!face]; font[!family]; font[!color]; font[!size]; font{!background-color}; a[!href]; a[!name]',
  84. /*
  85. * Core styles.
  86. */
  87. coreStyles_bold: { element: 'b' },
  88. coreStyles_italic: { element: 'i' },
  89. coreStyles_underline: { element: 'u' },
  90. coreStyles_strike: { element: 'strike' },
  91. /*
  92. * Font face.
  93. */
  94. // Define the way font elements will be applied to the document.
  95. // The "font" element will be used.
  96. font_style: {
  97. element: 'font',
  98. attributes: { 'face': '#(family)' }
  99. },
  100. /*
  101. * Font sizes.
  102. */
  103. fontSize_sizes: 'xx-small/1;x-small/2;small/3;medium/4;large/5;x-large/6;xx-large/7',
  104. fontSize_style: {
  105. element: 'font',
  106. attributes: { 'size': '#(size)' }
  107. },
  108. /*
  109. * Font colors.
  110. */
  111. colorButton_foreStyle: {
  112. element: 'font',
  113. attributes: { 'color': '#(color)' }
  114. },
  115. colorButton_backStyle: {
  116. element: 'font',
  117. styles: { 'background-color': '#(color)' }
  118. },
  119. /*
  120. * Styles combo.
  121. */
  122. stylesSet: [
  123. { name: 'Computer Code', element: 'code' },
  124. { name: 'Keyboard Phrase', element: 'kbd' },
  125. { name: 'Sample Text', element: 'samp' },
  126. { name: 'Variable', element: 'var' },
  127. { name: 'Deleted Text', element: 'del' },
  128. { name: 'Inserted Text', element: 'ins' },
  129. { name: 'Cited Work', element: 'cite' },
  130. { name: 'Inline Quotation', element: 'q' }
  131. ],
  132. on: {
  133. pluginsLoaded: configureTransformations,
  134. loaded: configureHtmlWriter
  135. }
  136. });
  137. /*
  138. * Add missing content transformations.
  139. */
  140. function configureTransformations( evt ) {
  141. var editor = evt.editor;
  142. editor.dataProcessor.htmlFilter.addRules( {
  143. attributes: {
  144. style: function( value, element ) {
  145. // Return #RGB for background and border colors
  146. return CKEDITOR.tools.convertRgbToHex( value );
  147. }
  148. }
  149. } );
  150. // Default automatic content transformations do not yet take care of
  151. // align attributes on blocks, so we need to add our own transformation rules.
  152. function alignToAttribute( element ) {
  153. if ( element.styles[ 'text-align' ] ) {
  154. element.attributes.align = element.styles[ 'text-align' ];
  155. delete element.styles[ 'text-align' ];
  156. }
  157. }
  158. editor.filter.addTransformations( [
  159. [ { element: 'p', right: alignToAttribute } ],
  160. [ { element: 'h1', right: alignToAttribute } ],
  161. [ { element: 'h2', right: alignToAttribute } ],
  162. [ { element: 'h3', right: alignToAttribute } ],
  163. [ { element: 'pre', right: alignToAttribute } ]
  164. ] );
  165. }
  166. /*
  167. * Adjust the behavior of htmlWriter to make it output HTML like FCKeditor.
  168. */
  169. function configureHtmlWriter( evt ) {
  170. var editor = evt.editor,
  171. dataProcessor = editor.dataProcessor;
  172. // Out self closing tags the HTML4 way, like <br>.
  173. dataProcessor.writer.selfClosingEnd = '>';
  174. // Make output formatting behave similar to FCKeditor.
  175. var dtd = CKEDITOR.dtd;
  176. for ( var e in CKEDITOR.tools.extend( {}, dtd.$nonBodyContent, dtd.$block, dtd.$listItem, dtd.$tableContent ) ) {
  177. dataProcessor.writer.setRules( e, {
  178. indent: true,
  179. breakBeforeOpen: true,
  180. breakAfterOpen: false,
  181. breakBeforeClose: !dtd[ e ][ '#' ],
  182. breakAfterClose: true
  183. });
  184. }
  185. }
  186. </script>
  187. </p>
  188. <p>
  189. <input type="submit" value="Submit">
  190. </p>
  191. </form>
  192. <div id="footer">
  193. <hr>
  194. <p>
  195. CKEditor - The text editor for the Internet - <a class="samples" href="https://ckeditor.com/">https://ckeditor.com</a>
  196. </p>
  197. <p id="copy">
  198. Copyright &copy; 2003-2023, <a class="samples" href="https://cksource.com/">CKSource</a> Holding sp. z o.o. All rights reserved.
  199. </p>
  200. </div>
  201. </body>
  202. </html>