token.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Token class
  2. 'use strict';
  3. /**
  4. * class Token
  5. **/
  6. /**
  7. * new Token(type, tag, nesting)
  8. *
  9. * Create new token and fill passed properties.
  10. **/
  11. function Token(type, tag, nesting) {
  12. /**
  13. * Token#type -> String
  14. *
  15. * Type of the token (string, e.g. "paragraph_open")
  16. **/
  17. this.type = type;
  18. /**
  19. * Token#tag -> String
  20. *
  21. * html tag name, e.g. "p"
  22. **/
  23. this.tag = tag;
  24. /**
  25. * Token#attrs -> Array
  26. *
  27. * Html attributes. Format: `[ [ name1, value1 ], [ name2, value2 ] ]`
  28. **/
  29. this.attrs = null;
  30. /**
  31. * Token#map -> Array
  32. *
  33. * Source map info. Format: `[ line_begin, line_end ]`
  34. **/
  35. this.map = null;
  36. /**
  37. * Token#nesting -> Number
  38. *
  39. * Level change (number in {-1, 0, 1} set), where:
  40. *
  41. * - `1` means the tag is opening
  42. * - `0` means the tag is self-closing
  43. * - `-1` means the tag is closing
  44. **/
  45. this.nesting = nesting;
  46. /**
  47. * Token#level -> Number
  48. *
  49. * nesting level, the same as `state.level`
  50. **/
  51. this.level = 0;
  52. /**
  53. * Token#children -> Array
  54. *
  55. * An array of child nodes (inline and img tokens)
  56. **/
  57. this.children = null;
  58. /**
  59. * Token#content -> String
  60. *
  61. * In a case of self-closing tag (code, html, fence, etc.),
  62. * it has contents of this tag.
  63. **/
  64. this.content = '';
  65. /**
  66. * Token#markup -> String
  67. *
  68. * '*' or '_' for emphasis, fence string for fence, etc.
  69. **/
  70. this.markup = '';
  71. /**
  72. * Token#info -> String
  73. *
  74. * Additional information:
  75. *
  76. * - Info string for "fence" tokens
  77. * - The value "auto" for autolink "link_open" and "link_close" tokens
  78. * - The string value of the item marker for ordered-list "list_item_open" tokens
  79. **/
  80. this.info = '';
  81. /**
  82. * Token#meta -> Object
  83. *
  84. * A place for plugins to store an arbitrary data
  85. **/
  86. this.meta = null;
  87. /**
  88. * Token#block -> Boolean
  89. *
  90. * True for block-level tokens, false for inline tokens.
  91. * Used in renderer to calculate line breaks
  92. **/
  93. this.block = false;
  94. /**
  95. * Token#hidden -> Boolean
  96. *
  97. * If it's true, ignore this element when rendering. Used for tight lists
  98. * to hide paragraphs.
  99. **/
  100. this.hidden = false;
  101. }
  102. /**
  103. * Token.attrIndex(name) -> Number
  104. *
  105. * Search attribute index by name.
  106. **/
  107. Token.prototype.attrIndex = function attrIndex(name) {
  108. var attrs, i, len;
  109. if (!this.attrs) { return -1; }
  110. attrs = this.attrs;
  111. for (i = 0, len = attrs.length; i < len; i++) {
  112. if (attrs[i][0] === name) { return i; }
  113. }
  114. return -1;
  115. };
  116. /**
  117. * Token.attrPush(attrData)
  118. *
  119. * Add `[ name, value ]` attribute to list. Init attrs if necessary
  120. **/
  121. Token.prototype.attrPush = function attrPush(attrData) {
  122. if (this.attrs) {
  123. this.attrs.push(attrData);
  124. } else {
  125. this.attrs = [ attrData ];
  126. }
  127. };
  128. /**
  129. * Token.attrSet(name, value)
  130. *
  131. * Set `name` attribute to `value`. Override old value if exists.
  132. **/
  133. Token.prototype.attrSet = function attrSet(name, value) {
  134. var idx = this.attrIndex(name),
  135. attrData = [ name, value ];
  136. if (idx < 0) {
  137. this.attrPush(attrData);
  138. } else {
  139. this.attrs[idx] = attrData;
  140. }
  141. };
  142. /**
  143. * Token.attrGet(name)
  144. *
  145. * Get the value of attribute `name`, or null if it does not exist.
  146. **/
  147. Token.prototype.attrGet = function attrGet(name) {
  148. var idx = this.attrIndex(name), value = null;
  149. if (idx >= 0) {
  150. value = this.attrs[idx][1];
  151. }
  152. return value;
  153. };
  154. /**
  155. * Token.attrJoin(name, value)
  156. *
  157. * Join value to existing attribute via space. Or create new attribute if not
  158. * exists. Useful to operate with token classes.
  159. **/
  160. Token.prototype.attrJoin = function attrJoin(name, value) {
  161. var idx = this.attrIndex(name);
  162. if (idx < 0) {
  163. this.attrPush([ name, value ]);
  164. } else {
  165. this.attrs[idx][1] = this.attrs[idx][1] + ' ' + value;
  166. }
  167. };
  168. module.exports = Token;