WRMCB=function(e){var c=console;if(c&&c.log&&c.error){c.log('Error running batched script.');c.error(e);}}
;
try {
/* module-key = 'com.atlassian.confluence.ext.newcode-macro-plugin:code-macro-bidi', location = 'scripts/code-macro-bidi-markers.js' */
/**
 * Bidirection character markers for:
 * - View page / blog post
 * - Comments (view only)
 * - Inline comments (view only), both server and browser rendered
 * - Preview
 * - Template preview (global and space)
 * - Confluence Questions:
 *   - questions
 *   - answers
 *   - comments
 *
 * Supported blocks:
 * - Code macro
 * - Noformat macro
 * - Preformatted text (including default pasted `<pre>`)
 */
define('confluence/code-macro/bidi-markers', function () {
    const blocksToFormat = [
        {
            // Code macro panel, after it was decorated by SyntaxHighlighter
            selector: '.code.panel[data-macro-name="code"] .codeContent .syntaxhighlighter .code .line',
            decorate: decorateElement
        },
        {
            // Noformat macro is `.preformatted.panel[data-macro-name="noformat"] pre`
            // Preformatted text is `pre` with no wrapper
            selector: 'pre',
            exclude: function (block) {
                // Must not use `[data-macro-name="code"]` because Preview does NOT render that attribute
                // msMatchesSelector is for IE11 compatibility only
                return (typeof block.matches === 'function' ? block.matches : block.msMatchesSelector).call(block, 'pre.syntaxhighlighter-pre');
            },
            decorate: decorateElement
        }
    ];
    const blocksToObserve = [
        {
            // Server-rendered Code macro panel is watched for when the SyntaxHighlighter replaces it with the decorated version.
            // While it would be syntactically valid to place `<span>` inside a `<pre>` before syntax highlighting, the SyntaxHighlighter library would turn the markers into a visible plain text.
            // Must not use `[data-macro-name="code"]` selector because Preview does NOT render that attribute
            // @see com.atlassian.confluence.ext.code.NewCodeMacro.getPanelCSSClass
            // @see com.atlassian.confluence.ext.code.NewCodeMacro.getPanelContentCSSClass
            selector: '.code.panel .codeContent',
            exclude: function (block) {
                // Selector for the server-rendered content of the code macro.
                // @see com.atlassian.confluence.ext.code.render.ContentFormatter.createRenderContent
                return block.querySelectorAll('pre.syntaxhighlighter-pre').length === 0;
            },
            moOptions: { childList: true },
            decorate: function (element) {
                // Selector for the content of the code macro after it was run through a frontend-side syntax highlighter.
                // @see shCore.js:getHtml()
                // @see shCore.js:getLineHtml()
                const codeLines = element.querySelectorAll('.syntaxhighlighter .code .line, .preformattedContent pre');
                Array.prototype.forEach.call(codeLines, decorateElement);
            },
            removeAfter: true
        },
        {
            selector: [
                '#content', // Inline comments and dynamically rendered page comments (e.g. noformat macro)
                '.cq-viewquestion' // Confluence questions - dynamically rendered comments for questions/answers
            ].join(','),
            moOptions: { childList: true, subtree: true },
            decorate: function (block) {
                if (!isElementNode(block)) {
                    return;
                }
                const contentRules = blocksToFormat.concat([
                    {
                        // Server-rendered inline comments because they're not handled by SyntaxHighlighter
                        selector: '.ic-content .code.panel[data-macro-name="code"] pre',
                        decorate: decorateElement
                    }
                ]);
                decorateElements(block, contentRules);
            }
        }
    ];

    const bidiRegex = /([\u202A-\u202E\u2066-\u2069])/g;

    const bidiTooltip = "\u0414\u0432\u0443\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u044b \u0438\u0437\u043c\u0435\u043d\u044f\u044e\u0442 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0442\u0435\u043a\u0441\u0442\u0430. \u041e\u043d\u0438 \u043c\u043e\u0433\u0443\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u043b\u044f \u043c\u0430\u0441\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u0432\u0440\u0435\u0434\u043e\u043d\u043e\u0441\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430.";

    /**
     * Tokenises HTML string by BiDi characters.
     * @param {string} input HTML string
     * @returns {Array}
     */
    function tokeniseByBidiCharacters(input) {
        return input.split(bidiRegex);
    }

    /**
     * @param {string} input HTML string
     * @returns {string} HTML string
     */
    function wrapCharacterInMarker(input) {
        if (!containsBidi(input)) {
            return input;
        }
        const hexCode = input.charCodeAt(0).toString(16).toUpperCase();
        const attrs = [];
        attrs.push('class="bidi-marker"');
        attrs.push('data-charcode="<U+' + hexCode + '>"');
        attrs.push('title="' + bidiTooltip + '"');
        attrs.push('aria-label="' + bidiTooltip + '"');
        return '<span ' + attrs.join(' ') + '>' + input + '</span>';
    }

    /**
     * @param {string} input
     * @returns {string}
     */
    function addMarkers(input) {
        return tokeniseByBidiCharacters(input).map(wrapCharacterInMarker).join('');
    }

    /**
     * @param {Node} element
     */
    function decorateTextNodes(element) {
        // Only decorate textNodes to prevent replacing Elements. Inline-comments would lose reference.
        const textNodes = Array.prototype.slice.call(element.childNodes);
        textNodes
            .filter(isTextNode)
            .filter(function (textNode) {
                return containsBidi(textNode.textContent);
            })
            .forEach(function (textNode) {
                const htmlNode = document.createElement('span');
                htmlNode.dataset.bidiMarker = true;
                htmlNode.innerHTML = addMarkers(textNode.textContent);
                element.replaceChild(htmlNode, textNode);
            });
    }

    /**
     * @param {HTMLElement} element
     */
    function decorateElement(element) {
        // Prevent double decoration
        if (element.dataset.bidiMarker) {
            return;
        }
        element.dataset.bidiMarker = true;
        try {
            // Get children recursively to only iterate over them once, make a copy of the live collection
            const tags = Array.prototype.slice.call(element.getElementsByTagName('*'));
            tags.forEach(decorateTextNodes);
            // Process direct childNodes of the element
            decorateTextNodes(element);
        } catch (e) {
            console.error('Failed to highlight birectional control characters in ', element);
        }
    }

    /**
     *
     * @param {Node} node
     * @returns {boolean}
     */
    function isElementNode(node) {
        return node.nodeType === Node.ELEMENT_NODE;
    }

    /**
     *
     * @param {Node} node
     * @returns {boolean}
     */
    function isTextNode(node) {
        return node.nodeType === Node.TEXT_NODE;
    }

    /**
     *
     * @param {string} input
     * @returns {boolean}
     */
    function containsBidi(input) {
        return bidiRegex.test(input);
    }

    /**
     *
     * @param {Node} parentElement
     * @param {Array} rules
     */
    function decorateElements(parentElement, rules) {
        Array.prototype.forEach.call(rules, function (rule) {
            const blocks = parentElement.querySelectorAll(rule.selector);
            Array.prototype.forEach.call(blocks, function (block) {
                if (rule.exclude && rule.exclude(block)) {
                    return;
                }
                rule.decorate(block);
            });
        });
    }

    /**
     *
     * @param {Node} parentElement
     * @param {Array} rules
     */
    function observeElements(parentElement, rules) {
        Array.prototype.forEach.call(rules, function (rule) {
            const blocks = parentElement.querySelectorAll(rule.selector);
            Array.prototype.forEach.call(blocks, function (block) {
                if (rule.exclude && rule.exclude(block)) {
                    return;
                }

                const observer = new MutationObserver(function (mutationsList) {
                    mutationsList.forEach(function (mutation) {
                        Array.prototype.forEach.call(mutation.addedNodes, rule.decorate);
                    });
                    if (rule.removeAfter) {
                        observer.disconnect();
                    }
                });
                observer.observe(block, rule.moOptions);
            });
        });
    }

    return function init() {
        decorateElements(document, blocksToFormat);
        observeElements(document, blocksToObserve);
    };
});

require('confluence/module-exporter').safeRequire('confluence/code-macro/bidi-markers', function (BidiMarkers) {
    if (document.readyState === 'loading') {
        window.addEventListener('DOMContentLoaded', BidiMarkers);
    } else {
        BidiMarkers();
    }
});

}catch(e){WRMCB(e)};