-
Notifications
You must be signed in to change notification settings - Fork 585
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
Fix broken angular block #1705
Open
armgjoka
wants to merge
3
commits into
BuilderIO:main
Choose a base branch
from
armgjoka:eng-8499
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+159
−1
Open
Fix broken angular block #1705
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@builder.io/mitosis': patch | ||
--- | ||
|
||
[Angular] Update innerHtml parsing to escape quotes |
137 changes: 137 additions & 0 deletions
137
packages/core/src/__tests__/custom-code-quote-conversion-angular.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { BuilderElement } from '@builder.io/sdk'; | ||
import { componentToAngular } from '../generators/angular'; | ||
import { blockToAngular } from '../generators/angular/blocks'; | ||
import { createMitosisNode } from '../helpers/create-mitosis-node'; | ||
import { builderElementToMitosisNode } from '../parsers/builder'; | ||
import { compileAwayBuilderComponents } from '../plugins/compile-away-builder-components'; | ||
import { MitosisComponent } from '../types/mitosis-component'; | ||
|
||
describe('CustomCode component with double quotes in Angular', () => { | ||
test('should properly convert double quotes when transforming from Builder to Angular', () => { | ||
// Create a Builder CustomCode element with HTML containing double quotes | ||
const builderElement: BuilderElement = { | ||
'@type': '@builder.io/sdk:Element', | ||
'@version': 2, | ||
id: 'builder-8e8834315d504381ad92024148b9a924', | ||
component: { | ||
name: 'Custom Code', | ||
options: { | ||
code: '<link href="https://fonts.googleapis.com/css2?family=Inter&display=swap" rel="stylesheet">\n <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js" defer></script>\n <script src="https://unpkg.com/@tailwindcss/browser@4"></script>', | ||
}, | ||
}, | ||
}; | ||
|
||
// Step 1: Convert Builder element to Mitosis node | ||
const mitosisNode = builderElementToMitosisNode(builderElement, { | ||
includeBuilderExtras: true, | ||
preserveTextBlocks: true, | ||
}); | ||
|
||
// Step 2: Create a Mitosis component containing the node | ||
const mitosisComponent: MitosisComponent = { | ||
'@type': '@builder.io/mitosis/component', | ||
name: 'TestComponent', | ||
meta: {}, | ||
imports: [], | ||
exports: {}, | ||
state: {}, | ||
refs: {}, | ||
hooks: { | ||
onMount: [], | ||
onEvent: [], | ||
}, | ||
context: { | ||
get: {}, | ||
set: {}, | ||
}, | ||
props: {}, | ||
inputs: [], | ||
subComponents: [], | ||
children: [mitosisNode], | ||
}; | ||
|
||
// Step 3: Apply the compileAwayBuilderComponents plugin to transform CustomCode | ||
const plugin = compileAwayBuilderComponents(); | ||
const transformedComponent = { ...mitosisComponent }; | ||
const pluginInstance = plugin(); | ||
if (pluginInstance.json && pluginInstance.json.pre) { | ||
pluginInstance.json.pre(transformedComponent); | ||
} | ||
|
||
// Step 4: Generate Angular code | ||
const angularCode = componentToAngular({ | ||
typescript: true, | ||
})({ component: transformedComponent }); | ||
|
||
// Step 5: Verify that double quotes in the innerHTML are properly escaped to single quotes | ||
expect(angularCode).toContain( | ||
"href='https://fonts.googleapis.com/css2?family=Inter&display=swap'", | ||
); | ||
expect(angularCode).not.toContain('href="https://fonts.googleapis.com'); | ||
|
||
// Make sure all script tags are using single quotes | ||
expect(angularCode).toContain( | ||
"src='https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js'", | ||
); | ||
expect(angularCode).toContain("src='https://unpkg.com/@tailwindcss/browser@4'"); | ||
}); | ||
|
||
// Adding a test specifically for innerHTML as a property | ||
test('should properly handle innerHTML as a direct property', () => { | ||
// HTML with double quotes that needs proper handling | ||
const htmlCode = | ||
'<div class="test-class" id="test-id"><p>Text with "quoted" content</p>' + | ||
'<script src="https://example.com/script.js"></script></div>'; | ||
|
||
// Create a Mitosis node with innerHTML as a property directly | ||
const mitosisNode = createMitosisNode({ | ||
name: 'div', | ||
properties: { | ||
innerHTML: htmlCode, | ||
}, | ||
}); | ||
|
||
const component: MitosisComponent = { | ||
'@type': '@builder.io/mitosis/component', | ||
name: 'InnerHTMLPropertyTest', | ||
meta: {}, | ||
imports: [], | ||
exports: {}, | ||
state: {}, | ||
refs: {}, | ||
hooks: { | ||
onMount: [], | ||
onEvent: [], | ||
}, | ||
context: { | ||
get: {}, | ||
set: {}, | ||
}, | ||
props: {}, | ||
inputs: [], | ||
subComponents: [], | ||
children: [mitosisNode], | ||
}; | ||
|
||
// Generate Angular template | ||
const template = blockToAngular({ | ||
root: component, | ||
json: mitosisNode, | ||
options: { typescript: true }, | ||
blockOptions: { sanitizeInnerHTML: false }, | ||
}); | ||
|
||
console.log('PROPERTY INNERHTML TEMPLATE:', template); | ||
|
||
// Verify quotes are properly handled | ||
expect(template).toContain("sanitizer.bypassSecurityTrustHtml('"); | ||
expect(template).toContain("class='test-class'"); | ||
expect(template).toContain("id='test-id'"); | ||
expect(template).toContain("src='https://example.com/script.js'"); | ||
expect(template).not.toContain('class="test-class"'); | ||
expect(template).not.toContain('id="test-id"'); | ||
|
||
// The generated Angular code should not have nested double quotes | ||
expect(template).not.toMatch(/="[^"]*"[^"]*"/); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in general, we have the approach of using
expect(template).toMatchInlineSnapshot()
so that we can notice any surprising changes across all tests when new PRs are made.If you don't mind adding inline snapshots for relevant objects. See https://github.com/BuilderIO/mitosis/blob/main/packages/core/src/__tests__/builder.test.ts for examples of how we use it.
oyou just write
toMatchInlineSnapshot()
, and once you run the tests it will populate it. See https://vitest.dev/guide/snapshot#inline-snapshots