token.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. * fence infostring
  75. **/
  76. this.info = '';
  77. /**
  78. * Token#meta -> Object
  79. *
  80. * A place for plugins to store an arbitrary data
  81. **/
  82. this.meta = null;
  83. /**
  84. * Token#block -> Boolean
  85. *
  86. * True for block-level tokens, false for inline tokens.
  87. * Used in renderer to calculate line breaks
  88. **/
  89. this.block = false;
  90. /**
  91. * Token#hidden -> Boolean
  92. *
  93. * If it's true, ignore this element when rendering. Used for tight lists
  94. * to hide paragraphs.
  95. **/
  96. this.hidden = false;
  97. }
  98. /**
  99. * Token.attrIndex(name) -> Number
  100. *
  101. * Search attribute index by name.
  102. **/
  103. Token.prototype.attrIndex = function attrIndex(name) {
  104. var attrs, i, len;
  105. if (!this.attrs) { return -1; }
  106. attrs = this.attrs;
  107. for (i = 0, len = attrs.length; i < len; i++) {
  108. if (attrs[i][0] === name) { return i; }
  109. }
  110. return -1;
  111. };
  112. /**
  113. * Token.attrPush(attrData)
  114. *
  115. * Add `[ name, value ]` attribute to list. Init attrs if necessary
  116. **/
  117. Token.prototype.attrPush = function attrPush(attrData) {
  118. if (this.attrs) {
  119. this.attrs.push(attrData);
  120. } else {
  121. this.attrs = [ attrData ];
  122. }
  123. };
  124. /**
  125. * Token.attrSet(name, value)
  126. *
  127. * Set `name` attribute to `value`. Override old value if exists.
  128. **/
  129. Token.prototype.attrSet = function attrSet(name, value) {
  130. var idx = this.attrIndex(name),
  131. attrData = [ name, value ];
  132. if (idx < 0) {
  133. this.attrPush(attrData);
  134. } else {
  135. this.attrs[idx] = attrData;
  136. }
  137. };
  138. /**
  139. * Token.attrGet(name)
  140. *
  141. * Get the value of attribute `name`, or null if it does not exist.
  142. **/
  143. Token.prototype.attrGet = function attrGet(name) {
  144. var idx = this.attrIndex(name), value = null;
  145. if (idx >= 0) {
  146. value = this.attrs[idx][1];
  147. }
  148. return value;
  149. };
  150. /**
  151. * Token.attrJoin(name, value)
  152. *
  153. * Join value to existing attribute via space. Or create new attribute if not
  154. * exists. Useful to operate with token classes.
  155. **/
  156. Token.prototype.attrJoin = function attrJoin(name, value) {
  157. var idx = this.attrIndex(name);
  158. if (idx < 0) {
  159. this.attrPush([ name, value ]);
  160. } else {
  161. this.attrs[idx][1] = this.attrs[idx][1] + ' ' + value;
  162. }
  163. };
  164. module.exports = Token;