Title: [203047] trunk
Revision
203047
Author
[email protected]
Date
2016-07-10 22:06:24 -0700 (Sun, 10 Jul 2016)

Log Message

Setting document.title reuses <title>'s textnode child
https://bugs.webkit.org/show_bug.cgi?id=28864
<rdar://problem/7186473>

Reviewed by Benjamin Poulain.

Source/WebCore:

Setting document.title should be equivalent to setting the 'textContent'
IDL attribute of the <title> element:
- https://html.spec.whatwg.org/multipage/dom.html#document.title

In particular, this means we should always create a new Text node and
replace all the <title>'s children with this new Node, as per:
- https://dom.spec.whatwg.org/#dom-node-textcontent

Previously, WebKit would in some cases reuse the existing <title>'s
Text node and merely update its data.

Firefox and Chrome behave as per the specification so this aligns our
behavior with other major browsers as well.

Test: fast/dom/title-setter-new-text-node.html

* dom/Document.cpp:
(WebCore::Document::setTitle):
- Call Node::setTextContent() instead of HTMLTitleElement::setText(),
  as per the specification.
- Take an ExceptionCode parameter and pass it to Node::setTextContent()
  as it may throw.

* dom/Document.h:
* dom/Document.idl:

* html/HTMLTitleElement.cpp:
(WebCore::HTMLTitleElement::setText):
Update implementation of HTMLTitleElement::setText() to call
setTextContent() as per the specification:
- https://html.spec.whatwg.org/multipage/semantics.html#dom-title-text

* html/HTMLTitleElement.h:
* html/HTMLTitleElement.idl:

* html/ImageDocument.cpp:
(WebCore::ImageDocument::finishedParsing):

* svg/SVGTitleElement.cpp:
* svg/SVGTitleElement.h:
Drop setText() setter which was duplicated from HTMLTitleElement::setText()
now that Document::setTitle() calls SVGTitleElement::setTextContent()
instead.

LayoutTests:

* fast/dom/title-setter-new-text-node-expected.txt: Added.
* fast/dom/title-setter-new-text-node.html: Added.
Add test coverage.

* fast/dom/title-text-property-2-expected.txt:
* fast/dom/title-text-property-expected.txt:
* http/tests/globalhistory/history-delegate-basic-title-expected.txt:
Rebaseline a few tests now that we do not reuse the <title>'s text
node child.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (203046 => 203047)


--- trunk/LayoutTests/ChangeLog	2016-07-11 04:28:03 UTC (rev 203046)
+++ trunk/LayoutTests/ChangeLog	2016-07-11 05:06:24 UTC (rev 203047)
@@ -1,3 +1,21 @@
+2016-07-10  Chris Dumez  <[email protected]>
+
+        Setting document.title reuses <title>'s textnode child
+        https://bugs.webkit.org/show_bug.cgi?id=28864
+        <rdar://problem/7186473>
+
+        Reviewed by Benjamin Poulain.
+
+        * fast/dom/title-setter-new-text-node-expected.txt: Added.
+        * fast/dom/title-setter-new-text-node.html: Added.
+        Add test coverage.
+
+        * fast/dom/title-text-property-2-expected.txt:
+        * fast/dom/title-text-property-expected.txt:
+        * http/tests/globalhistory/history-delegate-basic-title-expected.txt:
+        Rebaseline a few tests now that we do not reuse the <title>'s text
+        node child.
+
 2016-07-10  Commit Queue  <[email protected]>
 
         Unreviewed, rolling out r203037.

Added: trunk/LayoutTests/fast/dom/title-setter-new-text-node-expected.txt (0 => 203047)


--- trunk/LayoutTests/fast/dom/title-setter-new-text-node-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/fast/dom/title-setter-new-text-node-expected.txt	2016-07-11 05:06:24 UTC (rev 203047)
@@ -0,0 +1,16 @@
+Tests that setting document.title does not reuse title's text node child
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS document.title is "aaa"
+PASS oldTextNode.textContent is "aaa"
+document.title = 'bbb'
+PASS oldTextNode.textContent is "aaa"
+PASS oldTextNode is not newTextNode
+PASS document.title is "bbb"
+PASS newTextNode.textContent is "bbb"
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/fast/dom/title-setter-new-text-node-svg-expected.txt (0 => 203047)


