Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement DocumentOrShadowRoot
  • Loading branch information
TimothyGu committed Sep 27, 2019
1 parent abe6a89 commit 0f09068
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 16 deletions.
14 changes: 4 additions & 10 deletions lib/jsdom/living/nodes/Document-impl.js
Expand Up @@ -26,7 +26,10 @@ const { validateAndExtract } = require("../helpers/validate-names");
const { fireAnEvent } = require("../helpers/events");
const { shadowIncludingInclusiveDescendantsIterator } = require("../helpers/shadow-dom");

const DocumentOrShadowRootImpl = require("./DocumentOrShadowRoot-impl").implementation;
const GlobalEventHandlersImpl = require("./GlobalEventHandlers-impl").implementation;
const NonElementParentNodeImpl = require("./NonElementParentNode-impl").implementation;
const ParentNodeImpl = require("./ParentNode-impl").implementation;

const { clone, listOfElementsWithQualifiedName, listOfElementsWithNamespaceAndLocalName,
listOfElementsWithClassNames } = require("../node");
Expand All @@ -37,8 +40,6 @@ const CDATASection = require("../generated/CDATASection");
const Text = require("../generated/Text");
const DocumentFragment = require("../generated/DocumentFragment");
const DOMImplementation = require("../generated/DOMImplementation");
const NonElementParentNodeImpl = require("./NonElementParentNode-impl").implementation;
const ParentNodeImpl = require("./ParentNode-impl").implementation;
const Element = require("../generated/Element");
const HTMLUnknownElement = require("../generated/HTMLUnknownElement");
const SVGElement = require("../generated/SVGElement");
Expand Down Expand Up @@ -264,14 +265,6 @@ class DocumentImpl extends NodeImpl {
return this._currentScript;
}

get activeElement() {
if (this._lastFocusedElement) {
return this._lastFocusedElement;
}

return this.body;
}

get readyState() {
return this._currentDocumentReadiness;
}
Expand Down Expand Up @@ -871,6 +864,7 @@ class DocumentImpl extends NodeImpl {
}

