Sfoglia il codice sorgente

Docs: added some comments to `parseInlineStyles` in `view.Element`.

Szymon Cofalik 8 anni fa
parent
commit
ed5732fbff
1 ha cambiato i file con 18 aggiunte e 10 eliminazioni
  1. 18 10
      packages/ckeditor5-engine/src/view/element.js

+ 18 - 10
packages/ckeditor5-engine/src/view/element.js

@@ -685,31 +685,39 @@ export default class Element extends Node {
 // @param {Map.<String, String>} stylesMap Map to insert parsed properties and values.
 // @param {String} stylesString Styles to parse.
 function parseInlineStyles( stylesMap, stylesString ) {
+	// `null` if no quote was found in input string or last found quote was a closing quote. See below.
 	let quoteType = null;
 	let propertyNameStart = 0;
 	let propertyValueStart = 0;
 	let propertyName = null;
 
-	const tokens = [];
-
 	stylesMap.clear();
 
+	// Do not set anything if input string is empty.
 	if ( stylesString === '' ) {
 		return;
 	}
 
+	// Fix inline styles that do not end with `;` so they are compatible with algorithm below.
 	if ( stylesString.charAt( stylesString.length - 1 ) != ';' ) {
 		stylesString = stylesString + ';';
 	}
 
+	// Seek the whole string for "special characters".
 	for ( let i = 0; i < stylesString.length; i++ ) {
 		const char = stylesString.charAt( i );
 
 		if ( quoteType === null ) {
+			// No quote found yet or last found quote was a closing quote.
 			switch ( char ) {
 				case ':':
+					// Most of time colon means that property name just ended.
+					// Sometimes however `:` is found inside property value (for example in background image url).
 					if ( !propertyName ) {
+						// Treat this as end of property only if property name is not already saved.
+						// Save property name.
 						propertyName = stylesString.substr( propertyNameStart, i - propertyNameStart );
+						// Save this point as the start of property value.
 						propertyValueStart = i + 1;
 					}
 
@@ -717,31 +725,31 @@ function parseInlineStyles( stylesMap, stylesString ) {
 
 				case '"':
 				case '\'':
+					// Opening quote found (this is an opening quote, because `quoteType` is `null`).
 					quoteType = char;
 
 					break;
 
 				case ';':
+					// Property value just ended.
+					// Use previously stored property value start to obtain property value.
 					let propertyValue = stylesString.substr( propertyValueStart, i - propertyValueStart );
 
-					tokens.push( {
-						name: propertyName,
-						value: propertyValue
-					} );
+					// Save parsed part.
+					stylesMap.set( propertyName.trim(), propertyValue.trim() );
 
 					propertyName = null;
+
+					// Save this point as property name start. Property name starts immediately after previous property value ends.
 					propertyNameStart = i + 1;
 
 					break;
 			}
 		} else if ( char === quoteType ) {
+			// If a quote char is found and it is a closing quote, mark this fact by `null`-ing `quoteType`.
 			quoteType = null;
 		}
 	}
-
-	for ( let token of tokens ) {
-		stylesMap.set( token.name.trim(), token.value.trim() );
-	}
 }
 
 // Parses class attribute and puts all classes into classes set.