-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support serializing DocumentFragment
#6705
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,17 +37,23 @@ type Comment = { | |
data: string, | ||
nodeType: 8, | ||
}; | ||
type DocumentFragment = { | ||
children: Array<Element>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure how important typing this is? It's not an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. using the types from flow makes the other stuff fail here since we can't use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW, flow passes with this diff diff --git i/packages/pretty-format/src/plugins/dom_element.js w/packages/pretty-format/src/plugins/dom_element.js
index 26f0801f6..59957aa54 100644
--- i/packages/pretty-format/src/plugins/dom_element.js
+++ w/packages/pretty-format/src/plugins/dom_element.js
@@ -18,30 +18,6 @@ import {
printText,
} from './lib/markup';
-type Attribute = {
- name: string,
- value: string,
-};
-
-type Element = {
- attributes: Array<Attribute>,
- childNodes: Array<Element | Text | Comment>,
- nodeType: 1,
- tagName: string,
-};
-type Text = {
- data: string,
- nodeType: 3,
-};
-type Comment = {
- data: string,
- nodeType: 8,
-};
-type DocumentFragment = {
- children: Array<Element>,
- nodeType: 11,
-};
-
const ELEMENT_NODE = 1;
const TEXT_NODE = 3;
const COMMENT_NODE = 8;
@@ -76,17 +52,17 @@ export const serialize = (
refs: Refs,
printer: Printer,
): string => {
- if (node.nodeType === TEXT_NODE) {
+ if (node instanceof Text) {
return printText(node.data, config);
}
- if (node.nodeType === COMMENT_NODE) {
+ if (node instanceof Comment) {
return printComment(node.data, config);
}
let type;
- if (node.nodeType === FRAGMENT_NODE) {
+ if (node instanceof DocumentFragment) {
type = `Fragment`;
} else {
type = node.tagName.toLowerCase(); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice, maybe it's worth to use those? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It only works with |
||
nodeType: 11, | ||
}; | ||
|
||
const ELEMENT_NODE = 1; | ||
const TEXT_NODE = 3; | ||
const COMMENT_NODE = 8; | ||
const FRAGMENT_NODE = 11; | ||
|
||
const ELEMENT_REGEXP = /^((HTML|SVG)\w*)?Element$/; | ||
|
||
const testNode = (nodeType: any, name: any) => | ||
(nodeType === ELEMENT_NODE && ELEMENT_REGEXP.test(name)) || | ||
(nodeType === TEXT_NODE && name === 'Text') || | ||
(nodeType === COMMENT_NODE && name === 'Comment'); | ||
(nodeType === COMMENT_NODE && name === 'Comment') || | ||
(nodeType === FRAGMENT_NODE && name === 'DocumentFragment'); | ||
|
||
export const test = (val: any) => | ||
val && | ||
|
@@ -63,7 +69,7 @@ const propsReducer = (props, attribute) => { | |
}; | ||
|
||
export const serialize = ( | ||
node: Element | Text | Comment, | ||
node: Element | Text | Comment | DocumentFragment, | ||
config: Config, | ||
indentation: string, | ||
depth: number, | ||
|
@@ -78,24 +84,28 @@ export const serialize = ( | |
return printComment(node.data, config); | ||
} | ||
|
||
const type = node.tagName.toLowerCase(); | ||
const type = | ||
node.nodeType === FRAGMENT_NODE | ||
? `DocumentFragment` | ||
: node.tagName.toLowerCase(); | ||
|
||
if (++depth > config.maxDepth) { | ||
return printElementAsLeaf(type, config); | ||
} | ||
|
||
return printElement( | ||
type, | ||
printProps( | ||
Array.prototype.map.call(node.attributes, keysMapper).sort(), | ||
Array.prototype.reduce.call(node.attributes, propsReducer, {}), | ||
Array.prototype.map.call(node.attributes || [], keysMapper).sort(), | ||
Array.prototype.reduce.call(node.attributes || [], propsReducer, {}), | ||
config, | ||
indentation + config.indent, | ||
depth, | ||
refs, | ||
printer, | ||
), | ||
printChildren( | ||
Array.prototype.slice.call(node.childNodes), | ||
Array.prototype.slice.call(node.childNodes || node.children), | ||
config, | ||
indentation + config.indent, | ||
depth, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
example from https://developer.mozilla.org/en-US/docs/Web/API/Document/createDocumentFragment