/* * Copyright (c) 2017 Martin Donath * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy * of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING, SOFTWARE * DISTRIBUTED UNDER THE LICENSE IS DISTRIBUTED ON AN "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. * SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING PERMISSIONS AND * LIMITATIONS UNDER THE LICENSE. */ import inspect from "../util/inspect" /* ---------------------------------------------------------------------------- * Class * ------------------------------------------------------------------------- */ export class Context { /** * Create a context stack * * This is necessary for easy querying of iframes. * * @constructor * * @property {string} context_ - Context stack */ constructor() { this.stack_ = [] } /** * Push a context onto the stack * * @param {HTMLIFrameElement} context - Context * * @return {number} Size of stack */ push(context) { if (!(context instanceof this.window.Element)) throw new TypeError(`Invalid element: ${inspect(context)}`) if (!(context instanceof this.window.HTMLIFrameElement)) throw new TypeError(`Invalid element type: ${inspect(context.tagName)}`) /* Push context onto stack */ return this.stack_.push(context) } /** * Pop a context from the stack * * @return {HTMLIFrameElement} Context */ pop() { return this.stack_.pop() } /** * Retrieve window object for current context * * @return {Window} Window object */ get window() { return this.stack_.length ? this.stack_[this.stack_.length - 1].contentWindow : window } /** * Retrieve document object for current context * * @return {Document} Document object */ get document() { return this.stack_.length ? this.stack_[this.stack_.length - 1].contentDocument : document } } /* ---------------------------------------------------------------------------- * Export default context * ------------------------------------------------------------------------- */ const _ = new Context() export { _ as default }