Skip to content

Commit 29ac85b

Browse files
authored
Merge pull request #504 from rdkmaster/fix-issue-503
fixes issue 503
2 parents f34a9ef + 933c80a commit 29ac85b

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

src/app/demo/notification/full/demo.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ export class NotificationFullDemoComponent {
4747
showWithOptions() {
4848
JigsawNotification.show(this.message, {
4949
caption: this.caption, position: this.position, icon: this.icon,
50-
timeout: this.timeout * 1000, width: this.width, height: this.height
50+
timeout: this.timeout * 1000, width: this.width, height: this.height,
51+
innerHtmlContext: this
5152
});
5253
}
5354

src/jigsaw/directive/trusted-html/trusted-html.ts

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,36 @@ type CallbackValues = { [callbackName: string]: HtmlCallback };
1010
})
1111
export class JigsawTrustedHtml implements OnInit, OnDestroy {
1212
private static _callbacks = new Map<any, CallbackValues>();
13-
private static _contexts = [];
13+
private static _contexts: {context: any, counter: number}[] = [];
1414

1515
private static _getContextMagicNumber(context: any): number {
16-
return JigsawTrustedHtml._contexts.indexOf(context);
16+
return JigsawTrustedHtml._contexts.findIndex(i => i && i.context === context);
1717
}
1818

1919
private static _getContext(magicNumber: number): any {
2020
return JigsawTrustedHtml._contexts[magicNumber];
2121
}
2222

2323
private static _registerContext(context: any): void {
24-
if (CommonUtils.isDefined(context) && JigsawTrustedHtml._getContextMagicNumber(context) == -1) {
25-
JigsawTrustedHtml._contexts.push(context);
24+
if (CommonUtils.isUndefined(context)) {
25+
return;
26+
}
27+
let info = JigsawTrustedHtml._contexts.find(i => i && i.context === context);
28+
if (CommonUtils.isUndefined(info)) {
29+
info = {context: context, counter: 1};
30+
JigsawTrustedHtml._contexts.push(info);
31+
} else {
32+
info.counter++;
2633
}
2734
}
2835

2936
private static _jigsawInternalCallbackWrapper(callbackName: string, contextMagicNumber: number, ...args) {
30-
const context = JigsawTrustedHtml._getContext(contextMagicNumber);
31-
if (CommonUtils.isUndefined(context)) {
37+
const contextInfo = JigsawTrustedHtml._getContext(contextMagicNumber);
38+
if (CommonUtils.isUndefined(contextInfo)) {
3239
console.error('no context found by magic number, callbackName = ' + callbackName);
3340
return;
3441
}
35-
const callbacks = JigsawTrustedHtml._callbacks.get(context);
42+
const callbacks = JigsawTrustedHtml._callbacks.get(contextInfo.context);
3643
if (CommonUtils.isUndefined(callbacks)) {
3744
console.error('no callback cache info found by magic number, callbackName = ' + callbackName);
3845
return;
@@ -43,7 +50,7 @@ export class JigsawTrustedHtml implements OnInit, OnDestroy {
4350
console.log(`Hint: add a member method named ${callbackName} to the context object.`);
4451
return;
4552
}
46-
CommonUtils.safeInvokeCallback(context, callback, args);
53+
CommonUtils.safeInvokeCallback(contextInfo.context, callback, args);
4754
}
4855

4956
private static _declareCallback(context: any, name: string, callback: HtmlCallback) {
@@ -65,11 +72,22 @@ export class JigsawTrustedHtml implements OnInit, OnDestroy {
6572
callbacks = {};
6673
JigsawTrustedHtml._callbacks.set(context, callbacks);
6774
}
68-
callbacks[name] = CommonUtils.isDefined(callback) ? callback : context;
75+
callbacks[name] = callback;
6976
}
7077

7178
private static _clearCallbacks(context: any) {
72-
JigsawTrustedHtml._callbacks.delete(context);
79+
const idx = JigsawTrustedHtml._contexts.findIndex(i => i && i.context === context);
80+
if (idx == -1) {
81+
return;
82+
}
83+
const info = JigsawTrustedHtml._contexts[idx];
84+
info.counter--;
85+
if (info.counter == 0) {
86+
JigsawTrustedHtml._callbacks.delete(context);
87+
info.context = null;
88+
info.counter = -1;
89+
JigsawTrustedHtml._contexts[idx] = null;
90+
}
7391
}
7492

7593
//====================================================
@@ -82,6 +100,7 @@ export class JigsawTrustedHtml implements OnInit, OnDestroy {
82100

83101
private _contextMagicNumber: number = -1;
84102
private _trustedHtmlContext: any;
103+
private _initialized: boolean = false;
85104

86105
@Input()
87106
public get trustedHtmlContext(): any {
@@ -92,9 +111,6 @@ export class JigsawTrustedHtml implements OnInit, OnDestroy {
92111
if (CommonUtils.isUndefined(value)) {
93112
return;
94113
}
95-
if (CommonUtils.isDefined(this._trustedHtmlContext)) {
96-
JigsawTrustedHtml._clearCallbacks(this._trustedHtmlContext);
97-
}
98114
this._trustedHtmlContext = value;
99115
JigsawTrustedHtml._registerContext(value);
100116
this._contextMagicNumber = JigsawTrustedHtml._getContextMagicNumber(value);
@@ -117,12 +133,12 @@ export class JigsawTrustedHtml implements OnInit, OnDestroy {
117133
private _modifiedHtml:string;
118134

119135
private _updateHtml():void {
120-
if (!this._trustedHtml) {
136+
if (!this._trustedHtml || !this._initialized) {
121137
return;
122138
}
123139
const modifiedHtml = !this._trustedHtmlContext ? this._trustedHtml : this._trustedHtml
124-
.replace(/on(\w+)\s*=(['"])\s*([_$a-z][_$a-z0-9]*)\s*\((.*?)\)/ig,
125-
(found, event, quot, func, args) => {
140+
.replace(/(on|\()(\w+)\)?\s*=(['"])\s*([_$a-z][_$a-z0-9]*)\s*\((.*?)\)/ig,
141+
(found, prefix, event, quot, func, args) => {
126142
JigsawTrustedHtml._declareCallback(this._trustedHtmlContext, func, this._trustedHtmlContext[func]);
127143
const modified = `on${event}=${quot}_jigsawInternalCallbackWrapper(&quot;${func}&quot;,${this._contextMagicNumber}`;
128144
args = CommonUtils.isDefined(args) ? args.trim() : '';
@@ -147,6 +163,7 @@ export class JigsawTrustedHtml implements OnInit, OnDestroy {
147163
}
148164

149165
ngOnInit() {
166+
this._initialized = true;
150167
this._updateHtml();
151168
}
152169

0 commit comments

Comments
 (0)