//     Transforms text for the target device.  The default transformation is the identity transformation,
        //     which does not change the text.
        internal void RenderTransformedText(WmlTextWriter writer, string text)
        {
            bool leadingSpace  = false;
            bool setPendingP   = false;
            bool trailingSpace = false;

            // p's replaced by brs as in MMIT V1 for valid containment.
            text = LiteralControlAdapterUtility.PreprocessLiteralText(text);
            bool isEmpty = (text != null && text.Length == 0);

            if (isEmpty)
            {
                return;
            }

            if (writer.TopOfForm)
            {
                while (Regex.IsMatch(text, "^(?'space'\\s*)(?:<p|</p)\\s*>"))
                {
                    text = Regex.Replace(text, "^(?'space'\\s*)(?:<p|</p)\\s*>", "${space}", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
                }
            }

            if (setPendingP = Regex.IsMatch(text, "</p\\s*>\\s*$", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant))
            {
                text = Regex.Replace(text, "</p\\s*>(?'space'\\s*)$", "${space}", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
            }

            text = Regex.Replace(text, "<br\\s*/?>", "<br/>", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
            text = Regex.Replace(text, "</p\\s*>(?'space'\\s*)<p\\s*>", "<br/>${space}", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
            text = Regex.Replace(text, "(?:<p|</p)\\s*>", "<br/>", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);

            if (trailingSpace = Regex.IsMatch(text, "\\s+$"))
            {
                text = Regex.Replace(text, "\\s+$", String.Empty);
            }
            if (leadingSpace = Regex.IsMatch(text, "^\\s+"))
            {
                text = Regex.Replace(text, "^\\s+", String.Empty);
            }

            text = text.Replace("$", "$$");

            // Render text.
            if (text.Trim().Length > 0)
            {
                if (leadingSpace)
                {
                    writer.WriteLine();
                }
                Style emptyStyle = new Style();
                writer.BeginRender();          // write pending tags.
                writer.EnterStyle(emptyStyle); // VSWhidbey 114083
                writer.Write(text);
                writer.ExitStyle(emptyStyle);
                writer.EndRender();
                if (trailingSpace)
                {
                    writer.WriteLine();
                }
            }
            // Whidbey 19653 transform space as newline.  If we are at the top of the form (before the leading p),
            // don't need literal text -it won't be rendered. Similarly, if we are setting a pending p, no need to writeline.
            else if (!setPendingP && !writer.TopOfForm)
            {
                Debug.Assert(!isEmpty, "Empty text.  Should have returned before this point.");
                writer.WriteLine();
            }

            if (setPendingP)
            {
                writer.SetPendingP();
            }
        }
        //     Transforms text for the target device.  The default transformation is the identity transformation,
        //     which does not change the text.
        internal void RenderTransformedText(WmlTextWriter writer, string text) {
            bool leadingSpace = false;
            bool setPendingP = false;
            bool trailingSpace = false;

            // p's replaced by brs as in MMIT V1 for valid containment.
            text = LiteralControlAdapterUtility.PreprocessLiteralText(text);
            bool isEmpty = (text != null && text.Length == 0);
            if (isEmpty) {
                return;
            }

            if (writer.TopOfForm) {
                while (Regex.IsMatch(text, "^(?'space'\\s*)(?:<p|</p)\\s*>")) {
                    text = Regex.Replace(text, "^(?'space'\\s*)(?:<p|</p)\\s*>", "${space}", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
                }
            }

            if (setPendingP = Regex.IsMatch(text, "</p\\s*>\\s*$", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)) {
                text = Regex.Replace(text, "</p\\s*>(?'space'\\s*)$", "${space}", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
            }

            text = Regex.Replace(text, "<br\\s*/?>", "<br/>", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
            text = Regex.Replace(text, "</p\\s*>(?'space'\\s*)<p\\s*>", "<br/>${space}", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
            text = Regex.Replace(text, "(?:<p|</p)\\s*>", "<br/>", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);

            if (trailingSpace = Regex.IsMatch(text, "\\s+$")) {
                text = Regex.Replace(text, "\\s+$", String.Empty);
            }
            if (leadingSpace = Regex.IsMatch(text, "^\\s+")) {
                text = Regex.Replace(text, "^\\s+", String.Empty);
            }

            text = text.Replace("$", "$$");

            // Render text.
            if (text.Trim().Length > 0) {
                if (leadingSpace) {
                    writer.WriteLine();
                }
                Style emptyStyle = new Style();
                writer.BeginRender(); // write pending tags.
                writer.EnterStyle(emptyStyle); // VSWhidbey 114083
                writer.Write(text);
                writer.ExitStyle(emptyStyle);
                writer.EndRender();
                if (trailingSpace) {
                    writer.WriteLine();
                }
            }
            // Whidbey 19653 transform space as newline.  If we are at the top of the form (before the leading p),
            // don't need literal text -it won't be rendered. Similarly, if we are setting a pending p, no need to writeline.
            else if (!setPendingP && !writer.TopOfForm) {
                Debug.Assert(!isEmpty, "Empty text.  Should have returned before this point.");
                writer.WriteLine();
            }

            if (setPendingP) {
                writer.SetPendingP();
            }
        }