示例#1
0
        /// <summary>
        /// Adds the attribute with the adjusted prefix, namespace and name.
        /// </summary>
        /// <param name="element"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public static void SetAdjustedAttribute(Element element, String name, String value)
        {
            switch (name)
            {
                case "xlink:actuate":
                case "xlink:arcrole":
                case "xlink:href":
                case "xlink:role":
                case "xlink:show":
                case "xlink:title":
                case "xlink:type":
                    element.SetAttributeNode(new Attr(GetName(name), value) { Prefix = "xlink", NamespaceURI = Namespaces.XLink });
                    break;

                case "xml:base":
                case "xml:lang":
                case "xml:space":
                    element.SetAttributeNode(new Attr(GetName(name), value) { Prefix = "xml", NamespaceURI = Namespaces.Xml });
                    break;

                case "xmlns":
                    element.SetAttributeNS(Namespaces.XmlNS, name, value);
                    break;

                case "xmlns:xlink":
                    element.SetAttributeNode(new Attr(GetName(name), value) { Prefix = "xmlns", NamespaceURI = Namespaces.XmlNS });
                    break;

                default:
                    element.SetAttribute(name, value);
                    break;
            }
        }
        //
        // A good overview over the various classes for the
        // WPF flowdocument can be found here:
        //
        // http://msdn.microsoft.com/en-us/library/aa970909.aspx
        //

        Section RenderBox(Element element)
        {
            var box = new Section();
            Render(element, box);
            FlushBuffer(box.Blocks);
            return box;
        }
        public Boolean Check(Element element)
        {
            foreach (var attr in _attrs)
                if (!attr.Check(element))
                    return false;

            return true;
        }
        public override Boolean Apply(Element element)
        {
            if (!element.HasAttribute(Parent.Name))
                element.SetAttribute(Parent.Name, Value);
            else if (IsFixed)
                return element.GetAttribute(Parent.Name) == Value;

            return true;
        }
示例#5
0
        public NodeInspector(Element element)
        {
            nodes = new List<Node>();

            foreach (var child in element.ChildNodes)
            {
                if ((child is TextNode && !((TextNode)child).IsEmpty) || child is Element)
                    nodes.Add(child);
            }
        }
        public override Boolean Check(Element element)
        {
            if (element.HasAttribute(Parent.Name))
            {
                var attr = element.Attributes[Parent.Name];

                if (!Names.Contains(attr.Value))
                    return false;
            }

            return true;
        }
示例#7
0
        Boolean Inspect(Element element)
        {
            if (Validate(element))
            {
                foreach (var child in element.Children)
                {
                    if (!Inspect(child))
                        return false;
                }

                return true;
            }

            return false;
        }
        /// <summary>
        /// Creates a new document fragment with the given nodelist as
        /// children.
        /// </summary>
        /// <param name="html">The HTML source to use.</param>
        /// <param name="context">The context for the fragment mode.</param>
        internal DocumentFragment(String html, Element context)
            : this()
        {
            var owner = context.Owner;
            var configuration = Configuration.Clone(owner != null ? owner.Options : Configuration.Default);
            configuration.IsScripting = false;
            configuration.UseQuirksMode = context.Owner != null && context.Owner.QuirksMode != QuirksMode.Off;
            var parser = new HtmlParser(html, configuration);
            parser.SwitchToFragment(context);
            parser.Parse();
            var root = parser.Result.DocumentElement;

            while (root.HasChilds)
            {
                var child = root.FirstChild;
                root.RemoveChild(child);
                DefaultAppendChild(child);
            }
        }
示例#9
0
 /// <summary>
 /// Checks for each attribute on the token if the attribute is already present on the node.
 /// If it is not, the attribute and its corresponding value is added to the node.
 /// </summary>
 /// <param name="elementToken">The token with the source attributes.</param>
 /// <param name="element">The node with the target attributes.</param>
 void AppendAttributes(HtmlTagToken elementToken, Element element)
 {
     foreach (var attr in elementToken.Attributes)
     {
         if (!element.HasAttribute(attr.Key))
             element.SetAttribute(attr.Key, attr.Value);
     }
 }
 public abstract Boolean Apply(Element element);
 public abstract Boolean Check(Element element);
示例#12
0
文件: Node.cs 项目: rrsc/AngleSharp
        protected String LookupNamespacePrefix(String namespaceURI, Element originalElement)
        {
            if (!String.IsNullOrEmpty(_ns) && !String.IsNullOrEmpty(Prefix) && _ns == namespaceURI && originalElement.LookupNamespaceURI(Prefix) == namespaceURI)
                return Prefix;

            if(_attributes.Length > 0)
            {
                for (int i = 0; i < _attributes.Length; i++)
                {
                    if (_attributes[i].Prefix == "xmlns" && _attributes[i].NodeValue == namespaceURI && originalElement.LookupNamespaceURI(_attributes[i].LocalName) == namespaceURI)
                        return _attributes[i].LocalName;
                }
            }

            if (_parent != null)
                return _parent.LookupNamespacePrefix(namespaceURI, originalElement);

            return null;
        }
 public Boolean Check(Element element)
 {
     return _type.Check(element) && _value.Apply(element);
 }
示例#14
0
        Boolean ValidateAttribute(Element element)
        {
            var declared = new List<String>();

            foreach (var def in _dtd.Attributes)
            {
                if (def.Name == element.NodeName)
                {
                    if (!def.Check(element))
                        return false;

                    foreach (var attr in def.Declarations)
                        declared.Add(attr.Name);
                }
            }

            foreach (var attr in element.Attributes)
                if (!declared.Contains(attr.Name))
                    return false;

            return true;
        }
 public override Boolean Check(Element element)
 {
     return true;
 }
示例#16
0
 /// <summary>
 /// Creates a new style sheet included in another stylesheet.
 /// </summary>
 /// <param name="parent">The parent of the current stylesheet.</param>
 internal StyleSheet(StyleSheet parent)
     : this()
 {
     _owner = parent._owner;
     _parent = parent;
 }
示例#17
0
 static String OpenTag(Element element)
 {
     StartString('<').Append(element.TagName).Append(element.Attributes.ToHtml()).Append('>');
     return ReleaseString();
 }
示例#18
0
        static TreeNodeViewModel Create(Element node)
        {
            var vm = new TreeNodeViewModel { Value = OpenTag(node) };

            foreach (var element in SelectFrom(node.ChildNodes))
            {
                element.parent = vm.children;
                vm.children.Add(element);
            }

            if (vm.children.Count != 0)
                vm.expansionElement = new TreeNodeViewModel { Value = CloseTag(node) };

            return vm;
        }
示例#19
0
 static String CloseTag(Element element)
 {
     StartString("</").Append(element.TagName).Append('>');
     return ReleaseString();
 }
 /// <summary>
 /// Checks the element.
 /// </summary>
 /// <param name="element">The element to check.</param>
 /// <returns>True if everything is according to the definition, otherwise false.</returns>
 public Boolean Check(Element element)
 {
     var inspector = new NodeInspector(element);
     return Entry.Check(inspector) && inspector.IsCompleted;
 }
 public override Boolean Apply(Element element)
 {
     return true;
 }
        void Render(Element element, Section box)
        {
            foreach (var child in element.ChildNodes)
            {
                switch (child.NodeType)
                {
                    case NodeType.Text:
                        FillBuffer((TextNode)child);
                        break;

                    case NodeType.Element:
                        var node = (Element)child;

                        switch (node.TagName)
                        {
                            case "script":
                            case "style":
                                break;

                            case "img":
                                FillBuffer(Render((HTMLImageElement)node));
                                break;

                            case "b":
                            case "strong":
                                {
                                    var previous = currentFontWeight;
                                    currentFontWeight = FontWeights.Bold;
                                    Render(node, box);
                                    currentFontWeight = previous;
                                }
                                break;

                            case "i":
                                {
                                    var previous = currentFontStyle;
                                    currentFontStyle = FontStyles.Italic;
                                    Render(node, box);
                                    currentFontStyle = previous;
                                }
                                break;

                            case "u":
                                {
                                    var previous = currentUnderline;
                                    currentUnderline = true;
                                    Render(node, box);
                                    currentUnderline = previous;
                                }
                                break;

                            case "strike":
                                {
                                    var previous = currentStrike;
                                    currentStrike = true;
                                    Render(node, box);
                                    currentStrike = previous;
                                }
                                break;

                            case "br":
                                FillBuffer(new LineBreak());
                                break;

                            case "a":
                                {
                                    var previous = isHyperlink;
                                    isHyperlink = true;
                                    Render(node, box);
                                    isHyperlink = previous;
                                }
                                break;

                            case "ul":
                                FlushBuffer(box.Blocks);
                                box.Blocks.Add(Render((HTMLUListElement)node));
                                break;

                            case "ol":
                                FlushBuffer(box.Blocks);
                                box.Blocks.Add(Render((HTMLOListElement)node));
                                break;

                            case "dl":
                                FlushBuffer(box.Blocks);
                                box.Blocks.Add(Render((HTMLDListElement)node));
                                break;

                            case "p":
                                FlushBuffer(box.Blocks);
                                Render(node, box);
                                break;

                            case "div":
                                FlushBuffer(box.Blocks);
                                box.Blocks.Add(RenderBox(node));
                                break;

                            default:
                                Render(node, box);
                                break;
                        }

                        break;
                }
            }
        }
