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> /// Set one or more properties for the set of matched elements. /// </summary> /// /// <param name="name"> /// The property to set /// </param> /// <param name="value"> /// The value /// </param> /// /// <returns> /// The current CQ object /// </returns> public CQ Prop(string name, IConvertible value) { // Prop actually works on things other than boolean - e.g. SelectedIndex. For now though only use prop for booleans if (HtmlData.IsBoolean(name)) { SetProp(name, value); } else { Attr(name, value); } return(this); }
/// <summary> /// Get the value of an attribute for the first element in the set of matched elements. /// </summary> /// /// <param name="name"> /// The name of the attribute to get. /// </param> /// /// <returns> /// A string of the attribute value, or null if the attribute does not exist. /// </returns> /// /// <url> /// http://api.jquery.com/attr/#attr1 /// </url> public string Attr(string name) { if (Length > 0 && !string.IsNullOrEmpty(name)) { name = name.ToLower(); string value; var el = this[0]; switch (name) { case "class": return(el.ClassName); case "style": string st = el.Style.ToString(); return(st == "" ? null : st); default: if (el.TryGetAttribute(name, out value)) { if (HtmlData.IsBoolean(name)) { // Pre-1.6 and 1.6.1+ compatibility: always return the name of the attribute if it exists for // boolean attributes return(name); } else { return(value); } } else if (name == "value" && (el.NodeName == "INPUT" || el.NodeName == "SELECT" || el.NodeName == "OPTION")) { return(Val()); } else if (name == "value" && el.NodeName == "TEXTAREA") { return(el.InnerText); } break; } } return(null); }
/// <summary> /// Set one or more attributes for the set of matched elements. /// </summary> /// /// <exception cref="InvalidOperationException"> /// Thrown when attemting to change the type of an INPUT element that already exists on the DOM. /// </exception> /// /// <param name="name"> /// THe attribute name. /// </param> /// <param name="value"> /// The value to set. /// </param> /// /// <returns> /// The current CQ object. /// </returns> public CQ Attr(string name, IConvertible value) { // Make sure attempts to pass a JSON string end up a the right place if (Objects.IsJson(name) && value.GetType() == typeof(bool)) { return(AttrSet(name, (bool)value)); } // jQuery 1.7 compatibility bool isBoolean = HtmlData.IsBoolean(name); if (isBoolean) { // Using attr with empty string should set a property to "true. But prop() itself requires a truthy value. Check for this specifically. if (value is string && (string)value == String.Empty) { value = true; } SetProp(name, value); return(this); } string val; if (value is bool) { val = value.ToString().ToLower(); } else { val = GetValueString(value); } foreach (IDomElement e in Elements) { if ((e.NodeNameID == HtmlData.tagINPUT || e.NodeNameID == HtmlData.tagBUTTON) && name == "type" && !e.IsFragment) { throw new InvalidOperationException("Can't change type of \"input\" elements that have already been added to a DOM"); } e.SetAttribute(name, val); } return(this); }