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

Angular-innerHTML is not rendered #1638

Closed
SpaxR opened this issue Dec 16, 2024 · 5 comments
Closed

Angular-innerHTML is not rendered #1638

SpaxR opened this issue Dec 16, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@SpaxR
Copy link

SpaxR commented Dec 16, 2024

Describe the bug
When using [innerHTML]-Directive in Angular, nothing is put into the DOM.
With JSDom or Karma the content exists in the Document.

To Reproduce
CodeSandbox

  1. Install Angular with Jest and happy-dom / @happy-dom/jest-environment
  2. Create Component. For Example: <div [innerHTML]="'TEST'"></div>
  3. Write Test that reads the HTML: expect(fixture.nativeElement.innerHTML).toContain('TEST')
  4. Run Jest
  5. div is empty: <div></div>

Expected behavior
Angular should be able to render innerHTML into the DOM

Additional context
With a real browser, karma, or jest/jsdom the innerHTML is displayed,
so i guess the Problem is somewhere between happydom and Angular.

Angular doesn't trust the Content that gets provided via innerHTML, unless it can determine that it is "safe".
It is possible to bypass the security with Angular's DomSanitizer, but that gets hard to apply to third-party libraries.


My guess is that happy-dom does something that Angular doesn't deem safe,
and that's the point where my knowledge ends and help would be much appreciated.

@SpaxR SpaxR added the bug Something isn't working label Dec 16, 2024
@OlaviSau
Copy link
Contributor

What version are you using? This issue has been fixed in the latest release.

@SpaxR
Copy link
Author

SpaxR commented Dec 19, 2024

Should be the latest Version (15.11.7): package.json
And for completeness:

// jest.config.ts
import type { Config } from 'jest';
export default <Config>{
  preset: 'jest-preset-angular',
  setupFilesAfterEnv: [`${__dirname}/tests/setup.ts`],
  testEnvironment: '@happy-dom/jest-environment',
  testEnvironmentOptions: {
    width: 1920,
    height: 1080,
  },
};

setup.ts is only setting up mocks.

I also found Tickets, that i thought were similar, but not related:
#1494 #1498 #1533

I would try to figure it out myself, but it may take a while.

@OlaviSau
Copy link
Contributor

OlaviSau commented Dec 22, 2024

I think I was mixing up the sanitizer with another issue as I have fixed it using patch-package on our project.
This PR should fix the issue, in the meanwhile you can use https://www.npmjs.com/package/patch-package to work around the issue.

--- a/node_modules/happy-dom/lib/dom-parser/DOMParser.js
+++ b/node_modules/happy-dom/lib/dom-parser/DOMParser.js
@@ -27,13 +27,16 @@ export default class DOMParser {
         const root = XMLParser.parse(newDocument, string, { evaluateScripts: true });
         let documentElement = null;
         let documentTypeNode = null;
+        let bodyElement = null;
         for (const node of root[PropertySymbol.nodeArray]) {
             if (node['tagName'] === 'HTML') {
                 documentElement = node;
             }
             else if (node[PropertySymbol.nodeType] === NodeTypeEnum.documentTypeNode) {
                 documentTypeNode = node;
-            }
+            } else if (node['tagName'] === 'BODY') {
+				bodyElement = node;
+			}
             if (documentElement && documentTypeNode) {
                 break;
             }
@@ -63,7 +66,7 @@ export default class DOMParser {
                 default:
                     {
                         const documentElement = newDocument.createElement('html');
-                        const bodyElement = newDocument.createElement('body');
+						bodyElement = bodyElement ?? newDocument.createElement('body');
                         const headElement = newDocument.createElement('head');
                         documentElement.appendChild(headElement);
                         documentElement.appendChild(bodyElement);

@SpaxR
Copy link
Author

SpaxR commented Dec 23, 2024

That did the trick! I had to make the changes in the cjs-folder, but now everything renders correctly.
Thank you for this patch!

@SpaxR SpaxR closed this as completed Dec 23, 2024
@capricorn86
Copy link
Owner

Thank you for reporting @SpaxR! 🙂

This should now be fixed in v16.0.0, so the patch should not be necessary anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants