How to use the @lwc/shared.getOwnPropertyDescriptor function in @lwc/shared

To help you get started, we’ve selected a few @lwc/shared examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github salesforce / lwc / packages / @lwc / synthetic-shadow / src / env / node.ts View on Github external
const parentNodeGetter: (this: Node) => (Node & ParentNode) | null = getOwnPropertyDescriptor(
    Node.prototype,
    'parentNode'
)!.get!;

const ownerDocumentGetter: (this: Node) => Document | null = getOwnPropertyDescriptor(
    Node.prototype,
    'ownerDocument'
)!.get!;

const parentElementGetter: (this: Node) => Element | null = hasOwnProperty.call(
    Node.prototype,
    'parentElement'
)
    ? getOwnPropertyDescriptor(Node.prototype, 'parentElement')!.get!
    : getOwnPropertyDescriptor(HTMLElement.prototype, 'parentElement')!.get!; // IE11

const textContextSetter: (this: Node, s: string) => void = getOwnPropertyDescriptor(
    Node.prototype,
    'textContent'
)!.set!;

const childNodesGetter: (this: Node) => NodeListOf = hasOwnProperty.call(
    Node.prototype,
    'childNodes'
)
    ? getOwnPropertyDescriptor(Node.prototype, 'childNodes')!.get!
    : getOwnPropertyDescriptor(HTMLElement.prototype, 'childNodes')!.get!; // IE11

const isConnected = hasOwnProperty.call(Node.prototype, 'isConnected')
    ? getOwnPropertyDescriptor(Node.prototype, 'isConnected')!.get!
    : function(this: Node): boolean {
github salesforce / lwc / packages / @lwc / synthetic-shadow / src / faux-shadow / node.ts View on Github external
};

// IE11 extra patches for wrong prototypes
if (hasOwnProperty.call(HTMLElement.prototype, 'contains')) {
    defineProperty(
        HTMLElement.prototype,
        'contains',
        getOwnPropertyDescriptor(Node.prototype, 'contains') as PropertyDescriptor
    );
}

if (hasOwnProperty.call(HTMLElement.prototype, 'parentElement')) {
    defineProperty(
        HTMLElement.prototype,
        'parentElement',
        getOwnPropertyDescriptor(Node.prototype, 'parentElement') as PropertyDescriptor
    );
}
github salesforce / lwc / packages / @lwc / synthetic-shadow / src / env / element.ts View on Github external
)!.get!;

const lastElementChildGetter: (this: ParentNode) => Element | null = getOwnPropertyDescriptor(
    Element.prototype,
    'lastElementChild'
)!.get!;

const innerHTMLDescriptor = hasOwnProperty.call(Element.prototype, 'innerHTML')
    ? getOwnPropertyDescriptor(Element.prototype, 'innerHTML')
    : getOwnPropertyDescriptor(HTMLElement.prototype, 'innerHTML'); // IE11

const innerHTMLGetter: (this: Element) => string = innerHTMLDescriptor!.get!;
const innerHTMLSetter: (this: Element, s: string) => void = innerHTMLDescriptor!.set!;

const outerHTMLDescriptor = hasOwnProperty.call(Element.prototype, 'outerHTML')
    ? getOwnPropertyDescriptor(Element.prototype, 'outerHTML')
    : getOwnPropertyDescriptor(HTMLElement.prototype, 'outerHTML'); // IE11

const outerHTMLGetter: (this: Element) => string = outerHTMLDescriptor!.get!;
const outerHTMLSetter: (this: Element, s: string) => void = outerHTMLDescriptor!.set!;

const tagNameGetter: (this: Element) => string = getOwnPropertyDescriptor(
    Element.prototype,
    'tagName'
)!.get!;

const tabIndexDescriptor = getOwnPropertyDescriptor(HTMLElement.prototype, 'tabIndex');
const tabIndexGetter: (this: HTMLElement) => number = tabIndexDescriptor!.get!;
const tabIndexSetter: (this: HTMLElement, v: any) => void = tabIndexDescriptor!.set!;

