CKEditor 4 justify 插件 useComputedState 配置实战:从手动测试到源码原理

原创2026-10-15 13:17:091,689 阅读
文章标签:前端UI组件

CKEditor 4 justify 插件 useComputedState 配置实战:从手动测试到源码原理

useComputedState 是 CKEditor 4 中一项影响“对齐/文字方向按钮高亮状态”判断方式的全局配置。本文以仓库中的手动测试用例 usecomputedstate.md 及其配套 HTML 页面为主线,完整复现测试步骤与预期结果,并结合 justify 插件源码 与 核心配置定义 逐层剖析“计算值 vs 真实值”的判定逻辑,同时给出 RTL 文档场景的变体测试与对应的自动化测试用例,帮助你真正理解并正确配置该选项。

一、测试用例概述:三编辑器对照实验

该手动测试位于 tests/plugins/justify/manual/usecomputedstate.md,配套的 HTML 页面是 usecomputedstate.html。它通过在同一页面创建三个配置不同的编辑器实例,演示 useComputedState 三种取值下 justify 按钮状态的差异。

1.1 测试环境声明

测试文件头部通过 @bender-tags 元数据声明了版本、类型与插件依赖:

@bender-tags: 4.17.2, feature, 4989
@bender-ui: collapsed
@bender-ckeditor-plugins: toolbar, justify, wysiwygarea, elementspath, sourcearea, font, list
  • 版本:4.17.2,关联 issue 编号 4989(useComputedState 按钮状态修复);
  • UI 模式:collapsed(折叠式 UI,便于对比三个编辑器);
  • 依赖插件:toolbar、justify、wysiwygarea、elementspath、sourcearea、font、list。其中 wysiwygarea 提供经典可编辑区域,elementspath 用于显示元素路径,sourcearea 用于切换到源码视图验证实际 HTML。

1.2 测试页面构造

usecomputedstate.html 的关键构造如下:

<h2>Default (expected: right)</h2>
<div id="editor-default">
	<p>Lorem ipsum dolor sit amet.</p>
</div>

<h2>True (expected: right)</h2>
<div id="editor-true">
	<p>Lorem ipsum dolor sit amet.</p>
</div>

<h2>False (expected: none)</h2>
<div id="editor-false">
	<p>Lorem ipsum dolor sit amet.</p>
</div>

<script>
	CKEDITOR.addCss( 'p { text-align: right; }' );

	CKEDITOR.replace( 'editor-default' );
	CKEDITOR.replace( 'editor-true', {
		useComputedState: true
	} );
	CKEDITOR.replace( 'editor-false', {
		useComputedState: false
	} );
</script>

要点:

  • 通过 CKEDITOR.addCss( 'p { text-align: right; }' ) 在编辑区样式表层面强制所有 <p> 元素右对齐——注意这里并没有在任何 <p> 上写入内联样式或 align 属性;
  • 三个编辑器分别采用:默认配置、显式 useComputedState: true、显式 useComputedState: false。

1.3 测试步骤与预期结果

原文档给出的操作步骤为:

  1. 将光标放入段落内部(Put the cursor inside the paragraph);
  2. 检查 justify 各按钮的状态(Check the state of justify buttons);
  3. 对每个编辑器重复上述操作(Repeat the procedure for each editor)。

预期结果如下表:

编辑器 useComputedState 取值 预期的激活按钮 说明
editor-default 默认(true) 右对齐(justifyright) 即使段落上没有显式对齐样式,也因计算样式为 right 而高亮右对齐
editor-true true 右对齐(justifyright) 与默认行为一致
editor-false false 无任何按钮激活 段落上没有真实写入的对齐样式/属性,因此所有按钮都不高亮

预期(Expected):编辑器标题中提到的按钮被激活;非预期(Unexpected):其他某个按钮被激活。这直接点出该测试要验证的核心行为:useComputedState 决定按钮高亮是基于“计算后的样式”还是“真实写入的样式”。

二、useComputedState 配置项本身

2.1 配置定义与默认值

该配置定义于 core/config.js:

/**
 * Indicates that some of the editor features, like alignment and text
 * direction, should use the "computed value" of the feature to indicate its
 * on/off state instead of using the "real value".
 *
 * If enabled in a Left-To-Right written document, the "Left Justify"
 * alignment button will be shown as active, even if the alignment style is not
 * explicitly applied to the current paragraph in the editor.
 *
 *		config.useComputedState = false;
 *
 * @since 3.4.0
 * @cfg {Boolean} [useComputedState=true]
 */
