CSP Class Style Mode
For strict CSP environments (for example, inline style is blocked), use textStyleMode: 'class' so style output uses class + data-w-e-*.
TIP
The default mode is still inline, so existing projects keep working without changes.
Quick Start
ts
import { createEditor, IEditorConfig } from '@wangeditor-next/editor'
const editorConfig: Partial<IEditorConfig> = {
textStyleMode: 'class',
classStylePolicy: 'preserve-data',
styleClassTokens: {
color: ['rgb(1, 2, 3)'],
},
onClassStyleUnsupported(payload) {
console.warn('[class-style-unsupported]', payload)
},
}
const editor = createEditor({
selector: '#editor-container',
config: editorConfig,
html: '<p><br></p>',
})Make sure the editor stylesheet is loaded (it includes built-in token class styles):
html
<link
href="https://unpkg.com/@wangeditor-next/editor@latest/dist/css/style.css"
rel="stylesheet"
>Config Fields
textStyleMode
inline(default): style output goes tostyle.class: style output goes toclass + data-w-e-*.
classStylePolicy
Only works when textStyleMode: 'class'.
preserve-data(default): keepdata-w-e-*only, no class/inline (round-trip safe, may not render visual style).fallback-inline: keepdata-w-e-*and fallback to inline style (render-first behavior).strict: throw on unsupported tokens (no silent degradation).
styleClassTokens
- Registers additional accepted tokens.
- It only registers tokens and does not inject your custom CSS automatically.
onClassStyleUnsupported
Called when an unsupported token is detected. Useful for logging and monitoring.
Payload fields:
typevaluescene(renderortoHtml)fallback(preserve-data/inline/throw)message
Supported Text Style Types
colorbgColorfontSizefontFamilytextAlignlineHeightindent
CSS for Custom Tokens
For custom tokens, prefer rules based on data-w-e-* instead of hash class names:
css
[data-w-e-color="rgb(1, 2, 3)"] { color: rgb(1, 2, 3); }
[data-w-e-font-size="20px"] { font-size: 20px; }
[data-w-e-line-height="2"] { line-height: 2; }Module Behavior
basic-modules: text styles follow policy output (class/data/inline).list-module: list marker color class isw-e-list-color-*and keepsdata-w-e-color.table-module:border-stylein class mode follows policy (single-value class support, complex values degrade by policy).video-module,image,plugin-float-image: alignment and size prefer class/data output.
Migration Suggestion
- Start in test env with
textStyleMode: 'class'+classStylePolicy: 'preserve-data'. - Observe
onClassStyleUnsupportedlogs, then addstyleClassTokensand matching CSS. - Move to
fallback-inlineorstrictbased on your production policy.