Exemple #1
0
        private int ProcessTextNode(StringCollection tokens, int index, HtmlElement htmlElement, HtmlNodeCollection nodes)
        {
            if (tokens == null)
            {
                throw new ArgumentNullException(nameof(tokens));
            }

            if (nodes == null)
            {
                throw new ArgumentNullException(nameof(nodes));
            }

            var value = tokens[index];

            if (_removeEmptyElementText)
            {
                value = RemoveWhitespace(value);
            }

            value = DecodeScript(value);
            if (_removeEmptyElementText && (value == null || value.Length == 0))
            {
                return(++index);
            }

            if (htmlElement == null || !htmlElement.NoEscaping)
            {
                value = HtmlElementEncoder.DecodeValue(value);
            }

            nodes.Add(new HtmlText(value));

            return(++index);
        }
Exemple #2
0
        /// <summary>
        /// Parses the file.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <param name="separation">The separation.</param>
        /// <param name="htmlEncodeValues">if set to <c>true</c> [HTML encode values].</param>
        /// <param name="encoding">The encoding.</param>
        /// <returns></returns>
        public bool ParseFile(string filename, char separation, bool htmlEncodeValues, System.Text.Encoding encoding)
        {
            string buffer, lastGroup = string.Empty, groupName, keyName, keyValue;
            //string[] tempArray;

            // Try to open the ini file. If it doen't exist, the file is created.
            FileStream iniFile = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Read);

            // Initialize the streamreader object to read the content of the file then go
            // to the begin of the file.
            StreamReader fileRead = new StreamReader(iniFile, encoding);

            fileRead.BaseStream.Seek(0, SeekOrigin.Begin);

            // Now read all the file till the end.
            while (fileRead.Peek() > -1)
            {
                // Gets the line into the buffer string.
                buffer = fileRead.ReadLine().Trim();

                // Test if the string contain a subcategory delimiter.
                if (buffer.StartsWith("["))
                {
                    groupName = ParseGroup(buffer);
                    // Create a new item in the reference list.
                    if (!this.Groups.Contains(groupName))
                    {
                        this.Groups.Add(groupName);
                        lastGroup = groupName;
                    }
                }
                // Test if the string actually contain something intersting.
                else if (buffer.Length > 1 && buffer.IndexOf(separation) > -1 && !buffer.StartsWith(";") && !buffer.StartsWith("'"))
                {
                    // Split the string to get the Key and Value.
                    keyName  = buffer.Substring(0, buffer.IndexOf(separation));
                    keyValue = buffer.Substring(buffer.IndexOf(separation) + 1, buffer.Length - buffer.IndexOf(separation) - 1);

                    if (htmlEncodeValues)
                    {
                        keyValue = HtmlElementEncoder.EncodeValue(keyValue);
                    }
                    //tempArray = buffer.Split(separation);

                    //if (tempArray.Length > 1 && this.Groups.Contains(lastGroup))
                    if (keyName != string.Empty && this.Groups.Contains(lastGroup))
                    {
                        this.Groups[lastGroup].Keys.Add(keyName, keyValue);
                    }
                    //this.Groups[lastGroup].Keys.Add(tempArray[0], tempArray[1]);
                }
            }

            fileRead.Close();

            return(true);
        }
Exemple #3
0
        private int ProcessStartTag(StringCollection tokens, int index, Action <HtmlElement> processHtmlElement)
        {
            if (tokens == null)
            {
                throw new ArgumentNullException(nameof(tokens));
            }

            if (++index >= tokens.Count)
            {
                return(-1);
            }

            var tagName = tokens[index++];
            var element = new HtmlElement(tagName);

            while (index < tokens.Count &&
                   !EndTagIdentifier.Equals(tokens[index], StringComparison.OrdinalIgnoreCase) &&
                   !EndTagIdentifierVariation1.Equals(tokens[index], StringComparison.OrdinalIgnoreCase))
            {
                var attributeName  = tokens[index++];
                var attributeValue = string.Empty;

                if (index < tokens.Count &&
                    EqualityIdentifier.Equals(tokens[index], StringComparison.OrdinalIgnoreCase))
                {
                    attributeValue = HtmlElementEncoder.DecodeValue(++index < tokens.Count ? tokens[index] : null);
                    index++;
                }
                else if (index < tokens.Count)
                {
                    attributeValue = null;
                }

                var attribute = new HtmlAttribute(attributeName, attributeValue);
                element.Attributes.Add(attribute);
            }

            processHtmlElement?.Invoke(element);

            if (index < tokens.Count &&
                EndTagIdentifierVariation1.Equals(tokens[index], StringComparison.OrdinalIgnoreCase))
            {
                element.IsTerminated = true;
                index++;
            }
            else if (index < tokens.Count &&
                     EndTagIdentifier.Equals(tokens[index], StringComparison.OrdinalIgnoreCase))
            {
                index++;
            }

            return(index);
        }