Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/text node value #279

Merged
merged 3 commits into from May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions bridge/bindings/jsc/DOM/text_node.cc
Expand Up @@ -9,7 +9,7 @@ namespace kraken::binding::jsc {

void bindTextNode(std::unique_ptr<JSContext> &context) {
auto textNode = JSTextNode::instance(context.get());
JSC_GLOBAL_SET_PROPERTY(context, "TextNode", textNode->classObject);
JSC_GLOBAL_SET_PROPERTY(context, "Text", textNode->classObject);
}

std::unordered_map<JSContext *, JSTextNode *> JSTextNode::instanceMap{};
Expand All @@ -24,7 +24,7 @@ JSTextNode::~JSTextNode() {
instanceMap.erase(context);
}

JSTextNode::JSTextNode(JSContext *context) : JSNode(context, "TextNode") {}
JSTextNode::JSTextNode(JSContext *context) : JSNode(context, "Text") {}

JSObjectRef JSTextNode::instanceConstructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount,
const JSValueRef *arguments, JSValueRef *exception) {
Expand Down Expand Up @@ -53,6 +53,7 @@ JSValueRef JSTextNode::TextNodeInstance::getProperty(std::string &name, JSValueR

auto property = propertyMap[name];
switch (property) {
case TextNodeProperty::nodeValue:
case TextNodeProperty::textContent:
case TextNodeProperty::data: {
return m_data.makeString();
Expand All @@ -65,7 +66,7 @@ JSValueRef JSTextNode::TextNodeInstance::getProperty(std::string &name, JSValueR
}

bool JSTextNode::TextNodeInstance::setProperty(std::string &name, JSValueRef value, JSValueRef *exception) {
if (name == "data") {
if (name == "data" || name == "nodeValue") {
JSStringRef data = JSValueToStringCopy(_hostClass->ctx, value, exception);
m_data.setString(data);

Expand Down
2 changes: 1 addition & 1 deletion bridge/bindings/jsc/DOM/text_node.h
Expand Up @@ -25,7 +25,7 @@ class JSTextNode : public JSNode {

class TextNodeInstance : public NodeInstance {
public:
DEFINE_OBJECT_PROPERTY(TextNode, 3, data, textContent, nodeName)
DEFINE_OBJECT_PROPERTY(TextNode, 4, data, textContent, nodeValue, nodeName)

TextNodeInstance() = delete;
~TextNodeInstance();
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions integration_tests/specs/dom/nodes/textNode.ts
Expand Up @@ -108,4 +108,16 @@ describe('TextNode', () => {

await snapshot();
});

describe('nodeValue', () => {
it('assign nodeValue to update.', async () => {
const text = document.createTextNode('');
document.body.appendChild(text);

const TEXT = 'HELLO WORLD!';
text.nodeValue = TEXT;
await snapshot();
expect(text.nodeValue).toEqual(TEXT);
});
});
});
2 changes: 1 addition & 1 deletion kraken/example/lib/main.dart
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:kraken/kraken.dart';
import 'dart:ui';
import 'package:kraken_websocket/kraken_websocket.dart';
import 'dart:ui';

void main() {
KrakenWebsocket.initialize();
Expand Down
13 changes: 9 additions & 4 deletions kraken/lib/src/dom/element_manager.dart
Expand Up @@ -295,10 +295,10 @@ class ElementManager implements WidgetsBindingObserver, ElementsBindingObserver
assert(target != null);

if (target is Element) {
// Only Element has properties
// Only Element has properties.
target.setProperty(key, value);
} else if (target is TextNode && key == 'data') {
target.data = value;
} else if (target is TextNode && key == 'data' || key == 'nodeValue') {
(target as TextNode).data = value;
} else {
debugPrint('Only element has properties, try setting $key to Node(#$targetId).');
}
Expand All @@ -312,8 +312,11 @@ class ElementManager implements WidgetsBindingObserver, ElementsBindingObserver
if (target is Element) {
// Only Element has properties
return target.getProperty(key);
} else if (target is TextNode && key == 'data' || key == 'nodeValue') {
return (target as TextNode).data;
} else {
return null;
}
return null;
}

void removeProperty(int targetId, String key) {
Expand All @@ -323,6 +326,8 @@ class ElementManager implements WidgetsBindingObserver, ElementsBindingObserver

if (target is Element) {
target.removeProperty(key);
} else if (target is TextNode && key == 'data' || key == 'nodeValue') {
(target as TextNode).data = '';
} else {
debugPrint('Only element has properties, try removing $key from Node(#$targetId).');
}
Expand Down