document.mjs 986 B

12345678910111213141516171819202122232425262728293031
  1. /**
  2. * @license
  3. * SPDX-License-Identifier: Apache-2.0
  4. */
  5. import { unwrapHtml } from '../../internals/html_impl';
  6. /**
  7. * write safely calls {@link Document.write} on the given {@link Document} with
  8. * the given {@link SafeHtml}.
  9. */
  10. export function write(doc, text) {
  11. doc.write(unwrapHtml(text));
  12. }
  13. /**
  14. * Safely calls {@link Document.execCommand}. When command is insertHtml, a
  15. * SafeHtml must be passed in as value.
  16. */
  17. export function execCommand(doc, command, value) {
  18. const commandString = String(command);
  19. let valueArgument = value;
  20. if (commandString.toLowerCase() === 'inserthtml') {
  21. valueArgument = unwrapHtml(value);
  22. }
  23. return doc.execCommand(commandString, /* showUi= */ false, valueArgument);
  24. }
  25. /**
  26. * Safely calls {@link Document.execCommand}('insertHtml').
  27. * @deprecated Use safeDocument.execCommand.
  28. */
  29. export function execCommandInsertHtml(doc, html) {
  30. return doc.execCommand('insertHTML', /* showUi= */ false, unwrapHtml(html));
  31. }