Use CorrespondingUseElement() in SVGElement::TreeScopeForIdResolution

For elements sourced non-locally, CorrespondingElement() will give the
TreeScope of the document it was sourced from rather than the TreeScope
of the <use> element. Until we are able to resolve references at
ComputedStyle resolution, attempt to use the host of the shadow tree,
i.e the (outermost) <use> element. (This will not work when external
paint servers are supported, or with a paint server defined in the
referenced document.)

BUG=721167

Review-Url: https://codereview.chromium.org/2877973002
Cr-Commit-Position: refs/heads/master@{#471955}
diff --git a/third_party/WebKit/LayoutTests/svg/custom/resources/rect100x100.svg b/third_party/WebKit/LayoutTests/svg/custom/resources/rect100x100.svg
new file mode 100644
index 0000000..168d123d0
--- /dev/null
+++ b/third_party/WebKit/LayoutTests/svg/custom/resources/rect100x100.svg
@@ -0,0 +1,3 @@
+<svg xmlns="http://www.w3.org/2000/svg">
+  <rect id="rect" width="100" height="100"/>
+</svg>
diff --git a/third_party/WebKit/LayoutTests/svg/custom/use-external-local-fill-expected.html b/third_party/WebKit/LayoutTests/svg/custom/use-external-local-fill-expected.html
new file mode 100644
index 0000000..f718ea6
--- /dev/null
+++ b/third_party/WebKit/LayoutTests/svg/custom/use-external-local-fill-expected.html
@@ -0,0 +1,2 @@
+<!DOCTYPE html>
+<div style="width: 100px; height: 100px; background-color: green"></div>
diff --git a/third_party/WebKit/LayoutTests/svg/custom/use-external-local-fill.html b/third_party/WebKit/LayoutTests/svg/custom/use-external-local-fill.html
new file mode 100644
index 0000000..e72b31b
--- /dev/null
+++ b/third_party/WebKit/LayoutTests/svg/custom/use-external-local-fill.html
@@ -0,0 +1,7 @@
+<!DOCTYPE html>
+<svg>
+  <linearGradient id="g">
+    <stop stop-color="green"/>
+  </linearGradient>
+  <use href="resources/rect100x100.svg#rect" fill="url(#g) red"/>
+</svg>
diff --git a/third_party/WebKit/Source/core/svg/SVGElement.cpp b/third_party/WebKit/Source/core/svg/SVGElement.cpp
index b8cd4804..452f24e 100644
--- a/third_party/WebKit/Source/core/svg/SVGElement.cpp
+++ b/third_party/WebKit/Source/core/svg/SVGElement.cpp
@@ -88,7 +88,7 @@
 
 TreeScope& SVGElement::TreeScopeForIdResolution() const {
   const SVGElement* tree_scope_element = this;
-  if (const SVGElement* element = CorrespondingElement())
+  if (const SVGElement* element = CorrespondingUseElement())
     tree_scope_element = element;
   return tree_scope_element->GetTreeScope();
 }