Bloom.Book.HtmlDom.SetElementFromUserStringPreservingLineBreaks C# (CSharp) Method

SetElementFromUserStringPreservingLineBreaks() public static method

public static SetElementFromUserStringPreservingLineBreaks ( XmlElement node, string form ) : void
node System.Xml.XmlElement
form string
return void
        public static void SetElementFromUserStringPreservingLineBreaks(XmlElement node, string form)
        {
            //Note: this method is a compromise... it replaces a couple instances where we were
            //explicitly using innerText instead of innerXml, presumably on purpose. Of course that
            //makes it impossible to have any html markup. My particular need right now (BL-3832) is to
            //allow <br> to get through this filter. So that's all this does. A future alternative
            //might be to remove the filter altogether and see if there's a better way to handle
            //whatever scenarios the filtering was designed to prevent.

            // not InnerXml as it may contain things like SILA & LASI that are not valid XML
            const string kBR = "LINEBREAKHERE";
            var withBreaksHidden = form.Replace("<br />", kBR).Replace("<br/>", kBR);

            //going to innertext means we treat everything literally, for better or worse (definitely safer)
            node.InnerText = withBreaksHidden;
            // finally, unhide the breaks
            node.InnerXml = node.InnerXml.Replace(kBR, "<br/>");
        }

Usage Example

Example #1
0
        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 (&amp; &lt; 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.
                    }
                }
            }
        }
All Usage Examples Of Bloom.Book.HtmlDom::SetElementFromUserStringPreservingLineBreaks