Make Range constructable

http://dom.spec.whatwg.org/#interface-range

Now we can do `new Range()` instead of `document.createRange()`.

BUG=237901

Review URL: https://chromiumcodereview.appspot.com/14904005

git-svn-id: svn://svn.chromium.org/blink/trunk@149794 bbb929c8-8fbe-4397-9dbb-9b2b20218538
diff --git a/LayoutTests/fast/dom/Range/range-constructor-expected.txt b/LayoutTests/fast/dom/Range/range-constructor-expected.txt
new file mode 100644
index 0000000..397d271
--- /dev/null
+++ b/LayoutTests/fast/dom/Range/range-constructor-expected.txt
@@ -0,0 +1,18 @@
+Tests that we can create instances of Range using new
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS typeof new Range is "object"
+PASS Object.prototype.toString.call(new Range) is "[object Range]"
+PASS new Range instanceof Range is true
+PASS Object.getPrototypeOf(new Range) is Range.prototype
+PASS r.toString() is "Test"
+PASS new innerWindow.Range instanceof innerWindow.Range is true
+PASS Object.getPrototypeOf(new innerWindow.Range) is innerWindow.Range.prototype
+PASS r.toString() is "Inner"
+PASS successfullyParsed is true
+
+TEST COMPLETE
+Test
+
diff --git a/LayoutTests/fast/dom/Range/range-constructor.html b/LayoutTests/fast/dom/Range/range-constructor.html
new file mode 100644
index 0000000..9faffcd
--- /dev/null
+++ b/LayoutTests/fast/dom/Range/range-constructor.html
@@ -0,0 +1,31 @@
+<!DOCTYPE html>
+<script src="../../js/resources/js-test-pre.js"></script>
+<div id="test-div">Test</div>
+<script>
+
+description('Tests that we can create instances of Range using new');
+
+shouldBe('typeof new Range', '"object"');
+shouldBe('Object.prototype.toString.call(new Range)', '"[object Range]"');
+shouldBeTrue('new Range instanceof Range');
+shouldBe('Object.getPrototypeOf(new Range)', 'Range.prototype');
+
+var r = new Range();
+r.selectNodeContents(document.getElementById('test-div'));
+shouldBe('r.toString()', '"Test"')
+
+var frame = document.createElement('iframe');
+document.body.appendChild(frame);
+var innerWindow = frame.contentWindow;
+var innerDocument = frame.contentDocument;
+innerDocument.body.textContent = 'Inner';
+
+shouldBeTrue('new innerWindow.Range instanceof innerWindow.Range');
+shouldBe('Object.getPrototypeOf(new innerWindow.Range)', 'innerWindow.Range.prototype');
+
+var r = new innerWindow.Range();
+r.selectNodeContents(innerDocument.body);
+shouldBe('r.toString()', '"Inner"');
+
+</script>
+<script src="../../js/resources/js-test-post.js"></script>
diff --git a/LayoutTests/fast/dom/dom-constructors-expected.txt b/LayoutTests/fast/dom/dom-constructors-expected.txt
index 54bd171..5f02d38 100644
--- a/LayoutTests/fast/dom/dom-constructors-expected.txt
+++ b/LayoutTests/fast/dom/dom-constructors-expected.txt
@@ -100,7 +100,6 @@
 PASS TryAllocate('NodeList') is 'exception'
 PASS TryAllocate('Plugin') is 'exception'
 PASS TryAllocate('PluginArray') is 'exception'
-PASS TryAllocate('Range') is 'exception'
 PASS TryAllocate('Rect') is 'exception'
 PASS TryAllocate('StyleSheet') is 'exception'
 PASS TryAllocate('StyleSheetList') is 'exception'
@@ -131,6 +130,9 @@
 PASS TryAllocate('DOMParser') is '[object DOMParser]'
 PASS TryAllocate('DOMParser') is '[object DOMParser]'
 PASS TryAllocate('DOMParser') is '[object DOMParser]'
