Re: [svgwg] Revert change from SVGRect/SVGPoint/SVGMatrix to DOM equivalents (#706)

First I want to say that I support the current SVG 2 plans regarding using DOM geometry interfaces. I do not like putting burden on the web developer by exposing many similar data structures with different names and with minor differences, e.g. DOMPointInit, DOMPointReadOnly, DOMPoint and SVGPoint. If we can get rid of one of them, let's do it.

I would suggest the following if we want to have the DOM geometry interfaces alive. These interfaces should be explicit about what object owns them. A new interface should be defined for the "data owner" or the "object parent" or whatever, say we name it DOMDataOwner. The DOMDataOwner is not necessarily a Node or Element. DOMPoint, DOMRect and DOMMatrix should have a pointer to DOMDataOwner. This pointer can be null which means the object is detached. All the objects will be detached except the ones that are attached to SVGElements or SVG DOMDataOwner. So there should not be a performance issue for non the SVG case.

In C++, DOMDataOwner can be defined like this:
```
class DOMDataOwner {
public:
    virtual DOMDataOwner* owner() const { return nullptr; }
    virtual void commitChange()
    {
        if (!owner())
            return;
        owner()->commitChange();
    }
};
```

Let's consider this JavaScript code as an example of when we need to need to propagate a change through multiple DOMDataOwners to be committed to the Element attribute:
```
var polylineElement = document.createElementNS("http://www.w3.org/2000/svg", "polyline");
var animatedPoints = polylineElement.points;
var points = animatedPoints.baseVal;
var point = points.appendItem(new DOMPoint(0, 0));
point.setX(100);
console.log(polylineElement.getAttribute("points")); // This should print 100, 0
```
So in this example:

1. The owner of 'point' is 'points' i.e the owner of DOMPoint is SVGPointList.
2. The owner of 'points' is 'animatedPoints' i.e the owner of SVGPointList is SVGAnimatedPointList
3. The owner of 'animatedPoints' is 'polylineElement' i.e the owner SVGAnimatedPointList is SVGPolylineElement.

To have this working this way, the following changes can be made to the C++ classes declarations:

```
class DOMPoint final : public DOMPointReadOnly {
private:
    DOMDataOwner* m_owner { nullptr };
};

class SVGPointList final : public DOMDataOwner {
{
    DOMDataOwner* owner() const override { return m_owner; }
    void commitChange() override;
private:
    DOMDataOwner* m_owner { nullptr };
};

class SVGAnimatedPointList : public DOMDataOwner {
    DOMDataOwner* owner() const override { return m_element; }
private:
    SVGElement* m_element { nullptr };
};

class SVGElement : public DOMDataOwner {
{
    void commitChange() override;
};
```
And here is simplified C++ code that can make a change in DOMPoint be propagated to SVGPointList, SVGAnimatedPointList and SVGPolylineElement without having to have a virtual function in DOMPoint:

```
void DOMPoint::setX(double x)
{ 
    m_x = x;
    if (m_owner)
        m_owner->commitChange();
}

void SVGPointList::commitChange()
{
    setDirty(true);
    DOMDataOwner::commitChange();
}

void SVGElement::commitChange()
{
    for (auto& property : m_properties) {
        if (property.isDirty())
            Element::setAttribute(attributeName(property), property.valueAsString());
    }
}
```
DOMPoint, SVGPointList SVGAnimatedPointList and SVGElement should manage attaching to and detaching from the DOMDataOwner.

1. When an SVGElement creates an SVG property, the SVG property is attached to the SVGElement.
2. When an SVGElement is being deleted, it is detached from all its SVG properties
3. When an SVGAnimatedProperty or an SVGAnimatedPropertyList is created, its baseVal and animVal are attached to the SVGAnimatedProperty or the SVGAnimatedPropertyList.
4. When an SVGAnimatedProperty or SVGAnimatedPropertyList is being deleted, it is detached from its baseVal and animVal.
5. The specs of SVGList.appendItem, SVGList.removeItem, SVGList.replaceItem and SVGList.initialize define well how they are attached to and detached from items.
6. When an SVGList is being deleted, it is detached from all its items

So if DOMPoint, SVGPointList or SVGAnimatedPointList is detached, i.e. its owner() is null, the commitChange() will stop since there is no owner to propagate the change to.

Note: I do not know whether exposing DOMDataOwner through the IDL is beneficial or not. 

-- 
GitHub Notification of comment by shallawa
Please view or discuss this issue at https://github.com/w3c/svgwg/issues/706#issuecomment-511958964 using your GitHub account

Received on Tuesday, 16 July 2019 19:46:33 UTC