# All files / src/html MdAttributeRenderer.ts

78.12% Statements 50/64
73.52% Branches 25/34
88.23% Functions 15/17
78.12% Lines 50/64

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    1928x
    8x

    8x
    8x
    8x

    8x

    8x

    80x

    56x
    104x

    56x

    34x
    18x

    16x

    34x
    34x

    56x

    28x
    56x
    28x
    14x

    14x
    14x
    10x
    10x

    14x

    4x
    2x

    4x
    4x
    4x
    2x

    2x

    2x

    2x

    4x
    4x

    8x
    8x
    4x

    4x
    4x
    4x

    4x

    4x

    4x

    4x
    4x

    4x
    2x

     import has from 'lodash/has';
    import { getVslotShorthandName } from './vueSlotSyntaxProcessor';
    import type { MarkdownProcessor } from './MarkdownProcessor';
    import * as logger from '../utils/logger';
    import { createSlotTemplateNode } from './elements';
    import { MbNode, NodeOrText, parseHTML } from '../utils/node';

    const _ = {
      has,
    };

    /**
     * Class that is responsible for rendering markdown-in-attributes
     */
    export class MdAttributeRenderer {
      markdownProcessor: MarkdownProcessor;

      constructor(mdp: MarkdownProcessor) {
        this.markdownProcessor = mdp;
      }

      /**
       * Processes the markdown attribute of the provided element, inserting the corresponding <slot> child
       * if there is no pre-existing slot child with the name of the attribute present.
       * @param node Element to process
       * @param attribute Attribute name to process
       * @param isInline Whether to process the attribute with only inline markdown-it rules
       * @param slotName Name attribute of the <slot> element to insert, which defaults to the attribute name
       */
      processAttributeWithoutOverride(node: MbNode, attribute: string,
                                      isInline: boolean, slotName = attribute): void {
        const hasAttributeSlot = node.children
            && node.children.some(child => getVslotShorthandName(child) === slotName);

        if (!hasAttributeSlot && _.has(node.attribs, attribute)) {
          let rendered;
          if (isInline) {
            rendered = this.markdownProcessor.renderMdInline(node.attribs[attribute]);
          } else {
            rendered = this.markdownProcessor.renderMd(node.attribs[attribute]);
          }

          const attributeSlotElement: NodeOrText[] = createSlotTemplateNode(slotName, rendered);
          node.children = node.children
            ? attributeSlotElement.concat(node.children)
            : attributeSlotElement;
        }

        delete node.attribs[attribute];
      }

      /**
       * Checks if the node has both the given slot and the given attribute,
       * deleting the attribute and logging a warning if both the slot and attribute exist.
       * @param node Element to process
       * @param attribute Attribute name to process
       * @param slotName Name attribute of the <slot> element to insert, which defaults to the attribute name
       * @returns {boolean} whether the node has both the slot and attribute
       */
      // eslint-disable-next-line class-methods-use-this
      hasSlotOverridingAttribute(node: MbNode, attribute: string, slotName = attribute): boolean {
        const hasNamedSlot = node.children
          && node.children.some(child => getVslotShorthandName(child) === slotName);
        if (!hasNamedSlot || !node.attribs) {
          return false;
        }

        // If the slot is present, remove the attribute as the attribute has no effect.
        const hasAttribute = _.has(node.attribs, attribute);
        if (hasAttribute) {
          logger.warn(`${node.name} has a ${slotName} slot, '${attribute}' attribute has no effect.`);
          delete node.attribs[attribute];
        }

        return hasAttribute;
      }

      processPopoverAttributes(node: MbNode) {
        if (!this.hasSlotOverridingAttribute(node, 'header')) {
          this.processAttributeWithoutOverride(node, 'header', true);
        }

        // Warn if there is a content slot overriding the attributes 'content' or 'src'
        const hasSlotAndContentAttribute = this.hasSlotOverridingAttribute(node, 'content', 'content');
        const hasSlotAndSrcAttribute = this.hasSlotOverridingAttribute(node, 'src', 'content');
        if (hasSlotAndContentAttribute || hasSlotAndSrcAttribute) {
          return;
        }

        Iif (_.has(node.attribs, 'content') && _.has(node.attribs, 'src')) {
          logger.warn(`${node.name} has a 'content' attribute, 'src' attribute has no effect.`);
          delete node.attribs.src;
        }

        this.processAttributeWithoutOverride(node, 'content', true);
      }

      processTooltip(node: MbNode) {
        this.processAttributeWithoutOverride(node, 'content', true);
      }

      processModalAttributes(node: MbNode) {
        if (!this.hasSlotOverridingAttribute(node, 'header')) {
          this.processAttributeWithoutOverride(node, 'header', true);
        }
      }

      /*
       * Panels
       */

      processPanelAttributes(node: MbNode) {
        this.processAttributeWithoutOverride(node, 'alt', false, '_alt');
        if (!this.hasSlotOverridingAttribute(node, 'header')) {
          this.processAttributeWithoutOverride(node, 'header', false);
        }
      }

      /*
       * Questions, QOption, and Quizzes
       */

      processQuestion(node: MbNode) {
        this.processAttributeWithoutOverride(node, 'header', false);
        this.processAttributeWithoutOverride(node, 'hint', false);
        this.processAttributeWithoutOverride(node, 'answer', false);
      }

      processQOption(node: MbNode) {
        this.processAttributeWithoutOverride(node, 'reason', false);
      }

      processQuiz(node: MbNode) {
        this.processAttributeWithoutOverride(node, 'intro', false);
      }

      /*
       * Tabs
       */

      processTabAttributes(node: MbNode) {
        this.processAttributeWithoutOverride(node, 'header', true);
      }

      /*
       * Boxes
       */

      processBoxAttributes(node: MbNode) {
        this.processAttributeWithoutOverride(node, 'icon', true);
        this.processAttributeWithoutOverride(node, 'header', false);
      }

      /*
       * Dropdowns
       */

      processDropdownAttributes(node: MbNode) {
        if (!this.hasSlotOverridingAttribute(node, 'header')) {
          this.processAttributeWithoutOverride(node, 'header', true);
        }
      }

      /**
       * Thumbnails
       */

      processThumbnailAttributes(node: MbNode) {
        Iif (!node.attribs) {
          return;
        }

        const isImage = _.has(node.attribs, 'src') && node.attribs.src !== '';
        Iif (isImage) {
          return;
        }

        const text = _.has(node.attribs, 'text') ? node.attribs.text : '';
        Iif (text === '') {
          return;
        }

        const renderedText = this.markdownProcessor.renderMdInline(text);
        node.children = parseHTML(renderedText);
        delete node.attribs.text;
      }

      processScrollTopButtonAttributes(node: MbNode) {
        this.processAttributeWithoutOverride(node, 'icon', true);
      }
    }