Skip to content

Commit

Permalink
Sanitize style attributes in data attribute in DrawControl.js for ups…
Browse files Browse the repository at this point in the history
…tream serialization.

- Fix for jupyter-widgets#1119

- The main issue is that a `style()` function is added to the GeoJSON object in `create_obj()`, which triggers an error at serialization time: https://github.com/jupyter-widgets/ipywidgets/blob/e1718c2b3bf0b143580ef87f71c55fbc6ed50a77/packages/base/src/widget.ts#L587

- This is because `structuredClone` throws an error for functions: https://web.dev/structured-clone/#features-and-limitations. While the article mentions drawbacks for using `JSON.parse(JSON.stringify(...))`, I believe that this component does not produce any non-primitive properties, so will remove any offending lines.

- Tested by placing a breakpoint and testing directly on the browser.
  • Loading branch information
sufyanAbbasi authored Aug 11, 2023
1 parent ddeab67 commit a6a8b6b
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion js/src/controls/DrawControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ export class LeafletDrawControlView extends control.LeafletControlView {
let newData = [];
this.feature_group.eachLayer((layer) => {
const geoJson = layer.toGeoJSON();
geoJson.properties.style = layer.options;
// Sanitize layer options for serialization via `structuredClone`:
// https://web.dev/structured-clone/#features-and-limitations
const sanitizedLayerOptions = JSON.parse(JSON.stringify(layer.options));
geoJson.properties.style = sanitizedLayerOptions;
newData.push(geoJson);
});
this.model.set('data', newData);
Expand Down

0 comments on commit a6a8b6b

Please sign in to comment.