Skip to content

Commit cfe4d66

Browse files
authored
Add support for HTML inside of linkElements
1 parent f317a23 commit cfe4d66

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

showhide.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* showhide.js v1.1.0
2+
* showhide.js v1.2.1
33
*/
44
(function() {
55
var mLine = document.createElement( 'span' );
@@ -102,9 +102,9 @@
102102
this.showHideOptions.linkElement.classList.remove( 'shown' );
103103
this.showHideOptions.linkElement.classList.add( 'notshown' );
104104

105-
if( this.showHideOptions.showText )
106-
this.showHideOptions.linkElement.textContent = this.showHideOptions.showText;
107-
else {
105+
if( this.showHideOptions.showText ) {
106+
this.showHideOptions.linkTextNode.textContent = this.showHideOptions.showText;
107+
} else {
108108
if( resizeObserver )
109109
resizeObserver.unobserve( this );
110110
this.showHideOptions.linkElement.remove();
@@ -145,9 +145,9 @@
145145
this.showHideOptions.linkElement.classList.remove( 'notshown' );
146146
this.showHideOptions.linkElement.classList.add( 'shown' );
147147

148-
if( this.showHideOptions.hideText )
149-
this.showHideOptions.linkElement.textContent = this.showHideOptions.hideText;
150-
else {
148+
if( this.showHideOptions.hideText ) {
149+
this.showHideOptions.linkTextNode.textContent = this.showHideOptions.hideText;
150+
} else {
151151
if( resizeObserver )
152152
resizeObserver.unobserve( this );
153153
this.showHideOptions.linkElement.remove();
@@ -217,6 +217,19 @@
217217
this.showHideOptions.linkElement.classList.add( 'showHideText' );
218218
}
219219

220+
this.showHideOptions.linkTextNode = null;
221+
for( let childNode of this.showHideOptions.linkElement.childNodes ) {
222+
if( childNode.nodeType === Node.TEXT_NODE ) {
223+
this.showHideOptions.linkTextNode = childNode;
224+
break;
225+
}
226+
}
227+
228+
if( this.showHideOptions.linkTextNode === null ) {
229+
this.showHideOptions.linkTextNode = document.createTextNode('');
230+
this.showHideOptions.linkElement.appendChild( this.showHideOptions.linkTextNode );
231+
}
232+
220233
for( let extraElement of this.showHideOptions.extraElements )
221234
extraElement.showHideParent = this;
222235

@@ -265,9 +278,12 @@
265278
// Add click event for showing and hiding the content
266279
var clickListener = function( e ) {
267280
e.preventDefault();
268-
var target = e.target.parentElement;
269-
if( null === target.showHideOptions )
281+
let target = e.target.parentElement;
282+
let i = 0;
283+
while( !target.showHideOptions && i < 3 ) {
270284
target = target.parentElement;
285+
i++;
286+
}
271287

272288
var showHideOptions = target.showHideOptions;
273289
if( showHideOptions.state && showHideOptions.state === 'hidden' ) {

0 commit comments

Comments
 (0)