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

Expose .addClass/.removeClass for HTMLElement? #26

Open
ni-vo opened this issue Dec 16, 2019 · 0 comments
Open

Expose .addClass/.removeClass for HTMLElement? #26

ni-vo opened this issue Dec 16, 2019 · 0 comments

Comments

@ni-vo
Copy link

ni-vo commented Dec 16, 2019

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 }
iamabubakar pushed a commit to iamabubakar/node-fast-html-parser that referenced this issue Sep 8, 2020
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

No branches or pull requests

1 participant