markup.js 3.5 KB

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