JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.5.3
[ckeditor.git] / _samples / output_html.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\r
2 <!--\r
3 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
4 For licensing, see LICENSE.html or http://ckeditor.com/license\r
5 -->\r
6 <html xmlns="http://www.w3.org/1999/xhtml">\r
7 <head>\r
8         <title>HTML Compliant Output &mdash; CKEditor Sample</title>\r
9         <meta content="text/html; charset=utf-8" http-equiv="content-type" />\r
10         <script type="text/javascript" src="../ckeditor.js"></script>\r
11         <script src="sample.js" type="text/javascript"></script>\r
12         <link href="sample.css" rel="stylesheet" type="text/css" />\r
13 </head>\r
14 <body>\r
15         <h1 class="samples">\r
16                 CKEditor Sample &mdash; Producing HTML Compliant Output\r
17         </h1>\r
18         <div class="description">\r
19         <p>\r
20                 This sample shows how to configure CKEditor to output valid\r
21                 <a class="samples" href="http://www.w3.org/TR/html401/">HTML 4.01</a> code.\r
22                 Traditional HTML elements like <code>&lt;b&gt;</code>,\r
23                 <code>&lt;i&gt;</code>, and <code>&lt;font&gt;</code> are used in place of\r
24                 <code>&lt;strong&gt;</code>, <code>&lt;em&gt;</code>, and CSS styles.\r
25         </p>\r
26         <p>\r
27                 To add a CKEditor instance outputting legacy HTML 4.01 code, load the editor using a standard\r
28                 JavaScript call, and define CKEditor features to use the HTML compliant elements and attributes.\r
29         </p>\r
30         <p>\r
31                 A snippet of the configuration code can be seen below; check the source of this page for\r
32                 full definition:\r
33         </p>\r
34         <pre class="samples">CKEDITOR.replace( '<em>textarea_id</em>',\r
35         {\r
36                 coreStyles_bold : { element : 'b' },\r
37                 coreStyles_italic : { element : 'i' },\r
38 \r
39                 fontSize_style :\r
40                         {\r
41                                 element         : 'font',\r
42                                 attributes      : { 'size' : '#(size)' }\r
43                         }\r
44 \r
45                 // More definitions follow.\r
46         });</pre>\r
47         </div>\r
48 \r
49         <!-- This <div> holds alert messages to be display in the sample page. -->\r
50         <div id="alerts">\r
51                 <noscript>\r
52                         <p>\r
53                                 <strong>CKEditor requires JavaScript to run</strong>. In a browser with no JavaScript\r
54                                 support, like yours, you should still see the contents (HTML data) and you should\r
55                                 be able to edit it normally, without a rich editor interface.\r
56                         </p>\r
57                 </noscript>\r
58         </div>\r
59         <form action="sample_posteddata.php" method="post">\r
60                 <p>\r
61                         <label for="editor1">\r
62                                 Editor 1:</label>\r
63                         <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="http://ckeditor.com/"&gt;CKEditor&lt;/a&gt;.&lt;/p&gt;</textarea>\r
64                         <script type="text/javascript">\r
65                         //<![CDATA[\r
66 \r
67                                 CKEDITOR.replace( 'editor1',\r
68                                         {\r
69                                                 /*\r
70                                                  * Style sheet for the contents\r
71                                                  */\r
72                                                 contentsCss : 'body {color:#000; background-color#:FFF;}',\r
73 \r
74                                                 /*\r
75                                                  * Simple HTML5 doctype\r
76                                                  */\r
77                                                 docType : '<!DOCTYPE HTML>',\r
78 \r
79                                                 /*\r
80                                                  * Core styles.\r
81                                                  */\r
82                                                 coreStyles_bold : { element : 'b' },\r
83                                                 coreStyles_italic       : { element : 'i' },\r
84                                                 coreStyles_underline    : { element : 'u'},\r
85                                                 coreStyles_strike       : { element : 'strike' },\r
86 \r
87                                                 /*\r
88                                                  * Font face\r
89                                                  */\r
90                                                 // Define the way font elements will be applied to the document. The "font"\r
91                                                 // element will be used.\r
92                                                 font_style :\r
93                                                 {\r
94                                                                 element         : 'font',\r
95                                                                 attributes              : { 'face' : '#(family)' }\r
96                                                 },\r
97 \r
98                                                 /*\r
99                                                  * Font sizes.\r
100                                                  */\r
101                                                 fontSize_sizes : 'xx-small/1;x-small/2;small/3;medium/4;large/5;x-large/6;xx-large/7',\r
102                                                 fontSize_style :\r
103                                                         {\r
104                                                                 element         : 'font',\r
105                                                                 attributes      : { 'size' : '#(size)' }\r
106                                                         } ,\r
107 \r
108                                                 /*\r
109                                                  * Font colors.\r
110                                                  */\r
111                                                 colorButton_enableMore : true,\r
112 \r
113                                                 colorButton_foreStyle :\r
114                                                         {\r
115                                                                 element : 'font',\r
116                                                                 attributes : { 'color' : '#(color)' }\r
117                                                         },\r
118 \r
119                                                 colorButton_backStyle :\r
120                                                         {\r
121                                                                 element : 'font',\r
122                                                                 styles  : { 'background-color' : '#(color)' }\r
123                                                         },\r
124 \r
125                                                 /*\r
126                                                  * Styles combo.\r
127                                                  */\r
128                                                 stylesSet :\r
129                                                                 [\r
130                                                                         { name : 'Computer Code', element : 'code' },\r
131                                                                         { name : 'Keyboard Phrase', element : 'kbd' },\r
132                                                                         { name : 'Sample Text', element : 'samp' },\r
133                                                                         { name : 'Variable', element : 'var' },\r
134 \r
135                                                                         { name : 'Deleted Text', element : 'del' },\r
136                                                                         { name : 'Inserted Text', element : 'ins' },\r
137 \r
138                                                                         { name : 'Cited Work', element : 'cite' },\r
139                                                                         { name : 'Inline Quotation', element : 'q' }\r
140                                                                 ],\r
141 \r
142                                                 on : { 'instanceReady' : configureHtmlOutput }\r
143                                         });\r
144 \r
145 /*\r
146  * Adjust the behavior of the dataProcessor to avoid styles\r
147  * and make it look like FCKeditor HTML output.\r
148  */\r
149 function configureHtmlOutput( ev )\r
150 {\r
151         var editor = ev.editor,\r
152                 dataProcessor = editor.dataProcessor,\r
153                 htmlFilter = dataProcessor && dataProcessor.htmlFilter;\r
154 \r
155         // Out self closing tags the HTML4 way, like <br>.\r
156         dataProcessor.writer.selfClosingEnd = '>';\r
157 \r
158         // Make output formatting behave similar to FCKeditor\r
159         var dtd = CKEDITOR.dtd;\r
160         for ( var e in CKEDITOR.tools.extend( {}, dtd.$nonBodyContent, dtd.$block, dtd.$listItem, dtd.$tableContent ) )\r
161         {\r
162                 dataProcessor.writer.setRules( e,\r
163                         {\r
164                                 indent : true,\r
165                                 breakBeforeOpen : true,\r
166                                 breakAfterOpen : false,\r
167                                 breakBeforeClose : !dtd[ e ][ '#' ],\r
168                                 breakAfterClose : true\r
169                         });\r
170         }\r
171 \r
172         // Output properties as attributes, not styles.\r
173         htmlFilter.addRules(\r
174                 {\r
175                         elements :\r
176                         {\r
177                                 $ : function( element )\r
178                                 {\r
179                                         // Output dimensions of images as width and height\r
180                                         if ( element.name == 'img' )\r
181                                         {\r
182                                                 var style = element.attributes.style;\r
183 \r
184                                                 if ( style )\r
185                                                 {\r
186                                                         // Get the width from the style.\r
187                                                         var match = /(?:^|\s)width\s*:\s*(\d+)px/i.exec( style ),\r
188                                                                 width = match && match[1];\r
189 \r
190                                                         // Get the height from the style.\r
191                                                         match = /(?:^|\s)height\s*:\s*(\d+)px/i.exec( style );\r
192                                                         var height = match && match[1];\r
193 \r
194                                                         if ( width )\r
195                                                         {\r
196                                                                 element.attributes.style = element.attributes.style.replace( /(?:^|\s)width\s*:\s*(\d+)px;?/i , '' );\r
197                                                                 element.attributes.width = width;\r
198                                                         }\r
199 \r
200                                                         if ( height )\r
201                                                         {\r
202                                                                 element.attributes.style = element.attributes.style.replace( /(?:^|\s)height\s*:\s*(\d+)px;?/i , '' );\r
203                                                                 element.attributes.height = height;\r
204                                                         }\r
205                                                 }\r
206                                         }\r
207 \r
208                                         // Output alignment of paragraphs using align\r
209                                         if ( element.name == 'p' )\r
210                                         {\r
211                                                 style = element.attributes.style;\r
212 \r
213                                                 if ( style )\r
214                                                 {\r
215                                                         // Get the align from the style.\r
216                                                         match = /(?:^|\s)text-align\s*:\s*(\w*);/i.exec( style );\r
217                                                         var align = match && match[1];\r
218 \r
219                                                         if ( align )\r
220                                                         {\r
221                                                                 element.attributes.style = element.attributes.style.replace( /(?:^|\s)text-align\s*:\s*(\w*);?/i , '' );\r
222                                                                 element.attributes.align = align;\r
223                                                         }\r
224                                                 }\r
225                                         }\r
226 \r
227                                         if ( !element.attributes.style )\r
228                                                 delete element.attributes.style;\r
229 \r
230                                         return element;\r
231                                 }\r
232                         },\r
233 \r
234                         attributes :\r
235                                 {\r
236                                         style : function( value, element )\r
237                                         {\r
238                                                 // Return #RGB for background and border colors\r
239                                                 return convertRGBToHex( value );\r
240                                         }\r
241                                 }\r
242                 } );\r
243 }\r
244 \r
245 \r
246 /**\r
247 * Convert a CSS rgb(R, G, B) color back to #RRGGBB format.\r
248 * @param Css style string (can include more than one color\r
249 * @return Converted css style.\r
250 */\r
251 function convertRGBToHex( cssStyle )\r
252 {\r
253         return cssStyle.replace( /(?:rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\))/gi, function( match, red, green, blue )\r
254                 {\r
255                         red = parseInt( red, 10 ).toString( 16 );\r
256                         green = parseInt( green, 10 ).toString( 16 );\r
257                         blue = parseInt( blue, 10 ).toString( 16 );\r
258                         var color = [red, green, blue] ;\r
259 \r
260                         // Add padding zeros if the hex value is less than 0x10.\r
261                         for ( var i = 0 ; i < color.length ; i++ )\r
262                                 color[i] = String( '0' + color[i] ).slice( -2 ) ;\r
263 \r
264                         return '#' + color.join( '' ) ;\r
265                  });\r
266 }\r
267                         //]]>\r
268                         </script>\r
269                 </p>\r
270                 <p>\r
271                         <input type="submit" value="Submit" />\r
272                 </p>\r
273         </form>\r
274         <div id="footer">\r
275                 <hr />\r
276                 <p>\r
277                         CKEditor - The text editor for the Internet - <a class="samples" href="http://ckeditor.com/">http://ckeditor.com</a>\r
278                 </p>\r
279                 <p id="copy">\r
280                         Copyright &copy; 2003-2011, <a class="samples" href="http://cksource.com/">CKSource</a> - Frederico\r
281                         Knabben. All rights reserved.\r
282                 </p>\r
283         </div>\r
284 </body>\r
285 </html>\r