internal Document(GumboDocumentNode node, GumboFactory factory) : base(node, null) { _children = factory.CreateLazy(() => ImmutableArray.CreateRange(node.GetChildren().OrderBy(x => x.index_within_parent).Select(x => factory.CreateNode(x, this)))); HasDocType = node.document.has_doctype; Name = NativeUtf8.StringFromNativeUtf8(node.document.name); PublicIdentifier = NativeUtf8.StringFromNativeUtf8(node.document.public_identifier); SystemIdentifier = NativeUtf8.StringFromNativeUtf8(node.document.system_identifier); DocTypeQuirksMode = node.document.doc_type_quirks_mode; }
internal Attribute(GumboAttribute attribute, Element parent) { Parent = parent ?? throw new ArgumentNullException(nameof(parent)); Name = NativeUtf8.StringFromNativeUtf8(attribute.name); Value = NativeUtf8.StringFromNativeUtf8(attribute.value); OriginalName = NativeUtf8.StringFromNativeUtf8(attribute.original_name.data, (int)attribute.original_name.length); OriginalValue = NativeUtf8.StringFromNativeUtf8(attribute.original_value.data, (int)attribute.original_value.length); NameStart = attribute.name_start; NameEnd = attribute.name_end; ValueStart = attribute.value_start; ValueEnd = attribute.value_end; Namespace = attribute.attr_namespace; }
public Gumbo(string html, GumboLibraryOptions?options = null) { _options = CreateOptions(options); _html = NativeUtf8.NativeUtf8FromString(html); _outputPtr = NativeMethods.gumbo_parse(_html); var output = Marshal.PtrToStructure <GumboOutput>(_outputPtr); _gumboDocumentNode = output.GetDocument(); Errors = output.GetErrors(); var lazyFactory = new LazyFactory(() => _disposed, typeof(Gumbo).Name); _gumboFactory = new GumboFactory(lazyFactory); Document = (Document)_gumboFactory.CreateNode(_gumboDocumentNode); }
internal Element(GumboElementNode node, Node parent, GumboFactory factory) : base(node, parent) { _children = factory.CreateLazy(() => ImmutableArray.CreateRange(node.GetChildren().OrderBy(x => x.index_within_parent).Select(x => factory.CreateNode(x, this)))); _attributes = factory.CreateLazy(() => ImmutableArray.CreateRange(node.GetAttributes().Select(x => factory.CreateAttribute(x, this)))); _value = factory.CreateLazy(() => string.Concat(Children.Select(x => x is Element ? ((Element)x).Value : ((Text)x).Value))); StartPosition = node.element.start_pos; EndPosition = node.element.end_pos; Tag = node.element.tag; TagNamespace = node.element.tag_namespace; OriginalTag = NativeUtf8.StringFromNativeUtf8(node.element.original_tag.data, (int)node.element.original_tag.length); OriginalTagName = GetTagNameFromOriginalTag(node.element); OriginalEndTag = NativeUtf8.StringFromNativeUtf8(node.element.original_end_tag.data, (int)node.element.original_end_tag.length); NormalizedTagName = NativeUtf8.StringFromNativeUtf8(NativeMethods.gumbo_normalized_tagname(node.element.tag)); }
static XNode CreateXNode(GumboNode node) { switch (node.type) { case GumboNodeType.GUMBO_NODE_DOCUMENT: return(new XDocument(((GumboDocumentNode)node).GetChildren().Select(CreateXNode))); case GumboNodeType.GUMBO_NODE_ELEMENT: case GumboNodeType.GUMBO_NODE_TEMPLATE: var elementNode = (GumboElementNode)node; var elementName = GetName(elementNode.element.tag); var attributes = elementNode.GetAttributes().Select(x => new XAttribute(NativeUtf8.StringFromNativeUtf8(x.name), NativeUtf8.StringFromNativeUtf8(x.value))); return(new XElement(elementName, attributes, elementNode.GetChildren().Select(CreateXNode))); case GumboNodeType.GUMBO_NODE_TEXT: return(new XText(NativeUtf8.StringFromNativeUtf8(((GumboTextNode)node).text.text))); case GumboNodeType.GUMBO_NODE_CDATA: return(new XCData(NativeUtf8.StringFromNativeUtf8(((GumboTextNode)node).text.text))); case GumboNodeType.GUMBO_NODE_COMMENT: return(new XComment(NativeUtf8.StringFromNativeUtf8(((GumboTextNode)node).text.text))); case GumboNodeType.GUMBO_NODE_WHITESPACE: return(new XText(NativeUtf8.StringFromNativeUtf8(((GumboTextNode)node).text.text))); default: throw new NotImplementedException($"Node type '{node.type}' is not implemented"); } }
internal Text(GumboTextNode node, Node parent) : base(node, parent) { Value = NativeUtf8.StringFromNativeUtf8(node.text.text); StartPosition = node.text.start_pos; }
public static string MarshalToString(this GumboStringPiece stringPiece) => NativeUtf8.StringFromNativeUtf8(stringPiece.data, (int)stringPiece.length);