--- trunk/LayoutTests/fast/dom/title-setter-new-text-node-svg-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/fast/dom/title-setter-new-text-node-svg-expected.txt	2016-07-11 05:06:24 UTC (rev 203047)
@@ -0,0 +1,16 @@
+Tests that setting document.title does not reuse title's text node child
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS svgDocument.title is "aaa"
+PASS oldTextNode.textContent is "aaa"
+svgDocument.title = 'bbb'
+PASS oldTextNode.textContent is "aaa"
+PASS oldTextNode is not newTextNode
+PASS svgDocument.title is "bbb"
+PASS newTextNode.textContent is "bbb"
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/fast/dom/title-setter-new-text-node-svg.html (0 => 203047)


--- trunk/LayoutTests/fast/dom/title-setter-new-text-node-svg.html	                        (rev 0)
+++ trunk/LayoutTests/fast/dom/title-setter-new-text-node-svg.html	2016-07-11 05:06:24 UTC (rev 203047)
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<html>
+<body>
+<script src=""
+<script>
+description("Tests that setting document.title does not reuse title's text node child");
+
+var SVG_NAMESPACE = "http://www.w3.org/2000/svg";
+
+var svgDocument = document.implementation.createDocument(SVG_NAMESPACE, "svg", null);
+var title = svgDocument.createElementNS(SVG_NAMESPACE, "title");
+title.textContent = "aaa";
+svgDocument.documentElement.appendChild(title);
+
+oldTextNode = svgDocument.getElementsByTagName("title")[0].firstChild;
+shouldBeEqualToString("svgDocument.title", "aaa");
+shouldBeEqualToString("oldTextNode.textContent", "aaa");
+evalAndLog("svgDocument.title = 'bbb'");
+shouldBeEqualToString("oldTextNode.textContent", "aaa");
+newTextNode = svgDocument.getElementsByTagName("title")[0].firstChild;
+shouldNotBe("oldTextNode", "newTextNode");
+shouldBeEqualToString("svgDocument.title", "bbb");
+shouldBeEqualToString("newTextNode.textContent", "bbb");
+</script>
+<script src=""
+</body>
+</html>

Added: trunk/LayoutTests/fast/dom/title-setter-new-text-node.html (0 => 203047)


--- trunk/LayoutTests/fast/dom/title-setter-new-text-node.html	                        (rev 0)
+++ trunk/LayoutTests/fast/dom/title-setter-new-text-node.html	2016-07-11 05:06:24 UTC (rev 203047)
@@ -0,0 +1,29 @@
+<!DOCTYPE html>
+<html>
+<head>
+<title>aaa</title>
+<script src=""
+</head>
+<body _onload_="runTest()">
+<script>
+description("Tests that setting document.title does not reuse title's text node child");
+jsTestIsAsync = true;
+
+function runTest()
+{
+    oldTextNode = document.getElementsByTagName("title")[0].firstChild;
+    shouldBeEqualToString("document.title", "aaa");
+    shouldBeEqualToString("oldTextNode.textContent", "aaa");
+    evalAndLog("document.title = 'bbb'");
+    shouldBeEqualToString("oldTextNode.textContent", "aaa");
+    newTextNode = document.getElementsByTagName("title")[0].firstChild;
+    shouldNotBe("oldTextNode", "newTextNode");
+    shouldBeEqualToString("document.title", "bbb");
+    shouldBeEqualToString("newTextNode.textContent", "bbb");
+
+    finishJSTest();
+}
+</script>
+<script src=""
+</body>
+</html>

Modified: trunk/LayoutTests/fast/dom/title-text-property-2-expected.txt (203046 => 203047)


--- trunk/LayoutTests/fast/dom/title-text-property-2-expected.txt	2016-07-11 04:28:03 UTC (rev 203046)
+++ trunk/LayoutTests/fast/dom/title-text-property-2-expected.txt	2016-07-11 05:06:24 UTC (rev 203047)
@@ -1,4 +1,7 @@
 TITLE CHANGED: '1. setting document.title'
