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

TypeError: elem.getAttribute is not a function #100

Closed
moltar opened this issue Feb 24, 2021 · 13 comments
Closed

TypeError: elem.getAttribute is not a function #100

moltar opened this issue Feb 24, 2021 · 13 comments
Labels

Comments

@moltar
Copy link

moltar commented Feb 24, 2021

Version 3.0.0 (or 3.0.1) seems to intro an error:

TypeError: elem.getAttribute is not a function

const html = parse('...')

// happens here
html.querySelector('#foo')
@moltar
Copy link
Author

moltar commented Feb 24, 2021

      at Object.getAttributeValue (node_modules/node-html-parser/dist/matcher.js:11:17)
      at node_modules/css-select/lib/attributes.js:32:28
      at Object.findOne (node_modules/node-html-parser/dist/matcher.js:68:13)
      at node_modules/css-select/lib/index.js:117:27
      at Object.select [as selectOne] (node_modules/css-select/lib/index.js:64:16)
      at HTMLElement.Object.<anonymous>.HTMLElement.querySelector (node_modules/node-html-parser/dist/nodes/html.js:421:29)

...

@taoqf taoqf added the bug label Feb 24, 2021
@taoqf
Copy link
Owner

taoqf commented Feb 24, 2021

I need more details. here is an example of id selector, and the tests passed.

@olitoni
Copy link

olitoni commented Feb 24, 2021

I've got the same issue

  let root = parse(data);
  let test = root.querySelectorAll('.observed');
  console.log(test[0].querySelector('.search'))
  
  //Returns:
  TypeError: elem.getAttribute is not a function

@olitoni
Copy link

olitoni commented Feb 24, 2021

Just tried, 2.1.0 works absolutely fine and returns the HTMLElement from the code above

@kika
Copy link

kika commented Feb 24, 2021

For me this error happens when there's no match for the selector.

@moltar
Copy link
Author

moltar commented Feb 24, 2021

For me this error happens when there's no match for the selector.

Most likely the same for me. I'm checking for the existence of an element that I expect to be absent most of the time.

@kika
Copy link

kika commented Feb 25, 2021

I got around by using querySelectorAll and checking the length of the result. But I'd expect querySelector to return a falsey value, not a trap.

@taoqf
Copy link
Owner

taoqf commented Feb 25, 2021

I got around by using querySelectorAll and checking the length of the result. But I'd expect querySelector to return a falsey value, not a trap.

May I know what is the html you parsed? I should do some tests and try to fix that.

Just tried, 2.1.0 works absolutely fine and returns the HTMLElement from the code above

It will be easier for me to have the html.

I did some tests like that:

const root = parse('<div class="a b"></div>');
root.querySelectorAll('a').length.should.eql(0);
should.equal(root.querySelector('a'), null);
root.querySelectorAll('.a').length.should.eql(1);
root.querySelectorAll('.b').length.should.eql(1);
should.notEqual(root.querySelector('.a'), null);
const nodelist = root.querySelectorAll('.a,.b');
nodelist.length.should.eql(1);
const div = nodelist[0];
root.querySelector('.a,.b').should.eql(div);

taoqf added a commit that referenced this issue Feb 25, 2021
@taoqf
Copy link
Owner

taoqf commented Feb 25, 2021

I tried to fix the issue. If you guys won't give me any clue, I will publish 3.0.2 several hours later.

taoqf added a commit that referenced this issue Feb 25, 2021
@taoqf
Copy link
Owner

taoqf commented Feb 25, 2021

I think I fixed the issue. 3.0.3 published.

@taoqf
Copy link
Owner

taoqf commented Feb 25, 2021

Thank all of you.

@taoqf taoqf closed this as completed Feb 25, 2021
@kika
Copy link

kika commented Feb 25, 2021

Yeeep, can confirm, now querySelector works as expected if there's nothing in the HTML that matches the selector.

@moltar
Copy link
Author

moltar commented Feb 25, 2021

Thanks @taoqf! 🙌🏼

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

No branches or pull requests

4 participants