diff --git a/packages/pretty-format/src/index.js b/packages/pretty-format/src/index.js index b1c5e33425de..68de55f1d0ad 100644 --- a/packages/pretty-format/src/index.js +++ b/packages/pretty-format/src/index.js @@ -875,7 +875,13 @@ function prettyFormat(val: any, initialOptions?: InitialOptions): string { opts = normalizeOptions(initialOptions); } - const colors: Colors = {}; + const colors: Colors = { + comment: {close: '', open: ''}, + content: {close: '', open: ''}, + prop: {close: '', open: ''}, + tag: {close: '', open: ''}, + value: {close: '', open: ''}, + }; Object.keys(opts.theme).forEach(key => { if (opts.highlight) { const color = (colors[key] = style[opts.theme[key]]); @@ -888,8 +894,6 @@ function prettyFormat(val: any, initialOptions?: InitialOptions): string { `pretty-format: Option "theme" has a key "${key}" whose value "${opts.theme[key]}" is undefined in ansi-styles.`, ); } - } else { - colors[key] = {close: '', open: ''}; } }); diff --git a/packages/pretty-format/src/plugins/HTMLElement.js b/packages/pretty-format/src/plugins/HTMLElement.js index 6f0424412e13..8e8591248444 100644 --- a/packages/pretty-format/src/plugins/HTMLElement.js +++ b/packages/pretty-format/src/plugins/HTMLElement.js @@ -13,6 +13,28 @@ import type {Colors, Indent, Options, Print, Plugin} from 'types/PrettyFormat'; const escapeHTML = require('./lib/escapeHTML'); + +type Attribute = { + name: string, + value: string, +}; + +type HTMLElement = { + attributes: Array, + childNodes: Array, + nodeType: 1, + tagName: string, +}; +type HTMLText = { + data: string, + nodeType: 3, +}; + +type HTMLComment = { + data: string, + nodeType: 8, +}; + const HTML_ELEMENT_REGEXP = /(HTML\w*?Element)|Text|Comment/; const test = isHTMLElement; @@ -41,7 +63,7 @@ function printChildren(flatChildren, print, indent, colors, opts) { .join(opts.edgeSpacing); } -function printAttributes(attributes, indent, colors, opts) { +function printAttributes(attributes: Array, indent, colors, opts) { return attributes .sort() .map(attribute => { @@ -57,17 +79,15 @@ function printAttributes(attributes, indent, colors, opts) { } const print = ( - element: HTMLElement | Text | Comment, + element: HTMLElement | HTMLText | HTMLComment, print: Print, indent: Indent, opts: Options, colors: Colors, ): string => { - if (element instanceof Text) { + if (element.nodeType === 3) { return element.data; - } - - if (element instanceof Comment) { + } else if (element.nodeType === 8) { return ( colors.comment.open + '' + colors.comment.close ); diff --git a/types/PrettyFormat.js b/types/PrettyFormat.js index 02c52b811428..fde835d83f4c 100644 --- a/types/PrettyFormat.js +++ b/types/PrettyFormat.js @@ -9,7 +9,13 @@ */ 'use strict'; -export type Colors = Object; +export type Colors = { + comment: {close: string, open: string}, + content: {close: string, open: string}, + prop: {close: string, open: string}, + tag: {close: string, open: string}, + value: {close: string, open: string}, +}; export type Indent = string => string; export type Refs = Array; export type Print = any => string;