Skip to content

Commit

Permalink
Provide printText and printComment in markup.js for HTMLElement plugin (
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrottimark authored and cpojer committed Aug 24, 2017
1 parent a397aba commit 84b2502
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
14 changes: 4 additions & 10 deletions packages/pretty-format/src/plugins/html_element.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@

import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat';

import escapeHTML from './lib/escape_html';
import {
printChildren,
printComment,
printElement,
printElementAsLeaf,
printProps,
printText,
} from './lib/markup';

type Attribute = {
Expand Down Expand Up @@ -70,19 +71,12 @@ export const serialize = (
refs: Refs,
printer: Printer,
): string => {
const colors = config.colors;
if (node.nodeType === TEXT_NODE) {
return colors.content.open + escapeHTML(node.data) + colors.content.close;
return printText(node.data, config);
}

if (node.nodeType === COMMENT_NODE) {
return (
colors.comment.open +
'<!--' +
escapeHTML(node.data) +
'-->' +
colors.comment.close
);
return printComment(node.data, config);
}

const type = node.tagName.toLowerCase();
Expand Down
18 changes: 17 additions & 1 deletion packages/pretty-format/src/plugins/lib/markup.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,28 @@ export const printChildren = (
config.spacingOuter +
indentation +
(typeof child === 'string'
? colors.content.open + escapeHTML(child) + colors.content.close
? printText(child, config)
: printer(child, config, indentation, depth, refs)),
)
.join('');
};

export const printText = (text: string, config: Config): string => {
const contentColor = config.colors.content;
return contentColor.open + escapeHTML(text) + contentColor.close;
};

export const printComment = (comment: string, config: Config): string => {
const commentColor = config.colors.comment;
return (
commentColor.open +
'<!--' +
escapeHTML(comment) +
'-->' +
commentColor.close
);
};

// Separate the functions to format props, children, and element,
// so a plugin could override a particular function, if needed.
// Too bad, so sad: the traditional (but unnecessary) space
Expand Down

0 comments on commit 84b2502

Please sign in to comment.