Skip to content

Commit

Permalink
add csp option
Browse files Browse the repository at this point in the history
workaround for kazzkiq#64
  • Loading branch information
jozsefsallai committed Apr 29, 2020
1 parent fa72d5d commit a3dfc19
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 8 deletions.
35 changes: 35 additions & 0 deletions bin/buildStaticCSS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import fs from 'fs'
import path from 'path'
import CleanCSS from 'clean-css'

import { editorCss } from '../src/styles/editor'
import { defaultCssTheme } from '../src/styles/theme-default'

const minifier = new CleanCSS()

const styles = [
{
filename: 'editor.min.css',
module: editorCss
},
{
filename: 'theme-default.min.css',
module: defaultCssTheme
}
]

styles.forEach(style => {
const minified = minifier.minify(style.module)
try {
fs.writeFileSync(
path.join(__dirname, '..', 'build/css', style.filename),
minified.styles,
'utf8'
)
} catch (err) {
console.error(err.message)
process.exit(1)
}
})

console.log('Static styles exported successfully.')
2 changes: 1 addition & 1 deletion build/codeflask.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/codeflask.module.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions build/css/editor.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions build/css/theme-default.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ export interface CodeFlaskOptions {
areaId?: string
ariaLabelledby?: string
readonly?: boolean
csp?: boolean
}

export default class CodeFlask {
constructor(selectorOrElement: Element | string, opts: CodeFlaskOptions)

updateCode(newCode: string): void
updateCode(newCode: string): void
updateLanguage(newLanguage: string): void
addLanguage(name: string, options: LanguageDefinition): void

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"LICENSE"
],
"scripts": {
"build": "rollup -c",
"build": "rollup -c && node -r esm bin/buildStaticCSS",
"dev": "serve build & rollup -c -w",
"start": "serve public",
"pretest": "npm run build",
Expand All @@ -25,6 +25,8 @@
"devDependencies": {
"chai": "^4.1.2",
"chromedriver": "^2.38.3",
"clean-css": "^4.2.3",
"esm": "^3.2.25",
"micro": "^9.3.0",
"mocha": "^5.1.1",
"rollup": "^0.58.1",
Expand Down
10 changes: 6 additions & 4 deletions src/codeflask.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ export default class CodeFlask {
}

startEditor () {
const isCSSInjected = injectCss(editorCss, null, this.opts.styleParent)
if (!this.opts.csp) {
const isCSSInjected = injectCss(editorCss, null, this.opts.styleParent)

if (!isCSSInjected) {
throw Error('Failed to inject CodeFlask CSS.')
if (!isCSSInjected) {
throw Error('Failed to inject CodeFlask CSS.')
}
}

// The order matters (pre > code). Don't change it
Expand Down Expand Up @@ -131,7 +133,7 @@ export default class CodeFlask {
this.createLineNumbers()
}

if (this.opts.defaultTheme) {
if (this.opts.defaultTheme && !this.opts.csp) {
injectCss(defaultCssTheme, 'theme-default', this.opts.styleParent)
}

Expand Down

0 comments on commit a3dfc19

Please sign in to comment.