useComputedState: true,

从中可以提炼出三条事实:

  • 生效范围:影响“alignment(对齐)”和“text direction(文字方向)”等编辑器特性;
  • 默认值:true(自 3.4.0 引入,即绝大多数情况下按钮高亮使用“计算值”而非“真实值”);
  • 典型行为:在从左到右(LTR)的文档中,即使当前段落没有显式写入任何对齐样式,justifyleft 按钮也会显示为激活状态,因为浏览器计算出的默认 text-align 就是 left。

2.2 两种取值的行为差异

取值 判定依据 典型效果
true(默认) 元素的计算样式(computed style),并回退到文档方向 样式表 p { text-align: right; } 能驱动右对齐按钮高亮;LTR 文档中未设样式的段落会高亮左对齐
false 元素上真实存在的内联样式或 align 属性 只有显式写入 style="text-align:..." 或 align="..." 的段落才会高亮对应按钮

三、源码级原理:getAlignment 的两种判定路径

justify 插件的核心状态判定逻辑集中在 plugins/justify/plugin.js 的 getAlignment 函数:

function getAlignment( element, useComputedState ) {
	var align;
	if ( useComputedState )
		align = element.getComputedStyle( 'text-align' );
	else {
		while ( !element.hasAttribute || !( element.hasAttribute( 'align' ) || element.getStyle( 'text-align' ) ) ) {
			var parent = element.getParent();
			if ( !parent )
				break;
			element = parent;
		}
		align = element.getStyle( 'text-align' ) || element.getAttribute( 'align' ) || '';
	}

	// Sometimes computed values doesn't tell.
	align && ( align = align.replace( /(?:-(?:moz|webkit)-)?(?:start|auto)/i, '' ) );

	!align && useComputedState && ( align = element.getComputedStyle( 'direction' ) == 'rtl' ? 'right' : 'left' );

	return align;
}

3.1 计算值路径(useComputedState 为 true)

  • 直接调用 element.getComputedStyle( 'text-align' ),底层是 window.getComputedStyle( element )(IE 旧版回退到 currentStyle);
  • 因此样式表、继承样式都会反映到结果中,这正是手动测试里 CKEDITOR.addCss( 'p { text-align: right; }' ) 能被感知的原因;
  • 若计算值为 start/auto 或带浏览器前缀的值(如 -moz-start),会被正则 /(?:-(?:moz|webkit)-)?(?:start|auto)/i 清空——这些值“无法明确指示”某个对齐方向;
  • 当 align 仍为空时,进一步读取计算后的 direction(通过 getComputedStyle( 'direction' )):RTL 文档回退为 right,LTR 文档回退为 left。这就是为什么 LTR 文档中无显式样式的段落会默认高亮“左对齐”按钮。

3.2 真实值路径(useComputedState 为 false)

  • 从当前元素开始向上遍历,直到找到真实存在 align 属性或 text-align 内联样式的祖先元素;
  • 最终取 element.getStyle( 'text-align' ) || element.getAttribute( 'align' ) || '';
  • 由于 CSS 样式表中的规则不产生内联样式,测试页面中三个段落都没有真实写入的对齐样式,故 editor-false 中没有任何按钮被激活(expected: none)。

3.3 状态刷新与执行命令如何用到该配置

  • refresh(状态刷新):plugin.js 中根据光标所在块(path.block || path.blockLimit)计算对齐值,再与当前命令值比对,相等则 TRISTATE_ON,否则 TRISTATE_OFF;若当前元素不可对齐(如在 <ul> 上且 ACF 不允许对齐)则 TRISTATE_DISABLED。这里的核心调用就是 getAlignment( firstBlock, this.editor.config.useComputedState );
  • exec(执行对齐):plugin.js 中决定是否真正写入样式时同样引用该配置:var apply = ( this.state == CKEDITOR.TRISTATE_OFF ) && ( !useComputedState || ( getAlignment( block, true ) != this.value ) )——当 useComputedState 为 true 时,若块的计算对齐与目标一致,则跳过写入,避免产生冗余的内联样式。

四、RTL 变体测试:方向回退逻辑的验证

同目录下还有一份针对 RTL 文档的姊妹用例 usecomputedstatedir.md 与 usecomputedstatedir.html。页面结构与上文几乎一致,唯一区别是把注入的样式改为:

CKEDITOR.addCss( 'p { direction: rtl; }' );
  • 在 RTL 环境中,未显式对齐的段落计算对齐值会回退为 right,因此 Default 与 True 两个编辑器预期激活右对齐按钮(expected: right);
  • useComputedState: false 时依旧没有任何真实对齐样式,预期 none。

这组用例直接验证了 getAlignment 中 !align && useComputedState && ( align = element.getComputedStyle( 'direction' ) == 'rtl' ? 'right' : 'left' ) 这一方向回退分支。

五、自动化测试对照:同一场景的 Bender 断言

手动测试对应的自动化用例集中在 tests/plugins/justify/justify.js,覆盖了手动场景的完整矩阵,便于你在 CI 中回归验证:

自动化用例 配置 断言结果
useComputedState 默认值(#4989) 未显式设置 left: OFF,right: ON,center: OFF,justify: OFF
useComputedState 为 true useComputedState: true left: OFF,right: ON,center: OFF,justify: OFF
useComputedState 为 false useComputedState: false 四个按钮全部 OFF
默认值 + direction 样式 p { direction: rtl } right: ON
true + direction 样式 useComputedState: true + RTL right: ON
false + direction 样式 useComputedState: false + RTL 四个按钮全部 OFF

测试通过 assertCommandState( left, right, center, justify, bot ) 依次断言 justifyleft、justifyright、justifycenter、justifyblock 四个命令的状态(CKEDITOR.TRISTATE_OFF 为 1、CKEDITOR.TRISTATE_ON 为 2)。其中 RTL 场景使用 CKEDITOR.addCss( '.usecomputedstatetestdir { direction: rtl; }' ) 注入样式类,与手动用例的 p { direction: rtl; } 在判定逻辑上等价。这组用例与手动测试互相印证,可作为修改相关逻辑后的回归依据。

六、实测操作指南

要在本地复现并验证上述行为,可参考以下步骤:

  1. 准备环境:CKEditor 4 的测试基于 Bender.js(见仓库根目录 bender.js 与 bender-runner.config.json),先按仓库说明安装依赖并启动 Bender 测试服务器;
  2. 打开手动测试:在测试服务器中访问 tests/plugins/justify/manual/usecomputedstate.html(及 RTL 版本 usecomputedstatedir.html),页面会同时渲染三个编辑器;
  3. 依次操作:在 editor-default、editor-true、editor-false 中把光标分别放入段落内部,观察工具栏 justify 按钮的高亮状态,应与上文表格一致;
  4. 对照源码视图:切换到 sourcearea 提供的源码模式,确认光标所在 <p> 上没有真实写入的对齐样式或属性,从而理解“计算值高亮”与“真实值不高亮”的差异来源;
  5. 运行自动化用例:执行 tests/plugins/justify/justify.js 中 6 个 #4989 相关用例,验证状态矩阵符合预期。

七、常见问题与配置建议

  • 为什么 LTR 文档中左对齐按钮总是高亮? 因为默认 useComputedState: true,浏览器计算的 text-align 在无样式段落上为 left。若希望按钮只反映“真实写入”的对齐状态,应设置 config.useComputedState = false。
  • 设置 false 后对实际对齐操作有影响吗? exec 逻辑中,useComputedState: false 时始终按命令值写入样式(apply 不再受计算值影响),因此对齐操作本身不受限制,只是按钮高亮判定更保守。
  • ACF 与状态的关系:refresh 中还会用 editor.activeFilter.check(...) 校验当前块是否允许应用对齐样式/类(见 plugin.js),因此即便 useComputedState: true,在 ACF 不允许对齐的元素上按钮仍会进入 TRISTATE_DISABLED 状态,这与手动用例中 justifyrespectsacf* 系列测试覆盖的场景一致。

八、小结

useComputedState 是一个开关式的全局配置,它决定 justify 等特性按钮的“激活态”是基于浏览器计算样式(true,默认)还是基于真实写入的样式/属性(false)。手动测试 usecomputedstate.md 用三个编辑器直观展示了该差异,usecomputedstatedir.md 补充验证了 RTL 方向回退,而 justify 插件源码 中的 getAlignment 函数则是这一切行为的底层实现。理解这三者之间的对应关系,你就能在项目中对齐按钮的高亮行为做出精确控制,并能在改动后通过手动与自动化测试双重验证。

登录后查看全文
ckeditor4