/// <summary> /// Parses a If conditional comment /// </summary> /// <param name="expression">Conditional expression</param> /// <param name="type">Conditional comment type</param> private void ParseIfConditionalComment(string expression, HtmlConditionalCommentType type) { if (_conditionalCommentStack.Count > 0) { throw new MarkupParsingException( Strings.ErrorMessage_NotClosedConditionalComment, _innerContext.NodeCoordinates, _innerContext.GetSourceFragment()); } string processedExpression = expression.Trim(); _conditionalCommentStack.Push(new HtmlConditionalComment(expression, type)); if (type == HtmlConditionalCommentType.Hidden || type == HtmlConditionalCommentType.Revealed) { _conditionalCommentOpened = true; } _handlers.IfConditionalComment?.Invoke(_context, new HtmlConditionalComment(processedExpression, type)); }
/// <summary> /// Parses a End If conditional comment /// </summary> /// <param name="type">Conditional comment type</param> private void ParseEndIfConditionalComment(HtmlConditionalCommentType type) { if (_conditionalCommentStack.Count > 0) { var stackedConditionalComment = _conditionalCommentStack.Pop(); var stackedType = stackedConditionalComment.Type; if (type == HtmlConditionalCommentType.Hidden || type == HtmlConditionalCommentType.Revealed) { if (stackedType == type) { _conditionalCommentOpened = false; } else { throw new MarkupParsingException( Strings.ErrorMessage_InvalidEndIfConditionalComment, _innerContext.NodeCoordinates, _innerContext.GetSourceFragment()); } } else if (type == HtmlConditionalCommentType.RevealedValidating || type == HtmlConditionalCommentType.RevealedValidatingSimplified) { if (stackedType != HtmlConditionalCommentType.RevealedValidating && stackedType != HtmlConditionalCommentType.RevealedValidatingSimplified) { throw new MarkupParsingException( Strings.ErrorMessage_InvalidEndIfConditionalComment, _innerContext.NodeCoordinates, _innerContext.GetSourceFragment()); } } } else { throw new MarkupParsingException( Strings.ErrorMessage_IfConditionalCommentNotDeclared, _innerContext.NodeCoordinates, _innerContext.GetSourceFragment()); } _handlers.EndIfConditionalComment?.Invoke(_context, type); }
/// <summary> /// Parses a start tag /// </summary> /// <param name="tagName">Tag name</param> /// <param name="tagNameInLowercase">Tag name in lowercase</param> /// <param name="attributes">List of attributes</param> /// <param name="isEmptyTag">Flag that tag is empty</param> private void ParseStartTag(string tagName, string tagNameInLowercase, List <HtmlAttribute> attributes, bool isEmptyTag) { HtmlTagFlags tagFlags = GetTagFlagsByName(tagNameInLowercase); if (tagFlags.IsSet(HtmlTagFlags.Optional)) { HtmlTag lastStackedTag = _tagStack.LastOrDefault(); if (lastStackedTag != null && lastStackedTag.NameInLowercase == tagNameInLowercase) { ParseEndTag(lastStackedTag.Name, lastStackedTag.NameInLowercase); } else { if (tagNameInLowercase == "body" && _tagStack.Any(t => t.NameInLowercase == "head")) { HtmlTag headTag = _tagStack.First(t => t.NameInLowercase == "head"); ParseEndTag(headTag.Name, headTag.NameInLowercase); } } } if (tagFlags.IsSet(HtmlTagFlags.Empty)) { isEmptyTag = true; } else if (isEmptyTag) { tagFlags |= HtmlTagFlags.Empty; } int attributeCount = attributes.Count; for (int attributeIndex = 0; attributeIndex < attributeCount; attributeIndex++) { HtmlAttribute attribute = attributes[attributeIndex]; attribute.Type = _attributeTypeDeterminer.GetAttributeType(tagNameInLowercase, tagFlags, attribute.NameInLowercase, attributes); } var tag = new HtmlTag(tagName, tagNameInLowercase, attributes, tagFlags); if (!isEmptyTag) { if (_conditionalCommentOpened) { HtmlConditionalComment lastConditionalComment = _conditionalCommentStack.Peek(); HtmlConditionalCommentType lastConditionalCommentType = lastConditionalComment.Type; if (tagFlags.IsSet(HtmlTagFlags.EmbeddedCode) || lastConditionalCommentType == HtmlConditionalCommentType.RevealedValidating || lastConditionalCommentType == HtmlConditionalCommentType.RevealedValidatingSimplified) { _tagStack.Add(tag); } } else { _tagStack.Add(tag); } } _handlers.StartTag?.Invoke(_context, tag); if (tagFlags.IsSet(HtmlTagFlags.Xml) && !tagFlags.IsSet(HtmlTagFlags.NonIndependent)) { _xmlTagStack.Push(tagNameInLowercase); } }
/// <summary> /// Parses a If conditional comment /// </summary> /// <param name="expression">Conditional expression</param> /// <param name="type">Conditional comment type</param> private void ParseIfConditionalComment(string expression, HtmlConditionalCommentType type) { if (_conditionalCommentStack.Count > 0) { throw new MarkupParsingException( Strings.ErrorMessage_NotClosedConditionalComment, _innerContext.NodeCoordinates, _innerContext.GetSourceFragment()); } string processedExpression = expression.Trim(); _conditionalCommentStack.Push(new HtmlConditionalComment(expression, type)); if (type == HtmlConditionalCommentType.Hidden || type == HtmlConditionalCommentType.Revealed) { _conditionalCommentOpened = true; } if (_handlers.IfConditionalComment != null) { _handlers.IfConditionalComment(_context, new HtmlConditionalComment(processedExpression, type)); } }
/// <summary> /// Parses a End If conditional comment /// </summary> /// <param name="type">Conditional comment type</param> private void ParseEndIfConditionalComment(HtmlConditionalCommentType type) { if (_conditionalCommentStack.Count > 0) { var stackedConditionalComment = _conditionalCommentStack.Pop(); var stackedType = stackedConditionalComment.Type; if (type == HtmlConditionalCommentType.Hidden || type == HtmlConditionalCommentType.Revealed) { if (stackedType == type) { _conditionalCommentOpened = false; } else { throw new MarkupParsingException( Strings.ErrorMessage_InvalidEndIfConditionalComment, _innerContext.NodeCoordinates, _innerContext.GetSourceFragment()); } } else if (type == HtmlConditionalCommentType.RevealedValidating || type == HtmlConditionalCommentType.RevealedValidatingSimplified) { if (stackedType != HtmlConditionalCommentType.RevealedValidating && stackedType != HtmlConditionalCommentType.RevealedValidatingSimplified) { throw new MarkupParsingException( Strings.ErrorMessage_InvalidEndIfConditionalComment, _innerContext.NodeCoordinates, _innerContext.GetSourceFragment()); } } } else { throw new MarkupParsingException( Strings.ErrorMessage_IfConditionalCommentNotDeclared, _innerContext.NodeCoordinates, _innerContext.GetSourceFragment()); } if (_handlers.EndIfConditionalComment != null) { _handlers.EndIfConditionalComment(_context, type); } }
/// <summary> /// Constructs instance of HTML tag /// </summary> /// <param name="expression">Conditional expression</param> /// <param name="type">Conditional comment type</param> public HtmlConditionalComment(string expression, HtmlConditionalCommentType type) { Expression = expression; Type = type; }
/// <summary> /// End If conditional comments handler /// </summary> /// <param name="context">Markup parsing context</param> /// <param name="type">End If conditional comment type</param> private void EndIfConditionalCommentHandler(MarkupParsingContext context, HtmlConditionalCommentType type) { _currentNodeType = HtmlNodeType.EndIfConditionalComment; string endIfComment; switch (type) { case HtmlConditionalCommentType.Hidden: endIfComment = "<![endif]-->"; break; case HtmlConditionalCommentType.RevealedValidating: case HtmlConditionalCommentType.RevealedValidatingSimplified: endIfComment = "<!--<![endif]-->"; break; case HtmlConditionalCommentType.Revealed: endIfComment = "<![endif]>"; break; default: throw new NotSupportedException(); } _buffer.Add(endIfComment); }