+TITLE CHANGED: ''
+TITLE CHANGED: '1. setting document.title'
+TITLE CHANGED: ''
 TITLE CHANGED: '2. setting title.text'
 TITLE CHANGED: ''
 

Modified: trunk/LayoutTests/fast/dom/title-text-property-expected.txt (203046 => 203047)


--- trunk/LayoutTests/fast/dom/title-text-property-expected.txt	2016-07-11 04:28:03 UTC (rev 203046)
+++ trunk/LayoutTests/fast/dom/title-text-property-expected.txt	2016-07-11 05:06:24 UTC (rev 203047)
@@ -1,3 +1,4 @@
+TITLE CHANGED: ''
 TITLE CHANGED: 'This is the new title'
 Original title is: 'Original Title'
 Setting new title to: 'This is the new title'

Modified: trunk/LayoutTests/http/tests/globalhistory/history-delegate-basic-title-expected.txt (203046 => 203047)


--- trunk/LayoutTests/http/tests/globalhistory/history-delegate-basic-title-expected.txt	2016-07-11 04:28:03 UTC (rev 203046)
+++ trunk/LayoutTests/http/tests/globalhistory/history-delegate-basic-title-expected.txt	2016-07-11 05:06:24 UTC (rev 203047)
@@ -1,4 +1,6 @@
 WebView navigated to url "http://127.0.0.1:8000/globalhistory/history-delegate-basic-title.html" with title "" with HTTP equivalent method "GET".  The navigation was successful and was not a client redirect.
 WebView updated the title for history URL "http://127.0.0.1:8000/globalhistory/history-delegate-basic-title.html" to "Test Title 1".
 WebView updated the title for history URL "http://127.0.0.1:8000/globalhistory/history-delegate-basic-title.html" to "Test Title 2".
+WebView updated the title for history URL "http://127.0.0.1:8000/globalhistory/history-delegate-basic-title.html" to "".
+WebView updated the title for history URL "http://127.0.0.1:8000/globalhistory/history-delegate-basic-title.html" to "Test Title 2".
 This test sees if the history delegate is notified of title changes.

Modified: trunk/Source/WebCore/ChangeLog (203046 => 203047)


--- trunk/Source/WebCore/ChangeLog	2016-07-11 04:28:03 UTC (rev 203046)
+++ trunk/Source/WebCore/ChangeLog	2016-07-11 05:06:24 UTC (rev 203047)
@@ -1,3 +1,55 @@
+2016-07-10  Chris Dumez  <[email protected]>
+
+        Setting document.title reuses <title>'s textnode child
+        https://bugs.webkit.org/show_bug.cgi?id=28864
+        <rdar://problem/7186473>
+
+        Reviewed by Benjamin Poulain.
+
+        Setting document.title should be equivalent to setting the 'textContent'
+        IDL attribute of the <title> element:
+        - https://html.spec.whatwg.org/multipage/dom.html#document.title
+
+        In particular, this means we should always create a new Text node and
+        replace all the <title>'s children with this new Node, as per:
+        - https://dom.spec.whatwg.org/#dom-node-textcontent
+
+        Previously, WebKit would in some cases reuse the existing <title>'s
+        Text node and merely update its data.
+
+        Firefox and Chrome behave as per the specification so this aligns our
+        behavior with other major browsers as well.
+
+        Test: fast/dom/title-setter-new-text-node.html
+
+        * dom/Document.cpp:
+        (WebCore::Document::setTitle):
+        - Call Node::setTextContent() instead of HTMLTitleElement::setText(),
+          as per the specification.
+        - Take an ExceptionCode parameter and pass it to Node::setTextContent()
+          as it may throw.
+
+        * dom/Document.h:
+        * dom/Document.idl:
+
+        * html/HTMLTitleElement.cpp:
+        (WebCore::HTMLTitleElement::setText):
+        Update implementation of HTMLTitleElement::setText() to call
+        setTextContent() as per the specification:
+        - https://html.spec.whatwg.org/multipage/semantics.html#dom-title-text
+
+        * html/HTMLTitleElement.h:
+        * html/HTMLTitleElement.idl:
+
+        * html/ImageDocument.cpp:
+        (WebCore::ImageDocument::finishedParsing):
+
+        * svg/SVGTitleElement.cpp:
+        * svg/SVGTitleElement.h:
+        Drop setText() setter which was duplicated from HTMLTitleElement::setText()
+        now that Document::setTitle() calls SVGTitleElement::setTextContent()
+        instead.
+
 2016-07-10  Zalan Bujtas  <[email protected]>
 
         Fix LogicalSelectionOffsetCaches to work with detached render tree.

