Skip to content

Commit

Permalink
VC-3511 Add a notification to settings page on document.write
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitahl committed Sep 10, 2024
1 parent 9d7830e commit bce5a67
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
16 changes: 16 additions & 0 deletions public/components/wpVcSettings/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ window.ResizeSensor = ResizeSensorModule
const dataManager = getService('dataManager')
const localizations = dataManager.get('localizations')
const unsavedChangesText = localizations && localizations.unsavedChangesText ? localizations.unsavedChangesText : 'Changes may not be saved.'
const documentWriteWarning = localizations && localizations.documentWriteWarningSettings ? localizations.documentWriteWarningSettings : 'Avoiding using document.write as per MDN documentation. This may result in editor not working properly.'

export const dashboard = () => {
const dashboardContainer = document.querySelector('.vcv-settings')
Expand Down Expand Up @@ -108,6 +109,12 @@ export const dashboard = () => {
}
const submitButtonContainer = e.target.querySelector('.vcv-submit-button-container')
const submitButton = e.target.querySelector('.vcv-dashboard-button--save')
if (submitButton.id === 'submit_btn-vcv-global-css-js') {
const isValid = isValidEditorsValue()
if (!isValid) {
alert(documentWriteWarning)
}
}
// this will get all form fields and encode it as a string
const data = Array.from(
new window.FormData(e.target),
Expand Down Expand Up @@ -156,6 +163,15 @@ export const dashboard = () => {
window.jQuery(dataCollectionTableWrapper).slideToggle()
}

const isValidEditorsValue = () => {
const regex = /document\.write\s*\(/
const headerEditorValue = window.vcvGlobalJsHeadEditor.getValue().trim()
const footerEditorValue = window.vcvGlobalJsFooterEditor.getValue().trim()
const isValidHeadeEditor = headerEditorValue ? !regex.test(headerEditorValue) : true
const isValidFooterEditor = footerEditorValue ? !regex.test(footerEditorValue) : true
return isValidFooterEditor && isValidHeadeEditor
}

if (dataCollectionTableWrapper) {
if (urlHash.indexOf(dataCollectionTableWrapper.id) !== -1) {
dataCollectionTableWrapper.style.display = 'block'
Expand Down
8 changes: 4 additions & 4 deletions public/components/wpVcSettings/editors.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ export const initEditors = () => {
*/
const globalJsHead = document.querySelector('#vcv-settingsGlobalJsHead')
if (globalJsHead !== null) {
const globalJsHeadEditor = codeEditor.getEditor(globalJsHead, 'text/html', globalJsHead.value)
globalJsHeadEditor.on('change', async () => {
window.vcvGlobalJsHeadEditor = codeEditor.getEditor(globalJsHead, 'text/html', globalJsHead.value)
window.vcvGlobalJsHeadEditor.on('change', async () => {
setStatus('ready')
})
}

const globalJsFooter = document.querySelector('#vcv-settingsGlobalJsFooter')
if (globalJsFooter !== null) {
const globalJsFooterEditor = codeEditor.getEditor(globalJsFooter, 'text/html', globalJsFooter.value)
globalJsFooterEditor.on('change', async () => {
window.vcvGlobalJsFooterEditor = codeEditor.getEditor(globalJsFooter, 'text/html', globalJsFooter.value)
window.vcvGlobalJsFooterEditor.on('change', async () => {
setStatus('ready')
})
}
Expand Down
4 changes: 4 additions & 0 deletions visualcomposer/Helpers/Localizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2295,6 +2295,10 @@ public function getLocalizations()
'Warning: Custom JavaScript won\'t be saved. Avoid using document.write() as it can break the editor. Please refer to the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/write" target="_blank" rel="noopener noreferrer">MDN documentation</a> for more info.',
'visualcomposer'
),
'documentWriteWarningSettings' => __(
'Avoiding using document.write as per MDN documentation. This may result in editor not working properly.',
'visualcomposer'
),
];

return vcfilter('vcv:helpers:localizations:i18n', $locale);
Expand Down

0 comments on commit bce5a67

Please sign in to comment.