示例#23
0
        /// <summary>
        /// Modifies the node by appending all attributes and
        /// acknowledging the self-closing flag if set.
        /// </summary>
        /// <param name="element">The node which will be added to the list.</param>
        /// <param name="elementToken">The associated tag token.</param>
        /// <param name="acknowledgeSelfClosing">Should the self-closing be acknowledged?</param>
        void SetupElement(Element element, HtmlToken elementToken, bool acknowledgeSelfClosing)
        {
            var tag = (HtmlTagToken)elementToken;
            element.NodeName = tag.Name;

            if (tag.IsSelfClosing && !acknowledgeSelfClosing)
                RaiseErrorOccurred(ErrorCode.TagCannotBeSelfClosed);

            for (var i = 0; i < tag.Attributes.Count; i++)
                element.SetAttribute(tag.Attributes[i].Key, tag.Attributes[i].Value);
        }
示例#24
0
        /// <summary>
        /// Copies the attributes from the source element to the target element.
        /// Each attribute will be recreated on the target.
        /// </summary>
        /// <param name="source">The source of the attributes.</param>
        /// <param name="target">The target where to create the attributes.</param>
        protected static void CopyAttributes(Element source, Element target)
        {
            target._namespace = source._namespace;
            target._prefix = source._prefix;

            for (int i = 0; i < source._attributes.Count; i++)
                target.SetAttribute(source._attributes[i].Name, source._attributes[i].Value);
        }
        public override Boolean Check(Element element)
        {
            var attr = element.GetAttribute(Parent.Name);

            if (attr == null)
                return true;

            switch (Value)
            {
                case TokenizedType.ENTITIES:
                {
                    //TODO
                    break;
                }
                case TokenizedType.ENTITY:
                {
                    //TODO
                    break;
                }
                case TokenizedType.ID:
                {
                    if (String.IsNullOrEmpty(attr) || !attr[0].IsXmlNameStart())
                        return false;

                    for (int i = 1; i < attr.Length; i++)
                        if (!attr[i].IsXmlName())
                            return false;

                    //TODO only one ID per element
                    return true;
                }
                case TokenizedType.IDREF:
                {
                    if (String.IsNullOrEmpty(attr) || !attr[0].IsXmlNameStart())
                        return false;

                    for (int i = 1; i < attr.Length; i++)
                        if (!attr[i].IsXmlName())
                            return false;

                    //TODO check reference
                    return true;
                }
                case TokenizedType.IDREFS:
                {
                    var start = true;

                    for (int i = 0; i < attr.Length; i++)
                    {
                        if (!attr[i].IsSpaceCharacter())
                        {
                            if (start && !attr[i].IsXmlNameStart())
                                return false;
                            else if (!start && !attr[i].IsXmlName())
                                return false;
                            else if (start)
                                start = false;
                        }
                        else
                            start = true;
                    }

                    //TODO check references
                    return true;
                }
                case TokenizedType.NMTOKEN:
                {
                    for (int i = 0; i < attr.Length; i++)
                        if (!attr[i].IsXmlName())
                            return false;

                    return true;
                }
                case TokenizedType.NMTOKENS:
                {
                    for (int i = 0; i < attr.Length; i++)
                        if (!attr[i].IsSpaceCharacter() && !attr[i].IsXmlName())
                            return false;

                    break;
                }
            }

            return true;
        }
示例#26
0
 /// <summary>
 /// Returns a duplicate of the node on which this method was called.
 /// </summary>
 /// <param name="deep">Optional value: true if the children of the node should also be cloned, or false to clone only the specified node.</param>
 /// <returns>The duplicate node.</returns>
 public override INode Clone(Boolean deep = true)
 {
     var node = new Element(NodeName, Flags);
     CopyProperties(this, node, deep);
     CopyAttributes(this, node);
     return node;
 }
示例#27
0
        Boolean ValidateElement(Element element)
        {
            foreach (var def in _dtd.Elements)
            {
                if (def.Name == element.NodeName)
                {
                    if (!def.Check(element))
                        break;

                    IncreaseCounter(def.Name);
                    return true;
                }
            }

            return false;
        }
示例#28
0
        /// <summary>
        /// Copies the element and its attributes to create a new element.
        /// </summary>
        /// <param name="element">The old element (source).</param>
        /// <returns>The new element (target).</returns>
        Element CopyElement(Element element)
        {
            var newElement = HTMLElement.Factory(element.NodeName);
            newElement.NodeName = element.NodeName;

            for (int i = 0; i < element.Attributes.Length; i++)
            {
                var attr = element.Attributes[i];
                newElement.SetAttribute(attr.NodeName, attr.NodeValue);
            }

            return newElement;
        }
示例#29
0
 /// <summary>
 /// Validates the given element and its children (if any).
 /// </summary>
 /// <param name="element">The element to validate.</param>
 /// <returns>True if the given element and its children are valid, otherwise false.</returns>
 Boolean Validate(Element element)
 {
     return ValidateElement(element) && ValidateAttribute(element);
 }
 public override Boolean Apply(Element element)
 {
     return element.HasAttribute(Parent.Name);
 }