3 Copyright (c) 2003-2014, CKSource - Frederico Knabben. All rights reserved.
4 For licensing, see LICENSE.md or http://ckeditor.com/license
9 <title>HTML Compliant Output
— CKEditor Sample
</title>
10 <script src=
"../../../ckeditor.js"></script>
11 <script src=
"../../../samples/sample.js"></script>
12 <link href=
"../../../samples/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.">
20 <a href=
"../../../samples/index.html">CKEditor Samples
</a> » Producing HTML Compliant Output
22 <div class=
"description">
24 This sample shows how to configure CKEditor to output valid
25 <a class=
"samples" href=
"http://www.w3.org/TR/html401/">HTML
4.01</a> code.
26 Traditional HTML elements like
<code><b
></code>,
27 <code><i
></code>, and
<code><font
></code> are used in place of
28 <code><strong
></code>,
<code><em
></code>, and CSS styles.
31 To add a CKEditor instance outputting legacy HTML
4.01 code, load the editor using a standard
32 JavaScript call, and define CKEditor features to use the HTML compliant elements and attributes.
35 A snippet of the configuration code can be seen below; check the source of this page for
39 CKEDITOR.replace( '
<em>textarea_id
</em>', {
40 coreStyles_bold: { element: 'b' },
41 coreStyles_italic: { element: 'i' },
45 attributes: { 'size': '#(size)' }
51 <form action=
"../../../samples/sample_posteddata.php" method=
"post">
56 <textarea cols=
"80" id=
"editor1" name=
"editor1" rows=
"10"><p
>This is some
<b
>sample text
</b
>. You are using
<a
href=
"http://ckeditor.com/">CKEditor
</a
>.
</p
></textarea>
59 CKEDITOR.replace( 'editor1', {
61 * Ensure that htmlwriter plugin, which is required for this sample, is loaded.
63 extraPlugins: 'htmlwriter',
66 * Style sheet for the contents
68 contentsCss: 'body {color:#
000; background-color#:FFF;}',
71 * Simple HTML5 doctype
73 docType: '
<!DOCTYPE HTML
>',
76 * Allowed content rules which beside limiting allowed HTML
77 * will also take care of transforming styles to attributes
78 * (currently only for img - see transformation rules defined below).
80 * Read more: http://docs.ckeditor.com/#!/guide/dev_advanced_content_filter
83 'h1 h2 h3 p pre[align]; ' +
84 'blockquote code kbd samp var del ins cite q b i u strike ul ol li hr table tbody tr td th caption; ' +
85 'img[!src,alt,align,width,height]; font[!face]; font[!family]; font[!color]; font[!size]; font{!background-color}; a[!href]; a[!name]',
90 coreStyles_bold: { element: 'b' },
91 coreStyles_italic: { element: 'i' },
92 coreStyles_underline: { element: 'u' },
93 coreStyles_strike: { element: 'strike' },
99 // Define the way font elements will be applied to the document.
100 // The
"font" element will be used.
103 attributes: { 'face': '#(family)' }
109 fontSize_sizes: 'xx-small/
1;x-small/
2;small/
3;medium/
4;large/
5;x-large/
6;xx-large/
7',
112 attributes: { 'size': '#(size)' }
119 colorButton_foreStyle: {
121 attributes: { 'color': '#(color)' }
124 colorButton_backStyle: {
126 styles: { 'background-color': '#(color)' }
133 { name: 'Computer Code', element: 'code' },
134 { name: 'Keyboard Phrase', element: 'kbd' },
135 { name: 'Sample Text', element: 'samp' },
136 { name: 'Variable', element: 'var' },
137 { name: 'Deleted Text', element: 'del' },
138 { name: 'Inserted Text', element: 'ins' },
139 { name: 'Cited Work', element: 'cite' },
140 { name: 'Inline Quotation', element: 'q' }
144 pluginsLoaded: configureTransformations,
145 loaded: configureHtmlWriter
150 * Add missing content transformations.
152 function configureTransformations( evt ) {
153 var editor = evt.editor;
155 editor.dataProcessor.htmlFilter.addRules( {
157 style: function( value, element ) {
158 // Return #RGB for background and border colors
159 return CKEDITOR.tools.convertRgbToHex( value );
164 // Default automatic content transformations do not yet take care of
165 // align attributes on blocks, so we need to add our own transformation rules.
166 function alignToAttribute( element ) {
167 if ( element.styles[ 'text-align' ] ) {
168 element.attributes.align = element.styles[ 'text-align' ];
169 delete element.styles[ 'text-align' ];
172 editor.filter.addTransformations( [
173 [ { element: 'p', right: alignToAttribute } ],
174 [ { element: 'h1', right: alignToAttribute } ],
175 [ { element: 'h2', right: alignToAttribute } ],
176 [ { element: 'h3', right: alignToAttribute } ],
177 [ { element: 'pre', right: alignToAttribute } ]
182 * Adjust the behavior of htmlWriter to make it output HTML like FCKeditor.
184 function configureHtmlWriter( evt ) {
185 var editor = evt.editor,
186 dataProcessor = editor.dataProcessor;
188 // Out self closing tags the HTML4 way, like
<br>.
189 dataProcessor.writer.selfClosingEnd = '
>';
191 // Make output formatting behave similar to FCKeditor.
192 var dtd = CKEDITOR.dtd;
193 for ( var e in CKEDITOR.tools.extend( {}, dtd.$nonBodyContent, dtd.$block, dtd.$listItem, dtd.$tableContent ) ) {
194 dataProcessor.writer.setRules( e, {
196 breakBeforeOpen: true,
197 breakAfterOpen: false,
198 breakBeforeClose: !dtd[ e ][ '#' ],
199 breakAfterClose: true
207 <input type=
"submit" value=
"Submit">
213 CKEditor - The text editor for the Internet -
<a class=
"samples" href=
"http://ckeditor.com/">http://ckeditor.com
</a>
216 Copyright
© 2003-
2014,
<a class=
"samples" href=
"http://cksource.com/">CKSource
</a> - Frederico
217 Knabben. All rights reserved.