diff options
Diffstat (limited to 'sources/core/htmlparser/basicwriter.js')
-rw-r--r-- | sources/core/htmlparser/basicwriter.js | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/sources/core/htmlparser/basicwriter.js b/sources/core/htmlparser/basicwriter.js new file mode 100644 index 0000000..62a97ef --- /dev/null +++ b/sources/core/htmlparser/basicwriter.js | |||
@@ -0,0 +1,152 @@ | |||
1 | /** | ||
2 | * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved. | ||
3 | * For licensing, see LICENSE.md or http://ckeditor.com/license | ||
4 | */ | ||
5 | |||
6 | /** | ||
7 | * TODO | ||
8 | * | ||
9 | * @class | ||
10 | * @todo | ||
11 | */ | ||
12 | CKEDITOR.htmlParser.basicWriter = CKEDITOR.tools.createClass( { | ||
13 | /** | ||
14 | * Creates a basicWriter class instance. | ||
15 | * | ||
16 | * @constructor | ||
17 | */ | ||
18 | $: function() { | ||
19 | this._ = { | ||
20 | output: [] | ||
21 | }; | ||
22 | }, | ||
23 | |||
24 | proto: { | ||
25 | /** | ||
26 | * Writes the tag opening part for a opener tag. | ||
27 | * | ||
28 | * // Writes '<p'. | ||
29 | * writer.openTag( 'p', { class : 'MyClass', id : 'MyId' } ); | ||
30 | * | ||
31 | * @param {String} tagName The element name for this tag. | ||
32 | * @param {Object} attributes The attributes defined for this tag. The | ||
33 | * attributes could be used to inspect the tag. | ||
34 | */ | ||
35 | openTag: function( tagName ) { | ||
36 | this._.output.push( '<', tagName ); | ||
37 | }, | ||
38 | |||
39 | /** | ||
40 | * Writes the tag closing part for a opener tag. | ||
41 | * | ||
42 | * // Writes '>'. | ||
43 | * writer.openTagClose( 'p', false ); | ||
44 | * | ||
45 | * // Writes ' />'. | ||
46 | * writer.openTagClose( 'br', true ); | ||
47 | * | ||
48 | * @param {String} tagName The element name for this tag. | ||
49 | * @param {Boolean} isSelfClose Indicates that this is a self-closing tag, | ||
50 | * like `<br>` or `<img>`. | ||
51 | */ | ||
52 | openTagClose: function( tagName, isSelfClose ) { | ||
53 | if ( isSelfClose ) | ||
54 | this._.output.push( ' />' ); | ||
55 | else | ||
56 | this._.output.push( '>' ); | ||
57 | }, | ||
58 | |||
59 | /** | ||
60 | * Writes an attribute. This function should be called after opening the | ||
61 | * tag with {@link #openTagClose}. | ||
62 | * | ||
63 | * // Writes ' class="MyClass"'. | ||
64 | * writer.attribute( 'class', 'MyClass' ); | ||
65 | * | ||
66 | * @param {String} attName The attribute name. | ||
67 | * @param {String} attValue The attribute value. | ||
68 | */ | ||
69 | attribute: function( attName, attValue ) { | ||
70 | // Browsers don't always escape special character in attribute values. (#4683, #4719). | ||
71 | if ( typeof attValue == 'string' ) | ||
72 | attValue = CKEDITOR.tools.htmlEncodeAttr( attValue ); | ||
73 | |||
74 | this._.output.push( ' ', attName, '="', attValue, '"' ); | ||
75 | }, | ||
76 | |||
77 | /** | ||
78 | * Writes a closer tag. | ||
79 | * | ||
80 | * // Writes '</p>'. | ||
81 | * writer.closeTag( 'p' ); | ||
82 | * | ||
83 | * @param {String} tagName The element name for this tag. | ||
84 | */ | ||
85 | closeTag: function( tagName ) { | ||
86 | this._.output.push( '</', tagName, '>' ); | ||
87 | }, | ||
88 | |||
89 | /** | ||
90 | * Writes text. | ||
91 | * | ||
92 | * // Writes 'Hello Word'. | ||
93 | * writer.text( 'Hello Word' ); | ||
94 | * | ||
95 | * @param {String} text The text value. | ||
96 | */ | ||
97 | text: function( text ) { | ||
98 | this._.output.push( text ); | ||
99 | }, | ||
100 | |||
101 | /** | ||
102 | * Writes a comment. | ||
103 | * | ||
104 | * // Writes '<!-- My comment -->'. | ||
105 | * writer.comment( ' My comment ' ); | ||
106 | * | ||
107 | * @param {String} comment The comment text. | ||
108 | */ | ||
109 | comment: function( comment ) { | ||
110 | this._.output.push( '<!--', comment, '-->' ); | ||
111 | }, | ||
112 | |||
113 | /** | ||
114 | * Writes any kind of data to the ouput. | ||
115 | * | ||
116 | * writer.write( 'This is an <b>example</b>.' ); | ||
117 | * | ||
118 | * @param {String} data | ||
119 | */ | ||
120 | write: function( data ) { | ||
121 | this._.output.push( data ); | ||
122 | }, | ||
123 | |||
124 | /** | ||
125 | * Empties the current output buffer. | ||
126 | * | ||
127 | * writer.reset(); | ||
128 | */ | ||
129 | reset: function() { | ||
130 | this._.output = []; | ||
131 | this._.indent = false; | ||
132 | }, | ||
133 | |||
134 | /** | ||
135 | * Empties the current output buffer. | ||
136 | * | ||
137 | * var html = writer.getHtml(); | ||
138 | * | ||
139 | * @param {Boolean} reset Indicates that the {@link #reset} method is to | ||
140 | * be automatically called after retrieving the HTML. | ||
141 | * @returns {String} The HTML written to the writer so far. | ||
142 | */ | ||
143 | getHtml: function( reset ) { | ||
144 | var html = this._.output.join( '' ); | ||
145 | |||
146 | if ( reset ) | ||
147 | this.reset(); | ||
148 | |||
149 | return html; | ||
150 | } | ||
151 | } | ||
152 | } ); | ||