LibWeb: Allow splitting surrogate pairs in CharacterData.replaceData()

We're expected to handle this situation gracefully, and certainly not
by falling apart like we were.

Found by Domato.
This commit is contained in:
Andreas Kling 2024-07-19 20:02:14 +02:00 committed by Andreas Kling
parent 416c478876
commit 33207174a9
Notes: github-actions[bot] 2024-07-20 04:41:59 +00:00
3 changed files with 23 additions and 2 deletions

View File

@ -0,0 +1,5 @@
Before replaceData:
[0]: 53997
[1]: 56998
After replaceData:
[0]: 56998

View File

@ -0,0 +1,16 @@
<script src="../include.js"></script>
<script>
test(() => {
const str = '\uD2ED\uDEA6';
let t = document.createTextNode(str);
println("Before replaceData:");
for (let i = 0; i < t.length; ++i) {
println("[" + i + "]: " + t.data.charCodeAt(i));
}
t.replaceData(0, 1, '')
println("After replaceData:");
for (let i = 0; i < t.length; ++i) {
println("[" + i + "]: " + t.data.charCodeAt(i));
}
});
</script>

View File

@ -87,9 +87,9 @@ WebIDL::ExceptionOr<void> CharacterData::replace_data(size_t offset, size_t coun
// 6. Let delete offset be offset + datas length.
// 7. Starting from delete offset code units, remove count code units from nodes data.
StringBuilder builder;
builder.append(MUST(utf16_view.substring_view(0, offset).to_utf8()));
builder.append(MUST(utf16_view.substring_view(0, offset).to_utf8(Utf16View::AllowInvalidCodeUnits::Yes)));
builder.append(data);
builder.append(MUST(utf16_view.substring_view(offset + count).to_utf8()));
builder.append(MUST(utf16_view.substring_view(offset + count).to_utf8(Utf16View::AllowInvalidCodeUnits::Yes)));
m_data = MUST(builder.to_string());
// 8. For each live range whose start node is node and start offset is greater than offset but less than or equal to offset plus count, set its start offset to offset.