public void IsBlock() { Assert.IsTrue(HtmlData.IsBlock("div")); Assert.IsFalse(HtmlData.IsBlock("b")); Assert.IsFalse(HtmlData.IsBlock("input")); Assert.IsFalse(HtmlData.IsBlock("random")); }
private ulong[] GetKey(string what) { var token = HtmlData.Tokenize(what.Substring(1)); var key = new [] { what[0], token }; return(key); }
/// <summary> /// Sets style setting with no parsing /// </summary> /// <param name="name"></param> /// <param name="value"></param> public void SetRaw(string name, string value) { bool hadStyles = HasStyles; Styles[HtmlData.Tokenize(name)] = value; DoOnHasStylesChanged(hadStyles); }
/// <summary> /// Enumerates the attributes in this collection as a sequence of KeyValuePairs. /// </summary> /// /// <returns> /// A sequence of KeyValuePair<string,string> objects. /// </returns> protected IEnumerable <KeyValuePair <string, string> > GetAttributes() { foreach (var kvp in Attributes) { yield return(new KeyValuePair <string, string>(HtmlData.TokenName(kvp.Key).ToLower(), kvp.Value)); } }
/// <summary> /// Try to get a value for the specified attribute name. /// </summary> /// /// <param name="name"> /// The key. /// </param> /// <param name="value"> /// [out] The value. /// </param> /// /// <returns> /// true if the key was present, false if it fails. /// </returns> public bool TryGetValue(string name, out string value) { // do not use trygetvalue from dictionary. We need default handling in Get value = Get(name); return(value != null || Attributes.ContainsKey(HtmlData.Tokenize(name))); }
/// <summary> /// Convert an index key to human readable form. /// </summary> /// /// <param name="indexKeyArray"> /// Array of index keys. /// </param> /// <param name="indexSeparator"> /// The index separator. /// </param> /// /// <returns> /// The human readable key. /// </returns> public static string HumanReadableKey(object indexKeyArray, object indexSeparator) { string humanReadableKey = ""; int startIndex = 1; ushort[] indexKey = (ushort[])indexKeyArray; if (!indexKey[0].Equals(indexSeparator)) { ushort keyPart = (ushort)Convert.ChangeType(indexKey[1], typeof(ushort)); humanReadableKey = Convert.ChangeType(indexKey[0], typeof(char)) + HtmlData.TokenName(keyPart) + '/'; startIndex = 3; } for (int i = startIndex; i < indexKey.Length; i++) { ushort c = (ushort)Convert.ChangeType(indexKey[i], typeof(ushort)); humanReadableKey += ((ushort)c).ToString().PadLeft(3, '0'); if (i < indexKey.Length - 1) { humanReadableKey += '/'; } } return(humanReadableKey); }
private void ProcessOffer(HtmlData htmlData) { try { _logger.Log(LogType.Debug, $"Before getting processor"); var processor = _factory.Get(htmlData.OfferType); _logger.Log(LogType.Debug, $"After getting processor {processor.GetClassName()}"); _logger.Log(LogType.Info, $"Started to extract data from {htmlData.GetClassName()} with Id: {htmlData.Id}"); var offer = processor.Process(htmlData); _logger.Log(LogType.Info, $"Finished to extract data from {htmlData.GetClassName()} with Id: {htmlData.Id}"); _htmlDataRepository.Delete(htmlData); _logger.Log(LogType.Info, $"Removed obsolete data from {htmlData.GetClassName()} with Id: {htmlData.Id}"); using (var transaction = _offerRepository.GetTransaction()) { _offerRepository.Insert(offer, transaction); _logger.Log(LogType.Info, $"Inserted new {offer.GetClassName()} to database"); } } catch (Exception e) { using (var transaction = _htmlDataRepository.GetTransaction()) { htmlData.Status = Status.Failed; _htmlDataRepository.Update(htmlData, transaction); _logger.Log(LogType.Error, $"Failed to extract data from {htmlData.GetClassName()} with Id: {htmlData.Id}|{e.Message}"); } throw; } }
/// <summary> /// Returns a literal object for the text between HtmlStart (the last position of the end of a /// tag) and the current position. If !AllowLiterals then it's wrapped in a span. /// </summary> /// /// <param name="factory"> /// The HTML factory to operate against /// </param> /// <param name="literal"> /// [out] The literal. /// </param> /// /// <returns> /// true if it succeeds, false if it fails. /// </returns> public bool TryGetLiteral(HtmlElementFactory factory, out IDomObject literal) { if (Pos <= HtmlStart) { literal = null; return(false); } // There's plain text -return it as a literal. DomText lit; switch (InsertionMode) { case InsertionMode.Invalid: lit = new DomInvalidElement(); break; case InsertionMode.Text: InsertionMode = InsertionMode.Default; lit = new DomInnerText(); break; default: lit = new DomText(); break; } literal = lit; //if (factory.IsBound) //{ // lit.SetTextIndex(factory.Document, factory.Document.DocumentIndex.TokenizeString(HtmlStart, Pos - HtmlStart)); //} //else //{ string text = factory.Html.SubstringBetween(HtmlStart, Pos); literal.NodeValue = HtmlData.HtmlDecode(text); //} if (WrapLiterals) { DomElement wrapper = DomElement.Create("span"); wrapper.AppendChildUnsafe(literal); literal = wrapper; } if (Parent != null) { ((DomElement)Parent.Element).AppendChildUnsafe(literal); Reset(); return(false); } else { TokenizerState = TokenizerState.Finished; return(true); } }
/// <summary> /// Return the formatted string representation of this style, as HTML, or null if there is no /// style attribute. /// </summary> /// /// <returns> /// A string. /// </returns> public override string ToString() { string style = HasStyleAttribute ? "" : null; if (HasStyleAttribute) { if (QuickSetValue != null) { return(QuickSetValue); } else { bool first = true; foreach (var kvp in Styles) { if (!first) { style += " "; } else { first = false; } style += HtmlData.TokenName(kvp.Key) + ": " + kvp.Value + ";"; } } } return(style); }
/// <summary> /// Return the formatted string representation of this style, as HTML. /// </summary> /// /// <returns> /// A string. /// </returns> public override string ToString() { string style = String.Empty; if (HasStyles) { string delim = Styles.Count > 1 ? ";":""; bool first = true; foreach (var kvp in Styles) { if (!first) { style += " "; } else { first = false; } style += HtmlData.TokenName(kvp.Key) + ": " + kvp.Value + delim; } } return(style); }
public void AttributeEncode() { string onlyQuotes = "{\"someprop\": 1, \"someprop2\", \"someval\"}"; string onlyApos = "{'someprop': 1, 'someprop2', 'someval'}"; string both = "{\"someprop\": 1, \"someprop2\", \"o'brien\"}"; string neither = "plain old text"; string quoteChar; string result = HtmlData.AttributeEncode(onlyQuotes, true, out quoteChar); Assert.AreEqual(onlyQuotes, result, "With only quotes, nothing changed"); Assert.AreEqual("'", quoteChar, "Quote char was an apostrophe with only quotes"); result = HtmlData.AttributeEncode(onlyApos, true, out quoteChar); Assert.AreEqual(onlyApos, result, "With only apostrophes, nothing changed"); Assert.AreEqual("\"", quoteChar, "Quote char was a quote with only apos"); result = HtmlData.AttributeEncode(both, true, out quoteChar); string expected = "{\"someprop\": 1, \"someprop2\", \"o'brien\"}"; Assert.AreEqual(expected, result, "With both, only apostrophes changed"); Assert.AreEqual("'", quoteChar, "Quote char was an apos with both"); result = HtmlData.AttributeEncode(neither, true, out quoteChar); Assert.AreEqual(neither, result, "With neither, nothing changeed"); Assert.AreEqual("\"", quoteChar, "Quote char was a quote with both"); }
public void IsBoolean() { Assert.IsTrue(HtmlData.IsBoolean("checked")); Assert.IsFalse(HtmlData.IsBoolean("p")); Assert.IsFalse(HtmlData.IsBoolean("input")); Assert.IsFalse(HtmlData.IsBoolean("random")); }
/// <summary> /// Test whether the named property is set for the first element in the selection set. /// </summary> /// /// <remarks> /// When used to test the "selected" property of options in option groups, and none are /// explicitly marked as "selected", this will return "true" for the first option in the group, /// per browser DOM behavior. /// </remarks> /// /// <param name="name"> /// The property name. /// </param> /// /// <returns> /// true if it is set, false if not. /// </returns> public bool Prop(string name) { name = name.ToLower(); if (Length > 0 && HtmlData.IsBoolean(name)) { bool has = this[0].HasAttribute(name); // if there is nothing with the "selected" attribute, in non-multiple select lists, // the first one is selected by default by Sizzle. We will return that same information // when using prop. // TODO: this won't work for the "selected" selector. Need to move this logic into DomElement // and use selected property instead to make this work. I am not sure I agree with the jQuery // implementation anyway since querySelectorAll does NOT return this. if (name == "selected" && !has) { var owner = First().Closest("select"); string ownerSelected = owner.Val(); if (ownerSelected == String.Empty && !owner.Prop("multiple")) { return(ReferenceEquals(owner.Find("option").FirstOrDefault(), this[0])); } } return(has); } return(false); }
/// <summary> /// Sets style setting with no parsing /// </summary> /// <param name="name"></param> /// <param name="value"></param> public void SetRaw(string name, string value) { bool hadStyleAttribute = HasStyleAttribute; Styles[HtmlData.Tokenize(name)] = value; DoOnHasStyleAttributeChanged(hadStyleAttribute); }
/// <summary> /// 设置输入模式 /// </summary> public override void SetInputMode() { Tag = HtmlTag.Div; foreach (var option in Options) { var label = new HtmlElement(HtmlTag.Label); Append(label); label.AddClass("checkbox-inline"); var input = new HtmlElement(HtmlTag.Input); label.Append(input); input.Attribute(HtmlAttribute.Type, "checkbox"); input.Attribute(HtmlAttribute.Name, Name); input.Attribute(HtmlAttribute.Value, option.Value); if (Values.Contains(option.Value)) { input.Attribute(HtmlAttribute.Checked, "checked"); } if (PropertyContent.Disabled || PropertyContent.ReadOnly) { input.Attribute(HtmlAttribute.Disabled, "disabled"); } HtmlData.SetContext(input); label.Append(new HtmlRaw(option.Text.HtmlEncode())); } }
/// <summary> /// 设置输入模式 /// </summary> public override void SetInputMode() { Tag = HtmlTag.Div; foreach (var option in Source) { var label = new HtmlElement(HtmlTag.Label); Append(label); label.AddClass("radio-inline"); var input = new HtmlElement(HtmlTag.Input); label.Append(input); input.Attribute(HtmlAttribute.Type, "radio"); input.Attribute(HtmlAttribute.Name, Name); input.Attribute(HtmlAttribute.Value, option.Key.ToString().ToLower()); if (Value.HasValue && option.Key == Value.Value) { input.Attribute(HtmlAttribute.Checked, "checked"); } if (PropertyContent.Disabled || PropertyContent.ReadOnly) { input.Attribute(HtmlAttribute.Disabled, "disabled"); } HtmlData.SetContext(input); label.Append(new HtmlRaw(option.Value.HtmlEncode())); } }
public bool TryGetValue(string key, out string value) { // do not use trygetvalue from dictionary. We need default handling in Get value = Get(key); return(value != null || ContainsKey(HtmlData.Tokenize(key))); }
public void GetHtmlTest_Trap() { HtmlData target = GetTarget(null, "trap"); HtmlDocument actual; actual = target.GetHtmlDocument(31); Assert.IsTrue(actual.DocumentNode.InnerText.Contains("Falling Iron Portcullis")); }
protected IEnumerable <KeyValuePair <string, string> > stylesEnumerable() { foreach (var kvp in Styles) { yield return(new KeyValuePair <string, string>(HtmlData.TokenName(kvp.Key).ToLower(), kvp.Value)); } yield break; }
/// <summary> /// 设置输入模式 /// </summary> public override void SetInputMode() { Attribute(HtmlAttribute.Type, "hidden"); Attribute(HtmlAttribute.Name, Name); Attribute(HtmlAttribute.Value, Value); SetAttributeDisabledReadOnlyPlaceHolder(this, PropertyContent.Disabled); HtmlData.SetContext(this); }
/// <summary> /// Adding an attribute implementation /// </summary> /// <param name="name"></param> /// <param name="value"></param> private void Set(string name, string value) { if (String.IsNullOrEmpty(name)) { throw new ArgumentException("Cannot set an attribute with no name."); } name = name.CleanUp(); Set(HtmlData.Tokenize(name), value); }
private ushort[] GetKey(string what) { var token = HtmlData.Tokenize(what.Substring(1)); ushort[] key = new ushort[2] { what[0], token }; return(key); }
private string Get(string name) { name = name.CleanUp(); if (string.IsNullOrEmpty(name)) { return(null); } return(Get(HtmlData.Tokenize(name))); }
/// <summary> /// 设置输入模式 /// </summary> public override void SetInputMode() { Attribute(HtmlAttribute.Type, "number"); Attribute(HtmlAttribute.Name, Name); Attribute(HtmlAttribute.Value, Value == null ? string.Empty : Value.ToString()); SetAttributeDisabledReadOnlyPlaceHolder(this, PropertyContent.Disabled); AddClass("form-control"); HtmlData.SetContext(this); }
void ICollection <KeyValuePair <string, string> > .CopyTo(KeyValuePair <string, string>[] array, int arrayIndex) { array = new KeyValuePair <string, string> [Attributes.Count]; int index = 0; foreach (var kvp in Attributes) { array[index++] = new KeyValuePair <string, string>(HtmlData.TokenName(kvp.Key), kvp.Value); } }
/// <summary> /// 设置输入模式 /// </summary> public override void SetInputMode() { Tag = HtmlTag.Textarea; Attribute(HtmlAttribute.Name, Name); Attribute(HtmlAttribute.Rows, "5"); SetAttributeDisabledReadOnlyPlaceHolder(this, PropertyContent.Disabled); Text(Value); AddClass("form-control"); HtmlData.SetContext(this); }
/// <summary> /// Gets a style by name /// </summary> /// /// <param name="name"> /// The style name /// </param> /// /// <returns> /// The style, or null if it is not defined. /// </returns> public string GetStyle(string name) { string value = null; if (HasStyleAttribute) { Styles.TryGetValue(HtmlData.Tokenize(name), out value); } return(value); }
private IEnumerable <KeyValuePair <string, string> > stylesEnumerable() { if (HasStyleAttribute) { foreach (var kvp in Styles) { yield return(new KeyValuePair <string, string>(HtmlData.TokenName(kvp.Key).ToLower(), kvp.Value)); } } }
/// <summary> /// 设置输入模式 /// </summary> public override void SetInputMode() { Tag = HtmlTag.Div; AddClass("mulit-file-group"); if (!PropertyContent.Disabled && !PropertyContent.ReadOnly) { SetFileInput(); } foreach (var item in Value) { var group = new HtmlElement(HtmlTag.Div); group.AppendTo(this); group.AddClass("control-line"); var control = new HtmlElement(HtmlTag.Div); control.AppendTo(group); control.AddClass("form-control"); if (PropertyContent.ReadOnly) { control.Attribute(HtmlAttribute.ReadOnly, "readonly"); } if (PropertyContent.Disabled) { control.Attribute(HtmlAttribute.ReadOnly, "disabled"); } var icon = Util.ContentTypeMapping.Instance.ToIcon(item.ContentType, item.FileName); icon.CreateElement().AppendTo(control); var a = new HtmlElement(HtmlTag.A); a.Text(item.FileName); a.AddClass("icon-fa-text"); a.Attribute(HtmlAttribute.Href, item.Location); a.Attribute(HtmlAttribute.Target, "_none"); HtmlData.SetContext(a); a.AppendTo(control); var delInput = new HtmlElement(HtmlTag.Input); delInput.AppendTo(group); delInput.Attribute(HtmlAttribute.Type, "hidden"); delInput.Attribute(HtmlAttribute.Name, string.Format("{0}_DeleteMark", Name)); delInput.AddClass("del-file-input"); delInput.Attribute(HtmlAttribute.Value, "0"); if (FileOption.SupportDelete && (!PropertyContent.Disabled && !PropertyContent.ReadOnly)) { group.AddClass("input-group"); var span = new HtmlElement(HtmlTag.Span); span.AppendTo(group); span.AddClass("input-group-addon del-files"); span.Append(FontAwesome.Times.CreateElement()); } } SetFormValidator(); }
/// <summary> /// Renders this text node using document default rendering options /// </summary> /// /// <returns> /// A string of HTML-encoded text /// </returns> public override string Render() { var opts = DomRenderingOptions; return(HtmlData.HtmlEncode(NodeValue, opts.HasFlag(DomRenderingOptions.HtmlEncodingNone) ? HtmlEncodingMethod.HtmlEncodingNone : opts.HasFlag(DomRenderingOptions.HtmlEncodingMinimum) ? HtmlEncodingMethod.HtmlEncodingMinimum : HtmlEncodingMethod.HtmlEncodingFull)); }