Bug 1575980 - Implement StaticRange constructor; r=smaug
authorJohn Dai <jdai@mozilla.com>
Tue, 01 Oct 2019 17:51:32 +0000
changeset 495968 05c7e31860208e6853078365fc823b272daabf3a
parent 495967 c4157510d5a908864b988212a5b21ca92127bb0c
child 495969 f69570cd08304a581d2285100b79e5bd135484f6
push id114140
push userdvarga@mozilla.com
push dateWed, 02 Oct 2019 18:04:51 +0000
treeherdermozilla-inbound@32eb0ea893f3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1575980
milestone71.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1575980 - Implement StaticRange constructor; r=smaug Differential Revision: https://phabricator.services.mozilla.com/D47793
dom/base/StaticRange.cpp
dom/base/StaticRange.h
dom/webidl/StaticRange.webidl
testing/web-platform/meta/dom/idlharness.window.js.ini
testing/web-platform/meta/dom/ranges/StaticRange-constructor.html.ini
--- a/dom/base/StaticRange.cpp
+++ b/dom/base/StaticRange.cpp
@@ -66,31 +66,44 @@ NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INH
 NS_IMPL_CYCLE_COLLECTION_TRACE_END
 
 // static
 template <typename SPT, typename SRT, typename EPT, typename ERT>
 already_AddRefed<StaticRange> StaticRange::Create(
     const RangeBoundaryBase<SPT, SRT>& aStartBoundary,
     const RangeBoundaryBase<EPT, ERT>& aEndBoundary, ErrorResult& aRv) {
   RefPtr<StaticRange> staticRange = new StaticRange(aStartBoundary.Container());
-  aRv = staticRange->SetStartAndEnd(aStartBoundary, aEndBoundary);
-  if (NS_WARN_IF(aRv.Failed())) {
-    return nullptr;
-  }
+  staticRange->DoSetRange(aStartBoundary, aEndBoundary, nullptr);
+
   return staticRange.forget();
 }
 
 template <typename SPT, typename SRT, typename EPT, typename ERT>
 void StaticRange::DoSetRange(const RangeBoundaryBase<SPT, SRT>& aStartBoundary,
                              const RangeBoundaryBase<EPT, ERT>& aEndBoundary,
                              nsINode* aRootNode) {
   mStart = aStartBoundary;
   mEnd = aEndBoundary;
   mIsPositioned = mStart.IsSet();
 }
 
+/* static */
+already_AddRefed<StaticRange> StaticRange::Constructor(
+    const GlobalObject& global, const StaticRangeInit& init, ErrorResult& aRv) {
+  if (init.mStartContainer->NodeType() == nsINode::DOCUMENT_TYPE_NODE ||
+      init.mStartContainer->NodeType() == nsINode::ATTRIBUTE_NODE ||
+      init.mEndContainer->NodeType() == nsINode::DOCUMENT_TYPE_NODE ||
+      init.mEndContainer->NodeType() == nsINode::ATTRIBUTE_NODE) {
+    aRv.Throw(NS_ERROR_DOM_INVALID_NODE_TYPE_ERR);
+    return nullptr;
+  }
+
+  return Create(init.mStartContainer, init.mStartOffset, init.mEndContainer,
+                init.mEndOffset, aRv);
+}
+
 JSObject* StaticRange::WrapObject(JSContext* aCx,
                                   JS::Handle<JSObject*> aGivenProto) {
   return StaticRange_Binding::Wrap(aCx, this, aGivenProto);
 }
 
 }  // namespace dom
 }  // namespace mozilla
--- a/dom/base/StaticRange.h
+++ b/dom/base/StaticRange.h
@@ -5,27 +5,32 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef mozilla_dom_StaticRange_h
 #define mozilla_dom_StaticRange_h
 
 #include "mozilla/ErrorResult.h"
 #include "mozilla/RangeBoundary.h"
 #include "mozilla/dom/AbstractRange.h"
+#include "mozilla/dom/StaticRangeBinding.h"
 #include "nsWrapperCache.h"
 
 namespace mozilla {
 namespace dom {
 
 class StaticRange final : public AbstractRange {
  public:
   explicit StaticRange(nsINode* aNode) : AbstractRange(aNode) {}
   StaticRange() = delete;
   explicit StaticRange(const StaticRange& aOther) = delete;
 
+  static already_AddRefed<StaticRange> Constructor(const GlobalObject& global,
+                                                   const StaticRangeInit& init,
+                                                   ErrorResult& aRv);
+
   /**
    * Create() may return `StaticRange` instance which is initialized with
    * given range or points.  If it fails initializing new range with the
    * arguments, returns `nullptr`.  `ErrorResult` is set to an error only
    * when this returns `nullptr`.  The error code indicates the reason why
    * it couldn't initialize the instance.
    */
   static already_AddRefed<StaticRange> Create(
--- a/dom/webidl/StaticRange.webidl
+++ b/dom/webidl/StaticRange.webidl
@@ -5,13 +5,21 @@
  *
  * The origin of this IDL file is
  * https://dom.spec.whatwg.org/#staticrange
  *
  * Copyright  2012 W3C  (MIT, ERCIM, Keio), All Rights Reserved. W3C
  * liability, trademark and document use rules apply.
  */
 
-// Currently, StaticRange cannot be created from web apps.
 [Exposed=Window]
 interface StaticRange : AbstractRange {
+  [Throws]
+  constructor(StaticRangeInit init);
   // And no additional functions/properties.
 };
+
+dictionary StaticRangeInit {
+  required Node startContainer;
+  required unsigned long startOffset;
+  required Node endContainer;
+  required unsigned long endOffset;
+};
\ No newline at end of file
--- a/testing/web-platform/meta/dom/idlharness.window.js.ini
+++ b/testing/web-platform/meta/dom/idlharness.window.js.ini
@@ -8,14 +8,12 @@
     expected: FAIL
 
   [Document interface: xmlDoc must inherit property "origin" with the proper type]
     expected: FAIL
 
   [Document interface: new Document() must inherit property "origin" with the proper type]
     expected: FAIL
 
-  [StaticRange interface object length]
-    expected: FAIL
 
   [ShadowRoot interface: attribute onslotchange]
     expected: FAIL
 
deleted file mode 100644
--- a/testing/web-platform/meta/dom/ranges/StaticRange-constructor.html.ini
+++ /dev/null
@@ -1,49 +0,0 @@
-[StaticRange-constructor.html]
-  [Construct static range with endpoints in disconnected trees]
-    expected: FAIL
-
-  [Construct static range with Element startContainer and Text endContainer]
-    expected: FAIL
-
-  [Construct collapsed static range]
-    expected: FAIL
-
-  [Construct static range with Document container]
-    expected: FAIL
-
-  [Construct static range with standalone Node container]
-    expected: FAIL
-
-  [Construct static range with offset greater than length]
-    expected: FAIL
-
-  [Construct static range with Comment container]
-    expected: FAIL
-
-  [Construct inverted static range]
-    expected: FAIL
-
-  [Throw on DocumentType or Attr container]
-    expected: FAIL
-
-  [Construct static range with CDATASection container]
-    expected: FAIL
-
-  [Construct static range with Text container]
-    expected: FAIL
-
-  [Construct static range with Text startContainer and Element endContainer]
-    expected: FAIL
-
-  [Construct static range with DocumentFragment container]
-    expected: FAIL
-
-  [Construct static range with Element container]
-    expected: FAIL
-
-  [Construct static range with ProcessingInstruction container]
-    expected: FAIL
-
-  [Construct static range with endpoints in disconnected documents]
-    expected: FAIL
-