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

querySelectorAll doesn't match elements within a <script> tag #282

Closed
zedtux opened this issue Oct 10, 2024 · 1 comment
Closed

querySelectorAll doesn't match elements within a <script> tag #282

zedtux opened this issue Oct 10, 2024 · 1 comment

Comments

@zedtux
Copy link

zedtux commented Oct 10, 2024

Given an Angular HTML template being something like the following:

<div class="card">
  <div ng-include="'app/templates/_something.html'"></div>

  <div ng-include="'my_template.html'"></div>

  <script type="text/ng-template id="my_template.html">
    <div class="card">
      <div class="card-body">
        <div ng-include="'app/templates/_filters.html'"></div>
      </div>
    </div>
  </script>
</div>

When I try to match all the ng-include from that HTML code using the following JavaScript code:

const { parse } = require('node-html-parser');

const angularHtmlTemplate = `<div class="card">
  <div ng-include="'app/templates/_something.html'"></div>

  <div ng-include="'my_template.html'"></div>

  <script type="text/ng-template id="my_template.html">
    <div class="card">
      <div class="card-body">
        <div ng-include="'app/templates/_filters.html'"></div>
      </div>
    </div>
  </script>
</div>`

const parsedContent = parse(angularHtmlTemplate)

const ngIncludes = parsedContent.querySelectorAll("*[ng-include]")
console.log('ngIncludes are:')
ngIncludes.forEach((ngInclude) => console.log(ngInclude.getAttribute('ng-include')))

This show me only the 2 first ng-include, the one included within the script template is ignored:

ngIncludes are:
'app/templates/_something.html'
'my_template.html'

While if I change the <script> tag as <div> tag, querySelectorAll returns 3 items as expected.

@taoqf
Copy link
Owner

taoqf commented Dec 26, 2024

try this:

const parsedContent = parse(angularHtmlTemplate, {
	blockTextElements: {}
});

@taoqf taoqf closed this as completed Dec 26, 2024
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

2 participants