ashi009 / ashi009/node-fast-html-parser

Expose .addClass/.removeClass for HTMLElement?

Open
#26 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
166
Forks
150
PR merge metrics
No merged PRs in 30d

Description

I have been trying to inject a class name into a parsed HTMLElement and when I call rootNode.toString(), the class does not appear. Printing the element that gets fed into .exchangeChild does infact show the class, though.

Am I doing something wrong, or is there perhaps a workaround?

```
const targetElement = node as HTMLElement;
const parentElement = targetElement.parentNode as HTMLElement;

const component = createComponent(targetElement.tagName);

const renderedComponentHtml = minify(component.render(targetElement.attributes, targetElement.innerHTML));
const renderedElement = parse(renderedComponentHtml) as HTMLElement;

if (options.injectClass) {
const childElement = renderedElement.childNodes[0] as HTMLElement;
childElement.classNames = [targetElement.tagName];

console.log(renderedElement.childNodes[0]);
}

parentElement.exchangeChild(targetElement, renderedElement);
```

Output:
```
HTMLElement {
childNodes:
[ HTMLElement {
childNodes: [Array],
tagName: 'table',
rawAttrs: 'class="table"',
parentNode: [Circular],
classNames: [Array],
nodeType: 1 },
HTMLElement {
childNodes: [Array],
tagName: 'div',
rawAttrs: '',
parentNode: [Circular],
classNames: [],
nodeType: 1 },
HTMLElement {
childNodes: [Array],
tagName: 'table',
rawAttrs: 'class="table"',
parentNode: [Circular],
classNames: [Array],
nodeType: 1 } ],
tagName: 'body',
rawAttrs: '',
parentNode: null,
classNames: [ 'aur-base' ],
nodeType: 1 }
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.