markup.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. // Return empty string if keys is empty.
  10. /**
  11. * Copyright (c) 2014-present, Facebook, Inc. 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. *
  17. */
  18. const printProps = exports.printProps = (keys, props, config, indentation, depth, refs, printer) => {
  19. const indentationNext = indentation + config.indent;
  20. const colors = config.colors;
  21. return keys.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 = config.spacingOuter + indentationNext + printed + config.spacingOuter + indentation;
  27. }
  28. printed = '{' + printed + '}';
  29. }
  30. return config.spacingInner + indentation + colors.prop.open + key + colors.prop.close + '=' + colors.value.open + printed + colors.value.close;
  31. }).join('');
  32. };
  33. // Return empty string if children is empty.
  34. const printChildren = exports.printChildren = (children, config, indentation, depth, refs, printer) => {
  35. return children.map(child => config.spacingOuter + indentation + (typeof child === 'string' ? printText(child, config) : printer(child, config, indentation, depth, refs))).join('');
  36. };
  37. const printText = exports.printText = (text, config) => {
  38. const contentColor = config.colors.content;
  39. return contentColor.open + (0, _escape_html2.default)(text) + contentColor.close;
  40. };
  41. const printComment = exports.printComment = (comment, config) => {
  42. const commentColor = config.colors.comment;
  43. return commentColor.open + '<!--' + (0, _escape_html2.default)(comment) + '-->' + commentColor.close;
  44. };
  45. // Separate the functions to format props, children, and element,
  46. // so a plugin could override a particular function, if needed.
  47. // Too bad, so sad: the traditional (but unnecessary) space
  48. // in a self-closing tagColor requires a second test of printedProps.
  49. const printElement = exports.printElement = (type, printedProps, printedChildren, config, indentation) => {
  50. const tagColor = config.colors.tag;
  51. return tagColor.open + '<' + type + (printedProps && tagColor.close + printedProps + config.spacingOuter + indentation + tagColor.open) + (printedChildren ? '>' + tagColor.close + printedChildren + config.spacingOuter + indentation + tagColor.open + '</' + type : (printedProps && !config.min ? '' : ' ') + '/') + '>' + tagColor.close;
  52. };
  53. const printElementAsLeaf = exports.printElementAsLeaf = (type, config) => {
  54. const tagColor = config.colors.tag;
  55. return tagColor.open + '<' + type + tagColor.close + ' …' + tagColor.open + ' />' + tagColor.close;
  56. };