-
-
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
pretty-format: Support HTMLCollection and NodeList in DOMCollection plugin #7125
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
deded51
pretty-format: Support HTMLCollection and NodeList in DOMCollection p…
pedrottimark 69dc91f
Update CHANGELOG.md
pedrottimark 7f65212
Make recommended changes and add tests for options
pedrottimark 31a2b9e
Merge branch 'master' into dom-collection
SimenB File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,29 +9,26 @@ | |
|
||
import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat'; | ||
|
||
import {printObjectProperties} from '../collections'; | ||
import {printListItems, printObjectProperties} from '../collections'; | ||
|
||
const SPACE = ' '; | ||
|
||
const COLLECTION_NAMES = ['DOMStringMap', 'NamedNodeMap']; | ||
const OBJECT_NAMES = ['DOMStringMap', 'NamedNodeMap']; | ||
const ARRAY_REGEXP = /^(HTML\w*Collection|NodeList)$/; | ||
|
||
const testName = (name: any) => | ||
OBJECT_NAMES.indexOf(name) !== -1 || ARRAY_REGEXP.test(name); | ||
|
||
export const test = (val: any) => | ||
val && | ||
val.constructor && | ||
COLLECTION_NAMES.indexOf(val.constructor.name) !== -1; | ||
|
||
const convertCollectionToObject = (collection: any) => { | ||
let result = {}; | ||
|
||
if (collection.constructor.name === 'NamedNodeMap') { | ||
for (let i = 0; i < collection.length; i++) { | ||
result[collection[i].name] = collection[i].value; | ||
} | ||
} else { | ||
result = Object.assign({}, collection); | ||
} | ||
val.constructor.name && | ||
testName(val.constructor.name); | ||
|
||
return result; | ||
// Convert array of attribute objects to props object. | ||
const propsReducer = (props, attribute) => { | ||
props[attribute.name] = attribute.value; | ||
return props; | ||
}; | ||
|
||
export const serialize = ( | ||
|
@@ -42,23 +39,36 @@ export const serialize = ( | |
refs: Refs, | ||
printer: Printer, | ||
): string => { | ||
const name = collection.constructor.name; | ||
if (++depth > config.maxDepth) { | ||
return '[' + collection.constructor.name + ']'; | ||
return '[' + name + ']'; | ||
} | ||
|
||
return ( | ||
collection.constructor.name + | ||
SPACE + | ||
'{' + | ||
printObjectProperties( | ||
convertCollectionToObject(collection), | ||
config, | ||
indentation, | ||
depth, | ||
refs, | ||
printer, | ||
) + | ||
'}' | ||
(config.min ? '' : name + SPACE) + | ||
(OBJECT_NAMES.indexOf(name) !== -1 | ||
? '{' + | ||
printObjectProperties( | ||
name === 'NamedNodeMap' | ||
? Array.prototype.reduce.call(collection, propsReducer, {}) | ||
: Object.assign({}, collection), | ||
config, | ||
indentation, | ||
depth, | ||
refs, | ||
printer, | ||
) + | ||
'}' | ||
: '[' + | ||
printListItems( | ||
Array.prototype.slice.call(collection), | ||
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.
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. Yes, I will leave the compiling to Babel :) |
||
config, | ||
indentation, | ||
depth, | ||
refs, | ||
printer, | ||
) + | ||
']') | ||
); | ||
}; | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
jsdom 12 is released, and while we won't upgrade for some time, maybe we can normalize the name to
HTMLFormControlsCollection
?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.
Verified outside Jest that
form.elements
stillHTMLCollection
class in jsdom@12.2.0 so added the test. Wrote a comment so that we will know to update the criterion if it ever changes.