Skip to content

Commit

Permalink
Implement processSlotAttribute method (#2511)
Browse files Browse the repository at this point in the history
There is some duplicated code to process attributes that can
be overridden by slots. In addition, not every attribute that can be
overridden with slots are processed with the 
hasSlotOverridingAttribute method. As such, there will be no
warning in the logger when these attributes are overridden.

Let's put this duplicated code into a method and call it for every
attribute that can be overridden by a slot.
  • Loading branch information
luminousleek authored Apr 14, 2024
1 parent ff8e9b1 commit ff1a738
Showing 1 changed file with 33 additions and 26 deletions.
59 changes: 33 additions & 26 deletions packages/core/src/html/MdAttributeRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,23 @@ export class MdAttributeRenderer {
return hasAttribute;
}

processPopoverAttributes(node: MbNode) {
if (!this.hasSlotOverridingAttribute(node, 'header')) {
this.processAttributeWithoutOverride(node, 'header', true);
/**
* Checks if there is a pre-existing slot for the attribute.
* If there is a slot, it deletes the attribute and logs a warning.
* If there is no slot, it processes the markdown attribute.
* @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
*/
processSlotAttribute(node: MbNode, attribute: string, isInline: boolean, slotName = attribute) {
if (!this.hasSlotOverridingAttribute(node, attribute, slotName)) {
this.processAttributeWithoutOverride(node, attribute, isInline, slotName);
}
}

processPopoverAttributes(node: MbNode) {
this.processSlotAttribute(node, 'header', true);

// Warn if there is a content slot overriding the attributes 'content' or 'src'
const hasSlotAndContentAttribute = this.hasSlotOverridingAttribute(node, 'content', 'content');
Expand All @@ -96,69 +109,63 @@ export class MdAttributeRenderer {
}

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

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

/*
* Panels
*/

processPanelAttributes(node: MbNode) {
this.processAttributeWithoutOverride(node, 'alt', false, '_alt');
if (!this.hasSlotOverridingAttribute(node, 'header')) {
this.processAttributeWithoutOverride(node, 'header', false);
}
this.processSlotAttribute(node, 'alt', false, '_alt');
this.processSlotAttribute(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);
this.processSlotAttribute(node, 'header', false);
this.processSlotAttribute(node, 'hint', false);
this.processSlotAttribute(node, 'answer', false);
}

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

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

/*
* Tabs
*/

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

/*
* Boxes
*/

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

/*
* Dropdowns
*/

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

/**
Expand Down Expand Up @@ -186,12 +193,12 @@ export class MdAttributeRenderer {
}

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

processAnnotationPointAttributes(node: MbNode) {
this.processAttributeWithoutOverride(node, 'content', false);
this.processAttributeWithoutOverride(node, 'header', false);
this.processAttributeWithoutOverride(node, 'label', false);
this.processSlotAttribute(node, 'content', false);
this.processSlotAttribute(node, 'header', false);
this.processSlotAttribute(node, 'label', false);
}
}

0 comments on commit ff1a738

Please sign in to comment.