markup.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.printElementAsLeaf = exports.printElement = exports.printComment = exports.printText = exports.printChildren = exports.printProps = undefined;
  6. var _escape_html = require('./escape_html');
  7. var _escape_html2 = _interopRequireDefault(_escape_html);
  8. function _interopRequireDefault(obj) {
  9. return obj && obj.__esModule ? obj : {default: obj};
  10. }
  11. // Return empty string if keys is empty.
  12. /**
  13. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  14. *
  15. * This source code is licensed under the MIT license found in the
  16. * LICENSE file in the root directory of this source tree.
  17. *
  18. *
  19. */
  20. const printProps = (exports.printProps = (
  21. keys,
  22. props,
  23. config,
  24. indentation,
  25. depth,
  26. refs,
  27. printer
  28. ) => {
  29. const indentationNext = indentation + config.indent;
  30. const colors = config.colors;
  31. return keys
  32. .map(key => {
  33. const value = props[key];
  34. let printed = printer(value, config, indentationNext, depth, refs);
  35. if (typeof value !== 'string') {
  36. if (printed.indexOf('\n') !== -1) {
  37. printed =
  38. config.spacingOuter +
  39. indentationNext +
  40. printed +
  41. config.spacingOuter +
  42. indentation;
  43. }
  44. printed = '{' + printed + '}';
  45. }
  46. return (
  47. config.spacingInner +
  48. indentation +
  49. colors.prop.open +
  50. key +
  51. colors.prop.close +
  52. '=' +
  53. colors.value.open +
  54. printed +
  55. colors.value.close
  56. );
  57. })
  58. .join('');
  59. });
  60. // Return empty string if children is empty.
  61. const printChildren = (exports.printChildren = (
  62. children,
  63. config,
  64. indentation,
  65. depth,
  66. refs,
  67. printer
  68. ) =>
  69. children
  70. .map(
  71. child =>
  72. config.spacingOuter +
  73. indentation +
  74. (typeof child === 'string'
  75. ? printText(child, config)
  76. : printer(child, config, indentation, depth, refs))
  77. )
  78. .join(''));
  79. const printText = (exports.printText = (text, config) => {
  80. const contentColor = config.colors.content;
  81. return (
  82. contentColor.open + (0, _escape_html2.default)(text) + contentColor.close
  83. );
  84. });
  85. const printComment = (exports.printComment = (comment, config) => {
  86. const commentColor = config.colors.comment;
  87. return (
  88. commentColor.open +
  89. '<!--' +
  90. (0, _escape_html2.default)(comment) +
  91. '-->' +
  92. commentColor.close
  93. );
  94. });
  95. // Separate the functions to format props, children, and element,
  96. // so a plugin could override a particular function, if needed.
  97. // Too bad, so sad: the traditional (but unnecessary) space
  98. // in a self-closing tagColor requires a second test of printedProps.
  99. const printElement = (exports.printElement = (
  100. type,
  101. printedProps,
  102. printedChildren,
  103. config,
  104. indentation
  105. ) => {
  106. const tagColor = config.colors.tag;
  107. return (
  108. tagColor.open +
  109. '<' +
  110. type +
  111. (printedProps &&
  112. tagColor.close +
  113. printedProps +
  114. config.spacingOuter +
  115. indentation +
  116. tagColor.open) +
  117. (printedChildren
  118. ? '>' +
  119. tagColor.close +
  120. printedChildren +
  121. config.spacingOuter +
  122. indentation +
  123. tagColor.open +
  124. '</' +
  125. type
  126. : (printedProps && !config.min ? '' : ' ') + '/') +
  127. '>' +
  128. tagColor.close
  129. );
  130. });
  131. const printElementAsLeaf = (exports.printElementAsLeaf = (type, config) => {
  132. const tagColor = config.colors.tag;
  133. return (
  134. tagColor.open +
  135. '<' +
  136. type +
  137. tagColor.close +
  138. ' …' +
  139. tagColor.open +
  140. ' />' +
  141. tagColor.close
  142. );
  143. });