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     this.htmlOutput = 'html' == settings.element_format;
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     encode : false,
48     htmlOutput : false,
49     
50             /**
51     * Writes the a start element such as <p id="a">.
52     *
53     * @method start
54     * @param {String} name Name of the element.
55     * @param {Array} attrs Optional attribute array or undefined if it hasn't any.
56     * @param {Boolean} empty Optional empty state if the tag should end like <br />.
57     */
58     start: function(name, attrs, empty) {
59        var i, l, attr, value;
60        if (this.indent && this.indentBefore[name] && this.html.length > 0) {
61            this.value = this.html[this.html.length - 1];
62            value.length > 0 && '\n' !== value && this.html.push('\n');
63        }
64        this.html.push('<', name);
65        if (attrs) {
66            for (i = 0, l = attrs.length; i < l; i++) {
67                attr = attrs[i];
68                this.html.push(' ', attr.name, '="', this.encode(attr.value, true), '"');
69            }
70        }
71        this.html[this.html.length] = !empty || this.htmlOutput ? '>' : ' />';
72        if (empty && this.indent && this.indentAfter[name] && this.html.length > 0) {
73            value = this.html[this.html.length - 1];
74            value.length > 0 && '\n' !== value && this.html.push('\n');
75        }
76     },
77     /**
78      * Writes the a end element such as </p>.
79      *
80      * @method end
81      * @param {String} name Name of the element.
82      */
83     end: function(name) {
84         var value;
85          
86         this.html.push('</', name, '>');
87         if (this.indent && this.indentAfter[name] && this.html.length > 0) {
88             value = this.html[this.html.length - 1];
89             value.length > 0 && '\n' !== value && this.html.push('\n');
90         }
91     },
92     /**
93      * Writes a text node.
94      *
95      * @method text
96      * @param {String} text String to write out.
97      * @param {Boolean} raw Optional raw state if true the contents wont get encoded.
98      */
99     text: function(text, raw) {
100         text.length > 0 && (this.html[this.html.length] = raw ? text : encode(text));
101     },
102     /**
103      * Writes a cdata node such as <![CDATA[data]]>.
104      *
105      * @method cdata
106      * @param {String} text String to write out inside the cdata.
107      */
108     cdata: function(text) {
109         this.html.push('<![CDATA[', text, ']]>');
110     },
111     /**
112     * Writes a comment node such as <!-- Comment -->.
113     *
114     * @method cdata
115     * @param {String} text String to write out inside the comment.
116     */
117    comment: function(text) {
118        this.html.push('<!--', text, '-->');
119    },
120     /**
121      * Writes a PI node such as <?xml attr="value" ?>.
122      *
123      * @method pi
124      * @param {String} name Name of the pi.
125      * @param {String} text String to write out inside the pi.
126      */
127     pi: function(name, text) {
128         text ? this.html.push('<?', name, ' ', this.encode(text), '?>') : this.html.push('<?', name, '?>');
129         this.indent && this.html.push('\n');
130     },
131     /**
132      * Writes a doctype node such as <!DOCTYPE data>.
133      *
134      * @method doctype
135      * @param {String} text String to write out inside the doctype.
136      */
137     doctype: function(text) {
138         this.html.push('<!DOCTYPE', text, '>', this.indent ? '\n' : '');
139     },
140     /**
141      * Resets the internal buffer if one wants to reuse the writer.
142      *
143      * @method reset
144      */
145     reset: function() {
146         this.html.length = 0;
147     },
148     /**
149      * Returns the contents that got serialized.
150      *
151      * @method getContent
152      * @return {String} HTML contents that got written down.
153      */
154     getContent: function() {
155         return this.html.join('').replace(/\n$/, '');
156     }
157
158 });