Skip to content

Commit 4cae2a7

Browse files
author
Vishal Vora
committed
unsafe html
1 parent e2f2ebd commit 4cae2a7

5 files changed

Lines changed: 33 additions & 5 deletions

File tree

examples/html.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from datastack import datastack
2+
3+
ds = datastack(main=True)
4+
5+
6+
# HTML
7+
ds.subheader("HTML")
8+
ds.html(
9+
"""<div style="color:green">
10+
HTML text
11+
</div>""",
12+
allow_unsafe_html=False,
13+
)

frontend/src/app/app.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ <h3 style="font-size: calc(1.3rem + .4vw); font-weight:600">{{element.prop.data}
8282
</div>
8383
<div *ngIf="element.type == 'html'">
8484
<!-- {{element.prop.data}} -->
85-
<div [innerHTML]="element.prop.data"></div>
85+
<div [innerHTML]="senitized_html(element.prop)"></div>
8686
</div>
8787
<div *ngIf="element.type == 'editable_html'">
8888
<ng-container *ngTemplateOutlet="editable;context:{element:element}"></ng-container>

frontend/src/app/app.component.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, ElementRef, Renderer2, ViewChild } from '@angular/core';
1+
import { Component, ElementRef, Renderer2, ViewChild, ViewEncapsulation } from '@angular/core';
22
import { DomSanitizer } from '@angular/platform-browser';
33
import { Subject } from 'rxjs';
44
import { Observable } from 'rxjs/internal/Observable';
@@ -27,7 +27,8 @@ interface g {
2727
@Component({
2828
selector: 'app-root',
2929
templateUrl: './app.component.html',
30-
styleUrls: ['./app.component.css']
30+
styleUrls: ['./app.component.css'],
31+
encapsulation: ViewEncapsulation.None,
3132
})
3233

3334

@@ -561,6 +562,14 @@ plotly_click(element, event){
561562
this.req(element, {value: event.points, action:'chart_click'})
562563
}
563564

565+
senitized_html(prop){
566+
if (prop.allow_unsafe_html){
567+
return this.sanitizer.bypassSecurityTrustHtml(prop.data)
568+
}
569+
else{
570+
return prop.data
571+
}
572+
}
564573
}
565574

566575

lib/datastack/stacker/stacker.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,14 +679,19 @@ def write(self, data, id: Optional[str] = None):
679679
if not self.replace_block(id, block):
680680
self.append_block(block)
681681

682-
def html(self, html: str, id: Optional[str] = None):
682+
def html(
683+
self, html: str, allow_unsafe_html: bool = False, id: Optional[str] = None
684+
):
683685
"""Diapy html element
684686
685687
Parameters
686688
----------
687689
html : str
688690
html string
689691
692+
allow_unsafe_html : bool
693+
By default, any HTML tags found in the body will be escaped and therefore treated as pure text. This behavior may be turned off by setting this argument to True.
694+
690695
id : str
691696
An optional string or integer to use as the unique key for the element.
692697
@@ -703,6 +708,7 @@ def html(self, html: str, id: Optional[str] = None):
703708
"prop": {
704709
"data": html,
705710
"data_var": argname("html", vars_only=False, func=self.html),
711+
"allow_unsafe_html": allow_unsafe_html,
706712
},
707713
}
708714
if not self.replace_block(id, block):

lib/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
setuptools.setup(
66
name="pydatastack",
7-
version="0.0.8",
7+
version="0.0.9",
88
author="Vishal Vora, Mayur Pokiya, Karan Doshi",
99
description="The Fastes way to build apps in python",
1010
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)