private static void WriteScriptWithTags(HtmlTextWriter writer,
            string token,
            RegisteredScript activeRegistration) {

            // If the content already has script tags, we need to parse out the contents
            // so that the client doesn't have to. The contents may include more than one
            // script tag, but no other content (such as arbitrary HTML).
            string scriptContent = activeRegistration.Script;

            int lastIndex = 0;
            for (Match match = ScriptTagRegex.Match(scriptContent, lastIndex); match.Success; match = ScriptTagRegex.Match(scriptContent, lastIndex)) {
                CheckScriptTagTweenSpace(activeRegistration, scriptContent, lastIndex, match.Index - lastIndex);

                OrderedDictionary attrs = new OrderedDictionary();

                if (match.Groups["empty"].Captures.Count > 0) {
                    // Self-closing tag

                    // No need to do anything since attributes are processed later
                    lastIndex = match.Index + match.Length;
                }
                else {
                    // Open tag with explicit close tag

                    // Need to find close tag so that we can locate the inner contents
                    int indexOfEndOfScriptBeginTag = match.Index + match.Length;
                    int indexOfScriptEndTag = scriptContent.IndexOf("</script>", indexOfEndOfScriptBeginTag, StringComparison.OrdinalIgnoreCase);
                    if (indexOfScriptEndTag == -1) {
                        throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, AtlasWeb.ScriptRegistrationManager_NoCloseTag, activeRegistration.Type.FullName, activeRegistration.Key));
                    }
                    string scriptBlockContents = scriptContent.Substring(indexOfEndOfScriptBeginTag, (indexOfScriptEndTag - indexOfEndOfScriptBeginTag));

                    // Turn the text content into a text attribute
                    attrs.Add("text", scriptBlockContents);

                    lastIndex = indexOfScriptEndTag + 9;
                }

                // Process all the explicit attributes on the script tag
                CaptureCollection attrnames = match.Groups["attrname"].Captures;
                CaptureCollection attrvalues = match.Groups["attrval"].Captures;
                for (int i = 0; i < attrnames.Count; i++) {
                    string attribName = attrnames[i].ToString();
                    string attribValue = attrvalues[i].ToString();

                    // DevDev Bugs 123213: script elements registered with RegisterStartupScript are normally rendered
                    // into the html of the page. Any html encoded values in the attributes are interpreted by the
                    // browser, so the actual data is not html encoded. We must HtmlDecode any attribute values we find
                    // here to remain consistent during async posts, since the data will be dynamically injected into
                    // the dom, bypassing the browser's natural html decoding.
                    attribValue = HttpUtility.HtmlDecode(attribValue);
                    attrs.Add(attribName, attribValue);
                }

                // Serialize the attributes to JSON and write them out
                JavaScriptSerializer serializer = new JavaScriptSerializer();

                // Dev10# 877767 - Allow configurable UpdatePanel script block length
                // The default is JavaScriptSerializer.DefaultMaxJsonLength
                if (AppSettings.UpdatePanelMaxScriptLength > 0) {
                    serializer.MaxJsonLength = AppSettings.UpdatePanelMaxScriptLength;
                }  

                string attrText = serializer.Serialize(attrs);
                PageRequestManager.EncodeString(writer, token, "ScriptContentWithTags", attrText);
            }

            CheckScriptTagTweenSpace(activeRegistration, scriptContent, lastIndex, scriptContent.Length - lastIndex);

            if (lastIndex == 0) {
                throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, AtlasWeb.ScriptRegistrationManager_NoTags, activeRegistration.Type.FullName, activeRegistration.Key));
            }
        }
        public static void RegisterStartupScript(Control control, Type type, string key, string script, bool addScriptTags) {
            if (control == null) {
                throw new ArgumentNullException("control");
            }
            if (control.Page == null) {
                throw new ArgumentException(AtlasWeb.ScriptRegistrationManager_ControlNotOnPage, "control");
            }

            control.Page.ClientScript.RegisterStartupScript(type, key, script, addScriptTags);

            ScriptManager sm = ScriptManager.GetCurrent(control.Page);
            if (sm != null) {
                RegisteredScript entry =
                    new RegisteredScript(RegisteredScriptType.ClientStartupScript,
                        control,
                        type,
                        key,
                        script,
                        addScriptTags);
                sm.ScriptRegistration.ScriptStartupBlocks.Add(entry);
            }
        }
        public static void RegisterClientScriptInclude(Control control, Type type, string key, string url) {
            if (control == null) {
                throw new ArgumentNullException("control");
            }
            if (control.Page == null) {
                throw new ArgumentException(AtlasWeb.ScriptRegistrationManager_ControlNotOnPage, "control");
            }

            control.Page.ClientScript.RegisterClientScriptInclude(type, key, url);

            ScriptManager sm = ScriptManager.GetCurrent(control.Page);
            if (sm != null) {
                RegisteredScript entry = new RegisteredScript(control, type, key, url);
                sm.ScriptRegistration.ScriptBlocks.Add(entry);
            }
        }
        public static void RegisterClientScriptResource(Control control, Type type, string resourceName) {
            if (control == null) {
                throw new ArgumentNullException("control");
            }
            if (control.Page == null) {
                throw new ArgumentException(AtlasWeb.ScriptRegistrationManager_ControlNotOnPage, "control");
            }
            if (type == null) {
                throw new ArgumentNullException("type");
            }
            if (String.IsNullOrEmpty(resourceName)) {
                throw new ArgumentNullException("resourceName");
            }

            ScriptManager sm = ScriptManager.GetCurrent(control.Page);
            if (sm == null) {
                control.Page.ClientScript.RegisterClientScriptResource(type, resourceName);
            }
            else {
                Assembly assembly = AssemblyResourceLoader.GetAssemblyFromType(type);
                ScriptReference script = new ScriptReference {
                    Name = resourceName,
                    Assembly = assembly.FullName,
                    IsDirectRegistration = true,
                    ClientUrlResolver = sm
                };
                string resourceUrl = script.GetUrlInternal(sm, sm.Zip);
                control.Page.ClientScript.RegisterClientScriptInclude(type, resourceName, resourceUrl, true);
                RegisteredScript entry = new RegisteredScript(control, type, resourceName, resourceUrl);
                sm.ScriptRegistration.ScriptBlocks.Add(entry);
            }
        }
 private static void CheckScriptTagTweenSpace(RegisteredScript entry, string text, int start, int length) {
     // Check the range between the matches to make sure there is no extraneous content
     string tweenSpace = text.Substring(start, length);
     if (tweenSpace.Trim().Length != 0) {
         throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, AtlasWeb.ScriptRegistrationManager_InvalidChars, entry.Type.FullName, entry.Key, tweenSpace));
     }
 }