Skip to content

Commit

Permalink
Merge pull request #99 from ChildishGiant/deprecation-warn
Browse files Browse the repository at this point in the history
feat: 🗑️ add warning when using deprecated html setting
  • Loading branch information
ChildishGiant authored Mar 28, 2021
2 parents 09fab20 + ebe87f2 commit b79a7fe
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
13 changes: 10 additions & 3 deletions js/toasts.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@
*/
this.options = $.extend({}, Toast.defaults, options);
this.htmlMessage = this.options.html;
// Warn when using html
if (!!this.options.html)
console.warn(
'The html option is deprecated and will be removed in the future. See https://github.com/materializecss/materialize/pull/49'
);
// If the new unsafeHTML is used, prefer that
if (!!this.options.unsafeHTML){
if (!!this.options.unsafeHTML) {
this.htmlMessage = this.options.unsafeHTML;
}
this.message = this.options.text;
Expand Down Expand Up @@ -205,9 +210,11 @@
this.htmlMessage !== null &&
this.htmlMessage.nodeType === 1 &&
typeof this.htmlMessage.nodeName === 'string'
) { //if the htmlMessage is an HTML node, append it directly
) {
//if the htmlMessage is an HTML node, append it directly
toast.appendChild(this.htmlMessage);
} else if (!!this.htmlMessage.jquery) { // Check if it is jQuery object, append the node
} else if (!!this.htmlMessage.jquery) {
// Check if it is jQuery object, append the node
$(toast).append(this.htmlMessage[0]);
} else {
// Append as unsanitized html;
Expand Down
8 changes: 6 additions & 2 deletions js/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,14 @@

_setTooltipContent(tooltipContentEl) {
tooltipContentEl.textContent = this.options.text;
if (!!this.options.html){
if (!!this.options.html) {
// Warn when using html
console.warn(
'The html option is deprecated and will be removed in the future. See https://github.com/materializecss/materialize/pull/49'
);
$(tooltipContentEl).append(this.options.html);
}
if (!!this.options.unsafeHTML){
if (!!this.options.unsafeHTML) {
$(tooltipContentEl).append(this.options.unsafeHTML);
}
}
Expand Down
13 changes: 9 additions & 4 deletions test/html/tooltip.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,23 @@
<body>

<div class="container">
<a class="btn tooltipped" data-position="bottom" data-tooltip="I am a tooltip">Hover me!</a>
<a class="btn tooltipped" data-position="bottom" data-tooltip="<span class='color: red'>I am a tooltip</span>">Hover me!</a>
<a class="btn tooltipped" data-position="bottom">unsafeHTML</a>
<a class="btn tooltipped" data-position="bottom">html</a>
<a class="btn tooltipped" data-position="bottom">text</a>
<a class="btn tooltipped" data-position="bottom" data-tooltip="<span class='color: red'>I am a tooltip</span>">data-tooltip</a>
</div>

<!-- Scripts-->
<script src="../../bin/materialize.js"></script>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function () {
var elems = document.querySelectorAll('.tooltipped');
var instances = M.Tooltip.init(elems, {unsafeHTML: "<span class='color: red'>I am a tooltip</span>"});
var instances = M.Tooltip.init(elems[0], {unsafeHTML: "<span class='color: red'>I am a tooltip</span>"});
var instances = M.Tooltip.init(elems[1], {html: "<span class='color: red'>I am a tooltip</span>"});
var instances = M.Tooltip.init(elems[2], {text: "<span class='color: red'>I am a tooltip</span>"});
var instances = M.Tooltip.init(elems[3]);
});
</script>
</body>

</html>
</html>

0 comments on commit b79a7fe

Please sign in to comment.