Skip to content

Commit

Permalink
fix: make style relies on element.
Browse files Browse the repository at this point in the history
  • Loading branch information
andycall committed Nov 24, 2021
1 parent 71020d2 commit 89228e7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
5 changes: 1 addition & 4 deletions bridge/bindings/qjs/dom/element.cc
Original file line number Diff line number Diff line change
Expand Up @@ -593,10 +593,7 @@ JSClassID ElementInstance::classID() {
return Element::classId();
}

ElementInstance::~ElementInstance() {
// Should reset weak reference between style and element.
m_style->ownerEventTarget = nullptr;
}
ElementInstance::~ElementInstance() {}

JSValue ElementInstance::internalGetTextContent() {
JSValue array = JS_NewArray(m_ctx);
Expand Down
10 changes: 10 additions & 0 deletions bridge/bindings/qjs/dom/style_declaration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ JSValue CSSStyleDeclaration::getPropertyValue(QjsContext* ctx, JSValue this_val,
return returnValue;
}

StyleDeclarationInstance::StyleDeclarationInstance(CSSStyleDeclaration* cssStyleDeclaration, EventTargetInstance* ownerEventTarget)
: Instance(cssStyleDeclaration, "CSSStyleDeclaration", &m_exoticMethods, CSSStyleDeclaration::kCSSStyleDeclarationClassId, finalize), ownerEventTarget(ownerEventTarget) {
JS_DupValue(m_ctx, ownerEventTarget->instanceObject);
}
StyleDeclarationInstance::~StyleDeclarationInstance() {}

bool StyleDeclarationInstance::internalSetProperty(std::string& name, JSValue value) {
Expand Down Expand Up @@ -214,4 +218,10 @@ JSClassExoticMethods StyleDeclarationInstance::m_exoticMethods{
nullptr, nullptr, nullptr, nullptr, nullptr, getProperty, setProperty,
};

void StyleDeclarationInstance::gcMark(JSRuntime* rt, JSValue val, JS_MarkFunc* mark_func) {
Instance::gcMark(rt, val, mark_func);
// We should tel gc style relies on element
JS_MarkValue(rt, ownerEventTarget->instanceObject, mark_func);
}

} // namespace kraken::binding::qjs
5 changes: 3 additions & 2 deletions bridge/bindings/qjs/dom/style_declaration.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,16 @@ class CSSStyleDeclaration : public HostClass {
class StyleDeclarationInstance : public Instance {
public:
StyleDeclarationInstance() = delete;
explicit StyleDeclarationInstance(CSSStyleDeclaration* cssStyleDeclaration, EventTargetInstance* ownerEventTarget)
: Instance(cssStyleDeclaration, "CSSStyleDeclaration", &m_exoticMethods, CSSStyleDeclaration::kCSSStyleDeclarationClassId, finalize), ownerEventTarget(ownerEventTarget){};
explicit StyleDeclarationInstance(CSSStyleDeclaration* cssStyleDeclaration, EventTargetInstance* ownerEventTarget);
~StyleDeclarationInstance();
bool internalSetProperty(std::string& name, JSValue value);
void internalRemoveProperty(std::string& name);
JSValue internalGetPropertyValue(std::string& name);
std::string toString();
void copyWith(StyleDeclarationInstance* instance);

void gcMark(JSRuntime* rt, JSValue val, JS_MarkFunc* mark_func) override;

const EventTargetInstance* ownerEventTarget;

private:
Expand Down

0 comments on commit 89228e7

Please sign in to comment.