From c83f07a9f1b468b1ebcf8ecdc8b2ae2d077f24d8 Mon Sep 17 00:00:00 2001 From: Michael Gratton Date: Mon, 21 Jan 2019 20:55:03 +1100 Subject: [PATCH] Minor tweaks for ConversationPageState.isDescendantOf Use uppercase since that is what the DOM for HTML defaults to, use nodeName rather than tagName for cases when there the check is false and ancestor is the document element, add unit tests. --- test/js/conversation-page-state-test.vala | 43 +++++++++++++++++++++++ ui/conversation-web-view.js | 10 +++--- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/test/js/conversation-page-state-test.vala b/test/js/conversation-page-state-test.vala index 7a8c276a..7d49eedb 100644 --- a/test/js/conversation-page-state-test.vala +++ b/test/js/conversation-page-state-test.vala @@ -17,6 +17,9 @@ class ConversationPageStateTest : ClientWebViewTestCase { add_test("is_deceptive_text_deceptive_href", is_deceptive_text_deceptive_href); add_test("is_deceptive_text_non_matching_subdomain", is_deceptive_text_non_matching_subdomain); add_test("is_deceptive_text_different_domain", is_deceptive_text_different_domain); + add_test("is_descendant_of", is_descendant_of); + add_test("is_descendant_of_with_class", is_descendant_of_with_class); + add_test("is_descendant_of_no_match", is_descendant_of_no_match); try { ConversationWebView.load_resources(File.new_for_path("")); @@ -73,6 +76,46 @@ class ConversationPageStateTest : ClientWebViewTestCase { ConversationWebView.DeceptiveText.DECEPTIVE_DOMAIN); } + public void is_descendant_of() throws GLib.Error { + load_body_fixture("
ohhai
"); + assert( + WebKitUtil.to_bool( + run_javascript(""" + ConversationPageState.isDescendantOf( + document.getElementById('test'), "BLOCKQUOTE" + ); + """) + ) + ); + } + + public void is_descendant_of_with_class() throws GLib.Error { + load_body_fixture("
ohhai
"); + assert( + WebKitUtil.to_bool( + run_javascript(""" + ConversationPageState.isDescendantOf( + document.getElementById('test'), "BLOCKQUOTE", "test-class" + ); + """) + ) + ); + } + + public void is_descendant_of_no_match() throws GLib.Error { + load_body_fixture("
ohhai
"); + assert( + WebKitUtil.to_bool( + run_javascript(""" + ConversationPageState.isDescendantOf( + document.getElementById('test'), "DIV" + ); + """) + ) + ); + } + + protected override ConversationWebView set_up_test_view() { return new ConversationWebView(this.config); } diff --git a/ui/conversation-web-view.js b/ui/conversation-web-view.js index d3a81ef5..fb6d9aca 100644 --- a/ui/conversation-web-view.js +++ b/ui/conversation-web-view.js @@ -82,7 +82,7 @@ ConversationPageState.prototype = { // Only insert into a quote container if the element is a // top level blockquote - if (!ConversationPageState.isDescendantOf(blockquote, "blockquote")) { + if (!ConversationPageState.isDescendantOf(blockquote, "BLOCKQUOTE")) { let quoteHeight = blockquote.offsetHeight; // Only make the quote it controllable if it is tall enough @@ -171,7 +171,7 @@ ConversationPageState.prototype = { let div = possibleSigs.item(i); let innerHTML = div.innerHTML; if ((sigRegex.test(innerHTML) || alternateSigRegex.test(innerHTML)) && - !ConversationPageState.isDescendantOf(div, "blockquote")) { + !ConversationPageState.isDescendantOf(div, "BLOCKQUOTE")) { break; } } @@ -206,7 +206,7 @@ ConversationPageState.prototype = { // so that new lines are preserved. let dummy = document.createElement("DIV"); let includeDummy = false; - if (ConversationPageState.isDescendantOf(ancestor, "div", "plaintext")) { + if (ConversationPageState.isDescendantOf(ancestor, "DIV", "plaintext")) { dummy.classList.add("plaintext"); dummy.setAttribute("style", "white-space: pre-wrap;"); includeDummy = true; @@ -324,14 +324,14 @@ ConversationPageState.isDeceptiveText = function(text, href) { /** * See if this element has an ancestor with the given tag and class. * - * ancestorTag must be all lowercase. + * ancestorTag must be all uppercase. * * If ancestorClass is null, no class checking is done. */ ConversationPageState.isDescendantOf = function(node, ancestorTag, ancestorClass = null) { let ancestor = node.parentNode; while (ancestor != null) { - if (ancestor.tagName.toLowerCase() == ancestorTag) { + if (ancestor.nodeName.toUpperCase() == ancestorTag) { if (!ancestorClass || ancestor.classList.contains(ancestorClass)) { return true; }