Don't mutate children on DomNode (flutter/engine#33818)

* https://github.com/flutter/flutter/issues/104972

Don't mutate the children of a DOM node directly.

* nextSibling should be nullable.

* Remove redundant firstChild/lastChild fields.

* Update to parentNode instead of parentElement.
This commit is contained in:
Jackson Gardner
2022-06-06 13:09:46 -07:00
committed by GitHub
parent 7967ddaf1e
commit 7977f073a6
3 changed files with 66 additions and 6 deletions

View File

@@ -260,9 +260,11 @@ class HtmlViewEmbedder {
DomElement platformView,
DomElement headClipView,
) {
int indexInFlutterView = -1;
if (headClipView.parent != null) {
indexInFlutterView = skiaSceneHost!.children.indexOf(headClipView);
DomNode? headClipViewNextSibling;
bool headClipViewWasAttached = false;
if (headClipView.parentNode != null) {
headClipViewWasAttached = true;
headClipViewNextSibling = headClipView.nextSibling;
headClipView.remove();
}
DomElement head = platformView;
@@ -282,8 +284,8 @@ class HtmlViewEmbedder {
head.remove();
// If the chain was previously attached, attach it to the same position.
if (indexInFlutterView > -1) {
skiaSceneHost!.children.insert(indexInFlutterView, head);
if (headClipViewWasAttached) {
skiaSceneHost!.insertBefore(head, headClipViewNextSibling);
}
return head;
}
@@ -732,7 +734,12 @@ class HtmlViewEmbedder {
/// Deletes SVG clip paths, useful for tests.
void debugCleanupSvgClipPaths() {
_svgPathDefs?.children.single.children.forEach(removeElement);
final DomElement? parent = _svgPathDefs?.children.single;
if (parent != null) {
for (DomNode? child = parent.lastChild; child != null; child = parent.lastChild) {
parent.removeChild(child);
}
}
_svgClipDefs.clear();
}

View File

@@ -149,6 +149,7 @@ extension DomNodeExtension on DomNode {
DomElement? get parent => js_util.getProperty(this, 'parentElement');
String? get text => js_util.getProperty(this, 'textContent');
external DomNode? get parentNode;
external DomNode? get nextSibling;
external DomNode insertBefore(DomNode newNode, DomNode? referenceNode);
void remove() {
if (parentNode != null) {

View File

@@ -170,6 +170,58 @@ void testMain() {
return chain;
}
test('correctly offsets when clip chain length is changed', () async {
ui.platformViewRegistry.registerViewFactory(
'test-platform-view',
(int viewId) => html.DivElement()..id = 'view-0',
);
await createPlatformView(0, 'test-platform-view');
final EnginePlatformDispatcher dispatcher =
ui.window.platformDispatcher as EnginePlatformDispatcher;
LayerSceneBuilder sb = LayerSceneBuilder();
sb.pushOffset(3, 3);
sb.pushClipRect(ui.Rect.largest);
sb.pushOffset(6, 6);
sb.addPlatformView(0, width: 10, height: 10);
sb.pop();
dispatcher.rasterizer!.draw(sb.build().layerTree);
// Transformations happen on the slot element.
html.Element slotHost =
flutterViewEmbedder.sceneElement!.querySelector('flt-platform-view-slot')!;
expect(
getTransformChain(slotHost),
<String>[
'matrix(1, 0, 0, 1, 6, 6)',
'matrix(1, 0, 0, 1, 3, 3)',
],
);
sb = LayerSceneBuilder();
sb.pushOffset(3, 3);
sb.pushClipRect(ui.Rect.largest);
sb.pushOffset(6, 6);
sb.pushClipRect(ui.Rect.largest);
sb.pushOffset(9, 9);
sb.addPlatformView(0, width: 10, height: 10);
dispatcher.rasterizer!.draw(sb.build().layerTree);
// Transformations happen on the slot element.
slotHost =
flutterViewEmbedder.sceneElement!.querySelector('flt-platform-view-slot')!;
expect(
getTransformChain(slotHost),
<String>[
'matrix(1, 0, 0, 1, 9, 9)',
'matrix(1, 0, 0, 1, 6, 6)',
'matrix(1, 0, 0, 1, 3, 3)',
],
);
});
test('converts device pixels to logical pixels (no clips)', () async {
window.debugOverrideDevicePixelRatio(4);
ui.platformViewRegistry.registerViewFactory(