public void NullConditions() { MultiTextBase text = new MultiTextBase(); Assert.AreSame(string.Empty, text["foo"], "never before heard of alternative should give back an empty string"); Assert.AreSame(string.Empty, text["foo"], "second time"); Assert.AreSame(string.Empty, text.GetBestAlternative("fox")); text.SetAlternative("zox", ""); Assert.AreSame(string.Empty, text["zox"]); text.SetAlternative("zox", null); Assert.AreSame(string.Empty, text["zox"], "should still be empty string after setting to null"); text.SetAlternative("zox", "something"); text.SetAlternative("zox", null); Assert.AreSame(string.Empty, text["zox"], "should still be empty string after setting something and then back to null"); }
private static void CopyItemToFieldsInPages(HtmlDom dom, string key, string valueAttribute = null, string[] languagePreferences = null) { if (languagePreferences == null) { languagePreferences = new[] { "*", "en" } } ; MultiTextBase source = dom.GetBookSetting(key); foreach (XmlElement target in dom.SafeSelectNodes("//*[@data-derived='" + key + "']")) { //just put value into the text of the element if (string.IsNullOrEmpty(valueAttribute)) { //clear out what's there now target.RemoveAttribute("lang"); target.InnerText = ""; var form = source.GetBestAlternative(languagePreferences); if (form != null && !string.IsNullOrWhiteSpace(form.Form)) { // HtmlDom.GetBookSetting(key) returns the result of XmlNode.InnerXml which will be Html encoded (& < etc). // HtmlDom.SetElementFromUserStringPreservingLineBreaks() calls XmlNode.InnerText, which Html encodes if necessary. // So we need to decode here to prevent double encoding. See http://issues.bloomlibrary.org/youtrack/issue/BL-4585. // Note that HtmlDom.SetElementFromUserStringPreservingLineBreaks() handles embedded <br/> elements, but makes no // effort to handle p or div elements. var decoded = System.Web.HttpUtility.HtmlDecode(form.Form); HtmlDom.SetElementFromUserStringPreservingLineBreaks(target, decoded); target.SetAttribute("lang", form.WritingSystemId); //this allows us to set the font to suit the language } } else //Put the value into an attribute. The license image goes through this path. { target.SetAttribute(valueAttribute, source.GetBestAlternativeString(languagePreferences)); if (source.Empty) { //if the license image is empty, make sure we don't have some alternative text //about the image being missing or slow to load target.SetAttribute("alt", ""); //over in javascript land, @alt will get set appropriately when the image url is not empty. } } } }
private static void CopyItemToFieldsInPages(HtmlDom dom, string key, string valueAttribute = null, string[] languagePreferences = null) { if (languagePreferences == null) { languagePreferences = new[] { "*", "en" } } ; MultiTextBase source = dom.GetBookSetting(key); var target = dom.SelectSingleNode("//*[@data-derived='" + key + "']"); if (target == null) { return; } //just put value into the text of the element if (string.IsNullOrEmpty(valueAttribute)) { //clear out what's there now target.RemoveAttribute("lang"); target.InnerText = ""; var form = source.GetBestAlternative(languagePreferences); if (form != null && !string.IsNullOrWhiteSpace(form.Form)) { HtmlDom.SetElementFromUserStringPreservingLineBreaks(target, form.Form); target.SetAttribute("lang", form.WritingSystemId); //this allows us to set the font to suit the language } } else //Put the value into an attribute. The license image goes through this path. { target.SetAttribute(valueAttribute, source.GetBestAlternativeString(languagePreferences)); if (source.Empty) { //if the license image is empty, make sure we don't have some alternative text //about the image being missing or slow to load target.SetAttribute("alt", ""); //over in javascript land, @alt will get set appropriately when the image url is not empty. } } }
public void UsesNextAlternativeWhenMissing() { MultiTextBase multiTextBase = new MultiTextBase(); multiTextBase["wsWithNullElement"] = null; multiTextBase["wsWithEmptyElement"] = ""; multiTextBase["wsWithContent"] = "hello"; Assert.AreEqual(String.Empty, multiTextBase.GetExactAlternative("missingWs")); Assert.AreEqual(String.Empty, multiTextBase.GetExactAlternative("wsWithEmptyElement")); Assert.AreEqual("hello", multiTextBase.GetBestAlternative("missingWs")); Assert.AreEqual("hello", multiTextBase.GetBestAlternative("wsWithEmptyElement")); Assert.AreEqual("hello*", multiTextBase.GetBestAlternative("wsWithEmptyElement", "*")); Assert.AreEqual("hello", multiTextBase.GetBestAlternative("wsWithNullElement")); Assert.AreEqual("hello*", multiTextBase.GetBestAlternative("wsWithNullElement", "*")); Assert.AreEqual("hello", multiTextBase.GetExactAlternative("wsWithContent")); Assert.AreEqual("hello", multiTextBase.GetBestAlternative("wsWithContent")); Assert.AreEqual("hello", multiTextBase.GetBestAlternative("wsWithContent", "*")); }