<%--
/**
 * Copyright (c) 2000-2017 Liferay, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Liferay Enterprise
 * pSubscription License ("License"). You may not use this file except in
 * compliance with the License. You can obtain a copy of the License by
 * contacting Liferay, Inc. See the License for the specific language governing
 * permissions and limitations under the License, including but not limited to
 * distribution rights of the Software.
 *
 *
 *
 */
--%>

<%@ include file="/html/js/editor/init.jsp" %>

<%@ taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %>

<%
CustomCKEditorOutputData newOutputData = new CustomCKEditorOutputData(request);

newOutputData.wrap();

CustomCKEditorScriptData newScriptData = new CustomCKEditorScriptData(request);

newScriptData.wrap();
%>

<liferay-util:buffer var="html">
	<liferay-util:include page="/html/js/editor/ckeditor.jsp" />
</liferay-util:buffer>

<%
newOutputData.unwrap();

newScriptData.unwrap();
%>

<%= _replaceVariations(html) %>

<%!
private static class CustomCKEditorScriptData extends ScriptData {

	public CustomCKEditorScriptData(HttpServletRequest request) {
		_request = request;
	}

	@Override
	public void append(String portletId, StringBundler contentSB, String use) {
		_replaceVariations(contentSB);

		_scriptData.append(portletId, contentSB, use);
	}

	public void unwrap() {
		_request.setAttribute(WebKeys.AUI_SCRIPT_DATA, _scriptData);
	}

	public void wrap() {
		_scriptData = (ScriptData)_request.getAttribute(WebKeys.AUI_SCRIPT_DATA);

		if (_scriptData == null) {
			_scriptData = new ScriptData();
		}

		_request.setAttribute(WebKeys.AUI_SCRIPT_DATA, this);
	}

	private HttpServletRequest _request;
	private ScriptData _scriptData;

}

private static class CustomCKEditorOutputData extends OutputData {

	public CustomCKEditorOutputData(HttpServletRequest request) {
		_request = request;
	}

	@Override
	public void addData(String outputKey, String webKey, StringBundler sb) {
		_replaceVariations(sb);

		_outputData.addData(outputKey, webKey, sb);
	}

	public void unwrap() {
		_request.setAttribute(WebKeys.OUTPUT_DATA, _outputData);
	}

	public void wrap() {
		_outputData = (OutputData)_request.getAttribute(WebKeys.OUTPUT_DATA);

		if (_outputData == null) {
			_outputData = new OutputData();
		}

		_request.setAttribute(WebKeys.OUTPUT_DATA, this);
	}

	private OutputData _outputData;
	private HttpServletRequest _request;

}

private static String _replaceVariations(String content) {
	String ckEditor = "/ckeditor/";
	String ckEditorLatest = "/ckeditor_latest/";

	if (!content.contains(ckEditor)) {
		ckEditor = HtmlUtil.escapeAttribute(ckEditor);
		ckEditorLatest = HtmlUtil.escapeAttribute(ckEditorLatest);
	}

	content = StringUtil.replace(content, ckEditor, ckEditorLatest);

	content = StringUtil.replace(content, "CKEDITOR.env.isCompatible = true;", "");

	return content;
}

private static void _replaceVariations(StringBundler sb) {
	String content = sb.toString();

	content = _replaceVariations(content);

	sb.setIndex(0);

	sb.append(content);
}
%>