mirror of https://github.com/abpframework/abp.git
1 changed files with 52 additions and 0 deletions
@ -0,0 +1,52 @@ |
|||
import { ContentSecurityStrategy, CONTENT_SECURITY_STRATEGY } from './content-security.strategy'; |
|||
import { DomStrategy, DOM_STRATEGY } from './dom.strategy'; |
|||
|
|||
export abstract class ContentStrategy<T extends HTMLScriptElement | HTMLStyleElement = any> { |
|||
constructor( |
|||
public content: string, |
|||
protected domStrategy: DomStrategy = DOM_STRATEGY.AppendToHead(), |
|||
protected contentSecurityStrategy: ContentSecurityStrategy = CONTENT_SECURITY_STRATEGY.None(), |
|||
) {} |
|||
|
|||
abstract createElement(): T; |
|||
|
|||
insertElement() { |
|||
const element = this.createElement(); |
|||
|
|||
this.contentSecurityStrategy.applyCSP(element); |
|||
this.domStrategy.insertElement(element); |
|||
} |
|||
} |
|||
|
|||
export class StyleContentStrategy extends ContentStrategy<HTMLStyleElement> { |
|||
createElement(): HTMLStyleElement { |
|||
const element = document.createElement('style'); |
|||
element.textContent = this.content; |
|||
|
|||
return element; |
|||
} |
|||
} |
|||
|
|||
export class ScriptContentStrategy extends ContentStrategy<HTMLScriptElement> { |
|||
createElement(): HTMLScriptElement { |
|||
const element = document.createElement('script'); |
|||
element.textContent = this.content; |
|||
|
|||
return element; |
|||
} |
|||
} |
|||
|
|||
export const CONTENT_STRATEGY = { |
|||
AppendScriptToBody(content: string) { |
|||
return new ScriptContentStrategy(content, DOM_STRATEGY.AppendToBody()); |
|||
}, |
|||
AppendScriptToHead(content: string) { |
|||
return new ScriptContentStrategy(content, DOM_STRATEGY.AppendToHead()); |
|||
}, |
|||
AppendStyleToHead(content: string) { |
|||
return new StyleContentStrategy(content, DOM_STRATEGY.AppendToHead()); |
|||
}, |
|||
PrependStyleToHead(content: string) { |
|||
return new StyleContentStrategy(content, DOM_STRATEGY.PrependToHead()); |
|||
}, |
|||
}; |
|||
Loading…
Reference in new issue