Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cpojer committed Apr 28, 2017
1 parent d004fbe commit 9c07d02
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
10 changes: 7 additions & 3 deletions packages/pretty-format/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]]);
Expand All @@ -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: ''};
}
});

Expand Down
32 changes: 26 additions & 6 deletions packages/pretty-format/src/plugins/HTMLElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Attribute>,
childNodes: Array<HTMLElement | HTMLText | HTMLComment>,
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;

Expand Down Expand Up @@ -41,7 +63,7 @@ function printChildren(flatChildren, print, indent, colors, opts) {
.join(opts.edgeSpacing);
}

function printAttributes(attributes, indent, colors, opts) {
function printAttributes(attributes: Array<Attribute>, indent, colors, opts) {
return attributes
.sort()
.map(attribute => {
Expand All @@ -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 + '<!--' + element.data + '-->' + colors.comment.close
);
Expand Down
8 changes: 7 additions & 1 deletion types/PrettyFormat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>;
export type Print = any => string;
Expand Down

0 comments on commit 9c07d02

Please sign in to comment.