Skip to content

fix: Refactor demo snippets#6648

Merged
GZolla merged 26 commits intomainfrom
gzolla/clean-demo-snippet
Mar 25, 2026
Merged

fix: Refactor demo snippets#6648
GZolla merged 26 commits intomainfrom
gzolla/clean-demo-snippet

Conversation

@GZolla
Copy link
Copy Markdown
Contributor

@GZolla GZolla commented Mar 3, 2026

GAUD-9549

There is a lot of code on the demo snippet component that is no longer needed:

  • Code meant to support toggling rtl, a feature which has since been removed
  • code to support IE11
  • other opportunities to simplify and defects to fix

@GZolla GZolla requested a review from a team as a code owner March 3, 2026 21:42
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 3, 2026

Thanks for the PR! 🎉

We've deployed an automatic preview for this PR - you can see your changes here:

URL https://live.d2l.dev/prs/BrightspaceUI/core/pr-6648/

Note

The build needs to finish before your changes are deployed.
Changes to the PR will automatically update the instance.

text = text.replace(/^[\t]*\n/, '').replace(/\n[\t]*$/, '');
const templateMatch = text.match(/^[\t]*<template>[\n]*/);
this._isTemplate = templateMatch && templateMatch.length > 0;
text = text.replace(/^(\t*\n)*/, '').replace(/(\n\t*)*$/, '');
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regex now handles multiple whitelines, before the code would break if 2 white lines where included in the demos. Not something we should expect but it should not break either

Comment on lines +172 to -216
// fix script whitespace
text = setIndent(text.replace(/\t/g, ' '))
.replace(/<\/script>/g, '\n</script>')
.replace(/<script>/g, '<script>\n')
.replace(/<script type="module">/g, '<script type="module">\n')
.replace(/<script data-demo-hide(.+?)<\/script>/gis, '')
.split('\n');
let scriptIndent = 0;
lines = lines.map((l) => {
if (l.indexOf('<script') > -1) {
scriptIndent = l.match(/^(\s*)/)[0].length;
return l;
} else if (l.indexOf('</script>') > -1) {
const nl = this._repeat(' ', scriptIndent) + l ;
scriptIndent = 0;
return nl;
} else if (scriptIndent && !this._isTemplate) {
return this._repeat(' ', scriptIndent + 2) + l;
} else {
return l;
}
});
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before we would look for script tags, and only fix indentation for those, now the same logic is applied to the whole snippet on setIndent. This is needed for when custom lit templates can be set, the function can handle cases where we want to ignore the first line (e.g. a template that starts inline but spans multiple lines html`<some-tag>\n ...) or we want to set an indentation level(e.g. nested templates)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember many years ago when the snippet was originally built, the indentation was really finicky. Not critical, but I wish we had a few tests that covered the indentation/formatting scenarios.

});
}

_applyAttr(name, value, applyToShadowRoot) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was meant to handle setting rtl, that feature was removed so this is not needed anymore. This is now handled by _getDemoNodes() and _handleSkeletonChange()

}
}

_repeat(value, times) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a guard for string.repeat on IE11

@EdwinACL831
Copy link
Copy Markdown
Contributor

I might ask, if there are any existing tests that will fail after these changes. or in other words, are there any tests that need to be updated ? if not, should new tests be added for these changes ?

@GZolla
Copy link
Copy Markdown
Contributor Author

GZolla commented Mar 4, 2026

I might ask, if there are any existing tests that will fail after these changes. or in other words, are there any tests that need to be updated ? if not, should new tests be added for these changes ?

Good point, the tests are very barebones, I'll add some

dlockhart
dlockhart previously approved these changes Mar 5, 2026
@dlockhart
Copy link
Copy Markdown
Member

Hmm, looks like a test is timing out in Webkit...

@GZolla GZolla dismissed dlockhart’s stale review March 5, 2026 17:48

modified _hasSkeleton

dlockhart
dlockhart previously approved these changes Mar 5, 2026
@GZolla GZolla marked this pull request as draft March 5, 2026 19:24
@GZolla GZolla dismissed dlockhart’s stale review March 5, 2026 19:25

Still need to figure out the timing issue

@GZolla GZolla marked this pull request as ready for review March 6, 2026 19:26
Comment thread components/demo/demo-snippet.js Outdated
Comment thread package.json Outdated
"test": "npm run lint && npm run test:translations && npm run test:unit && npm run test:axe",
"test:axe": "d2l-test-runner axe --chrome",
"test:unit": "d2l-test-runner",
"test:unit": "d2l-test-runner --safari",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be removed?

@GZolla GZolla merged commit b59fe71 into main Mar 25, 2026
7 checks passed
@GZolla GZolla deleted the gzolla/clean-demo-snippet branch March 25, 2026 15:13
@d2l-github-release-tokens
Copy link
Copy Markdown

🎉 This PR is included in version 3.227.9 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants