]>
git.immae.eu Git - perso/Immae/Projets/packagist/piedsjaloux-ckeditor-component.git/blob - sources/core/htmlparser.js
2 * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
3 * For licensing, see LICENSE.md or http://ckeditor.com/license
7 * Provides an "event like" system to parse strings of HTML data.
9 * var parser = new CKEDITOR.htmlParser();
10 * parser.onTagOpen = function( tagName, attributes, selfClosing ) {
13 * parser.parse( '<p>Some <b>text</b>.</p>' ); // Alerts 'p', 'b'.
16 * @constructor Creates a htmlParser class instance.
18 CKEDITOR
.htmlParser = function() {
20 htmlPartsRegex: /<(?:(?:\/([^>]+)>)|(?:!--([\S|\s]*?)-->)|(?:([^\/\s>]+)((?:\s+[\w\-:.]+(?:\s*=\s*?(?:(?:"[^"]*")|(?:'[^']*')|[^\s"'\/>]+))?)*)[\S\s]*?(\/?)>))/g
25 var attribsRegex
= /([\w\-:.]+)(?:(?:\s*=\s*(?:(?:"([^"]*)")|(?:'([^']*)')|([^\s>]+)))|(?=\s|$))/g,
26 emptyAttribs
= { checked: 1, compact: 1, declare: 1, defer: 1, disabled: 1, ismap: 1, multiple: 1, nohref: 1, noresize: 1, noshade: 1, nowrap: 1, readonly: 1, selected: 1 };
28 CKEDITOR
.htmlParser
.prototype = {
30 * Function to be fired when a tag opener is found. This function
31 * should be overriden when using this class.
33 * var parser = new CKEDITOR.htmlParser();
34 * parser.onTagOpen = function( tagName, attributes, selfClosing ) {
35 * alert( tagName ); // e.g. 'b'
37 * parser.parse( '<!-- Example --><b>Hello</b>' );
39 * @param {String} tagName The tag name. The name is guarantted to be lowercased.
40 * @param {Object} attributes An object containing all tag attributes. Each
41 * property in this object represent and attribute name and its value is the attribute value.
42 * @param {Boolean} selfClosing `true` if the tag closes itself, false if the tag doesn't.
44 onTagOpen: function() {},
47 * Function to be fired when a tag closer is found. This function
48 * should be overriden when using this class.
50 * var parser = new CKEDITOR.htmlParser();
51 * parser.onTagClose = function( tagName ) {
52 * alert( tagName ); // 'b'
54 * parser.parse( '<!-- Example --><b>Hello</b>' );
56 * @param {String} tagName The tag name. The name is guarantted to be lowercased.
58 onTagClose: function() {},
61 * Function to be fired when text is found. This function
62 * should be overriden when using this class.
64 * var parser = new CKEDITOR.htmlParser();
65 * parser.onText = function( text ) {
66 * alert( text ); // 'Hello'
68 * parser.parse( '<!-- Example --><b>Hello</b>' );
70 * @param {String} text The text found.
72 onText: function() {},
75 * Function to be fired when CDATA section is found. This function
76 * should be overriden when using this class.
78 * var parser = new CKEDITOR.htmlParser();
79 * parser.onCDATA = function( cdata ) {
80 * alert( cdata ); // 'var hello;'
82 * parser.parse( '<script>var hello;</script>' );
84 * @param {String} cdata The CDATA been found.
86 onCDATA: function() {},
89 * Function to be fired when a commend is found. This function
90 * should be overriden when using this class.
92 * var parser = new CKEDITOR.htmlParser();
93 * parser.onComment = function( comment ) {
94 * alert( comment ); // ' Example '
96 * parser.parse( '<!-- Example --><b>Hello</b>' );
98 * @param {String} comment The comment text.
100 onComment: function() {},
103 * Parses text, looking for HTML tokens, like tag openers or closers,
104 * or comments. This function fires the onTagOpen, onTagClose, onText
105 * and onComment function during its execution.
107 * var parser = new CKEDITOR.htmlParser();
108 * // The onTagOpen, onTagClose, onText and onComment should be overriden
110 * parser.parse( '<!-- Example --><b>Hello</b>' );
112 * @param {String} html The HTML to be parsed.
114 parse: function( html
) {
117 cdata
; // The collected data inside a CDATA section.
119 while ( ( parts
= this._
.htmlPartsRegex
.exec( html
) ) ) {
120 var tagIndex
= parts
.index
;
121 if ( tagIndex
> nextIndex
) {
122 var text
= html
.substring( nextIndex
, tagIndex
);
130 nextIndex
= this._
.htmlPartsRegex
.lastIndex
;
132 // "parts" is an array with the following items:
133 // 0 : The entire match for opening/closing tags and comments.
134 // : Group filled with the tag name for closing tags.
135 // 2 : Group filled with the comment text.
136 // 3 : Group filled with the tag name for opening tags.
137 // 4 : Group filled with the attributes part of opening tags.
140 if ( ( tagName
= parts
[ 1 ] ) ) {
141 tagName
= tagName
.toLowerCase();
143 if ( cdata
&& CKEDITOR
.dtd
.$cdata
[ tagName
] ) {
144 // Send the CDATA data.
145 this.onCDATA( cdata
.join( '' ) );
150 this.onTagClose( tagName
);
155 // If CDATA is enabled, just save the raw match.
157 cdata
.push( parts
[ 0 ] );
162 if ( ( tagName
= parts
[ 3 ] ) ) {
163 tagName
= tagName
.toLowerCase();
165 // There are some tag names that can break things, so let's
166 // simply ignore them when parsing. (#5224)
167 if ( /="/.test( tagName
) )
172 attribsPart
= parts
[ 4 ],
173 selfClosing
= !!parts
[ 5 ];
176 while ( ( attribMatch
= attribsRegex
.exec( attribsPart
) ) ) {
177 var attName
= attribMatch
[ 1 ].toLowerCase(),
178 attValue
= attribMatch
[ 2 ] || attribMatch
[ 3 ] || attribMatch
[ 4 ] || '';
180 if ( !attValue
&& emptyAttribs
[ attName
] )
181 attribs
[ attName
] = attName
;
183 attribs
[ attName
] = CKEDITOR
.tools
.htmlDecodeAttr( attValue
);
187 this.onTagOpen( tagName
, attribs
, selfClosing
);
189 // Open CDATA mode when finding the appropriate tags.
190 if ( !cdata
&& CKEDITOR
.dtd
.$cdata
[ tagName
] )
197 if ( ( tagName
= parts
[ 2 ] ) )
198 this.onComment( tagName
);
201 if ( html
.length
> nextIndex
)
202 this.onText( html
.substring( nextIndex
, html
.length
) );