markup.js 3.3 KB

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