Roo/htmleditor/TidyWriter.js
[roojs1] / Roo / htmleditor / TidyWriter.js
1 /***
2  * This is based loosely on tinymce 
3  * @class Roo.htmleditor.TidyWriter
4  * 
5  */
6
7 Roo.htmleditor.TidyWriter = function(settings)
8 {
9     
10     // indent, indentBefore, indentAfter, encode, htmlOutput, html = [];
11     Roo.apply(this, settings);
12     this.html = [];
13     
14     this.indentBefore =this.makeMap(settings.indent_before || '');
15     this.indentAfter = this.makeMap(settings.indent_after || '');
16     this.encode = Roo.htmleditor.TidyEntities.getEncodeFunc(settings.entity_encoding || 'raw', settings.entities);
17     
18 }
19 Roo.apply(Roo.htmleditor.TidyWriter,
20           {
21      
22
23     makeMap : function (items, delim, map) {
24                 var i;
25
26                 items = items || [];
27                 delim = delim || ',';
28
29                 if (typeof items == "string") {
30                         items = items.split(delim);
31                 }
32
33                 map = map || {};
34
35                 i = items.length;
36                 while (i--) {
37                         map[items[i]] = {};
38                 }
39
40                 return map;
41         },
42
43
44     indent : 0,
45     indentBefore : false,
46     indentAfter : false,
47     encod : false,
48     
49     encode = Entities.getEncodeFunc(settings.entity_encoding || 'raw', settings.entities);
50         htmlOutput = 'html' == settings.element_format;
51     
52
53             /**
54              * Writes the a start element such as <p id="a">.
55              *
56              * @method start
57              * @param {String} name Name of the element.
58              * @param {Array} attrs Optional attribute array or undefined if it hasn't any.
59              * @param {Boolean} empty Optional empty state if the tag should end like <br />.
60              */
61             start: function(name, attrs, empty) {
62                 var i, l, attr, value;
63                 if (indent && indentBefore[name] && html.length > 0) {
64                     value = html[html.length - 1];
65                     value.length > 0 && '\n' !== value && html.push('\n');
66                 }
67                 html.push('<', name);
68                 if (attrs) {
69                     for (i = 0, l = attrs.length; i < l; i++) {
70                         attr = attrs[i];
71                         html.push(' ', attr.name, '="', encode(attr.value, true), '"');
72                     }
73                 }
74                 html[html.length] = !empty || htmlOutput ? '>' : ' />';
75                 if (empty && indent && indentAfter[name] && html.length > 0) {
76                     value = html[html.length - 1];
77                     value.length > 0 && '\n' !== value && html.push('\n');
78                 }
79             },
80             /**
81              * Writes the a end element such as </p>.
82              *
83              * @method end
84              * @param {String} name Name of the element.
85              */
86             end: function(name) {
87                 var value;
88                 /*if (indent && indentBefore[name] && html.length > 0) {
89                 value = html[html.length - 1];
90                 if (value.length > 0 && value !== '\n')
91                     html.push('\n');
92             }*/
93                 html.push('</', name, '>');
94                 if (indent && indentAfter[name] && html.length > 0) {
95                     value = html[html.length - 1];
96                     value.length > 0 && '\n' !== value && html.push('\n');
97                 }
98             },
99             /**
100              * Writes a text node.
101              *
102              * @method text
103              * @param {String} text String to write out.
104              * @param {Boolean} raw Optional raw state if true the contents wont get encoded.
105              */
106             text: function(text, raw) {
107                 text.length > 0 && (html[html.length] = raw ? text : encode(text));
108             },
109             /**
110              * Writes a cdata node such as <![CDATA[data]]>.
111              *
112              * @method cdata
113              * @param {String} text String to write out inside the cdata.
114              */
115             cdata: function(text) {
116                 html.push('<![CDATA[', text, ']]>');
117             },
118             /**
119              * Writes a comment node such as <!-- Comment -->.
120              *
121              * @method cdata
122              * @param {String} text String to write out inside the comment.
123              */
124             comment: function(text) {
125                 html.push('<!--', text, '-->');
126             },
127             /**
128              * Writes a PI node such as <?xml attr="value" ?>.
129              *
130              * @method pi
131              * @param {String} name Name of the pi.
132              * @param {String} text String to write out inside the pi.
133              */
134             pi: function(name, text) {
135                 text ? html.push('<?', name, ' ', encode(text), '?>') : html.push('<?', name, '?>');
136                 indent && html.push('\n');
137             },
138             /**
139              * Writes a doctype node such as <!DOCTYPE data>.
140              *
141              * @method doctype
142              * @param {String} text String to write out inside the doctype.
143              */
144             doctype: function(text) {
145                 html.push('<!DOCTYPE', text, '>', indent ? '\n' : '');
146             },
147             /**
148              * Resets the internal buffer if one wants to reuse the writer.
149              *
150              * @method reset
151              */
152             reset: function() {
153                 html.length = 0;
154             },
155             /**
156              * Returns the contents that got serialized.
157              *
158              * @method getContent
159              * @return {String} HTML contents that got written down.
160              */
161             getContent: function() {
162                 return html.join('').replace(/\n$/, '');
163             }
164         };
165     };
166 });