Modified: trunk/Source/WebCore/dom/Document.cpp (203046 => 203047)


--- trunk/Source/WebCore/dom/Document.cpp	2016-07-11 04:28:03 UTC (rev 203046)
+++ trunk/Source/WebCore/dom/Document.cpp	2016-07-11 05:06:24 UTC (rev 203047)
@@ -1623,7 +1623,7 @@
     }
 }
 
-void Document::setTitle(const String& title)
+void Document::setTitle(const String& title, ExceptionCode& ec)
 {
     if (!m_titleElement) {
         if (isHTMLDocument() || isXHTMLDocument()) {
@@ -1646,9 +1646,9 @@
     updateTitle(StringWithDirection(title, LTR));
 
     if (is<HTMLTitleElement>(m_titleElement.get()))
-        downcast<HTMLTitleElement>(*m_titleElement).setText(title);
+        downcast<HTMLTitleElement>(*m_titleElement).setTextContent(title, ec);
     else if (is<SVGTitleElement>(m_titleElement.get()))
-        downcast<SVGTitleElement>(*m_titleElement).setText(title);
+        downcast<SVGTitleElement>(*m_titleElement).setTextContent(title, ec);
 }
 
 void Document::updateTitleElement(Element* newTitleElement)

Modified: trunk/Source/WebCore/dom/Document.h (203046 => 203047)


--- trunk/Source/WebCore/dom/Document.h	2016-07-11 04:28:03 UTC (rev 203046)
+++ trunk/Source/WebCore/dom/Document.h	2016-07-11 05:06:24 UTC (rev 203047)
@@ -863,7 +863,7 @@
 
     // Used by DOM bindings; no direction known.
     String title() const { return m_title.string(); }
-    void setTitle(const String&);
+    void setTitle(const String&, ExceptionCode&);
 
     void titleElementAdded(Element& titleElement);
     void titleElementRemoved(Element& titleElement);

Modified: trunk/Source/WebCore/dom/Document.idl (203046 => 203047)


--- trunk/Source/WebCore/dom/Document.idl	2016-07-11 04:28:03 UTC (rev 203046)
+++ trunk/Source/WebCore/dom/Document.idl	2016-07-11 05:06:24 UTC (rev 203047)
@@ -174,7 +174,7 @@
     // Moved down from HTMLDocument
 
     // FIXME: Should not have [TreatNullAs=EmptyString].
-    [TreatNullAs=EmptyString] attribute DOMString title;
+    [TreatNullAs=EmptyString, SetterRaisesException] attribute DOMString title;
 
     readonly attribute DOMString referrer;
 #if defined(LANGUAGE_JAVASCRIPT) && LANGUAGE_JAVASCRIPT

Modified: trunk/Source/WebCore/html/HTMLTitleElement.cpp (203046 => 203047)


--- trunk/Source/WebCore/html/HTMLTitleElement.cpp	2016-07-11 04:28:03 UTC (rev 203046)
+++ trunk/Source/WebCore/html/HTMLTitleElement.cpp	2016-07-11 05:06:24 UTC (rev 203047)
@@ -92,25 +92,9 @@
     return StringWithDirection(text(), direction);
 }
 
-void HTMLTitleElement::setText(const String& value)
+void HTMLTitleElement::setText(const String& value, ExceptionCode& ec)
 {
-    Ref<HTMLTitleElement> protectedThis(*this);
-    
-    if (!value.isEmpty() && hasOneChild() && is<Text>(*firstChild())) {
-        downcast<Text>(*firstChild()).setData(value);
-        return;
-    }
-
-    // We make a copy here because entity of "value" argument can be Document::m_title,
-    // which goes empty during removeChildren() invocation below,
-    // which causes HTMLTitleElement::childrenChanged(), which ends up calling Document::setTitle().
-    String valueCopy(value);
-
-    if (hasChildNodes())
-        removeChildren();
-
-    if (!valueCopy.isEmpty())
-        appendChild(document().createTextNode(valueCopy), IGNORE_EXCEPTION);
+    setTextContent(value, ec);
 }
 
 }

