Writer.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * Writer.js
  3. *
  4. * Copyright, Moxiecode Systems AB
  5. * Released under LGPL License.
  6. *
  7. * License: http://www.tinymce.com/license
  8. * Contributing: http://www.tinymce.com/contributing
  9. */
  10. /**
  11. * This class is used to write HTML tags out it can be used with the Serializer or the SaxParser.
  12. *
  13. * @class tinymce.html.Writer
  14. * @example
  15. * var writer = new tinymce.html.Writer({indent: true});
  16. * var parser = new tinymce.html.SaxParser(writer).parse('<p><br></p>');
  17. * console.log(writer.getContent());
  18. *
  19. * @class tinymce.html.Writer
  20. * @version 3.4
  21. */
  22. define("tinymce/html/Writer", [
  23. "tinymce/html/Entities",
  24. "tinymce/util/Tools"
  25. ], function(Entities, Tools) {
  26. var makeMap = Tools.makeMap;
  27. /**
  28. * Constructs a new Writer instance.
  29. *
  30. * @constructor
  31. * @method Writer
  32. * @param {Object} settings Name/value settings object.
  33. */
  34. return function(settings) {
  35. var html = [], indent, indentBefore, indentAfter, encode, htmlOutput;
  36. settings = settings || {};
  37. indent = settings.indent;
  38. indentBefore = makeMap(settings.indent_before || '');
  39. indentAfter = makeMap(settings.indent_after || '');
  40. encode = Entities.getEncodeFunc(settings.entity_encoding || 'raw', settings.entities);
  41. htmlOutput = settings.element_format == "html";
  42. return {
  43. /**
  44. * Writes the a start element such as <p id="a">.
  45. *
  46. * @method start
  47. * @param {String} name Name of the element.
  48. * @param {Array} attrs Optional attribute array or undefined if it hasn't any.
  49. * @param {Boolean} empty Optional empty state if the tag should end like <br />.
  50. */
  51. start: function(name, attrs, empty) {
  52. var i, l, attr, value;
  53. if (indent && indentBefore[name] && html.length > 0) {
  54. value = html[html.length - 1];
  55. if (value.length > 0 && value !== '\n') {
  56. html.push('\n');
  57. }
  58. }
  59. html.push('<', name);
  60. if (attrs) {
  61. for (i = 0, l = attrs.length; i < l; i++) {
  62. attr = attrs[i];
  63. html.push(' ', attr.name, '="', encode(attr.value, true), '"');
  64. }
  65. }
  66. if (!empty || htmlOutput) {
  67. html[html.length] = '>';
  68. } else {
  69. html[html.length] = ' />';
  70. }
  71. if (empty && indent && indentAfter[name] && html.length > 0) {
  72. value = html[html.length - 1];
  73. if (value.length > 0 && value !== '\n') {
  74. html.push('\n');
  75. }
  76. }
  77. },
  78. /**
  79. * Writes the a end element such as </p>.
  80. *
  81. * @method end
  82. * @param {String} name Name of the element.
  83. */
  84. end: function(name) {
  85. var value;
  86. /*if (indent && indentBefore[name] && html.length > 0) {
  87. value = html[html.length - 1];
  88. if (value.length > 0 && value !== '\n')
  89. html.push('\n');
  90. }*/
  91. html.push('</', name, '>');
  92. if (indent && indentAfter[name] && html.length > 0) {
  93. value = html[html.length - 1];
  94. if (value.length > 0 && value !== '\n') {
  95. html.push('\n');
  96. }
  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. if (text.length > 0) {
  108. html[html.length] = raw ? text : encode(text);
  109. }
  110. },
  111. /**
  112. * Writes a cdata node such as <![CDATA[data]]>.
  113. *
  114. * @method cdata
  115. * @param {String} text String to write out inside the cdata.
  116. */
  117. cdata: function(text) {
  118. html.push('<![CDATA[', text, ']]>');
  119. },
  120. /**
  121. * Writes a comment node such as <!-- Comment -->.
  122. *
  123. * @method cdata
  124. * @param {String} text String to write out inside the comment.
  125. */
  126. comment: function(text) {
  127. html.push('<!--', text, '-->');
  128. },
  129. /**
  130. * Writes a PI node such as <?xml attr="value" ?>.
  131. *
  132. * @method pi
  133. * @param {String} name Name of the pi.
  134. * @param {String} text String to write out inside the pi.
  135. */
  136. pi: function(name, text) {
  137. if (text) {
  138. html.push('<?', name, ' ', text, '?>');
  139. } else {
  140. html.push('<?', name, '?>');
  141. }
  142. if (indent) {
  143. html.push('\n');
  144. }
  145. },
  146. /**
  147. * Writes a doctype node such as <!DOCTYPE data>.
  148. *
  149. * @method doctype
  150. * @param {String} text String to write out inside the doctype.
  151. */
  152. doctype: function(text) {
  153. html.push('<!DOCTYPE', text, '>', indent ? '\n' : '');
  154. },
  155. /**
  156. * Resets the internal buffer if one wants to reuse the writer.
  157. *
  158. * @method reset
  159. */
  160. reset: function() {
  161. html.length = 0;
  162. },
  163. /**
  164. * Returns the contents that got serialized.
  165. *
  166. * @method getContent
  167. * @return {String} HTML contents that got written down.
  168. */
  169. getContent: function() {
  170. return html.join('').replace(/\n$/, '');
  171. }
  172. };
  173. };
  174. });