Skip to content

Commit c3005e3

Browse files
committed
fix: internal live preview editing styles leak on users source code
1 parent 97e56ad commit c3005e3

File tree

1 file changed

+54
-4
lines changed

1 file changed

+54
-4
lines changed

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,7 +1773,21 @@ function RemoteFunctions(config) {
17731773
len = elem.attributes.length;
17741774
for (i = 0; i < len; i++) {
17751775
node = elem.attributes.item(i);
1776+
1777+
// skip internal attributes that shouldn't be serialized
1778+
if (node.name === "draggable" && node.value === "true") {
1779+
continue;
1780+
}
17761781
value = (node.name === "data-brackets-id") ? parseInt(node.value, 10) : node.value;
1782+
// Clean internal style properties
1783+
if (node.name === "style") {
1784+
const cleanedStyle = _cleanInternalStyles(value);
1785+
1786+
if (cleanedStyle.trim() === '') {
1787+
continue; // Skip empty style attribute
1788+
}
1789+
value = cleanedStyle;
1790+
}
17771791
json.attributes[node.name] = value;
17781792
}
17791793

@@ -1834,7 +1848,7 @@ function RemoteFunctions(config) {
18341848
element.setAttribute("contenteditable", "true");
18351849
element.focus();
18361850

1837-
element._originalContent = element.innerHTML;
1851+
element._originalContent = cleanupElementProperties(element);
18381852

18391853
// Add event listeners for editing
18401854
function onBlur() {
@@ -1864,6 +1878,30 @@ function RemoteFunctions(config) {
18641878
};
18651879
}
18661880

1881+
// Helper function to clean internal style properties
1882+
function _cleanInternalStyles(styleValue) {
1883+
if (typeof styleValue !== "string") {
1884+
return styleValue;
1885+
}
1886+
1887+
let cleanedStyle = styleValue;
1888+
1889+
// remove internal background color
1890+
cleanedStyle = cleanedStyle.replace(/background-color:\s*rgba\(0,\s*162,\s*255,\s*0\.2\)\s*;?\s*/gi, "");
1891+
1892+
// remove internal outline
1893+
cleanedStyle = cleanedStyle.replace(/outline:\s*rgb\(66,\s*133,\s*244\)\s+solid\s+1px\s*;?\s*/gi, "");
1894+
cleanedStyle = cleanedStyle.replace(/outline:\s*1px\s+solid\s+#4285F4\s*;?\s*/gi, "");
1895+
1896+
// clean up any extra spaces or semicolons
1897+
cleanedStyle = cleanedStyle
1898+
.replace(/;\s*;/g, ";")
1899+
.replace(/^\s*;\s*/, "")
1900+
.replace(/\s*;\s*$/, "");
1901+
1902+
return cleanedStyle;
1903+
}
1904+
18671905
// this function is to remove the internal properties from elements before getting the innerHTML
18681906
// then add all the properties back to the elements
18691907
// internal properties such as 'data-brackets-id', 'data-ld-highlight' etc
@@ -1880,9 +1918,21 @@ function RemoteFunctions(config) {
18801918
el.removeAttribute('data-ld-highlight');
18811919
}
18821920

1883-
// Remove empty style attribute
1884-
if (el.hasAttribute('style') && el.getAttribute('style').trim() === '') {
1885-
el.removeAttribute('style');
1921+
// remove the draggable attribute added internally
1922+
if (el.hasAttribute('draggable')) {
1923+
el.removeAttribute('draggable');
1924+
}
1925+
1926+
// remove internal style properties
1927+
if (el.hasAttribute('style')) {
1928+
const style = el.getAttribute('style');
1929+
const cleanedStyle = _cleanInternalStyles(style);
1930+
1931+
if (cleanedStyle.trim() === '') {
1932+
el.removeAttribute('style');
1933+
} else {
1934+
el.setAttribute('style', cleanedStyle);
1935+
}
18861936
}
18871937
});
18881938

0 commit comments

Comments
 (0)