Skip to content
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

Implement normalize function #166

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

esmith164
Copy link

Functionality Added: Implemented the normalize function within the Node class.
Purpose: Merges adjacent text nodes and removes empty text nodes to clean up the DOM structure.
Methodology:

  • Traverses the childNodes list of a node.
  • Merges the content of adjacent text nodes.
  • Removes empty text nodes from the list.
  • Recursively normalizes child nodes for non-text nodes.

TypeScript Compliance: Ensured type safety by handling potential null values in nodeValue.

@b-fuze
Copy link
Owner

b-fuze commented Jun 26, 2024

Hey Ethan, please add tests and run deno fmt


// Merge adjacent text nodes
while (nextNode && nextNode.nodeType === NodeType.TEXT_NODE) {
if (currentNode.nodeValue) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need this if, adding an empty string would have the same effect

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, noticing that this if will drop the text from non-empty text nodes that follow empty text nodes

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, following the standard algorithm outlined here if any of the first text nodes found before others is empty then it's deleted and then the first non-empty text node will receive your currentNode treatment as you have here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants