CKEditor 4 Link 插件邮箱地址解析:`emailwithquestionmark` manual 测试与问题问号(2138)全解读
CKEditor 4 Link 插件邮箱地址解析:emailwithquestionmark manual 测试与问题问号(#2138)全解读
CKEditor 4 的 Link 插件负责创建与编辑 mailto: 邮件链接,但其内部的 href 解析正则曾存在一个隐蔽缺陷:当收件人地址本身包含问号(?)时,打开链接对话框会把地址误截断。本篇文章以仓库中的手动测试用例 tests/plugins/link/manual/emailwithquestionmark.md 为线索,完整还原该 Bug(issue #2138,修复于 4.13.0)的表现、根因、修复后行为,并结合 plugins/link 的源码与自动化测试,给出可复现、可验证的完整分析。读完本文,你将掌握 Link 插件邮件链接的解析与回写链路、问号在 mailto: URI 中的歧义,以及如何通过 Bender 手动测试与单元测试双重验证该行为。
一、manual 测试文件在讲什么:一段面向人眼验收的 Bender 测试
与常见的自动化测试不同,CKEditor 4 仓库用 Bender 框架组织"手动测试"(manual test),每个用例由一对同名文件组成:.md 描述验收步骤,.html 提供可运行页面。本次关联文档正是这一对文件中的描述部分。
先看测试描述原文(tests/plugins/link/manual/emailwithquestionmark.md):
@bender-tags: bug, link, 4.13.0, 2138
@bender-ui: collapsed
@bender-ckeditor-plugins: link, toolbar, wysiwygarea, sourcearea, undo
* Open link dialog for email link inside editor.
**Expected:**
`E-Mail Address` address input value is ck?editor@cksource.com.
**Unexpected:**
`E-Mail Address` address input value changed to `ck`.
配套页面 tests/plugins/link/manual/emailwithquestionmark.html 提供了被测场景:
<div id="editor">
<p>Mail to <a href="mailto:ck?editor@cksource.com">CKSource</a>!</p>
</div>
<script>
CKEDITOR.replace( 'editor' );
</script>
文件头部的三个 @bender-* 元标签含义如下:
| 标签 | 值 | 含义 |
|---|---|---|
@bender-tags |
bug, link, 4.13.0, 2138 |
该用例属于 Bug 回归测试,针对 link 插件,标注了引入/修复版本 4.13.0 及对应 issue 编号 #2138 |
@bender-ui |
collapsed |
测试时编辑器 UI 折叠,便于聚焦对话框行为 |
@bender-ckeditor-plugins |
link, toolbar, wysiwygarea, sourcearea, undo |
手动测试页启动编辑器时加载的最小插件集合,其中 wysiwygarea 提供经典编辑区域,sourcearea 可查看源码,undo 用于验证撤销行为 |
执行方式是:用 Bender 打开该 manual 测试页,点击"Mail to CKSource"链接弹出链接对话框,观察"E-Mail Address"(即对话框中的邮箱地址输入框)中回显的值。修复后的正确行为是完整保留 ck?editor@cksource.com,而修复前则被截断成 ck——问号及其后的所有字符都丢失了。
二、问题本质:? 在 mailto: URI 中的双重身份
要理解 #2138,必须先理解 mailto: URI 的语法歧义。在 mailto: 协议中,问号 ? 是查询字符串(query string)的开始标记,用于携带邮件参数,例如:
mailto:someone@example.com?subject=Hello&body=World
其中 subject= 与 body= 是 CKEditor Link 对话框支持的两个标准参数(对应对话框中的"Message Subject"与"Message Body"输入框,见 plugins/link/dialogs/link.js)。
但问题在于:邮箱地址本身(尤其本地部分,即 @ 之前)在某些邮件系统中可以包含 ? 字符。此时 mailto:ck?editor@cksource.com 中的 ? 究竟是查询串分隔符,还是地址的一部分?CKEditor 4 的选择是:只要问号后面跟的不是 subject= 或 body=,就应当把它视为地址的一部分,完整保留用户输入。这正是 4.13.0 修复的核心语义。
该修复也被记录在 CHANGES.md 的版本日志中:
#2138: Fixed: An email address containing a question mark is mishandled by the Link plugin.
三、源码根因:emailRegex 的"温和负向先行断言"是如何修好的
邮件链接解析的核心在 plugins/link/plugin.js,文件顶部定义了一组正则常量:
var javascriptProtocolRegex = /^javascript:/,
emailRegex = /^(?:mailto)(?:(?!\?(subject|body)=).)+/i,
emailSubjectRegex = /subject=([^;?:@&=$,\/]*)/i,
emailBodyRegex = /body=([^;?:@&=$,\/]*)/i,
...
其中 emailRegex 是 #2138 修复的关键所在。它的结构可以拆解为:
^(?:mailto):锚定开头并匹配mailto:前缀(不区分大小写,末尾的i标志);(?:(?!\?(subject|body)=).)+:一个带负向先行断言(negative lookahead)的字符循环,每消费一个字符前先检查当前位置是否以?subject=或?body=开头,若是则停止消费。
这种"每步都做负向断言"的写法在正则术语中称为 tempered greedy token(温和的贪婪匹配),效果是:贪婪地吃掉整个地址,但遇到 ?subject= 或 ?body= 时刹车。因此:
- 对
mailto:ck?editor@cksource.com,?e不是?subject=/?body=,断言成立,?被当作地址字符消费,匹配结果为完整地址ck?editor@cksource.com; - 对
mailto:job@cksource.com?subject=Test,遇到?subject=时断言失败,循环停止,地址部分正确地停在job@cksource.com,?subject=Test留给后续的emailSubjectRegex处理。
修复前若使用简单写法(例如直接 /(?:mailto)([^?]+)/ 之类以 ? 作为地址终止符),? 之后的一切都会被当作查询串剥离,于是 ck?editor@cksource.com 被截成 ck——正是 manual 测试中 "Unexpected" 描述的现象。
配套的两个参数正则同样值得注意:
emailSubjectRegex = /subject=([^;?:@&=$,\/]*)/i,
emailBodyRegex = /body=([^;?:@&=$,\/]*)/i,
其捕获组字符类 [^;?:@&=$,\/] 显式排除了 ? : @ & = $ , / ; 等字符,确保查询串分隔符以及邮箱地址特殊字符不会泄漏进主题/正文取值,从另一侧保护了解析边界。
四、完整链路:从 href 到对话框,再到回写
manual 测试只描述了"打开对话框看地址输入框",其背后的调用链值得完整梳理,因为 #2138 的修复正是在这条链路的第一环。
4.1 打开对话框:onShow → parseLinkAttributes
当用户点击或光标位于链接内时,对话框的 onShow 钩子被触发(见 plugins/link/dialogs/link.js)。它的核心逻辑是:
var data = plugin.parseLinkAttributes( editor, firstLink );
...
this.setupContent( data );
parseLinkAttributes 定义在 plugins/link/plugin.js,它先取 data-cke-saved-href 或 href 属性,然后按顺序尝试匹配 javascript 保护地址、锚点(#)、电话(tel:)、邮件(mailto:)与普通 URL。其中邮件分支正是上一节 emailRegex 的消费位置:
else if ( ( emailMatch = href.match( emailRegex ) ) ) {
var subjectMatch = href.match( emailSubjectRegex ),
bodyMatch = href.match( emailBodyRegex ),
email = ( retval.email = {} );
retval.type = 'email';
email.address = emailMatch[ 0 ].replace( 'mailto:', '' );
subjectMatch && ( email.subject = decodeURIComponent( subjectMatch[ 1 ] ) );
bodyMatch && ( email.body = decodeURIComponent( bodyMatch[ 1 ] ) );
}
可以看到 email.address 就是 emailRegex 匹配结果去掉 mailto: 前缀后的剩余部分——正则怎么切,地址就怎么存。#2138 修复后,ck?editor@cksource.com 得以完整存入 data.email.address。
4.2 字段回显:emailAddress 的 setup
setupContent( data ) 会把解析结果分发到各字段的 setup 回调。邮件地址输入框(id 为 emailAddress)定义在 plugins/link/dialogs/link.js:
{
type: 'text',
id: 'emailAddress',
label: linkLang.emailAddress, // 即 "E-Mail Address"
required: true,
validate: function() { ... notEmpty( linkLang.noEmail ) ... },
setup: function( data ) {
if ( data.email ) {
this.setValue( data.email.address );
}
...
},
commit: function( data ) {
if ( !data.email ) {
data.email = {};
}
data.email.address = this.getValue();
}
}
manual 测试中 "Expected" 与 "Unexpected" 的分歧点,正是 this.setValue( data.email.address ) 这一行:地址解析被截断,这里回显的就是截断后的 ck。字段标签来自语言包,E-Mail Address 对应 plugins/link/lang/en.js 中的 emailAddress 键。
4.3 确认保存:getLinkAttributes 重新组装 href
用户点击 OK 后,commit 回调收集各字段值到 data.email,随后由 getLinkAttributes(见 plugins/link/plugin.js)重新生成 data-cke-saved-href:
case 'email':
var email = data.email,
address = email.address,
linkHref;
switch ( emailProtection ) {
case '':
case 'encode':
var subject = encodeURIComponent( email.subject || '' ),
body = encodeURIComponent( email.body || '' ),
argList = [];
subject && argList.push( 'subject=' + subject );
body && argList.push( 'body=' + body );
argList = argList.length ? '?' + argList.join( '&' ) : '';
if ( emailProtection == 'encode' ) {
linkHref = [ 'javascript:void(location.href=\'mailto:\'+',
protectEmailAddressAsEncodedString( address ) ];
argList && linkHref.push( '+\'', escapeSingleQuote( argList ), '\'' );
linkHref.push( ')' );
} else {
linkHref = [ 'mailto:', address, argList ];
}
break;
...
}
set[ 'data-cke-saved-href' ] = linkHref.join( '' );
关键点是 argList 只在存在 subject/body 时才以 ? 拼接。由于地址本身可能已含 ?,这里依赖"地址优先、参数后置"的组装顺序:mailto:ck?editor@cksource.com 无参数时原样保留,有参数时(例如用户填了主题)会组装为 mailto:ck?editor@cksource.com?subject=...&body=...——此时读取侧靠 emailRegex 的负向断言仍能把地址正确切到第一个 ?subject= 之前,形成闭合的读写循环。
4.4 显示文本联动
除了地址输入框,Link 对话框还有一个"Display Text"字段。当链接类型为 email 时,对话框会用地址作为链接显示文本(见 plugins/link/dialogs/link.js 与 plugins/link/dialogs/link.js):
text = new CKEDITOR.dom.text( data.linkText || ( data.type == 'email' ?
data.email.address : attributes.set[ 'data-cke-saved-href' ] ), editor.document );
这解释了为什么地址解析出错时,不仅"E-Mail Address"框被截断,链接文本同样可能被改写为截断后的 ck——两个输入框共用同一个 data.email.address。相邻的另一个 manual 用例 tests/plugins/link/manual/emailwithatsign.md(针对 #2027)测试的正是"含 @ 的显示文本不被 href 覆盖"这一联动行为,可以对照阅读。
五、自动化测试佐证:五组 ? 场景全部覆盖
除了 manual 测试,同一 Bug 还有对应的自动化单元测试,位于 tests/plugins/link/link.js:
// (#2138)
'test email address with "?"': assertEmail( 'mailto:ck?editor@cksource.com' ),
'test email address with two "?"': assertEmail( 'mailto:ck?edi?tor@cksource.com' ),
'test email address with "?" at the beginning': assertEmail( 'mailto:?ck?editor@cksource.com' ),
'test email address with "?" in domain': assertEmail( 'mailto:?ck?editor@cksou?rce.com' ),
'test email address with "?" and arguments': assertEmail( 'mailto:ck?editor@cksource.com?subject=cke4&body=hello' ),
这五条用例系统性覆盖了问号可能出现的位置:
- 地址中间一个问号;
- 地址中间连续两个问号;
- 地址开头即问号;
- 问号出现在域名部分(
@之后); - 地址含问号且同时携带
subject=/body=参数——这是最容易回归出错的组合,因为它同时考验emailRegex的负向断言边界与参数正则的配合。
辅助函数 assertEmail 定义在 tests/plugins/link/link.js:
function assertEmail( link ) {
return function() {
var bot = this.editorBots.noValidation;
bot.setHtmlWithSelection( '<h1>Mail to <a href="' + link + '">[CKSource]</a>!</h1>' );
bot.dialog( 'link', function( dialog ) {
dialog.getButton( 'ok' ).click();
assert.areSame( '<h1>Mail to <a href="' + link + '">CKSource</a>!</h1>', bot.getData() );
} );
};
}
测试流程为:把包含待测 href 的链接置入编辑器并选中,打开链接对话框直接点击 OK(不修改任何字段),最后断言编辑器输出的 HTML 与输入完全一致。这个"打开对话框→确认→原样输出"的往返(round-trip)测试,从行为层面保证了地址在"解析→回显→回写"全链路中不被改写,与 manual 测试形成互补:自动化测试批量验证五种问号位置,manual 测试则让测试人员亲眼确认对话框中的回显值。
六、延伸:config.emailProtection 与解析边界的相互作用
#2138 的修复对默认的 mailto: 裸地址生效,但 Link 插件还支持通过 plugins/link/dialogs/link.js 中注释文档化的 config.emailProtection 配置对邮件地址做混淆保护,取值有三种:
| 取值 | 效果 | href 形态 |
|---|---|---|
''(默认,空字符串) |
不保护,直接输出 mailto: |
mailto:tester@ckeditor.com?subject=subject&body=body |
'encode' |
将地址转为字符码拼接 | javascript:void(location.href='mailto:'+String.fromCharCode(116,101,...)+'?subject=...') |
'mt(NAME,DOMAIN,SUBJECT,BODY)' 形式 |
通过自定义函数调用保护 | javascript:mt('name','domain','subject','body') |
在 parseLinkAttributes 中,emailProtection == 'encode' 的地址会先被还原成 mailto: 形态再走 emailRegex(见 plugins/link/plugin.js),函数调用形式则由 functionCallProtectedEmailLinkRegex 单独还原,并通过 email.name + '@' + email.domain 拼回地址(split( '@', 2 ) 切分,见 plugins/link/plugin.js)。可以推断:无论采用哪种保护方式,只要最终还原出的地址字符串含 ?,都会受益于 #2138 的解析修复;但函数调用形式因按 @ 拆分为 name/domain 两个参数,若问号落在 @ 两侧的不同参数中,其边界行为仍以 plugins/link/plugin.js 中 protectEmailLinkAsFunction 的 encodeURIComponent 处理为准。
仓库中还有专项测试文件 tests/plugins/link/emailprotectionencode.js 与 tests/plugins/link/emailprotectionfunctionalcall.js,需要验证保护模式时可作为参考。
七、如何在本地复现与验证
- 阅读源码确认正则行为:打开 plugins/link/plugin.js 查看
emailRegex,将mailto:ck?editor@cksource.com代入/(?:mailto)(?:(?!\?(subject|body)=).)+/i验证匹配结果为完整地址。 - 运行自动化测试:在仓库根目录执行 Bender(
bender run),运行 tests/plugins/link/link.js 中五条#2138用例,全部通过即说明修复生效。 - 运行 manual 测试:启动 Bender 后打开 tests/plugins/link/manual/emailwithquestionmark.html,按 tests/plugins/link/manual/emailwithquestionmark.md 的步骤操作:点击 "Mail to CKSource" 链接 → 打开链接对话框 → 确认 "E-Mail Address" 输入框显示
ck?editor@cksource.com而非ck。如需对照 Bug 修复前的表现,可将emailRegex临时替换为以?截断地址的旧实现并重跑本用例。 - 边界组合验证:在地址含
?的基础上于对话框中填写 Message Subject 与 Message Body 后保存,再重新打开对话框,确认地址、主题、正文三者均正确回显,且源码视图中的href为mailto:ck?editor@cksource.com?subject=...&body=...形态。
八、总结
emailwithquestionmark 这个 manual 测试用例虽短,却精准锚定了一个典型的"URI 语法歧义"类 Bug:CKEditor 4 通过 emailRegex 中的温和负向先行断言 (?:(?!\?(subject|body)=).)+,在"问号是查询串分隔符"与"问号是地址字符"之间找到了可预测的边界——只要 ? 后面不是 subject= 或 body=,就一律视为地址的一部分。配合 emailSubjectRegex / emailBodyRegex 的字符类排除,以及自动化往返测试的五行覆盖,Link 插件在 4.13.0 起对含问号邮箱地址做到了完整保留、不丢数据。理解这条链路(href → parseLinkAttributes → 对话框 setup/commit → getLinkAttributes → href),不仅有助于排查 mailto 类问题,也为理解该插件对 URL、锚点、电话、javascript 保护地址的统一解析架构提供了入口。