X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/bed2c29a33f6e109ce1dd2ef76fc9fbd7a217080..refs/pull/5676/head:/resources/js/wysiwyg/utils/dom.ts diff --git a/resources/js/wysiwyg/utils/dom.ts b/resources/js/wysiwyg/utils/dom.ts index d5c63a816..8bacd1002 100644 --- a/resources/js/wysiwyg/utils/dom.ts +++ b/resources/js/wysiwyg/utils/dom.ts @@ -52,12 +52,19 @@ export type StyleMap = Map; /** * Creates a map from an element's styles. * Uses direct attribute value string handling since attempting to iterate - * over .style will expand out any shorthand properties (like 'padding') making + * over .style will expand out any shorthand properties (like 'padding') * rather than being representative of the actual properties set. */ export function extractStyleMapFromElement(element: HTMLElement): StyleMap { - const map: StyleMap = new Map(); const styleText= element.getAttribute('style') || ''; + return styleStringToStyleMap(styleText); +} + +/** + * Convert string-formatted styles into a StyleMap. + */ +export function styleStringToStyleMap(styleText: string): StyleMap { + const map: StyleMap = new Map(); const rules = styleText.split(';'); for (const rule of rules) { @@ -70,4 +77,23 @@ export function extractStyleMapFromElement(element: HTMLElement): StyleMap { } return map; +} + +/** + * Convert a StyleMap into inline string style text. + */ +export function styleMapToStyleString(map: StyleMap): string { + const parts = []; + for (const [style, value] of map.entries()) { + parts.push(`${style}:${value}`); + } + return parts.join(';'); +} + +export function setOrRemoveAttribute(element: HTMLElement, name: string, value: string|null|undefined) { + if (value) { + element.setAttribute(name, value); + } else { + element.removeAttribute(name); + } } \ No newline at end of file