1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 23:37:36 +00:00

LibWeb: Implement the document title attribute closer to the spec

The main differences between our current implementation and the spec
are:

    * The title element need not be a child of the head element.

    * If the title element does not exist, the default value should be
      the empty string - we currently return a null string.

    * We've since added AOs for several of the spec steps here, so we
      do not need to implement those steps inline.
This commit is contained in:
Timothy Flynn 2023-06-07 16:28:06 -04:00 committed by Andreas Kling
parent 43b3673198
commit f2a28e83de
4 changed files with 124 additions and 32 deletions

View file

@ -0,0 +1,14 @@
1: ""
2a: 0
2b: 1
2c: "This is a title!"
2d: "This is a title!"
3: ""
4a: 3
4b: ""
4c: ""
4d: ""
4e: 3
4f: "This is another title!"
4g: ""
4h: ""

View file

@ -0,0 +1,42 @@
<script src="include.js"></script>
<script>
test(() => {
// The title is the empty string by default.
println(`1: "${document.title}"`);
// When the title is set, and a title element does not exist, one is added to to head element.
let titleElements = document.getElementsByTagName('title');
println(`2a: ${titleElements.length}`)
document.title = 'This is a title!';
titleElements = document.getElementsByTagName('title');
println(`2b: ${titleElements.length}`)
println(`2c: "${titleElements[0].innerText}"`);
println(`2d: "${document.title}"`);
// Removing the title element sets the title back to default.
titleElements[0].remove();
println(`3: "${document.title}"`);
// After adding several title elements to the body, setting the title updates the text
// content of only the first title element.
document.body.appendChild(document.createElement('title'));
document.body.appendChild(document.createElement('title'));
document.body.appendChild(document.createElement('title'));
titleElements = document.getElementsByTagName('title');
println(`4a: ${titleElements.length}`)
println(`4b: "${titleElements[0].innerText}"`);
println(`4c: "${titleElements[1].innerText}"`);
println(`4d: "${titleElements[2].innerText}"`);
document.title = 'This is another title!';
titleElements = document.getElementsByTagName('title');
println(`4e: ${titleElements.length}`)
println(`4f: "${titleElements[0].innerText}"`);
println(`4g: "${titleElements[1].innerText}"`);
println(`4h: "${titleElements[2].innerText}"`);
});
</script>