LibJS: Implement String.prototype.substr according to the spec

Fixes #6325

The JavaScript on the HTML Spec site that caused the crash is:
    window.location.hash.substr(1)

Of course, window.location.hash can be the empty string. The spec allows
for calling substr(1) on an empty string, but our partial implementation
wasn't handling it properly.
This commit is contained in:
Timothy Flynn 2021-04-14 17:46:18 -04:00 committed by Andreas Kling
commit b6093ae2e3
Notes: sideshowbarker 2024-07-18 20:19:54 +09:00
2 changed files with 22 additions and 16 deletions

View file

@ -1,6 +1,9 @@
test("basic functionality", () => {
expect(String.prototype.substr).toHaveLength(2);
expect("".substr(1)).toBe("");
expect("".substr()).toBe("");
expect("".substr(-1)).toBe("");
expect("hello friends".substr()).toBe("hello friends");
expect("hello friends".substr(1)).toBe("ello friends");
expect("hello friends".substr(0, 5)).toBe("hello");