Mutation observers doesn't get notified of character data mutation made by the parser
authorrniwa@webkit.org <rniwa@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 21 Nov 2018 21:33:48 +0000 (21:33 +0000)
committerrniwa@webkit.org <rniwa@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 21 Nov 2018 21:33:48 +0000 (21:33 +0000)
https://bugs.webkit.org/show_bug.cgi?id=191874

Reviewed by Antti Koivisto.

Source/WebCore:

Fixed the bug that CharacterData::parserAppendData was never notifying MutationObserver.

Test: fast/dom/MutationObserver/observe-parser-character-data-change.html

* dom/CharacterData.cpp:
(WebCore::CharacterData::parserAppendData):

LayoutTests:

Added a regression test.

* fast/dom/MutationObserver/observe-parser-character-data-change-expected.txt: Added.
* fast/dom/MutationObserver/observe-parser-character-data-change.html: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@238429 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/fast/dom/MutationObserver/observe-parser-character-data-change-expected.txt [new file with mode: 0644]
LayoutTests/fast/dom/MutationObserver/observe-parser-character-data-change.html [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/dom/CharacterData.cpp

index 854baf2..0618c22 100644 (file)
@@ -1,3 +1,15 @@
+2018-11-21  Ryosuke Niwa  <rniwa@webkit.org>
+
+        Mutation observers doesn't get notified of character data mutation made by the parser
+        https://bugs.webkit.org/show_bug.cgi?id=191874
+
+        Reviewed by Antti Koivisto.
+
+        Added a regression test.
+
+        * fast/dom/MutationObserver/observe-parser-character-data-change-expected.txt: Added.
+        * fast/dom/MutationObserver/observe-parser-character-data-change.html: Added.
+
 2018-11-21  Zalan Bujtas <zalan@apple.com>
 
         [LFC][IFC] Border should be considered as non-breakable space
diff --git a/LayoutTests/fast/dom/MutationObserver/observe-parser-character-data-change-expected.txt b/LayoutTests/fast/dom/MutationObserver/observe-parser-character-data-change-expected.txt
new file mode 100644 (file)
index 0000000..2f837ac
--- /dev/null
@@ -0,0 +1,4 @@
+This tests observing the character data change by the HTML parser.
+
+PASS
+ab
diff --git a/LayoutTests/fast/dom/MutationObserver/observe-parser-character-data-change.html b/LayoutTests/fast/dom/MutationObserver/observe-parser-character-data-change.html
new file mode 100644 (file)
index 0000000..e6ee30c
--- /dev/null
@@ -0,0 +1,37 @@
+<!DOCTYPE html>
+<html>
+<body>
+<p>This tests observing the character data change by the HTML parser.</p>
+<pre id="log"></pre>
+<div id="parent"><script>
+
+if (window.testRunner)
+    testRunner.dumpAsText();
+
+const log = document.getElementById('log');
+
+document.write('a');
+let observer = new MutationObserver((mutations) => {
+    let result = 'PASS';
+    if (mutations.length != 1)
+        result = `FAIL - expected 1 mutation record but got ${mutations.length}`;
+    else if (mutations[0].type != 'characterData')
+        result = `FAIL - expected characterData mutation record but got ${mutations[0].type}`;
+    else if (mutations[0].oldValue != 'a')
+        result = `FAIL - expected oldValue to be "a" but got "${mutations[0].oldValue}"`;
+    log.textContent = result;
+});
+observer.observe(document.getElementById('parent'), {childList: true, subtree: true, characterData: true, characterDataOldValue: true});
+document.write('b');
+
+function fail() {
+    if (!log.textContent)
+        log.textContent = 'FAIL - did not receive a mutation record in time';
+}
+
+Promise.resolve().then(fail);
+window.onload = () => fail;
+
+</script></div>
+</body>
+</html>
\ No newline at end of file
index 5dc1720..35c3a00 100644 (file)
@@ -1,3 +1,17 @@
+2018-11-20  Ryosuke Niwa  <rniwa@webkit.org>
+
+        Mutation observers doesn't get notified of character data mutation made by the parser
+        https://bugs.webkit.org/show_bug.cgi?id=191874
+
+        Reviewed by Antti Koivisto.
+
+        Fixed the bug that CharacterData::parserAppendData was never notifying MutationObserver.
+
+        Test: fast/dom/MutationObserver/observe-parser-character-data-change.html
+
+        * dom/CharacterData.cpp:
+        (WebCore::CharacterData::parserAppendData):
+
 2018-11-21  Claudio Saavedra  <csaavedra@igalia.com>
 
         [SOUP] Follow-up robustness improvements to the certificate decoder
index 075d572..bf4d227 100644 (file)
@@ -96,6 +96,7 @@ unsigned CharacterData::parserAppendData(const String& string, unsigned offset,
     if (!characterLengthLimit)
         return 0;
 
+    String oldData = m_data;
     if (string.is8Bit())
         m_data.append(string.characters8() + offset, characterLengthLimit);
     else
@@ -107,6 +108,10 @@ unsigned CharacterData::parserAppendData(const String& string, unsigned offset,
 
     notifyParentAfterChange(ContainerNode::ChildChangeSource::Parser);
 
+    auto mutationRecipients = MutationObserverInterestGroup::createForCharacterDataMutation(*this);
+    if (UNLIKELY(mutationRecipients))
+        mutationRecipients->enqueueMutationRecord(MutationRecord::createCharacterData(*this, oldData));
+
     return characterLengthLimit;
 }