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

Feat/support vue router #711

Merged
merged 3 commits into from
Oct 11, 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
16 changes: 15 additions & 1 deletion bridge/bindings/jsc/KOM/history.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ JSValueRef JSHistory::getProperty(std::string &name, JSValueRef *exception) {
return JSValueMakeNumber(context->context(), m_previous_stack.size() + m_next_stack.size());
} else if (name == "state") {
HistoryItem& history = m_previous_stack.top();
return JSValueMakeFromJSONString(ctx, history.state);
if (history.state == nullptr) {
return nullptr;
} else {
return JSValueMakeFromJSONString(ctx, history.state);
}
}

return HostObject::getProperty(name, exception);
Expand Down Expand Up @@ -239,9 +243,19 @@ JSValueRef JSHistory::replaceState(JSContextRef ctx, JSObjectRef function, JSObj
HistoryItem history = { JSStringCreateWithUTF8CString(Uri::toString(uri).c_str()), jsonState, false };

m_previous_stack.pop();

m_previous_stack.push(history);

return nullptr;
}

void bindHistory(std::unique_ptr<JSContext> &context) {
JSStringHolder windowKeyHolder = JSStringHolder(context.get(), "window");
JSValueRef windowValue = JSObjectGetProperty(context->context(), context->global(), windowKeyHolder.getString(), nullptr);
JSObjectRef windowObject = JSValueToObject(context->context(), windowValue, nullptr);
auto window = static_cast<WindowInstance *>(JSObjectGetPrivate(windowObject));

JSC_GLOBAL_SET_PROPERTY(context, "history", window->history_->jsObject);
}

} // namespace kraken::binding::jsc
2 changes: 2 additions & 0 deletions bridge/bindings/jsc/KOM/history.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class JSHistory : public HostObject {
JSFunctionHolder m_replaceState{context, jsObject, this, "replaceState", replaceState};
};

void bindHistory(std::unique_ptr<JSContext> &context);

} // namespace kraken::binding::jsc

#endif // KRAKENBRIDGE_HISTORY_H
51 changes: 29 additions & 22 deletions bridge/bindings/jsc/foundation.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ namespace kraken::binding::jsc {
struct Uri
{
public:
std::string QueryString, Path, Protocol, Host, Port;
std::string QueryString, Path, Protocol, Host, Port, Fragment;

static std::string toString(Uri& uri) {
return uri.Protocol + "://"
+ uri.Host
+ (uri.Port == "" ? "" : ":" + uri.Port)
+ (uri.Path.find("/") == 0 ? uri.Path : "/" + uri.Path)
+ (uri.QueryString.find("?") == 0 ? uri.QueryString : "?" + uri.QueryString);
+ (uri.QueryString.find("?") == 0 || uri.QueryString == "" ? uri.QueryString : "?" + uri.QueryString)
+ (uri.Fragment.find("#") == 0 || uri.Fragment == "" ? uri.Fragment : "#" + uri.Fragment);
}

static Uri Parse(const std::string &uri)
Expand All @@ -31,55 +32,61 @@ struct Uri
// Get query start.
iterator_t queryStart = std::find(uri.begin(), uriEnd, '?');

// Protocol.
// Get protocol.
iterator_t protocolStart = uri.begin();
// "://");.
iterator_t protocolEnd = std::find(protocolStart, uriEnd, ':');

if (protocolEnd != uriEnd)
{
if (protocolEnd != uriEnd) {
std::string prot = &*(protocolEnd);
if ((prot.length() > 3) && (prot.substr(0, 3) == "://"))
{
if ((prot.length() > 3) && (prot.substr(0, 3) == "://")) {
result.Protocol = std::string(protocolStart, protocolEnd);
// ://.
protocolEnd += 3;
}
else
// No protocol
} else {
// No protocol.
protocolEnd = uri.begin();
}
else
// No protocol
}
} else {
// No protocol.
protocolEnd = uri.begin();
}

// Host.
iterator_t hostStart = protocolEnd;

// Get fragment start.
iterator_t fragmentStart = std::find(hostStart, uriEnd, '#');

// Get pathStart.
iterator_t pathStart = std::find(hostStart, uriEnd, '/');
iterator_t pathStart = std::find(hostStart, fragmentStart, '/');

// Check for port.
iterator_t hostEnd = std::find(protocolEnd,
(pathStart != uriEnd) ? pathStart : queryStart,
':');
iterator_t hostEnd = std::find(protocolEnd, (pathStart != uriEnd) ? pathStart : queryStart, ':');

result.Host = std::string(hostStart, hostEnd);

// Port.
if ((hostEnd != uriEnd) && ((&*(hostEnd))[0] == ':'))
{
if ((hostEnd != uriEnd) && ((&*(hostEnd))[0] == ':')) {
hostEnd++;
iterator_t portEnd = (pathStart != uriEnd) ? pathStart : queryStart;
result.Port = std::string(hostEnd, portEnd);
}

// Path.
if (pathStart != uriEnd)
if (pathStart != uriEnd) {
result.Path = std::string(pathStart, queryStart);
}

// Query.
if (queryStart != uriEnd)
result.QueryString = std::string(queryStart, uri.end());
if (queryStart != uriEnd) {
result.QueryString = std::string(queryStart, fragmentStart);
}

// Fragment.
if (fragmentStart != uriEnd) {
result.Fragment = std::string(fragmentStart, uri.end());
}

return result;

Expand Down
1 change: 1 addition & 0 deletions bridge/bridge_jsc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ JSBridge::JSBridge(int32_t contextId, const JSExceptionHandler &handler) : conte
bindSVGElement(m_context);
bindDocumentFragment(m_context);
bindWindow(m_context);
bindHistory(m_context);
bindPerformance(m_context);
bindCSSStyleDeclaration(m_context);
bindScreen(m_context);
Expand Down