File: htmlparser.js

package info (click to toggle)
ckeditor 4.4.4%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 25,632 kB
  • ctags: 2,419
  • sloc: sh: 190; python: 37; makefile: 29; php: 15; xml: 5
file content (240 lines) | stat: -rw-r--r-- 7,931 bytes parent folder | download
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* bender-tags: editor,unit */

// Wrapper of the combination of htmlParser with htmlWriter, for convenience of
// testing, formatting of writer has been disabled.
function htmlParse( htmlString ) {
	var writer = new CKEDITOR.htmlParser.basicWriter(),
		fragment = CKEDITOR.htmlParser.fragment.fromHtml( htmlString );
	fragment.writeHtml( writer );
	return writer.getHtml();
}

bender.test(
{
		test_parse_inline : function() {
			assert.areSame( '<span class="outer">text</span>',
					htmlParse( '<span class="outer"><span></span>text</span>' ) );
		},

		test_parse_inline_2 : function() {
			assert.areSame( '<span class="outer">text</span>',
					htmlParse( '<span class="outer"><span><span></span></span>text</span>' ) );
		},

		test_parse_inline_3 : function() {
			assert.areSame( '<span class="outer"><b>text</b></span>',
					htmlParse( '<span class="outer"><span><b></span>text</b></span>' ) );
		},

		test_parse_inline_4 : function() {
			assert.areSame( '<span class="outer"><span><b>some</b></span>text</span>',
					htmlParse( '<span class="outer"><span><b>some</span></b>text</span>' ) );
		},

		test_parse_inline_5 : function() {
			assert.areSame( '<strong>some</strong>text',
					htmlParse( '<strong>some</span></strong>text' ) );
		},

		test_survive_1 : function() {
			assert.areSame( '<span class="outer"><span data-cke-survive="true"></span></span>',
					htmlParse( '<span class="outer"><span data-cke-survive="true"></span><span></span></span>' ) );
		},

		test_survive_2 : function() {
			assert.areSame( '<span class="outer"><span class="cls" data-cke-survive="true"></span>text</span>',
					htmlParse( '<span class="outer"><span class="cls" data-cke-survive="true"><span></span></span>text<span><span class="cls"></span></span></span>' ) );
		},

		test_survive_3 : function() {
			assert.areSame( '<span class="outer"><span data-cke-survive="true">text</span>text</span>',
					htmlParse( '<span class="outer"><span data-cke-survive="true">text</span>text</span>' ) );
		},

		// Attribute name may contains hypen and dot.(#4351)
		// Ref: http://www.w3.org/TR/xml/#NT-Name
		test_fromHtml_attribute_name : function() {
			var html = '<p attr-name="value" attr_name="value" attr.name="value" attr:name="value">text</p>';
			assert.areSame( html,
				htmlParse( html ),
				'Attributes doesn\'t match.' );
		},

		// Test white-spaces inside inline elements are well preserved, while
		// white-spaces before block-level elements are trimmed. (#4656)
		test_trim_whitespaces : function() {
			assert.areSame( '<div>some <strong>bold</strong> text<p>paragraph</p></div>',
				htmlParse( '<div>some <strong>bold</strong> text <p>\nparagraph</p></div>' ),
				'White-spaces don\'t match.' );
		},

		// Attributes may have the < or > character. (#7513)
		test_lt_gt_on_attributes : function() {
			assert.areSame( '<p title="a &lt; b &gt; c" class="test">Sample</p>',
				htmlParse( '<p title="a < b > c" class="test">Sample</p>' ) );
		},

		test_onTagOpen : function() {
			var counter = 0,
				parser = new CKEDITOR.htmlParser();

			parser.onTagOpen = function( tagName, attributes, selfClosing ) {
					counter += 1;

					assert.areSame( 'p', tagName, 'Tag name doesn\'t match' );
					assert.areSame( 'a\nb\r\nc', attributes[ 'class' ], 'Attribute class doesn\'t match' );
					assert.areSame( 'b', attributes.title, 'Attribute title doesn\'t match' );
					assert.areSame( 'c', attributes.name, 'Attribute name doesn\'t match' );
					assert.areSame( false, selfClosing );
				};

			// Chrome, Opera and Fx return attributes only in "" quotes.
			// But this case is properly parsed now so, as there maybe are
			// some other browsers that return attributes in different manner,
			// it can be usefull to test this for regressions.
			parser.parse( '<p class="a\nb\r\nc" title=\'b\' name=c></p>' );
			assert.areSame( 1, counter );


			parser.onTagOpen = function( tagName, attributes, selfClosing ) {
					counter += 1;

					assert.areSame( false, selfClosing, false );
					assert.areSame( 'p', tagName );
					assert.areSame( 'a', attributes.name );
					assert.areSame( 'b', attributes.title );
				};

			counter = 0;
			parser.parse( '<p\nname="a" title="b">' );
			parser.parse( '<p name="a"\r\ntitle="b">' );
			assert.areSame( 2, counter );


			parser.onTagOpen = function( tagName, attributes, selfClosing ) {
					counter += 1;

					assert.isTrue( selfClosing, 'Should be recognized as a self closing tag' );
					assert.areSame( 'br', tagName );
				};

			counter = 0;
			parser.parse( '<br />' );
			parser.parse( '<br class="a"/>' );
			assert.areSame( 2, counter );
			// shouldn't call onOpenTag
			parser.parse( '<!--<br />-->' );
			assert.areSame( 2, counter );
		},

		test_onTagClose : function() {
			var counter = 0,
				parser = new CKEDITOR.htmlParser();

			parser.onTagClose = function( tagName ) {
					counter += 1;
					assert.areSame( 'p', tagName );
				};

			parser.parse( '</p>' );
			parser.parse( '<!--</p-->' );
			assert.areSame( 1, counter );
		},

		test_onComment : function() {
			var counter = 0,
				parser = new CKEDITOR.htmlParser();

			parser.onComment = function( comment ) {
					counter += 1;
				};

			parser.parse( '<!--abc-->' );
			parser.parse( '\\<!--\\-->' );
			assert.areSame( 2, counter );
			// this is valid comment for Fx, Chrome and Opera
			// but in innerHTML they return <!---->
			parser.parse( '<!-->' );
			assert.areSame( 2, counter );
		},

		test_onCDATA : function() {
			var style_html = '<style>%s</style>',
				counter = 0,
				nodes_counter = 0,
				parser = new CKEDITOR.htmlParser();

			parser.onCDATA = function() {
					counter += 1;
				};

			parser.onTagOpen = parser.onTagClose = function( tagName ) {
					if ( tagName !== 'style' )
						nodes_counter += 1;
				};
			parser.onComment = parser.onText = function() {
					nodes_counter += 1;
				};

			parser.parse( style_html.replace( '%s', '<![CDATA[]]>' ) );
			assert.areSame( 1, counter );

			parser.parse( style_html.replace( '%s', '<![CDATA[a\r\nb\nc\n]]>' ) );
			assert.areSame( 2, counter );

			parser.parse( style_html.replace( '%s', '/*<![CDATA[<p></p><br /><!--abc<p>-->]]>*/' ) );
			assert.areSame( 3, counter );

			assert.areSame( 0, nodes_counter );
		},

		test_parse_cdata : function() {
			assert.areSame( '\<script\><![CDATA[[abc]>\</script\>',
				 htmlParse( '\<script\><![CDATA[[abc]>\</script\>' ) );

			assert.areSame( '<style><![CDATA[[abc]></style>',
				 htmlParse( '<style><![CDATA[[abc]></style>' ) );

			// In true XHTML mode this should be valid anywhere in code
			// (and it can be returned by browser - Fx and Chrome do this)
			// But this is really rare case to have this outside script or style
			// assert.areSame( '<![CDATA[[]>',
			// 	 htmlParse( '<![CDATA[[]>' ) );
		},

		test_6946_1 : function() {
			assert.areSame( '<dl><dd>a</dd><dt>b</dt></dl>',
				 htmlParse( '<dl><dd>a</dd><dt>b</dt></dl>' ) );
		},

		test_6946_2 : function() {
			assert.areSame( '<ul><li><ol></ol></li></ul>',
				 htmlParse( '<ul><ol></ul>' ) );
		},

		test_6946_3 : function() {
			assert.areSame( '<ul><li>1</li></ul><dl><dt>2</dt><dd>3</dd></dl>',
				 htmlParse( '<li>1</li><dt>2</dt><dd>3</dd>' ) );
		},

		test_6975 : function() {
			assert.areSame( '<dl><dt>foo</dt><dd>bar</dd><dt>baz</dt><dd>quz</dd></dl>',
				 htmlParse( '<dl><dt>foo<dd>bar<dt>baz<dd>quz</dl>' ) );
		},

		test_7494 : function() {
			assert.areSame( '<ol><li>1<ol><li>2</li></ol></li><li>3</li></ol>',
				 htmlParse( '<ol><li>1</li><ol><li>2</li></ol><li>3</li></ol>' ) );
		},

		test_7497 : function() {
			assert.areSame( '<p>1</p><ul><li>2</li></ul><p>3</p>',
				 htmlParse( '<p>1</p><li>2</li><p>3</p>' ) );
		},

		'test attributes are decoded': function() {
			var elP = CKEDITOR.htmlParser.fragment.fromHtml( '<p foo="&lt;&quot;&gt;">bar</p>' ).children[ 0 ];

			assert.areSame( '<">', elP.attributes.foo );
		}
} );