+FAIL TryAllocate('Range') should be [object Range]. Was .
+FAIL TryAllocate('Range') should be [object Range]. Was .
+FAIL TryAllocate('Range') should be [object Range]. Was .
 PASS TryAllocate('XMLHttpRequest') is '[object XMLHttpRequest]'
 PASS TryAllocate('XMLHttpRequest') is '[object XMLHttpRequest]'
 PASS TryAllocate('XMLHttpRequest') is '[object XMLHttpRequest]'
diff --git a/LayoutTests/fast/dom/dom-constructors.html b/LayoutTests/fast/dom/dom-constructors.html
index 9198d2b..77cd66d 100644
--- a/LayoutTests/fast/dom/dom-constructors.html
+++ b/LayoutTests/fast/dom/dom-constructors.html
@@ -45,7 +45,7 @@
     'HTMLCollection', 'MediaList', 'MimeType',
     'MimeTypeArray', 'MutationEvent', 'NamedNodeMap',
     'NodeFilter', 'NodeList', 'Plugin',
-    'PluginArray', 'Range', 'Rect', 'StyleSheet', 'StyleSheetList',
+    'PluginArray', 'Rect', 'StyleSheet', 'StyleSheetList',
     'TextEvent', 'XPathResult', 'BarInfo',
     'CanvasGradient', 'CanvasPattern', 'Console', 'Selection',
     'Window', 'History', 'HTMLOptionsCollection', 'Location',
@@ -55,7 +55,7 @@
 
 // These objects should have a working constructor.
 var objects_constructor = [
-    'DOMParser', 'XMLHttpRequest', 'XMLSerializer', 'XPathEvaluator',
+    'DOMParser', 'Range', 'XMLHttpRequest', 'XMLSerializer', 'XPathEvaluator',
     'XSLTProcessor'
 ];
 
diff --git a/Source/core/dom/Range.cpp b/Source/core/dom/Range.cpp
index 87ccff1..af491c3 100644
--- a/Source/core/dom/Range.cpp
+++ b/Source/core/dom/Range.cpp
@@ -104,6 +104,11 @@
     return adoptRef(new Range(ownerDocument, start.containerNode(), start.computeOffsetInContainerNode(), end.containerNode(), end.computeOffsetInContainerNode()));
 }
 
+PassRefPtr<Range> Range::create(ScriptExecutionContext* context)
+{
+    return adoptRef(new Range(toDocument(context)));
+}
+
 Range::~Range()
 {
     // Always detach (even if we've already detached) to fix https://bugs.webkit.org/show_bug.cgi?id=26044
diff --git a/Source/core/dom/Range.h b/Source/core/dom/Range.h
index 7e65656..95f8356 100644
--- a/Source/core/dom/Range.h
+++ b/Source/core/dom/Range.h
@@ -51,6 +51,7 @@
     static PassRefPtr<Range> create(PassRefPtr<Document>);
     static PassRefPtr<Range> create(PassRefPtr<Document>, PassRefPtr<Node> startContainer, int startOffset, PassRefPtr<Node> endContainer, int endOffset);
     static PassRefPtr<Range> create(PassRefPtr<Document>, const Position&, const Position&);
+    static PassRefPtr<Range> create(ScriptExecutionContext*);
     ~Range();
 
     Document* ownerDocument() const { return m_ownerDocument.get(); }
diff --git a/Source/core/dom/Range.idl b/Source/core/dom/Range.idl
index 3504bad..2d825b1 100644
--- a/Source/core/dom/Range.idl
+++ b/Source/core/dom/Range.idl
@@ -20,7 +20,9 @@
 
 // Introduced in DOM Level 2:
 [
-    ImplementationLacksVTable
+    Constructor,
+    CallWith=ScriptExecutionContext,
+    ImplementationLacksVTable,
 ] interface Range {
 
     [GetterRaisesException] readonly attribute Node startContainer;