Modified: trunk/Source/WebCore/html/HTMLTitleElement.h (203046 => 203047)


--- trunk/Source/WebCore/html/HTMLTitleElement.h	2016-07-11 04:28:03 UTC (rev 203046)
+++ trunk/Source/WebCore/html/HTMLTitleElement.h	2016-07-11 05:06:24 UTC (rev 203047)
@@ -32,7 +32,7 @@
     static Ref<HTMLTitleElement> create(const QualifiedName&, Document&);
 
     String text() const;
-    void setText(const String&);
+    void setText(const String&, ExceptionCode&);
 
     const StringWithDirection& textWithDirection() const { return m_title; }
 

Modified: trunk/Source/WebCore/html/HTMLTitleElement.idl (203046 => 203047)


--- trunk/Source/WebCore/html/HTMLTitleElement.idl	2016-07-11 04:28:03 UTC (rev 203046)
+++ trunk/Source/WebCore/html/HTMLTitleElement.idl	2016-07-11 05:06:24 UTC (rev 203047)
@@ -18,6 +18,6 @@
  */
 
 interface HTMLTitleElement : HTMLElement {
-    attribute DOMString text;
+    [SetterRaisesException] attribute DOMString text;
 };
 

Modified: trunk/Source/WebCore/html/ImageDocument.cpp (203046 => 203047)


--- trunk/Source/WebCore/html/ImageDocument.cpp	2016-07-11 04:28:03 UTC (rev 203046)
+++ trunk/Source/WebCore/html/ImageDocument.cpp	2016-07-11 05:06:24 UTC (rev 203047)
@@ -164,7 +164,7 @@
             String name = decodeURLEscapeSequences(url().lastPathComponent());
             if (name.isEmpty())
                 name = url().host();
-            setTitle(imageTitle(name, size));
+            setTitle(imageTitle(name, size), IGNORE_EXCEPTION);
         }
 
         imageUpdated();

Modified: trunk/Source/WebCore/svg/SVGTitleElement.cpp (203046 => 203047)


--- trunk/Source/WebCore/svg/SVGTitleElement.cpp	2016-07-11 04:28:03 UTC (rev 203046)
+++ trunk/Source/WebCore/svg/SVGTitleElement.cpp	2016-07-11 05:06:24 UTC (rev 203047)
@@ -63,25 +63,4 @@
     document().titleElementTextChanged(*this);
 }
 
-void SVGTitleElement::setText(const String& value)
-{
-    Ref<SVGTitleElement> protectedThis(*this);
-
-    if (!value.isEmpty() && hasOneChild() && is<Text>(*firstChild())) {
-        downcast<Text>(*firstChild()).setData(value);
-        return;
-    }
-
-    // We make a copy here because entity of "value" argument can be Document::m_title,
-    // which goes empty during removeChildren() invocation below,
-    // which causes SVGTitleElement::childrenChanged(), which ends up calling Document::setTitle().
-    String valueCopy(value);
-
-    if (hasChildNodes())
-        removeChildren();
-
-    if (!valueCopy.isEmpty())
-        appendChild(document().createTextNode(valueCopy), IGNORE_EXCEPTION);
 }
-
-}

Modified: trunk/Source/WebCore/svg/SVGTitleElement.h (203046 => 203047)


--- trunk/Source/WebCore/svg/SVGTitleElement.h	2016-07-11 04:28:03 UTC (rev 203046)
+++ trunk/Source/WebCore/svg/SVGTitleElement.h	2016-07-11 05:06:24 UTC (rev 203047)
@@ -30,8 +30,6 @@
 public:
     static Ref<SVGTitleElement> create(const QualifiedName&, Document&);
 
-    void setText(const String&);
-
 private:
     SVGTitleElement(const QualifiedName&, Document&);
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to