Skip to content

Commit 9b19989

Browse files
balejkc0dev0id
authored andcommitted
extension: rewrite dom_element:client_rects using JSC
1 parent bca6857 commit 9b19989

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

extension/clib/dom_element.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "extension/clib/dom_document.h"
2727
#include "common/luajs.h"
2828
#include "common/luauniq.h"
29+
#include "common/util.h"
2930
#include "extension/extension.h"
3031

3132
#define REG_KEY "luakit.uniq.registry.dom_element"
@@ -649,34 +650,35 @@ luaH_dom_element_remove_event_listener(lua_State *L)
649650
return 1;
650651
}
651652

652-
#if WEBKIT_CHECK_VERSION(2,18,0)
653653
static gint
654654
luaH_dom_element_client_rects(lua_State *L)
655655
{
656656
dom_element_t *element = luaH_check_dom_element(L, 1);
657-
WebKitDOMClientRectList *rects = webkit_dom_element_get_client_rects(element->element);
658-
int num_rects = webkit_dom_client_rect_list_get_length(rects);
657+
JSCValue *rects = jsc_value_object_invoke_method(L_DOM_ELEMENT_TO_JSC_VALUE(element), "getClientRects", G_TYPE_NONE);
658+
659+
JSCValue *length = jsc_value_object_get_property(rects, "length");
660+
int num_rects = jsc_value_to_int32(length);
661+
g_object_unref(length);
659662

660663
lua_createtable(L, num_rects, 0);
661664
for (int i = 0; i < num_rects; ++i) {
662-
WebKitDOMClientRect* rect = webkit_dom_client_rect_list_item(rects, i);
665+
JSCValue *rect = jsc_value_object_get_property_at_index(rects, i);
663666
lua_newtable(L);
664-
#define PROP(prop) \
665-
lua_pushnumber(L, webkit_dom_client_rect_get_##prop(rect)); \
666-
lua_setfield(L, -2, #prop);
667-
PROP(top)
668-
PROP(right)
669-
PROP(bottom)
670-
PROP(left)
671-
PROP(width)
672-
PROP(height)
673-
#undef PROP
667+
char *properties[] = {"top", "right", "bottom", "left", "width", "height"};
668+
for (int j = 0; j < LENGTH(properties); j++) {
669+
JSCValue *property = jsc_value_object_get_property(rect, properties[j]);
670+
lua_pushnumber(L, jsc_value_to_int32(property));
671+
lua_setfield(L, -2, properties[j]);
672+
g_object_unref(property);
673+
}
674674
lua_rawseti(L, -2, i+1);
675+
g_object_unref(rect);
675676
}
676677

678+
g_object_unref(rects);
679+
677680
return 1;
678681
}
679-
#endif
680682

681683
static int luaH_dom_element_push_attribute(lua_State *L, char *attribute)
682684
{
@@ -769,9 +771,7 @@ luaH_dom_element_index(lua_State *L)
769771
PF_CASE(SUBMIT, luaH_dom_element_submit)
770772
PF_CASE(ADD_EVENT_LISTENER, luaH_dom_element_add_event_listener)
771773
PF_CASE(REMOVE_EVENT_LISTENER, luaH_dom_element_remove_event_listener)
772-
#if WEBKIT_CHECK_VERSION(2,18,0)
773774
PF_CASE(CLIENT_RECTS, luaH_dom_element_client_rects)
774-
#endif
775775

776776
case L_TK_CHILD_COUNT:
777777
return luaH_dom_element_push_property(L, "childElementCount");

lib/select_wm.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ local _M = {}
1414

1515
local ui = ipc_channel("select_wm")
1616

17-
local has_client_rects_api = tonumber(luakit.webkit_version:match("^2%.(%d+)%.")) > 16
18-
1917
-- Label making
2018

2119
-- Calculates the minimum number of characters needed in a hint given a
@@ -185,7 +183,7 @@ local function get_element_bb_if_visible(element, wbb, client_rects)
185183
-- Find the element bounding box
186184
local r
187185

188-
if has_client_rects_api and not element.first_child then
186+
if not element.first_child then
189187
r = element:client_rects()
190188
for i=#r,1,-1 do
191189
if r[i].width == 0 or r[i].height == 0 then table.remove(r, i) end

0 commit comments

Comments
 (0)