element.d.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * @license
  3. * SPDX-License-Identifier: Apache-2.0
  4. */
  5. /**
  6. * @fileoverview This contains safe wrappers for properties that aren't specific
  7. * to one kind of HTMLElement (like innerHTML), plus other setters and functions
  8. * that are not tied to elements (like location.href or Worker constructor).
  9. */
  10. import { SafeAttributePrefix } from '../../internals/attribute_impl';
  11. import { SafeHtml } from '../../internals/html_impl';
  12. import { SafeStyle } from '../../internals/style_impl';
  13. /**
  14. * Safely set {@link Element.innerHTML} on a given ShadowRoot or Element which
  15. * may not be a `<script>` element or a `<style>` element.
  16. */
  17. export declare function setInnerHtml<T extends Element | ShadowRoot>(elOrRoot: Exclude<T, HTMLScriptElement | HTMLStyleElement>, v: SafeHtml): void;
  18. /**
  19. * Safely set {@link Element.outerHTML} for the given Element.
  20. */
  21. export declare function setOuterHtml(e: Element, v: SafeHtml): void;
  22. /**
  23. * Set `ElementCSSInlineStyle.cssText` for the given `ElementCSSInlineStyle`.
  24. */
  25. export declare function setCssText(e: ElementCSSInlineStyle, v: SafeStyle): void;
  26. /**
  27. * Safely call {@link Element.insertAdjacentHTML} for the given Element.
  28. */
  29. export declare function insertAdjacentHtml<T extends Element>(element: Exclude<T, HTMLScriptElement | HTMLStyleElement>, position: 'afterbegin' | 'afterend' | 'beforebegin' | 'beforeend', v: SafeHtml): void;
  30. /**
  31. * Given a set of known-to-be-safe prefixes (e.g., "data-", "aria-", "js"),
  32. * return a setter function that allows you to set attributes on an element,
  33. * as long as the names of the attributes to be set has one of the prefixes.
  34. *
  35. * The returned setter ensures that setting any dangerous attribute, e.g.,
  36. * "src", "href" will cause an exception. This is intended to be used as the
  37. * safe alterantive of `Element#setAttribute`, when applications need to set
  38. * attributes that do not have security implications and do not have a
  39. * corresponding DOM property.
  40. */
  41. export declare function buildPrefixedAttributeSetter(prefix: SafeAttributePrefix, ...otherPrefixes: readonly SafeAttributePrefix[]): (e: Element, attr: string, value: string) => void;
  42. /**
  43. * The safe alternative to Element#setAttribute. The function takes a list of
  44. * `SafeAttributePrefix`, making developer intention explicit. The attribute
  45. * to be set must has one of the safe prefixes, otherwise the function throws
  46. * an Error.
  47. */
  48. export declare function setPrefixedAttribute(attrPrefixes: readonly SafeAttributePrefix[], e: Element, attr: string, value: string): void;