Example #1
0
        private string To_XML_Safe_Name(string Name)
        {
            int idx = Name.IndexOf('-');

            if (idx >= 0 && Name.Length < 2)
            {
                throw new DomSyntaxError();
            }
            if (idx >= 0 && (Name[idx + 1] >= 'a' || Name[idx + 1] <= 'z'))
            {
                throw new DomSyntaxError();
            }

            bool hasAlpha = false;

            for (int i = 0; i < Name.Length; i++)
            {
                if (UnicodeCommon.Is_ASCII_Upper_Alpha(Name[i]))
                {
                    hasAlpha = true; break;
                }
            }

            /* 2) For each ASCII upper alpha in name, insert a U+002D HYPHEN-MINUS character (-) before the character and replace the character with the same character converted to ASCII lowercase. */
            StringBuilder sb = new StringBuilder();

            if (hasAlpha)
            {
                for (int i = 0; i < Name.Length; i++)
                {
                    if (UnicodeCommon.Is_ASCII_Upper_Alpha(Name[i]))
                    {
                        sb.Append('-');
                        sb.Append(UnicodeCommon.To_ASCII_Lower_Alpha(Name[i]));
                    }
                    else
                    {
                        sb.Append(Name[i]);
                    }
                }
            }
            else
            {
                sb.Append(Name);
            }
            /* 3) Insert the string data- at the front of name. */
            sb.Insert(0, "data-");

            string safeName = sb.ToString();

            /* 4) If name does not match the XML Name production, throw an "InvalidCharacterError" DOMException. */
            if (!XMLCommon.Is_Valid_Name(safeName))
            {
                throw new InvalidCharacterError($"The Name '{safeName}' is not a valid XML name production.");
            }

            return(safeName);
        }
Example #2
0
    {/* Docs: https://dom.spec.whatwg.org/#domimplementation */
        public DocumentType createDocumentType(string qualifiedName, string publicId = "", string systemId = "")
        {
            /* If qualifiedName does not match the Name production, an "InvalidCharacterError" DOMException is thrown */
            if (!XMLCommon.Is_Valid_Name(qualifiedName))
            {
                throw new InvalidCharacterError($"qualifiedName '{qualifiedName}' is not in valid XML format!");
            }

            /* ...and if it does not match the QName production, a "NamespaceError" DOMException is thrown. */
            if (!XMLCommon.Is_Valid_QName(qualifiedName))
            {
                throw new NamespaceError();
            }

            return(new DocumentType(qualifiedName, publicId, systemId));
        }