markup.js 3.6 KB

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