Make Comment constructable
http://dom.spec.whatwg.org/#comment
This allows us to do `new Comment('abc')` instead of
`document.createComment('abc')`.
BUG=238233
Review URL: https://chromiumcodereview.appspot.com/14931009
git-svn-id: svn://svn.chromium.org/blink/trunk@149821 bbb929c8-8fbe-4397-9dbb-9b2b20218538
diff --git a/LayoutTests/fast/dom/Comment/comment-constructor-expected.txt b/LayoutTests/fast/dom/Comment/comment-constructor-expected.txt
new file mode 100644
index 0000000..e8daa4d
--- /dev/null
+++ b/LayoutTests/fast/dom/Comment/comment-constructor-expected.txt
@@ -0,0 +1,19 @@
+This tests that Comment is constructable.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS new Comment("one").data is "one"
+PASS new Comment().data is ""
+PASS new Comment("two").ownerDocument is document
+PASS typeof new Comment is "object"
+PASS Object.prototype.toString.call(new Comment) is "[object Comment]"
+PASS new Comment instanceof Comment is true
+PASS Object.getPrototypeOf(new Comment) is Comment.prototype
+PASS new innerWindow.Comment("three").ownerDocument is innerDocument
+PASS new innerWindow.Comment instanceof innerWindow.Comment is true
+PASS Object.getPrototypeOf(new innerWindow.Comment) is innerWindow.Comment.prototype
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
diff --git a/LayoutTests/fast/dom/Comment/comment-constructor.html b/LayoutTests/fast/dom/Comment/comment-constructor.html
new file mode 100644
index 0000000..9e92bbe
--- /dev/null
+++ b/LayoutTests/fast/dom/Comment/comment-constructor.html
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<body>
+<script src="../../js/resources/js-test-pre.js"></script>
+<script>
+
+description('This tests that Comment is constructable.');
+
+shouldBe('new Comment("one").data', '"one"');
+shouldBe('new Comment().data', '""');
+shouldBe('new Comment("two").ownerDocument', 'document');
+
+shouldBe('typeof new Comment', '"object"');
+shouldBe('Object.prototype.toString.call(new Comment)', '"[object Comment]"');
+shouldBeTrue('new Comment instanceof Comment');
+shouldBe('Object.getPrototypeOf(new Comment)', 'Comment.prototype');
+
+var frame = document.createElement('iframe');
+document.body.appendChild(frame);
+var innerWindow = frame.contentWindow;
+var innerDocument = frame.contentDocument;
+
+shouldBe('new innerWindow.Comment("three").ownerDocument', 'innerDocument')
+shouldBeTrue('new innerWindow.Comment instanceof innerWindow.Comment');
+shouldBe('Object.getPrototypeOf(new innerWindow.Comment)', 'innerWindow.Comment.prototype');
+
+</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 b2f6e49..b5d76b8 100644
--- a/LayoutTests/fast/dom/dom-constructors-expected.txt
+++ b/LayoutTests/fast/dom/dom-constructors-expected.txt
@@ -6,7 +6,6 @@
PASS TryAllocate('Attr') is 'exception'
PASS TryAllocate('CharacterData') is 'exception'
PASS TryAllocate('CDATASection') is 'exception'
-PASS TryAllocate('Comment') is 'exception'
PASS TryAllocate('Document') is 'exception'
PASS TryAllocate('DocumentType') is 'exception'
PASS TryAllocate('Element') is 'exception'
@@ -125,6 +124,9 @@
PASS TryAllocate('EventTarget') is 'no constructor'
PASS TryAllocate('EventListener') is 'no constructor'
PASS TryAllocate('NPObject') is 'no constructor'
+PASS TryAllocate('Comment') is '[object Comment]'
+PASS TryAllocate('Comment') is '[object Comment]'
+PASS TryAllocate('Comment') is '[object Comment]'
PASS TryAllocate('DOMParser') is '[object DOMParser]'
PASS TryAllocate('DOMParser') is '[object DOMParser]'
PASS TryAllocate('DOMParser') is '[object DOMParser]'
diff --git a/LayoutTests/fast/dom/dom-constructors.html b/LayoutTests/fast/dom/dom-constructors.html
index 689f1a2..0c1d059 100644
--- a/LayoutTests/fast/dom/dom-constructors.html
+++ b/LayoutTests/fast/dom/dom-constructors.html
@@ -16,7 +16,6 @@
'Attr',
'CharacterData',
'CDATASection',
- 'Comment',
'Document',
'DocumentType',
'Element',
@@ -133,6 +132,7 @@
// These objects should have a working constructor.
var objects_constructor = [
+ 'Comment',
'DOMParser',
'DocumentFragment',
'Range',
diff --git a/Source/core/dom/Comment.cpp b/Source/core/dom/Comment.cpp
index 5b8374f..eb525bd 100644
--- a/Source/core/dom/Comment.cpp
+++ b/Source/core/dom/Comment.cpp
@@ -37,6 +37,11 @@
return adoptRef(new Comment(document, text));
}
+PassRefPtr<Comment> Comment::create(ScriptExecutionContext* context, const String& text)
+{
+ return adoptRef(new Comment(toDocument(context), text));
+}
+
String Comment::nodeName() const
{
return commentAtom.string();
diff --git a/Source/core/dom/Comment.h b/Source/core/dom/Comment.h
index df79fbb..5ef2285 100644
--- a/Source/core/dom/Comment.h
+++ b/Source/core/dom/Comment.h
@@ -27,9 +27,12 @@
namespace WebCore {
+class ScriptExecutionContext;
+
class Comment FINAL : public CharacterData {
public:
static PassRefPtr<Comment> create(Document*, const String&);
+ static PassRefPtr<Comment> create(ScriptExecutionContext*, const String&);
private:
Comment(Document*, const String&);
diff --git a/Source/core/dom/Comment.idl b/Source/core/dom/Comment.idl
index 5c07e2f..3a46dd7 100644
--- a/Source/core/dom/Comment.idl
+++ b/Source/core/dom/Comment.idl
@@ -17,6 +17,9 @@
* Boston, MA 02110-1301, USA.
*/
-interface Comment : CharacterData {
+[
+ Constructor([Default=NullString] optional DOMString data),
+ CallWith=ScriptExecutionContext
+] interface Comment : CharacterData {
};