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

Fixed: inspecting document.title deleted it as a side-effect (when cached) #266

Conversation

philipp-classen
Copy link
Contributor

Minimal reproducible example with linkedom 0.16.10:

import { parseHTML } from 'linkedom/cached';

const html = '<!DOCTYPE html><html><head><title>Test</title></head><body></body></html>';
const { document } = parseHTML(html);
console.log('Title:', document.title); // Title: Test
console.log('Title:', document.title); // Title:          <-- missing (title removed as a side-effect)

philipp-classen added a commit to ghostery/ghostery-extension that referenced this pull request Mar 22, 2024
Switching to linkedom/cached introduced a regression. See
WebReflection/linkedom#266
smalluban pushed a commit to ghostery/ghostery-extension that referenced this pull request Mar 25, 2024
Switching to linkedom/cached introduced a regression. See
WebReflection/linkedom#266
@@ -77,13 +77,13 @@ export class HTMLDocument extends Document {
*/
get title() {
const {head} = this;
let title = head.getElementsByTagName('title').shift();
let title = head.getElementsByTagName('title')[0];
Copy link
Owner

Choose a reason for hiding this comment

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

I think these days I would go for .at(0) instead ... so this could be:

get title() {
  const {head} = this;
  return head.getElementsByTagName('title').at(0)?.textContent || '';
}

@@ -77,8 +77,7 @@ export class HTMLDocument extends Document {
*/
get title() {
const {head} = this;
let title = head.getElementsByTagName('title')[0];
return title ? title.textContent : '';
return head.getElementsByTagName('title')[0]?.textContent || '';
Copy link
Owner

Choose a reason for hiding this comment

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

.at(0) ... not [0] ... [0] bails out performance if no title exists, .at(0) doesn't suffer this issue.

@WebReflection WebReflection merged commit 444a040 into WebReflection:main Mar 25, 2024
2 checks passed
@WebReflection
Copy link
Owner

Thanks! It's up and running. If you had an issue please be sure that gets closed too 👋

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