File: nestedprotectedcomplex.html

package info (click to toggle)
ckeditor 4.16.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 258,804 kB
  • sloc: javascript: 239,590; sh: 184; makefile: 64; python: 37; php: 15; xml: 5
file content (144 lines) | stat: -rw-r--r-- 3,674 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<p>
	<button id="check">Check source protection</button>
</p>
<p id="result" style="color: white;"></p>
<div id="editor">
	<h2>First widget</h2>
	<div class="testwidget">
		<div class="header">
			<p>I have a <strong>very important</strong> message</p>
		</div>
		<div class="content">
			<div class="testwidget">
				<div class="header">
					<p>Yeah, it's very important</p>
				</div>
				<div class="content">
					<p>CKEditor rulez</p>
					<script src="data:text/javascript,''"></script>
				</div>
				<div class="footer">
					<p>As you, it was really <strong>very important</strong> one.</p>
				</div>
			</div>
		</div>
		<div class="footer">
			<p>End of the message</p>
		</div>
	</div>
	<h2>Second widget</h2>
	<div class="testwidget">
		<div class="header">
			<p>I have a <strong>very important</strong> message</p>
		</div>
		<div class="content">
			<div class="testwidget">
				<div class="header">
					<p>Yeah, it's very important</p>
				</div>
				<div class="content">
					<p>CKEditor rulez</p>
					<script src="data:text/javascript,''"></script>
				</div>
				<div class="footer">
					<p>As you, it was really <strong>very important</strong> one.</p>
				</div>
			</div>
		</div>
		<div class="footer">
			<p>End of the message</p>
		</div>
	</div>
	<h2>Third widget</h2>
	<div class="testwidget">
		<div class="header">
			<p>I have a <strong>very important</strong> message</p>
		</div>
		<div class="content">
			<div class="testwidget">
				<div class="header">
					<p>Yeah, it's very important</p>
				</div>
				<div class="content">
					<p>CKEditor rulez</p>
					<script src="data:text/javascript,''"></script>
				</div>
				<div class="footer">
					<p>As you, it was really <strong>very important</strong> one.</p>
				</div>
			</div>
		</div>
		<div class="footer">
			<p>End of the message</p>
		</div>
	</div>
</div>

<script>
CKEDITOR.plugins.add( 'testwidget', {
	requires: 'widget',

	init: function( editor ) {
		editor.widgets.add( 'testwidget', {

			template: '<div class="testwidget">' +
					'<div class="header"></div>' +
					'<div class="content"></div>' +
					'<div class="footer"></div>' +
				'</div>',

			editables: {
				header: {
					selector: '.header',
					allowedContent: 'p;script[src];div(*);strong'
				},

				content: {
					selector: '.content',
					allowedContent: 'p;script[src];div(*);strong'
				},

				footer: {
					selector: '.footer',
					allowedContent: 'p;script[src];div(*);strong'
				}
			},

			allowedContent:
				'div(!testwidget); div(!content);',

			requiredContent: 'div(testwidget);',

			upcast: function( element ) {
				return element.name == 'div' && element.hasClass( 'testwidget' );
			}
		} );
	},
} );

CKEDITOR.replace( 'editor', {
	extraPlugins: 'testwidget',
	extraAllowedContent: 'div(testwidget,content,header,footer);script[src];h2;strong',
	on: {
		instanceReady: function( evt ) {
			var editor = evt.editor,
				button = CKEDITOR.document.getById( 'check' ),
				result = CKEDITOR.document.getById( 'result' ),
				protectedRegex = /<!--{cke_protected}.+?-->/g,
				unprotectedRegex = /<script src="data:text\/javascript,''"><\/script>/g;

			button.on( 'click', function() {
				var editable = editor.editable(),
					isWysiwyg = editor.mode === 'wysiwyg',
					html = isWysiwyg ? editable.getHtml() : editable.getValue(),
					modeRegex = isWysiwyg ? protectedRegex : unprotectedRegex,
					match = html.match( modeRegex ),
					isOk = match && match.length === 3;

				result.setHtml( ( isOk ? 'Ok' : 'Not ok' ) + ' (editor mode: ' + editor.mode + ')' );
				result.setStyle( 'background-color', isOk ? 'green' : 'red' );
			} );
		}
	}
} );
</script>