-
-
Notifications
You must be signed in to change notification settings - Fork 232
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
refactor: Make location info parser tests work #440
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
55d414b
Serialize any kind of node in `serializeOuter`
fb55 38639f3
Remove `null` values from document types
fb55 529b6ad
Make location info parser tests work
fb55 cc987e6
Return an empty string for unknown nodes
fb55 d9865d3
Remove `else` after return in `serializeNode`
fb55 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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 |
---|---|---|
|
@@ -4,7 +4,6 @@ import { TreeAdapter, TreeAdapterTypeMap } from 'parse5/dist/tree-adapters/inter | |
import * as assert from 'node:assert'; | ||
import * as fs from 'node:fs'; | ||
import * as path from 'node:path'; | ||
import { escapeString } from 'parse5/dist/serializer/index.js'; | ||
import * as parse5 from 'parse5/dist/index.js'; | ||
import { | ||
removeNewLines, | ||
|
@@ -13,22 +12,19 @@ import { | |
normalizeNewLine, | ||
generateTestsForEachTreeAdapter, | ||
} from './common.js'; | ||
import * as doctype from 'parse5/dist/common/doctype.js'; | ||
|
||
function walkTree<T extends TreeAdapterTypeMap>( | ||
document: T['document'], | ||
parent: T['parentNode'], | ||
treeAdapter: TreeAdapter<T>, | ||
handler: (node: T['node']) => void | ||
): void { | ||
const stack = [...treeAdapter.getChildNodes(document)]; | ||
let node; | ||
while ((node = stack.shift())) { | ||
const children = treeAdapter.getChildNodes(node); | ||
for (const node of treeAdapter.getChildNodes(parent)) { | ||
if (treeAdapter.isElementNode(node)) { | ||
walkTree(node, treeAdapter, handler); | ||
} | ||
|
||
handler(node); | ||
|
||
if (children?.length) { | ||
stack.unshift(...children); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -75,7 +71,10 @@ function assertAttrsLocation(location: ElementLocation, serializedNode: string, | |
assert.ok(location.attrs, 'Expected attrs to be defined'); | ||
|
||
for (const attr of Object.values(location.attrs)) { | ||
const expected = serializedNode.slice(attr.startOffset, attr.endOffset); | ||
const expected = serializedNode.slice( | ||
attr.startOffset - location.startOffset, | ||
attr.endOffset - location.startOffset | ||
); | ||
|
||
assertLocation(attr, expected, html, lines); | ||
} | ||
|
@@ -113,42 +112,44 @@ export function generateLocationInfoParserTests( | |
//Then for each node in the tree we run the serializer and compare results with the substring | ||
//obtained via the location info from the expected serialization results. | ||
it(`Location info (Parser) - ${test.name}`, async () => { | ||
const serializerOpts = { treeAdapter }; | ||
const html = escapeString(test.data); | ||
const html = test.data; | ||
const lines = html.split(/\r?\n/g); | ||
|
||
const parserOpts = { | ||
treeAdapter, | ||
sourceCodeLocationInfo: true, | ||
}; | ||
|
||
const parsingResult = await parse(html, parserOpts); | ||
const parsingResult = parse(html, parserOpts); | ||
const document = parsingResult.node; | ||
|
||
walkTree(document, treeAdapter, (node) => { | ||
const location = treeAdapter.getNodeSourceCodeLocation(node); | ||
|
||
if (location) { | ||
const fragment = treeAdapter.createDocumentFragment(); | ||
assert.ok(location); | ||
|
||
treeAdapter.appendChild(fragment, node); | ||
const serializedNode = treeAdapter.isDocumentTypeNode(node) | ||
? `<${doctype.serializeContent( | ||
treeAdapter.getDocumentTypeNodeName(node), | ||
treeAdapter.getDocumentTypeNodePublicId(node), | ||
treeAdapter.getDocumentTypeNodeSystemId(node) | ||
)}>` | ||
: parse5.serializeOuter(node, { treeAdapter }); | ||
|
||
const serializedNode = parse5.serialize(fragment, serializerOpts); | ||
assertLocation(location, serializedNode, html, lines); | ||
|
||
assertNodeLocation(location, serializedNode, html, lines); | ||
|
||
// TODO: None of the cases below are ever matched. | ||
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. This is the TODO that started this PR. |
||
|
||
if (location.startTag) { | ||
assertStartTagLocation(location, serializedNode, html, lines); | ||
} | ||
if (treeAdapter.isElementNode(node)) { | ||
assertStartTagLocation(location, serializedNode, html, lines); | ||
|
||
if (location.endTag) { | ||
assertEndTagLocation(location, serializedNode, html, lines); | ||
} | ||
|
||
if (location.attrs) { | ||
assertAttrsLocation(location, serializedNode, html, lines); | ||
} else { | ||
// If we don't have `location.attrs`, we expect that the node has no attributes. | ||
assert.strictEqual(treeAdapter.getAttrList(node).length, 0); | ||
} | ||
} | ||
}); | ||
|
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.
This is the
escapeString
call that invalidated the test.