[{"id":"domapisadvanced-001","courseId":"dom-apis-advanced","domain":"dom-tree","topic":"getRootNode and shadow roots","prompt":"A component's <span> sits inside the open shadow root of a host element. The code runs span.getRootNode() with no arguments. What does the call return?","hint":"Check what the options object's default setting does when the walk up reaches a shadow boundary.","explain":"A root is the top of the tree a node belongs to, found by walking up its ancestors. Encapsulated subtrees add a boundary on that walk, and an option controls whether the walk stops there or continues past it.","source":"https://developer.mozilla.org/en-US/docs/Web/API/Node/getRootNode","interaction":{"type":"single"},"options":[{"id":"a","text":"The ShadowRoot that contains the span","why":"By default the lookup stops at the shadow boundary, so the result is the shadow root the span lives in."},{"id":"b","text":"The document, because shadow roots are skipped by default","whyNot":"The document is returned only when composed is true; by default the lookup stops at the shadow root."},{"id":"c","text":"The host element, since the span is rendered under it","whyNot":"The host is an ordinary element, not a root; inside a shadow tree the call returns the associated ShadowRoot."},{"id":"d","text":"null, because the span is not in the document tree","whyNot":"An element in a shadow tree still has a root, which getRootNode() returns; it does not return null here."}],"answer":"a","explanation":"getRootNode() returns the root of the tree a node belongs to. Inside a shadow tree the default (composed: false) stops at the ShadowRoot; composed: true continues past it.","concept":"getRootNode() returns the root of the tree a node belongs to. Inside a shadow tree the default (composed: false) stops at the ShadowRoot; composed: true continues past it.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","code":"const span = host.shadowRoot\n  .querySelector(\"span\");\nspan.getRootNode();","takeaways":["getRootNode() stops at the shadow root unless you pass { composed: true }."]},{"id":"domapisadvanced-002","courseId":"dom-apis-advanced","domain":"dom-tree","topic":"customElements.define upgrades","prompt":"Which TWO statements about calling customElements.define() are correct?","hint":"Think about which elements a define() call reaches, and what the registry keeps track of.","explain":"Registering a custom element is a step with effects on the existing page, not just on future elements. Some parts of the class are read only once, at registration, and the registry tracks what it has already been given.","source":"https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry/define","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"Matching elements already connected to the document, including ones inside shadow trees, are upgraded when define() runs.","why":"Connected elements that use this registry and match the definition are upgraded, shadow trees included."},{"id":"b","text":"A second define() call with a name that is already registered throws a NotSupportedError DOMException.","why":"The registry rejects a name it already holds, and that rejection is a NotSupportedError."},{"id":"c","text":"Elements already on the page keep their old behavior until reload, because define() only affects elements created later.","whyNot":"define() upgrades matching elements that are already connected; they do not wait for a reload."},{"id":"d","text":"A name with uppercase letters, like MyCard, is accepted because the registry lowercases it before storing it.","whyNot":"Valid custom element names contain no ASCII uppercase letters, so an uppercase name throws a SyntaxError."},{"id":"e","text":"The constructor and lifecycle methods are re-read each time an element of that name is created, so later edits apply.","whyNot":"They are retrieved only once, when define() is called, so later edits to the class have no effect."},{"id":"f","text":"Customizing a <p> works without the extends option, because the class extends HTMLParagraphElement anyway.","whyNot":"Customized built-in elements must pass the options parameter with extends set to the built-in name."}],"answer":"ab","explanation":"define() upgrades matching elements already connected to the document, including those in shadow trees. Reusing a registered name throws NotSupportedError, and names may not contain uppercase letters.","concept":"define() upgrades matching elements already connected to the document, including those in shadow trees. Reusing a registered name throws NotSupportedError, and names may not contain uppercase letters.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["Defining a name can upgrade elements already on the page, not only elements created afterwards."]},{"id":"domapisadvanced-003","courseId":"dom-apis-advanced","domain":"dom-tree","topic":"Custom element lifecycle callbacks","prompt":"A custom element that defines connectedMoveCallback() still runs its disconnectedCallback() and connectedCallback() when Node.insertBefore() moves it.","hint":"Think about which move method the connectedMoveCallback() hook is tied to.","explain":"Lifecycle hooks run as an element is added, removed or moved. Some moves have their own hook, so the method used decides which hooks run.","source":"https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","why":"insertBefore() fires disconnectedCallback() and connectedCallback(); connectedMoveCallback() runs only for Element.moveBefore()."},{"id":"b","text":"False","whyNot":"insertBefore() does fire the disconnect and connect callbacks; only Element.moveBefore() routes the move to connectedMoveCallback()."}],"answer":"a","explanation":"Node.insertBefore() moves an element by disconnecting and reconnecting it, firing disconnectedCallback() and connectedCallback(). connectedMoveCallback() replaces them only for Element.moveBefore().","concept":"Node.insertBefore() moves an element by disconnecting and reconnecting it, firing disconnectedCallback() and connectedCallback(). connectedMoveCallback() replaces them only for Element.moveBefore().","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["Only Element.moveBefore() triggers connectedMoveCallback(); insertBefore() still fires the connect and disconnect callbacks."]},{"id":"domapisadvanced-004","courseId":"dom-apis-advanced","domain":"dom-tree","topic":"MutationObserver","prompt":"A MutationObserver is watching a list for changes. Your code calls observer.takeRecords() and gets an array back. What does that call do to the observer's pending notifications?","hint":"Compare this call with disconnect(), and think about what happens to the notifications still being held.","explain":"A MutationObserver holds change notifications until its callback is run. Its methods can start or stop watching, and some can also reach into the notifications that are still being held.","source":"https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver","interaction":{"type":"single"},"options":[{"id":"a","text":"It returns the pending records and removes them from the queue","why":"takeRecords() returns every pending MutationRecord in a new array and empties the notification queue."},{"id":"b","text":"It returns only the records the callback has already received","whyNot":"Records already delivered to the callback are no longer pending; takeRecords() returns the queued ones."},{"id":"c","text":"It stops the observer, the same as calling disconnect()","whyNot":"disconnect() stops further notifications; takeRecords() only drains the queue of notifications already pending."},{"id":"d","text":"It waits for the next microtask, then returns every record so far","whyNot":"takeRecords() returns the pending queue immediately, rather than after a delay."}],"answer":"a","explanation":"takeRecords() returns all pending MutationRecord objects in a new array and removes them from the queue. disconnect() is the call that stops further notifications.","concept":"takeRecords() returns all pending MutationRecord objects in a new array and removes them from the queue. disconnect() is the call that stops further notifications.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["takeRecords() empties the pending queue; disconnect() is what stops further notifications."]},{"id":"domapisadvanced-005","courseId":"dom-apis-advanced","domain":"dom-tree","topic":"insertAdjacentHTML positions","prompt":"Put the four insertAdjacentHTML positions in the order they fall in the document, reading from before a <p> element's opening tag to after its closing tag.","hint":"Some positions lie outside the element's tags and others sit inside its content.","explain":"Insertion points around an element sit at different depths, some outside its tags and some inside its content. The method takes a keyword naming one spot, which you can place along a line of markup.","source":"https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML","interaction":{"type":"sort","items":[{"text":"beforebegin","why":"Sits just before the element's opening tag, outside it."},{"text":"afterbegin","why":"Sits inside the element, before its first child."},{"text":"beforeend","why":"Sits inside the element, after its last child."},{"text":"afterend","why":"Sits just after the element's closing tag, outside it."}]},"options":[],"answer":"o:abcd","explanation":"insertAdjacentHTML() positions are relative to the element's tags: beforebegin and afterend lie outside it, while afterbegin and beforeend lie inside its content.","concept":"insertAdjacentHTML() positions are relative to the element's tags: beforebegin and afterend lie outside it, while afterbegin and beforeend lie inside its content.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["insertAdjacentHTML() places content relative to the element's tags, so outside positions never touch its children."]}]