const matches: (this: Element, selector: string) => boolean = hasOwnProperty.call(
    Element.prototype,
github salesforce / lwc / packages / @lwc / synthetic-shadow / src / faux-shadow / event-target.ts View on Github external
/*
 * Copyright (c) 2018, salesforce.com, inc.
 * All rights reserved.
 * SPDX-License-Identifier: MIT
 * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
 */
import { getOwnPropertyDescriptor, defineProperties } from '@lwc/shared';
import { addCustomElementEventListener, removeCustomElementEventListener } from './events';
import { isHostElement } from './shadow-root';

const eventTargetGetter: (this: Event) => Element = getOwnPropertyDescriptor(
    Event.prototype,
    'target'
)!.get!;
const eventCurrentTargetGetter: (this: Event) => Element | null = getOwnPropertyDescriptor(
    Event.prototype,
    'currentTarget'
)!.get!;

// These methods are usually from EventTarget.prototype, but that's not available in IE11, the next best thing
// is Node.prototype, which is an EventTarget as well.
const {
    dispatchEvent,
    addEventListener: superAddEventListener,
    removeEventListener: superRemoveEventListener,
} = Node.prototype;

function addEventListenerPatched(
    this: Element,
    type: string,
    listener: EventListener,
github salesforce / lwc / packages / @lwc / synthetic-shadow / src / env / node.ts View on Github external
Node.prototype,
    'parentElement'
)
    ? getOwnPropertyDescriptor(Node.prototype, 'parentElement')!.get!
    : getOwnPropertyDescriptor(HTMLElement.prototype, 'parentElement')!.get!; // IE11

const textContextSetter: (this: Node, s: string) => void = getOwnPropertyDescriptor(
    Node.prototype,
    'textContent'
)!.set!;

const childNodesGetter: (this: Node) => NodeListOf = hasOwnProperty.call(
    Node.prototype,
    'childNodes'
)
    ? getOwnPropertyDescriptor(Node.prototype, 'childNodes')!.get!
    : getOwnPropertyDescriptor(HTMLElement.prototype, 'childNodes')!.get!; // IE11

const isConnected = hasOwnProperty.call(Node.prototype, 'isConnected')
    ? getOwnPropertyDescriptor(Node.prototype, 'isConnected')!.get!
    : function(this: Node): boolean {
          const doc = ownerDocumentGetter.call(this);
          // IE11
          return (
              // if doc is null, it means `this` is actually a document instance which
              // is always connected
              doc === null ||
              (compareDocumentPosition.call(doc, this) & DOCUMENT_POSITION_CONTAINED_BY) !== 0
          );
      };

export {
github salesforce / lwc / packages / @lwc / synthetic-shadow / src / env / node.ts View on Github external
DOCUMENT_FRAGMENT_NODE,
} = Node;

const {
    appendChild,
    cloneNode,
    compareDocumentPosition,
    insertBefore,
    removeChild,
    replaceChild,
    hasChildNodes,
} = Node.prototype;

const { contains } = HTMLElement.prototype;

const firstChildGetter: (this: Node) => ChildNode | null = getOwnPropertyDescriptor(
    Node.prototype,
    'firstChild'
)!.get!;

const lastChildGetter: (this: Node) => ChildNode | null = getOwnPropertyDescriptor(
    Node.prototype,
    'lastChild'
)!.get!;

const textContentGetter: (this: Node) => string = getOwnPropertyDescriptor(
    Node.prototype,
    'textContent'
)!.get!;

const parentNodeGetter: (this: Node) => (Node & ParentNode) | null = getOwnPropertyDescriptor(
    Node.prototype,
github salesforce / lwc / packages / @lwc / synthetic-shadow / src / env / element.ts View on Github external
import { hasOwnProperty, getOwnPropertyDescriptor } from '@lwc/shared';

const {
    hasAttribute,
    getAttribute,
    setAttribute,
    removeAttribute,
    querySelectorAll,
    getBoundingClientRect,
    getElementsByTagName,
    getElementsByTagNameNS,
} = Element.prototype;

const { addEventListener, removeEventListener } = Element.prototype;

const childElementCountGetter: (this: ParentNode) => number = getOwnPropertyDescriptor(
    Element.prototype,
    'childElementCount'
)!.get!;

const firstElementChildGetter: (this: ParentNode) => Element | null = getOwnPropertyDescriptor(
    Element.prototype,
    'firstElementChild'
)!.get!;

const lastElementChildGetter: (this: ParentNode) => Element | null = getOwnPropertyDescriptor(
    Element.prototype,
    'lastElementChild'
)!.get!;

const innerHTMLDescriptor = hasOwnProperty.call(Element.prototype, 'innerHTML')
    ? getOwnPropertyDescriptor(Element.prototype, 'innerHTML')
github salesforce / lwc / packages / @lwc / synthetic-shadow / src / env / node.ts View on Github external
hasChildNodes,
} = Node.prototype;

const { contains } = HTMLElement.prototype;

const firstChildGetter: (this: Node) => ChildNode | null = getOwnPropertyDescriptor(
    Node.prototype,
    'firstChild'
)!.get!;

const lastChildGetter: (this: Node) => ChildNode | null = getOwnPropertyDescriptor(
    Node.prototype,
    'lastChild'
)!.get!;

const textContentGetter: (this: Node) => string = getOwnPropertyDescriptor(
    Node.prototype,
    'textContent'
)!.get!;

const parentNodeGetter: (this: Node) => (Node & ParentNode) | null = getOwnPropertyDescriptor(
    Node.prototype,
    'parentNode'
)!.get!;

const ownerDocumentGetter: (this: Node) => Document | null = getOwnPropertyDescriptor(
    Node.prototype,
    'ownerDocument'
)!.get!;

const parentElementGetter: (this: Node) => Element | null = hasOwnProperty.call(
    Node.prototype,
github salesforce / lwc / packages / @lwc / synthetic-shadow / src / polyfills / mouse-event / polyfill.ts View on Github external
/*
 * Copyright (c) 2018, salesforce.com, inc.
 * All rights reserved.
 * SPDX-License-Identifier: MIT
 * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
 */
import { defineProperty, isNull, getOwnPropertyDescriptor } from '@lwc/shared';
import { pathComposer } from '../../3rdparty/polymer/path-composer';
import { retarget } from '../../3rdparty/polymer/retarget';
import { eventCurrentTargetGetter } from '../../env/dom';
import { isNodeShadowed } from '../../shared/node-ownership';
import { getOwnerDocument } from '../../shared/utils';

const mouseEventRelatedTargetGetter = getOwnPropertyDescriptor(
    FocusEvent.prototype,
    'relatedTarget'
)!.get as () => EventTarget | null;

defineProperty(MouseEvent.prototype, 'relatedTarget', {
    get(this: Event): EventTarget | null | undefined {
        const relatedTarget = mouseEventRelatedTargetGetter.call(this);
        if (isNull(relatedTarget)) {
            return null;
        }
        if (!(relatedTarget instanceof Node) || !isNodeShadowed(relatedTarget as Node)) {
            return relatedTarget;
        }
        let pointOfReference = eventCurrentTargetGetter.call(this);
        if (isNull(pointOfReference)) {
            pointOfReference = getOwnerDocument(relatedTarget as Node);
github salesforce / lwc / packages / @lwc / synthetic-shadow / src / env / node.ts View on Github external
cloneNode,
    compareDocumentPosition,
    insertBefore,
    removeChild,
    replaceChild,
    hasChildNodes,
} = Node.prototype;

const { contains } = HTMLElement.prototype;

const firstChildGetter: (this: Node) => ChildNode | null = getOwnPropertyDescriptor(
    Node.prototype,
    'firstChild'
)!.get!;

const lastChildGetter: (this: Node) => ChildNode | null = getOwnPropertyDescriptor(
    Node.prototype,
    'lastChild'
)!.get!;

const textContentGetter: (this: Node) => string = getOwnPropertyDescriptor(
    Node.prototype,
    'textContent'
)!.get!;

const parentNodeGetter: (this: Node) => (Node & ParentNode) | null = getOwnPropertyDescriptor(
    Node.prototype,
    'parentNode'
)!.get!;

const ownerDocumentGetter: (this: Node) => Document | null = getOwnPropertyDescriptor(
    Node.prototype,