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.
This commit is contained in:
parent
44d871f6f1
commit
c83f07a9f1
2 changed files with 48 additions and 5 deletions
|
|
@ -17,6 +17,9 @@ class ConversationPageStateTest : ClientWebViewTestCase<ConversationWebView> {
|
|||
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> {
|
|||
ConversationWebView.DeceptiveText.DECEPTIVE_DOMAIN);
|
||||
}
|
||||
|
||||
public void is_descendant_of() throws GLib.Error {
|
||||
load_body_fixture("<blockquote><div id='test'>ohhai</div></blockquote>");
|
||||
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("<blockquote class='test-class'><div id='test'>ohhai</div></blockquote>");
|
||||
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("<blockquote class='test-class'><div id='test'>ohhai</div></blockquote>");
|
||||
assert(
|
||||
WebKitUtil.to_bool(
|
||||
run_javascript("""
|
||||
ConversationPageState.isDescendantOf(
|
||||
document.getElementById('test'), "DIV"
|
||||
);
|
||||
""")
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
protected override ConversationWebView set_up_test_view() {
|
||||
return new ConversationWebView(this.config);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue