Skip to content

Commit

Permalink
Implement document.title correctly per-spec
Browse files Browse the repository at this point in the history
Small changes to whitespace behavior and what happens when there is no head element.
  • Loading branch information
domenic committed Dec 19, 2016
1 parent ae19887 commit 5261738
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
11 changes: 11 additions & 0 deletions lib/jsdom/living/helpers/traversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,14 @@ exports.firstChildWithHTMLLocalNames = (parent, localNamesSet) => {
}
return null;
};

exports.firstDescendantWithHTMLLocalName = (parent, localName) => {
const iterator = domSymbolTree.treeIterator(parent);
for (const descendant of iterator) {
if (descendant._localName === localName && descendant._namespaceURI === "http://www.w3.org/1999/xhtml") {
return descendant;
}
}
return null;
};

37 changes: 24 additions & 13 deletions lib/jsdom/living/nodes/Document-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const NODE_TYPE = require("../node-type");
const memoizeQuery = require("../../utils").memoizeQuery;
const firstChildWithHTMLLocalName = require("../helpers/traversal").firstChildWithHTMLLocalName;
const firstChildWithHTMLLocalNames = require("../helpers/traversal").firstChildWithHTMLLocalNames;
const firstDescendantWithHTMLLocalName = require("../helpers/traversal").firstDescendantWithHTMLLocalName;
const whatwgURL = require("whatwg-url");
const domSymbolTree = require("../helpers/internal-constants").domSymbolTree;
const DOMException = require("../../web-idl/DOMException");
Expand Down Expand Up @@ -536,23 +537,33 @@ class DocumentImpl extends NodeImpl {
}

get title() {
const head = this.head;
const title = head ? firstChildWithHTMLLocalName(head, "title") : null;
return title ? title.textContent : "";
// TODO SVG

const titleElement = firstDescendantWithHTMLLocalName(this, "title");
let value = titleElement !== null ? titleElement.textContent : "";
value = value.replace(/[ \t\n\f\r]+/g, " ").replace(/^[ \t\n\f\r]+/, "").replace(/[ \t\n\f\r]+$/, "");
return value;
}

set title(val) {
let title = firstChildWithHTMLLocalName(this.head, "title");
if (!title) {
title = this.createElement("TITLE");
let head = this.head;
if (!head) {
head = this.createElement("HEAD");
this.documentElement.insertBefore(head, this.documentElement.firstChild);
}
head.appendChild(title);
// TODO SVG

const titleElement = firstDescendantWithHTMLLocalName(this, "title");
const headElement = this.head;

if (titleElement === null && headElement === null) {
return;
}
title.textContent = val;

let element;
if (titleElement !== null) {
element = titleElement;
} else {
element = this.createElement("title");
headElement.appendChild(element);
}

element.textContent = val;
}

get head() {
Expand Down
9 changes: 9 additions & 0 deletions test/web-platform-tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ describe("Web Platform Tests", () => {
"html/browsers/offline/browser-state/navigator_online_online.html",
// "html/browsers/windows/browsing-context-first-created.xhtml", // jsdom will try to feed <![CDATA[ to the script parser, causing errors
"html/dom/documents/dom-tree-accessors/Document.body.html",
"html/dom/documents/dom-tree-accessors/document.title-01.html",
"html/dom/documents/dom-tree-accessors/document.title-02.xhtml",
"html/dom/documents/dom-tree-accessors/document.title-03.html",
"html/dom/documents/dom-tree-accessors/document.title-04.xhtml",
"html/dom/documents/dom-tree-accessors/document.title-05.html",
"html/dom/documents/dom-tree-accessors/document.title-06.html",
"html/dom/documents/dom-tree-accessors/document.title-07.html",
"html/dom/documents/dom-tree-accessors/document.title-08.html",
// "html/dom/documents/dom-tree-accessors/document.title-09.html" // SVG stuff
"html/dom/dynamic-markup-insertion/document-writeln/document.writeln-02.html",
"html/dom/dynamic-markup-insertion/document-writeln/document.writeln-03.html",
"html/dom/elements/global-attributes/classlist-nonstring.html",
Expand Down

0 comments on commit 5261738

Please sign in to comment.