-
Notifications
You must be signed in to change notification settings - Fork 979
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[angular-gen2]: embed and custom code block and text block angular sanitizing html warning #3809
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@builder.io/sdk-angular": patch | ||
--- | ||
|
||
Fix: mark text as safe to remove the Angular stripping/warning message |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@builder.io/sdk-angular': patch | ||
--- | ||
|
||
Fix: Embed and Custom Code block to support embedding iframes |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
export const EMBED_AND_CUSTOM_CODE = { | ||
ownerId: 'ad30f9a246614faaa6a03374f83554c9', | ||
lastUpdateBy: null, | ||
createdDate: 1735908946457, | ||
id: '70b6e19247c94038a1325afa0a3b3b70', | ||
'@version': 4, | ||
name: 'embed-angular', | ||
modelId: '17c6065109ef4062ba083f5741f4ee6a', | ||
published: 'draft', | ||
meta: { | ||
hasLinks: false, | ||
lastPreviewUrl: | ||
'http://localhost:4200/embed-angular?builder.space=ad30f9a246614faaa6a03374f83554c9&builder.user.permissions=read%2Ccreate%2Cpublish%2CeditCode%2CeditDesigns%2Cadmin%2CeditLayouts%2CeditLayers%2CeditContentPriority&builder.user.role.name=Admin&builder.user.role.id=admin&builder.cachebust=true&builder.preview=page&builder.noCache=true&builder.allowTextEdit=true&__builder_editing__=true&builder.overrides.page=70b6e19247c94038a1325afa0a3b3b70&builder.overrides.70b6e19247c94038a1325afa0a3b3b70=70b6e19247c94038a1325afa0a3b3b70&builder.overrides.page:/embed-angular=70b6e19247c94038a1325afa0a3b3b70&builder.options.locale=Default', | ||
kind: 'page', | ||
}, | ||
priority: -925, | ||
query: [ | ||
{ | ||
'@type': '@builder.io/core:Query', | ||
property: 'urlPath', | ||
operator: 'is', | ||
value: '/embed-angular', | ||
}, | ||
], | ||
data: { | ||
inputs: [], | ||
title: 'embed-angular', | ||
themeId: false, | ||
blocks: [ | ||
{ | ||
'@type': '@builder.io/sdk:Element', | ||
'@version': 2, | ||
id: 'builder-c8f3819ed1b64478a7a2ff41ae1aa458', | ||
component: { | ||
name: 'Custom Code', | ||
options: { | ||
code: '<div style="left: 0; width: 100%; height: 0; position: relative; padding-bottom: 56.25%;"><iframe src="https://www.youtube.com/embed/oU3H581uCsA?rel=0" style="top: 0; left: 0; width: 100%; height: 100%; position: absolute; border: 0;" allowfullscreen scrolling="no" allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share;"></iframe></div>', | ||
scriptsClientOnly: false, | ||
}, | ||
}, | ||
responsiveStyles: { | ||
large: { | ||
display: 'flex', | ||
flexDirection: 'column', | ||
position: 'relative', | ||
flexShrink: '0', | ||
boxSizing: 'border-box', | ||
marginTop: '20px', | ||
}, | ||
}, | ||
}, | ||
{ | ||
'@type': '@builder.io/sdk:Element', | ||
'@version': 2, | ||
id: 'builder-307d260eeb2f465980c6e8c4d0a0af11', | ||
component: { | ||
name: 'Embed', | ||
options: { | ||
url: 'https://www.youtube.com/watch?v=oU3H581uCsA', | ||
content: | ||
'<div style="left: 0; width: 100%; height: 0; position: relative; padding-bottom: 56.25%;"><iframe src="https://www.youtube.com/embed/oU3H581uCsA?rel=0" style="top: 0; left: 0; width: 100%; height: 100%; position: absolute; border: 0;" allowfullscreen scrolling="no" allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share;"></iframe></div>', | ||
}, | ||
}, | ||
responsiveStyles: { | ||
large: { | ||
display: 'flex', | ||
flexDirection: 'column', | ||
position: 'relative', | ||
flexShrink: '0', | ||
boxSizing: 'border-box', | ||
marginTop: '20px', | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
metrics: { | ||
clicks: 0, | ||
impressions: 0, | ||
}, | ||
variations: {}, | ||
lastUpdated: 1736507314861, | ||
testRatio: 1, | ||
createdBy: 'RuGeCLr9ryVt1xRazFYc72uWwIK2', | ||
lastUpdatedBy: 'RuGeCLr9ryVt1xRazFYc72uWwIK2', | ||
folders: [], | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -718,6 +718,55 @@ const ANGULAR_COMPONENT_REF_UPDATE_TEMPLATE_SSR = () => ({ | |
}, | ||
}); | ||
|
||
const ANGULAR_MARK_SAFE_INNER_HTML = () => ({ | ||
code: { | ||
post: (code) => { | ||
if ( | ||
code.includes('selector: "builder-text"') || | ||
code.includes('selector: "builder-embed"') || | ||
code.includes('selector: "custom-code"') | ||
) { | ||
code = | ||
`import { DomSanitizer } from "@angular/platform-browser";\nimport { ChangeDetectionStrategy } from "@angular/core";\n` + | ||
code; | ||
|
||
// add changeDetection: ChangeDetectionStrategy.OnPush | ||
const changeDetectionIndex = code.indexOf('selector:'); | ||
if (changeDetectionIndex !== -1) { | ||
code = | ||
code.slice(0, changeDetectionIndex) + | ||
`changeDetection: ChangeDetectionStrategy.OnPush,\n` + | ||
code.slice(changeDetectionIndex); | ||
} | ||
|
||
// add constructor with sanitizer | ||
const constructorIndex = code.indexOf('constructor'); | ||
if (constructorIndex === -1) { | ||
// not found | ||
const ngOnChangesIndex = code.indexOf('ngOnChanges'); | ||
code = | ||
code.slice(0, ngOnChangesIndex) + | ||
`constructor(protected sanitizer: DomSanitizer) {}\n` + | ||
code.slice(ngOnChangesIndex); | ||
} else { | ||
throw new Error( | ||
'constructor found which should not be here. If you see this, please fix the ANGULAR_TEXT_MARK_SAFE_HTML Plugin.' | ||
); | ||
} | ||
|
||
const variableName = code.match(/\[innerHTML\]="([^"]+)"/)?.[1]; | ||
if (variableName) { | ||
code = code.replace( | ||
`[innerHTML]="${variableName}"`, | ||
`[innerHTML]="sanitizer.bypassSecurityTrustHtml(${variableName})"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @samijaber this was needed for Embed and Custom Code block as well so I extended this PR to fix them too There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. long-term I think this should be baked into mitosis: the angular generator should always use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. great idea, i've created a ticket for it |
||
); | ||
} | ||
} | ||
return code; | ||
}, | ||
}, | ||
}); | ||
|
||
/** | ||
* @type {MitosisConfig} | ||
*/ | ||
|
@@ -746,6 +795,7 @@ module.exports = { | |
ANGULAR_ADD_UNUSED_PROP_TYPES, | ||
ANGULAR_NOWRAP_INTERACTIVE_ELEMENT_PLUGIN, | ||
ANGULAR_COMPONENT_REF_UPDATE_TEMPLATE_SSR, | ||
ANGULAR_MARK_SAFE_INNER_HTML, | ||
], | ||
}, | ||
solid: { | ||
|
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.
We now pass
json
as the 2nd arg forcost
plugins: https://github.com/BuilderIO/mitosis/blob/f3fabea5d912662e5e6454fd0f9088cb98c72f65/packages/core/src/modules/plugins.ts#L44-L84This lets you do cleaner checks, like
['Text', 'Embed', 'CustomCode'].includes(json.name)
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.
done!