Exemplo n.º 1
0
        /// <summary>
        /// Removes given node from <see cref="Content"/>.
        /// </summary>
        /// <param name="node">Node to remove.</param>
        /// <returns>Current container.</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public Fb2Container RemoveContent(Fb2Node node)
        {
            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            if (!IsEmpty && content.Contains(node))
            {
                content.Remove(node);
                node.Parent = null;
            }

            return(this);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Adds given node to <see cref="Content"/>.
        /// </summary>
        /// <param name="node">Child node to add to Content.</param>
        /// <returns>Current container.</returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InvalidNodeException"></exception>
        /// <exception cref="UnexpectedNodeException"></exception>
        public Fb2Container AddContent(Fb2Node node)
        {
            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            if (!Fb2NodeFactory.IsKnownNode(node))
            {
                throw new InvalidNodeException(node);
            }

            var nodeName   = node.Name;
            var isTextNode = node is TextItem;

            if (isTextNode && !CanContainText)
            {
                throw new UnexpectedNodeException(Name, nodeName);
            }

            if (!AllowedElements.Contains(nodeName) && !isTextNode)
            {
                throw new UnexpectedNodeException(Name, nodeName);
            }

            if (isTextNode)
            {
                return(TryMergeTextContent((node as TextItem) !.Content));
            }

            node.Parent = this;
            if (node.NodeMetadata == null && NodeMetadata?.DefaultNamespace != null) // copy parent default namespace to prevent serialization issues
            {
                node.NodeMetadata = new Fb2NodeMetadata(NodeMetadata?.DefaultNamespace);
            }

            content.Add(node);
            return(this);
        }
Exemplo n.º 3
0
 private XNode ToXmlInternal(Fb2Node element) =>
 element is TextItem textItem ? (XNode) new XText(textItem.Content) : element.ToXml();