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

This commit is contained in:
Shannon Booth 2024-10-20 13:50:39 +13:00 committed by Tim Ledbetter
commit b999f925dc
Notes: github-actions[bot] 2024-10-20 10:19:54 +00:00
3 changed files with 25 additions and 2 deletions

View file

@ -0,0 +1,5 @@
Before substringData:
[0]: 55357
[1]: 56374
After substringData(0, 1):
[0]: 55357

View file

@ -0,0 +1,18 @@
<script src="../include.js"></script>
<script>
test(() => {
const str = '\uD83D\uDC36'; // 🐶
let t = document.createTextNode(str);
println("Before substringData:");
for (let i = 0; i < t.length; ++i) {
println("[" + i + "]: " + t.data.charCodeAt(i));
}
// Break the surrogate pair
const invalidSubstring = t.substringData(0, 1);
println("After substringData(0, 1):");
for (let i = 0; i < invalidSubstring.length; ++i) {
println("[" + i + "]: " + invalidSubstring.charCodeAt(i));
}
});
</script>

View file

@ -57,10 +57,10 @@ WebIDL::ExceptionOr<String> CharacterData::substring_data(size_t offset, size_t
// 3. If offset plus count is greater than length, return a string whose value is the code units from the offsetth code unit // 3. If offset plus count is greater than length, return a string whose value is the code units from the offsetth code unit
// to the end of nodes data, and then return. // to the end of nodes data, and then return.
if (offset + count > length) if (offset + count > length)
return MUST(utf16_view.substring_view(offset).to_utf8()); return MUST(utf16_view.substring_view(offset).to_utf8(Utf16View::AllowInvalidCodeUnits::Yes));
// 4. Return a string whose value is the code units from the offsetth code unit to the offset+countth code unit in nodes data. // 4. Return a string whose value is the code units from the offsetth code unit to the offset+countth code unit in nodes data.
return MUST(utf16_view.substring_view(offset, count).to_utf8()); return MUST(utf16_view.substring_view(offset, count).to_utf8(Utf16View::AllowInvalidCodeUnits::Yes));
} }
// https://dom.spec.whatwg.org/#concept-cd-replace // https://dom.spec.whatwg.org/#concept-cd-replace