Пример #1
0
        private IElement CreateElem(EStyleCommand cmd, string styleAttr, IDocument document, Func <string> attributeValue)
        {
            IElement elem = null;
            var      name = GetElementName(cmd);

            switch (cmd)
            {
            case EStyleCommand.Font:
                elem = document.CreateElement(name);
                elem.SetAttribute(HTMLConstants.Style, ReDefineStyle(styleAttr, HTMLConstants.StyleFontFamily, attributeValue()));
                break;

            case EStyleCommand.Bold:
            case EStyleCommand.Emphasis:
                elem = document.CreateElement(name);
                break;

            case EStyleCommand.Color:
                elem = document.CreateElement(name);
                elem.SetAttribute(HTMLConstants.Style, ReDefineStyle(styleAttr, HTMLConstants.StyleColor, attributeValue()));
                break;

            case EStyleCommand.BackGroundColor:
                elem = document.CreateElement(name);
                elem.SetAttribute(HTMLConstants.Style, ReDefineStyle(styleAttr, HTMLConstants.StyleBackColor, attributeValue()));
                break;
            }
            return(elem);
        }
Пример #2
0
 private bool CommandIsBlockNodeCommand(EStyleCommand cmd)
 {
     return(cmd == EStyleCommand.TextAlignCentre ||
            cmd == EStyleCommand.TextAlignJustify ||
            cmd == EStyleCommand.TextAlignLeft ||
            cmd == EStyleCommand.TextAlignRight);
 }
Пример #3
0
        private void ApplyElementStyle(EStyleCommand cmd, INode node, Func <string> attributeValue, IDocument document)
        {
            INode parentElem = null;

            switch (cmd)
            {
            case EStyleCommand.Font:
                StyleAttribute = ReDefineStyle(StyleAttribute, HTMLConstants.StyleFontFamily, attributeValue());
                GetParentElement(node).SetAttribute(HTMLConstants.Style, StyleAttribute);
                break;

            case EStyleCommand.Bold:
                parentElem = ParentElementAlreadyExists(node, HTMLConstants.SpanStrong);
                if (parentElem != null)
                {
                    RemoveFromParentKeepContent(node, HTMLConstants.SpanStrong, document);
                }
                else
                {
                    var strong = document.CreateElement(HTMLConstants.SpanStrong);
                    GetParentElement(node).ReplaceChild(strong, node);
                    strong.AppendChild(node);
                }
                break;

            case EStyleCommand.Emphasis:
                parentElem = ParentElementAlreadyExists(node, HTMLConstants.SpanEm);
                if (parentElem != null)
                {
                    RemoveFromParentKeepContent(node, HTMLConstants.SpanEm, document);
                }
                else
                {
                    var em = document.CreateElement(HTMLConstants.SpanEm);
                    em.TextContent   = node.TextContent;
                    node.TextContent = "";
                    GetParentElement(node).ReplaceChild(em, node);
                }
                break;

            case EStyleCommand.Color:
                StyleAttribute = ReDefineStyle(StyleAttribute, HTMLConstants.StyleColor, attributeValue());
                GetParentElement(node).SetAttribute(HTMLConstants.Style, StyleAttribute);
                break;

            case EStyleCommand.BackGroundColor:
                StyleAttribute = ReDefineStyle(StyleAttribute, HTMLConstants.StyleBackColor, attributeValue());
                GetParentElement(node).SetAttribute(HTMLConstants.Style, StyleAttribute);
                break;

            case EStyleCommand.FormatClear:
                GetParentElement(node).SetAttribute(HTMLConstants.Style, "");
                break;
            }
        }
Пример #4
0
        private string GetElementName(EStyleCommand cmd)
        {
            switch (cmd)
            {
            case EStyleCommand.Bold:
                return(HTMLConstants.SpanStrong);

            case EStyleCommand.Emphasis:
                return(HTMLConstants.SpanEm);

            default:
                return(HTMLConstants.SpanTag);
            }
        }