eventAccessors.createEventAccessor(DocumentImpl.prototype, "readystatechange");
mixin(DocumentImpl.prototype, DocumentOrShadowRootImpl.prototype);
mixin(DocumentImpl.prototype, GlobalEventHandlersImpl.prototype);
mixin(DocumentImpl.prototype, NonElementParentNodeImpl.prototype);
mixin(DocumentImpl.prototype, ParentNodeImpl.prototype);
Expand Down
1 change: 0 additions & 1 deletion lib/jsdom/living/nodes/Document.webidl
Expand Up @@ -89,7 +89,6 @@ partial interface Document {

// user interaction
readonly attribute WindowProxy? defaultView;
readonly attribute Element? activeElement;
boolean hasFocus();
// [CEReactions] attribute DOMString designMode;
// [CEReactions] boolean execCommand(DOMString commandId, optional boolean showUI = false, optional DOMString value = "");
Expand Down
27 changes: 27 additions & 0 deletions lib/jsdom/living/nodes/DocumentOrShadowRoot-impl.js
@@ -0,0 +1,27 @@
"use strict";
const NODE_TYPE = require("../node-type");
const { getRoot, retarget } = require("../helpers/shadow-dom");

class DocumentOrShadowRootImpl {
get activeElement() {
let candidate = this._ownerDocument._lastFocusedElement || this._ownerDocument.body;
if (!candidate) {
return null;
}
candidate = retarget(candidate, this);
if (getRoot(candidate) !== this) {
return null;
}
if (candidate.nodeType !== NODE_TYPE.DOCUMENT_NODE) {
return candidate;
}
if (candidate.body !== null) {
return candidate.body;
}
return candidate.documentElement;
}
}

module.exports = {
implementation: DocumentOrShadowRootImpl
};
10 changes: 10 additions & 0 deletions lib/jsdom/living/nodes/DocumentOrShadowRoot.webidl
@@ -0,0 +1,10 @@
// https://dom.spec.whatwg.org/#documentorshadowroot
interface mixin DocumentOrShadowRoot {
};
Document includes DocumentOrShadowRoot;
ShadowRoot includes DocumentOrShadowRoot;

// https://html.spec.whatwg.org/multipage/dom.html#documentorshadowroot
partial interface mixin DocumentOrShadowRoot {
readonly attribute Element? activeElement;
};
4 changes: 4 additions & 0 deletions lib/jsdom/living/nodes/ShadowRoot-impl.js
Expand Up @@ -3,8 +3,10 @@
const { parseFragment } = require("../../browser/parser");
const { fragmentSerialization } = require("../domparsing/serialization.js");
const { getRoot } = require("../helpers/shadow-dom");
const { mixin } = require("../../utils");

const DocumentFragment = require("./DocumentFragment-impl").implementation;
const DocumentOrShadowRootImpl = require("./DocumentOrShadowRoot-impl").implementation;

class ShadowRootImpl extends DocumentFragment {
constructor(args, privateData) {
Expand Down Expand Up @@ -40,6 +42,8 @@ class ShadowRootImpl extends DocumentFragment {
}
}

mixin(ShadowRootImpl.prototype, DocumentOrShadowRootImpl.prototype);

module.exports = {
implementation: ShadowRootImpl
};
6 changes: 1 addition & 5 deletions test/web-platform-tests/to-run.yaml
Expand Up @@ -852,15 +852,13 @@ DocumentOrShadowRoot-prototype-elementFromPoint.html: [fail, offsetTop not imple
Element-interface-attachShadow-custom-element.html: [fail, CustomElement.define is not implemented]
MouseEvent-prototype-offsetX-offsetY.html: [fail, offsetTop not implemented]
Range-prototype-insertNode.html: [fail, Range is not implemented]
ShadowRoot-interface.html: [fail, shadowRoot.styleSheet and shadowRoot.activeElement are not yet implemented]
ShadowRoot-interface.html: [fail, shadowRoot.styleSheet is not yet implemented]
form-control-form-attribute.html: [fail, Form association doesn't respect the spec]
leaktests/html-collection.html: [fail, Document.all is not implemented]
leaktests/window-frames.html: [fail, Window.name is not implemeneted]
offsetParent-across-shadow-boundaries.html: [fail, offsetParent not implemented]
scroll-to-the-fragment-in-shadow-tree.html: [fail, Requires a layout engine]
slotchange-customelements.html: [fail, CustomElement.define is not implemented]
untriaged/elements-and-dom-objects/shadowroot-object/shadowroot-attributes/activeElement-confirm-return-null.html: [fail, ShadowRoot.activeElement is not implemented]
untriaged/elements-and-dom-objects/shadowroot-object/shadowroot-attributes/test-007.html: [fail, ShadowRoot.activeElement is not implemented]
untriaged/elements-and-dom-objects/shadowroot-object/shadowroot-attributes/test-011.html: [fail, ShadowRoot.stylesheets is not implemented]
untriaged/elements-and-dom-objects/shadowroot-object/shadowroot-methods/test-004.html: [fail, Range is not implemented]
untriaged/elements-and-dom-objects/shadowroot-object/shadowroot-methods/test-006.html: [fail, Range is not implemented]
Expand All @@ -873,8 +871,6 @@ untriaged/styles/test-001.html: [fail, offsetTop not implemented]
untriaged/styles/test-003.html: [fail, ShadowRoot.stylesheets is not implemented]
untriaged/styles/test-005.html: [fail, offsetTop not implemented]
untriaged/styles/test-008.html: [fail, offsetTop and offsetHeight not implemented]
untriaged/user-interaction/active-element/test-001.html: [fail, ShadowRoot.activeElement is not implemented]
untriaged/user-interaction/active-element/test-002.html: [fail, ShadowRoot.activeElement is not implemented]
untriaged/user-interaction/editing/inheritance-of-content-editable-001.html: [fail, contentEditable is not implemented]
untriaged/user-interaction/ranges-and-selections/test-001.html: [fail, Range is not implemented]
untriaged/user-interaction/ranges-and-selections/test-002.html: [fail, Range is not implemented]
Expand Down

0 comments on commit 0f09068

Please sign in to comment.