Commit 27ab57ac authored by Lu Chen's avatar Lu Chen Committed by Commit Bot

Move static functions to anonymous namespace.

Move static private functions in AnchorElementMetrics to anonymous namespace.

Bug: 850624
Change-Id: I5429b8ce97d9259a761f16717bad3e3ae88fe774
Reviewed-on: https://chromium-review.googlesource.com/1115814Reviewed-by: default avatarKouhei Ueno <kouhei@chromium.org>
Reviewed-by: default avatarRyan Sturm <ryansturm@chromium.org>
Reviewed-by: default avatarTarun Bansal <tbansal@chromium.org>
Commit-Queue: Lu Chen <chelu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#570609}
parent 91f9bf30
...@@ -16,11 +16,19 @@ ...@@ -16,11 +16,19 @@
#include "third_party/blink/renderer/core/paint/paint_layer.h" #include "third_party/blink/renderer/core/paint/paint_layer.h"
#include "third_party/blink/renderer/core/paint/paint_layer_scrollable_area.h" #include "third_party/blink/renderer/core/paint/paint_layer_scrollable_area.h"
#include "third_party/blink/renderer/platform/histogram.h" #include "third_party/blink/renderer/platform/histogram.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
namespace blink { namespace blink {
int AnchorElementMetrics::AccumulatedScrollOffset( namespace {
const HTMLAnchorElement& anchor_element) {
// Helper function that returns the root document the anchor element is in.
Document* GetRootDocument(const HTMLAnchorElement& anchor) {
return anchor.GetDocument().GetFrame()->LocalFrameRoot().GetDocument();
}
// Accumulated scroll offset of all frames up to the local root frame.
int AccumulatedScrollOffset(const HTMLAnchorElement& anchor_element) {
IntSize offset; IntSize offset;
Frame* frame = anchor_element.GetDocument().GetFrame(); Frame* frame = anchor_element.GetDocument().GetFrame();
while (frame && frame->View() && frame->IsLocalFrame()) { while (frame && frame->View() && frame->IsLocalFrame()) {
...@@ -30,7 +38,8 @@ int AnchorElementMetrics::AccumulatedScrollOffset( ...@@ -30,7 +38,8 @@ int AnchorElementMetrics::AccumulatedScrollOffset(
return offset.Height(); return offset.Height();
} }
bool AnchorElementMetrics::IsInIFrame(const HTMLAnchorElement& anchor_element) { // Whether the element is inside an iframe.
bool IsInIFrame(const HTMLAnchorElement& anchor_element) {
Frame* frame = anchor_element.GetDocument().GetFrame(); Frame* frame = anchor_element.GetDocument().GetFrame();
while (frame && frame->IsLocalFrame()) { while (frame && frame->IsLocalFrame()) {
HTMLFrameOwnerElement* owner = HTMLFrameOwnerElement* owner =
...@@ -42,8 +51,8 @@ bool AnchorElementMetrics::IsInIFrame(const HTMLAnchorElement& anchor_element) { ...@@ -42,8 +51,8 @@ bool AnchorElementMetrics::IsInIFrame(const HTMLAnchorElement& anchor_element) {
return false; return false;
} }
bool AnchorElementMetrics::ContainsImage( // Whether the anchor element contains an image element.
const HTMLAnchorElement& anchor_element) { bool ContainsImage(const HTMLAnchorElement& anchor_element) {
for (Node* node = FlatTreeTraversal::FirstChild(anchor_element); node; for (Node* node = FlatTreeTraversal::FirstChild(anchor_element); node;
node = FlatTreeTraversal::Next(*node, &anchor_element)) { node = FlatTreeTraversal::Next(*node, &anchor_element)) {
if (IsHTMLImageElement(*node)) if (IsHTMLImageElement(*node))
...@@ -52,24 +61,18 @@ bool AnchorElementMetrics::ContainsImage( ...@@ -52,24 +61,18 @@ bool AnchorElementMetrics::ContainsImage(
return false; return false;
} }
bool AnchorElementMetrics::IsSameHost(const HTMLAnchorElement& anchor_element) { // Whether the link target has the same host as the root document.
bool IsSameHost(const HTMLAnchorElement& anchor_element) {
String source_host = GetRootDocument(anchor_element)->Url().Host(); String source_host = GetRootDocument(anchor_element)->Url().Host();
String target_host = anchor_element.Href().Host(); String target_host = anchor_element.Href().Host();
return source_host == target_host; return source_host == target_host;
} }
bool AnchorElementMetrics::IsUrlIncrementedByOne( // Returns true if the two strings only differ by one number, and
const HTMLAnchorElement& anchor_element) { // the second number equals the first number plus one. Examples:
if (!IsSameHost(anchor_element)) // example.com/page9/cat5, example.com/page10/cat5 => true
return false; // example.com/page9/cat5, example.com/page10/cat10 => false
bool IsStringIncrementedByOne(const String& source, const String& target) {
String source_url = GetRootDocument(anchor_element)->Url().GetString();
String target_url = anchor_element.Href().GetString();
return IsStringIncrementedByOne(source_url, target_url);
}
bool AnchorElementMetrics::IsStringIncrementedByOne(const String& source,
const String& target) {
// Consecutive numbers should differ in length by at most 1. // Consecutive numbers should differ in length by at most 1.
int length_diff = target.length() - source.length(); int length_diff = target.length() - source.length();
if (length_diff < 0 || length_diff > 1) if (length_diff < 0 || length_diff > 1)
...@@ -106,13 +109,19 @@ bool AnchorElementMetrics::IsStringIncrementedByOne(const String& source, ...@@ -106,13 +109,19 @@ bool AnchorElementMetrics::IsStringIncrementedByOne(const String& source,
source.Substring(source_right) == target.Substring(target_right); source.Substring(source_right) == target.Substring(target_right);
} }
Document* AnchorElementMetrics::GetRootDocument( // Extract source and target link url, and return IsStringIncrementedByOne().
const HTMLAnchorElement& anchor) { bool IsUrlIncrementedByOne(const HTMLAnchorElement& anchor_element) {
return anchor.GetDocument().GetFrame()->LocalFrameRoot().GetDocument(); if (!IsSameHost(anchor_element))
return false;
String source_url = GetRootDocument(anchor_element)->Url().GetString();
String target_url = anchor_element.Href().GetString();
return IsStringIncrementedByOne(source_url, target_url);
} }
IntRect AnchorElementMetrics::AbsoluteElementBoundingBoxRect( // Returns the bounding box rect of a layout object, including visual
const LayoutObject* layout_object) { // overflows.
IntRect AbsoluteElementBoundingBoxRect(const LayoutObject* layout_object) {
Vector<LayoutRect> rects; Vector<LayoutRect> rects;
layout_object->AddElementVisualOverflowRects(rects, LayoutPoint()); layout_object->AddElementVisualOverflowRects(rects, LayoutPoint());
...@@ -121,6 +130,8 @@ IntRect AnchorElementMetrics::AbsoluteElementBoundingBoxRect( ...@@ -121,6 +130,8 @@ IntRect AnchorElementMetrics::AbsoluteElementBoundingBoxRect(
.EnclosingBoundingBox(); .EnclosingBoundingBox();
} }
} // anonymous namespace
base::Optional<AnchorElementMetrics> AnchorElementMetrics::CreateFrom( base::Optional<AnchorElementMetrics> AnchorElementMetrics::CreateFrom(
const HTMLAnchorElement* anchor_element) { const HTMLAnchorElement* anchor_element) {
LocalFrame* local_frame = anchor_element->GetDocument().GetFrame(); LocalFrame* local_frame = anchor_element->GetDocument().GetFrame();
......
...@@ -9,14 +9,10 @@ ...@@ -9,14 +9,10 @@
#include "third_party/blink/renderer/core/core_export.h" #include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/platform/heap/handle.h" #include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/wtf/allocator.h" #include "third_party/blink/renderer/platform/wtf/allocator.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
namespace blink { namespace blink {
class Document;
class HTMLAnchorElement; class HTMLAnchorElement;
class IntRect;
class LayoutObject;
class CORE_EXPORT AnchorElementMetrics { class CORE_EXPORT AnchorElementMetrics {
STACK_ALLOCATED(); STACK_ALLOCATED();
...@@ -51,35 +47,6 @@ class CORE_EXPORT AnchorElementMetrics { ...@@ -51,35 +47,6 @@ class CORE_EXPORT AnchorElementMetrics {
bool GetIsUrlIncrementedByOne() const { return is_url_incremented_by_one_; } bool GetIsUrlIncrementedByOne() const { return is_url_incremented_by_one_; }
private: private:
// Friend class for testing.
friend class AnchorElementMetricsTest;
// Accumulated scroll offset of all frames up to the local root frame.
static int AccumulatedScrollOffset(const HTMLAnchorElement&);
// Whether the element is inside an iframe.
static bool IsInIFrame(const HTMLAnchorElement&);
// Whether the anchor element contains an image element.
static bool ContainsImage(const HTMLAnchorElement&);
// Whether the link target has the same host as the root document.
static bool IsSameHost(const HTMLAnchorElement&);
// Returns true if the two strings only differ by one number, and
// the second number equals the first number plus one. Examples:
// example.com/page9/cat5, example.com/page10/cat5 => true
// example.com/page9/cat5, example.com/page10/cat10 => false
static bool IsUrlIncrementedByOne(const HTMLAnchorElement&);
static bool IsStringIncrementedByOne(const String&, const String&);
// Helper function that returns the root document the anchor element is in.
static Document* GetRootDocument(const HTMLAnchorElement&);
// Returns the bounding box rect of a layout object, including visual
// overflows.
static IntRect AbsoluteElementBoundingBoxRect(const LayoutObject*);
// The anchor element that this class is associated with. // The anchor element that this class is associated with.
Member<const HTMLAnchorElement> anchor_element_; Member<const HTMLAnchorElement> anchor_element_;
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "third_party/blink/renderer/core/paint/paint_layer_scrollable_area.h" #include "third_party/blink/renderer/core/paint/paint_layer_scrollable_area.h"
#include "third_party/blink/renderer/core/testing/sim/sim_request.h" #include "third_party/blink/renderer/core/testing/sim/sim_request.h"
#include "third_party/blink/renderer/core/testing/sim/sim_test.h" #include "third_party/blink/renderer/core/testing/sim/sim_test.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string.h"
namespace blink { namespace blink {
...@@ -18,6 +19,20 @@ class AnchorElementMetricsTest : public SimTest { ...@@ -18,6 +19,20 @@ class AnchorElementMetricsTest : public SimTest {
static constexpr int kViewportWidth = 400; static constexpr int kViewportWidth = 400;
static constexpr int kViewportHeight = 600; static constexpr int kViewportHeight = 600;
// Helper function to test IsUrlIncrementedByOne().
bool IsIncrementedByOne(const String& source, const String& target) {
SimRequest main_resource(source, "text/html");
LoadURL(source);
main_resource.Complete("<a id='anchor' href=''>example</a>");
HTMLAnchorElement* anchor_element =
ToHTMLAnchorElement(GetDocument().getElementById("anchor"));
anchor_element->SetHref(AtomicString(target));
return AnchorElementMetrics::CreateFrom(anchor_element)
.value()
.GetIsUrlIncrementedByOne();
}
protected: protected:
AnchorElementMetricsTest() = default; AnchorElementMetricsTest() = default;
...@@ -25,26 +40,28 @@ class AnchorElementMetricsTest : public SimTest { ...@@ -25,26 +40,28 @@ class AnchorElementMetricsTest : public SimTest {
SimTest::SetUp(); SimTest::SetUp();
WebView().Resize(WebSize(kViewportWidth, kViewportHeight)); WebView().Resize(WebSize(kViewportWidth, kViewportHeight));
} }
bool IsIncrementedByOne(const String& a, const String& b) {
return AnchorElementMetrics::IsStringIncrementedByOne(a, b);
}
}; };
// Test for IsUrlIncrementedByOne(). // Test for IsUrlIncrementedByOne().
TEST_F(AnchorElementMetricsTest, IsUrlIncrementedByOne) { TEST_F(AnchorElementMetricsTest, IsUrlIncrementedByOne) {
EXPECT_TRUE(IsIncrementedByOne("example.com/p1", "example.com/p2"));
EXPECT_TRUE(IsIncrementedByOne("example.com/?p=9", "example.com/?p=10"));
EXPECT_TRUE(IsIncrementedByOne("example.com/?p=12", "example.com/?p=13"));
EXPECT_TRUE( EXPECT_TRUE(
IsIncrementedByOne("example.com/p9/cat1", "example.com/p10/cat1")); IsIncrementedByOne("http://example.com/p1", "http://example.com/p2"));
EXPECT_TRUE(IsIncrementedByOne("http://example.com/?p=9",
EXPECT_FALSE(IsIncrementedByOne("example.com", "")); "http://example.com/?p=10"));
EXPECT_FALSE(IsIncrementedByOne("example.com/1", "google.com/2")); EXPECT_TRUE(IsIncrementedByOne("http://example.com/?p=12",
EXPECT_FALSE(IsIncrementedByOne("example.com/p1", "example.com/p1")); "http://example.com/?p=13"));
EXPECT_FALSE(IsIncrementedByOne("example.com/p2", "example.com/p1")); EXPECT_TRUE(IsIncrementedByOne("http://example.com/p9/cat1",
"http://example.com/p10/cat1"));
EXPECT_FALSE(
IsIncrementedByOne("http://example.com/1", "https://example.com/2"));
EXPECT_FALSE(
IsIncrementedByOne("http://example.com/1", "http://google.com/2"));
EXPECT_FALSE(
IsIncrementedByOne("http://example.com/p1", "http://example.com/p1"));
EXPECT_FALSE( EXPECT_FALSE(
IsIncrementedByOne("example.com/p9/cat1", "example.com/p10/cat2")); IsIncrementedByOne("http://example.com/p2", "http://example.com/p1"));
EXPECT_FALSE(IsIncrementedByOne("http://example.com/p9/cat1",
"http://example.com/p10/cat2"));
} }
// The main frame contains an anchor element, which contains an image element. // The main frame contains an anchor element, which contains an image element.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment