Example #1
0
static void test_dom_node_hierarchy_navigation(DomNodeFixture* fixture, gconstpointer data)
{
    WebKitDOMDocument* document;
    WebKitDOMHTMLHeadElement* head;
    WebKitDOMHTMLBodyElement* body;
    WebKitDOMNodeList* list;
    WebKitDOMNode* ptr;
    gulong i, length;

    document = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(fixture->webView));
    g_assert(document);
    g_assert(WEBKIT_DOM_IS_DOCUMENT(document));
    head = webkit_dom_document_get_head(document);
    g_assert(head);
    g_assert(WEBKIT_DOM_IS_HTML_HEAD_ELEMENT(head));

    /* Title, head's child */
    g_assert(webkit_dom_node_has_child_nodes(WEBKIT_DOM_NODE(head)));
    list = webkit_dom_node_get_child_nodes(WEBKIT_DOM_NODE(head));
    g_assert_cmpint(webkit_dom_node_list_get_length(list), ==, 1);
    ptr = webkit_dom_node_list_item(list, 0);
    g_assert(ptr);
    g_assert(WEBKIT_DOM_IS_HTML_TITLE_ELEMENT(ptr));
    g_object_unref(list);

    /* Body, Head sibling */
    ptr = webkit_dom_node_get_next_sibling(WEBKIT_DOM_NODE(head));
    g_assert(ptr);
    body = WEBKIT_DOM_HTML_BODY_ELEMENT(ptr);
    g_assert(WEBKIT_DOM_IS_HTML_BODY_ELEMENT(body));

    /* There is no third sibling */
    ptr = webkit_dom_node_get_next_sibling(ptr);
    g_assert(ptr == NULL);

    /* Body's previous sibling is Head */
    ptr = webkit_dom_node_get_previous_sibling(WEBKIT_DOM_NODE(body));
    g_assert(ptr);
    g_assert(WEBKIT_DOM_IS_HTML_HEAD_ELEMENT(ptr));

    /* Body has 3 children */
    g_assert(webkit_dom_node_has_child_nodes(WEBKIT_DOM_NODE(body)));
    list = webkit_dom_node_get_child_nodes(WEBKIT_DOM_NODE(body));
    length = webkit_dom_node_list_get_length(list);
    g_assert_cmpint(length, ==, 3);

    /* The three of them are P tags */
    for (i = 0; i < length; i++) {
        ptr = webkit_dom_node_list_item(list, i);
        g_assert(ptr);
        g_assert(WEBKIT_DOM_IS_HTML_PARAGRAPH_ELEMENT(ptr));
    }

    /* Go backwards */
    for (i = 0; ptr; ptr = webkit_dom_node_get_previous_sibling(ptr), i++)
        /* Nothing */;

    g_assert_cmpint(i, ==, 3);
    g_object_unref(list);
}
Example #2
0
static void
inject_adblock_css(WebKitDOMDocument *doc,
                   Exten             *exten)
{
    // Get css rules
    gchar *domain = webkit_dom_document_get_domain(doc);
    GError *err = NULL;
    gchar *css = domain_elem_hide_css(domain, exten, &err);
    if(err != NULL) {
        printf("Failed to retrieve element hide CSS: %s\n", err->message);
        g_error_free(err);
        return;
    }

    // Add CSS
    WebKitDOMElement *style_elem = webkit_dom_document_create_element(
            doc,
            "STYLE",
            &err);
    if(err != NULL) {
        printf("Failed to inject style: %s\n", err->message);
        g_error_free(err);
        g_free(domain);
        g_free(css);
        return;
    }
    webkit_dom_html_element_set_inner_html(
            WEBKIT_DOM_HTML_ELEMENT(style_elem),
            css,
            &err);
    if(err != NULL) {
        printf("Failed to inject style: %s\n", err->message);
        g_object_unref(style_elem);
        g_error_free(err);
        g_free(domain);
        g_free(css);
        return;
    }
    WebKitDOMHTMLHeadElement *head = webkit_dom_document_get_head(doc);
    webkit_dom_node_append_child(
            WEBKIT_DOM_NODE(head),
            WEBKIT_DOM_NODE(style_elem),
            &err);
    g_object_unref(style_elem);
    if(err != NULL) {
        printf("Failed to inject style: %s\n", err->message);
        g_error_free(err);
        g_free(domain);
        g_free(css);
        return;
    }
    g_free(domain);
    g_free(css);
}
Example #3
0
static void test_dom_document_garbage_collection(DomDocumentFixture* fixture, gconstpointer data)
{
    guint count = 0;
    g_assert(fixture);
    WebKitWebView* view = (WebKitWebView*)fixture->webView;
    g_assert(view);
    WebKitDOMDocument* document = webkit_web_view_get_dom_document(view);
    g_assert(document);
    g_object_weak_ref(G_OBJECT(document), (GWeakNotify)weak_notify, &count);
    WebKitDOMHTMLHeadElement* head = webkit_dom_document_get_head(document);
    g_assert(head);
    g_object_weak_ref(G_OBJECT(head), (GWeakNotify)weak_notify, &count);
    WebKitDOMHTMLElement* body = webkit_dom_document_get_body(document);
    g_assert(body);
    g_object_weak_ref(G_OBJECT(body), (GWeakNotify)weak_notify, &count);
    WebKitDOMHTMLCollection *collection = webkit_dom_document_get_links(document);
    g_assert(collection);
    g_object_weak_ref(G_OBJECT(collection), (GWeakNotify)weak_notify, &count);

    webkit_web_view_load_string(WEBKIT_WEB_VIEW(view), HTML_DOCUMENT_LINKS, NULL, NULL, NULL);

    while (g_main_context_pending(NULL))
        g_main_context_iteration(NULL, FALSE);

    g_assert_cmpuint(count, ==, 3);

    g_object_unref(collection);
    g_assert_cmpuint(count, ==, 4);

    count = 0;

    document = webkit_web_view_get_dom_document(view);
    g_assert(document);
    g_object_weak_ref(G_OBJECT(document), (GWeakNotify)weak_notify, &count);
    head = webkit_dom_document_get_head(document);
    g_assert(head);
    g_object_weak_ref(G_OBJECT(head), (GWeakNotify)weak_notify, &count);
    body = webkit_dom_document_get_body(document);
    g_assert(body);
    g_object_weak_ref(G_OBJECT(body), (GWeakNotify)weak_notify, &count);
    collection = webkit_dom_document_get_links(document);
    g_assert(collection);
    g_object_weak_ref(G_OBJECT(collection), (GWeakNotify)weak_notify, &count);
    /* Ask twice for the same object */
    WebKitDOMHTMLCollection* collection2 = webkit_dom_document_get_links(document);
    g_assert(collection2);
    g_object_weak_ref(G_OBJECT(collection2), (GWeakNotify)weak_notify, &count);

    g_object_unref(document);
    g_object_unref(head);
    g_object_unref(body);
    g_object_unref(collection);
    g_object_unref(collection2);

    g_assert_cmpuint(count, ==, 5);

    webkit_web_view_load_string(WEBKIT_WEB_VIEW(view), HTML_DOCUMENT_IFRAME, NULL, NULL, NULL);

    while (g_main_context_pending(NULL))
        g_main_context_iteration(NULL, FALSE);

    count = 0;

    document = webkit_web_view_get_dom_document(view);
    WebKitDOMElement* div = webkit_dom_document_get_element_by_id(document, "test");
    g_assert(div);
    g_object_weak_ref(G_OBJECT(div), (GWeakNotify)weak_notify, &count);
    WebKitDOMElement* iframe = webkit_dom_document_get_element_by_id(document, "iframe");
    g_assert(iframe);

    webkit_dom_element_set_attribute(iframe, "src", "data:<html><head></head></html>", NULL);

    while (g_main_context_pending(NULL))
        g_main_context_iteration(NULL, FALSE);

    WebKitDOMDocument* iframeDocument = webkit_dom_html_iframe_element_get_content_document(WEBKIT_DOM_HTML_IFRAME_ELEMENT(iframe));
    g_assert(iframeDocument);
    head = webkit_dom_document_get_head(iframeDocument);
    g_assert(head);
    g_object_weak_ref(G_OBJECT(head), (GWeakNotify)weak_notify, &count);

    webkit_dom_element_set_attribute(iframe, "src", "about:blank", NULL);

    while (g_main_context_pending(NULL))
        g_main_context_iteration(NULL, FALSE);

    g_assert_cmpuint(count, ==, 1);

    webkit_web_view_load_string(WEBKIT_WEB_VIEW(view), HTML_DOCUMENT_LINKS, NULL, NULL, NULL);

    while (g_main_context_pending(NULL))
        g_main_context_iteration(NULL, FALSE);

    g_assert_cmpuint(count, ==, 2);

    count = 0;

    document = webkit_web_view_get_dom_document(view);
    g_assert(document);
    g_object_weak_ref(G_OBJECT(document), (GWeakNotify)weak_notify, &count);
    /* Ask twice for the Document */
    WebKitDOMDocument* document2 = webkit_web_view_get_dom_document(view);
    g_assert(document2);
    g_object_weak_ref(G_OBJECT(document2), (GWeakNotify)weak_notify, &count);
    head = webkit_dom_document_get_head(document);
    g_assert(head);
    g_object_weak_ref(G_OBJECT(head), (GWeakNotify)weak_notify, &count);
    body = webkit_dom_document_get_body(document);
    g_assert(body);
    g_object_weak_ref(G_OBJECT(body), (GWeakNotify)weak_notify, &count);
    collection = webkit_dom_document_get_links(document);
    g_assert(collection);
    g_object_weak_ref(G_OBJECT(collection), (GWeakNotify)weak_notify, &count);

    gtk_widget_destroy(fixture->webView);
    fixture->webView = NULL;

    g_assert_cmpuint(count, ==, 4);

    g_object_unref(collection);

    g_assert_cmpuint(count, ==, 5);
}