Пример #5
0
        /// <summary>
        /// Applies/Unapplies style to the elements in the given range.
        /// </summary>
        /// <param name="range">Range</param>
        /// <param name="cmd">A style command</param>
        /// <param name="document"></param>
        /// <param name="attributeValue">The attribute value, that fits the style command</param>
        public void ApplyStyleCommand(MarkUpRange range, EStyleCommand cmd, IDocument document, Func <string> attributeValue)
        {
            IElement elem;

            void createStyleElement()
            {
                elem = CreateElem(cmd, StyleAttribute, document, attributeValue);
            }

            var isCmdForBlockElement = CommandIsBlockNodeCommand(cmd);

            if (cmd == EStyleCommand.FormatClear)
            {
                var elementWithStyle = FindParentElementWithAStyleAttribute(Node) as IElement;
                if (elementWithStyle != null)
                {
                    elementWithStyle.RemoveAttribute(HTMLConstants.Style);
                    if (elementWithStyle.NodeName == HTMLConstants.SpanTag)
                    {
                        RemoveFromParentKeepContent(Node, HTMLConstants.SpanTag, document);
                    }
                }
                return;
            }

            if (isCmdForBlockElement)
            {
                var blockElement = FindBlockParent(Node) as IElement;
                if (blockElement != null)
                {
                    var blockstyleAttr = blockElement.GetAttribute(HTMLConstants.Style);
                    StyleAttribute = ReDefineStyle(blockstyleAttr, HTMLConstants.TextAlign, attributeValue());
                    blockElement.SetAttribute(HTMLConstants.Style, StyleAttribute);
                }
                return;
            }

            var    rangeLength    = (range.PositionEnd - range.PositionStart);
            var    posEndWord     = range.PositionEnd - Offset;
            var    posStartWord   = range.PositionStart > Offset ? range.PositionStart - Offset : 0;
            string nodeText       = Node.TextContent;
            int    nodeTextLength = Node.TextContent.Length;

            if (nodeText.Trim().Length == 0)
            {
                return;
            }

            _styleattribute = GetParentElement(Node).GetAttribute(HTMLConstants.Style);
            if (RangeOutreachesTexNodeLength(range, nodeTextLength))
            {
                ApplyElementStyle(cmd, this.Node, attributeValue, document);
                return;
            }

            if (ContainsZeroCharachtersOrOnlyWhiteSpaces(posStartWord, posEndWord, nodeText))
            {
                return;
            }

            var poslength = posEndWord - posStartWord;

            if (poslength == 0)
            {
                poslength = 1;
            }

            string extractedTextRange = nodeText.Substring(posStartWord, poslength);

            //rangeLength == 0 -> no selection only cursor. Then apply style to the word under the cursor
            if (rangeLength == 0)
            {
                var startIndex = Math.Max(nodeText.LastIndexOf(" ", posStartWord), nodeText.LastIndexOf((char)160, posStartWord));
                startIndex = startIndex == -1 ? 0 : startIndex;

                var endIndex = Math.Max(nodeText.IndexOf(" ", posStartWord), nodeText.IndexOf((char)160, posStartWord));
                endIndex = endIndex == -1 ? nodeTextLength : endIndex;

                extractedTextRange = ExtractWord(startIndex, endIndex, nodeText);
                if (extractedTextRange == "")
                {
                    return;
                }
                posStartWord = startIndex == 0 ? startIndex : startIndex + 1;
            }
            posEndWord = extractedTextRange.Length;

            if (ExtractedRangeEqualsTextRange(extractedTextRange, nodeText) || isCmdForBlockElement)
            {
                ApplyElementStyle(cmd, Node, attributeValue, document);
                return;
            }

            if (ExtractedRangeIsAtStartOfTextRange(posStartWord))
            {
                createStyleElement();
                elem.TextContent = nodeText.Substring(0, posEndWord);

                ((ICharacterData)Node).Data = nodeText.Substring(posEndWord);
                Node.InsertBefore(new[] { elem });
                return;
            }


            if (ExtractedRangeIsAtEndOfTextRange(posStartWord, posEndWord, extractedTextRange))
            {
                createStyleElement();
                elem.TextContent = nodeText.Substring(posStartWord);
                Node.TextContent = nodeText.Substring(0, posStartWord);
                Node.InsertAfter(new[] { elem });
                return;
            }

            //range is in the middle of the textnode
            var newElementname = GetElementName(cmd);

            if (newElementname != HTMLConstants.SpanTag)
            {
                var alreadyPresent = ParentElementAlreadyExists(Node, newElementname.ToUpper());
                if (alreadyPresent != null && alreadyPresent.IndexOf(Node) != 0)
                {
                    //redefine all
                    ApplyElementStyle(cmd, Node, attributeValue, document);
                    return;
                }
            }
            createStyleElement();
            elem.TextContent = nodeText.Substring(posStartWord, posEndWord);

            Node.TextContent = nodeText.Substring(0, posStartWord);
            var restNode = Node.Clone();

            restNode.TextContent = nodeText.Substring(posStartWord + posEndWord);
            Node.ReplaceWith(new[] { Node.Clone(), elem, restNode });
        }