阻止CKEditor在源代码模式下格式化代码 [英] Prevent CKEditor from formatting code in source mode

查看:1116
本文介绍了阻止CKEditor在源代码模式下格式化代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在源代码模式下查看时,如何在CKEditor中阻止自动格式化

How can you prevent any automatic formatting when in CKEditor when viewing in source mode?

我想直接编辑HTML源代码使用WYSIWYG界面,但每当我写新行或布局标签如何缩进他们,当我切换到所见即所得模式,然后再回到源模式,所有格式化。

I like to edit HTML source code directly instead of using the WYSIWYG interface, but whenever I write new lines, or layout tags how I would indent them, it all gets formatted when I switch to WYSIWYG mode and then back to source mode again.

我偶然发现了一个CKEditor开发票,保护ProtectedSource元素的格式化,暗示了一个可能已经存在一次的时间,这将是我的后一个设置。我只想知道在源代码模式下编辑时如何完全关闭所有自动格式化。

I stumbled upon a CKEditor dev ticket, Preserve formatting of ProtectedSource elements, that alluded to a setting which may have existed once upon a time which would be exactly what I'm after. I just want to know how I can completely turn off all automatic formatting when editing in source mode.

我想出了一个解决方案,我认为这是一个万无一失愉快的一个)。

I came up with a solution I thought would be foolproof (albeit not a pleasant one).

我学习了 protectedSource 设置,所以我想,也许我可以使用并在所有HTML之前创建一个HTML注释标签,然后在一个正则表达式中查找注释标签到 protectedSource 数组中,但即使是这样)不工作。

I learned about the protectedSource setting, so I thought, well maybe I can just use that and create an HTML comment tag before all my HTML and another after it and then push a regular expression finding the comment tags into the protectedSource array, but even that (believe it or not) doesn't work.

我在CKEditor之外的浏览器中尝试了我的表达式,并且工作,但CKEditor不保护代码预期(我怀疑是涉及评论标签的错误,因为我可以得到它与其他字符串一起工作)。如果你想知道,这是我希望的工作,作为一个工作,但不是:

I've tried my expression straight up in the browser outside of CKEditor and it is working, but CKEditor doesn't protect the code as expected (which I suspect is a bug involving comment tags, since I can get it to work with other strings). In case you are wondering, this is what I had hoped would work as a work-around, but doesn't:

config.protectedSource.push( /<!-- src -->[\s\S]*<!-- end src-->/gi );

以及我计划在做什么(似乎是缺乏一个设置禁用格式化源模式)是将所有的HTML嵌入到注释的标签中,如下所示:

and what I planned on doing (for what appears to be the lack of a setting to disable formatting in source mode) was to nest all my HTML within the commented tags like this:

<!-- src -->
<div>some code that shouldn't be messed with (but is)</div>
<!-- end src -->

我想听听如果有人对这个场景有任何建议,我已经描述了,或者甚至有人可以只是填补我为什么我不能得到 protectedSource 使用两个注释标签正常工作。

I'd love to hear if anyone has any suggestions for this scenario, or knows of a setting which I have described, or even if someone can just fill me in as to why I can't get protectedSource to work properly with two comment tags.

我真的认为这是一个错误,因为我可以得到这么多其他表达式工作正常,我甚至可以保护HTML在单个评论标签的区域内,

I really think it's gotta be a bug because I can get so many other expressions to work fine, and I can even protect HTML within the area of a single comment tag, but I simply cannot get HTML within two different comment tags to stay untouched.

推荐答案

我的解决方案是在我的评论中使用评论系统,但在将页面内容提供给CKEditor之前,将它们转换为自定义HTML标记。然后,保存后,将它们转换回我的注释标签。

My solution to this was to use comments in my system, but before feeding the page content to CKEditor, convert them to custom HTML tags. Then, upon save, convert them back to my comment tags.

对于你的语法,这将是这样的PHP。将网页内容打印到textarea之前:

For your syntax that would be something like this in PHP. Before printing the page content to the textarea:

$content = str_replace(array('<!-- src -->','<!-- end src -->'),array('<protected>','</protected>'),$content);

保存结果内容之前:

$content = str_replace(array('<protected>','</protected>'),array('<!-- src -->','<!-- end src -->'),$content);

在CKEditor配置中:

In the CKEditor configuration:

protectedSource:[/<protected>[\s\S]*<\/protected>/g]

希望有帮助!

这篇关于阻止CKEditor在源代码模式下格式化代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