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

Selector should not retrieve template content #2643

Open
andolf opened this issue Jul 13, 2022 · 3 comments
Open

Selector should not retrieve template content #2643

andolf opened this issue Jul 13, 2022 · 3 comments

Comments

@andolf
Copy link

andolf commented Jul 13, 2022

The MDN documentation for the <template> element specifies that its content is processed, but never rendered. This means that if you try to query the content you should end up with nothing. This behaviour was seemingly broken in rc4.

Simple code example:

const {load} = require("cheerio");

const $ = load(`<template id="temp">
  <div id="insert">
    <p>In a template</p>
  </div>
</template>
<div id="container"></div>`);

console.log("Inside template:", $("#insert").length); // 1, should be 0

const templateElement = $("#temp");
const templateContentClone = templateElement.contents();
$("#container").append(templateContentClone);

console.log("Now appended:", $("#insert").length); // outputs 1 as expected (but outputs 0 in rc3)

Whether this is a problem related to parse5 I don't know yet, the original implementation says that template contents cannot be queried directly but testing in a browser, you can query it as soon as it is added to the body and thus rendered.

@andolf
Copy link
Author

andolf commented Jul 13, 2022

Parsing the content with parse5 seems to correctly parse the structure of the template element with no childNodes and the content property filled:

{
      nodeName: 'template',
      tagName: 'template',
      attrs: [Array],
      namespaceURI: 'http://www.w3.org/1999/xhtml',
      childNodes: [],
      parentNode: [Circular *1],
      content: [Object]
}

@paed01
Copy link

paed01 commented Oct 27, 2022

Any progress with this issue?

@fb55
Copy link
Member

fb55 commented Oct 31, 2022

No progress. This is an issue, and patches for css-select are welcome.

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

3 participants