Example #1
0
        /// <summary>
        /// Renders the style property
        /// </summary>
        /// <param name="tag"></param>
        private void WriteAttributes(HtmlTag tag)
        {
            foreach (KeyValuePair <string, object> attribute in tag.FilteredAttributes)
            {
                if (attribute.Value is HtmlTag)
                {
                    // HTML doesn't allow tags in attributes unlike code markup
                    continue;
                }
                string value = attribute.Value as string;

                this.writer.Write(' ');
                if (String.IsNullOrEmpty(value))
                {
                    HtmlDistiller.HtmlAttributeEncode(attribute.Key, this.writer);
                }
                else if (String.IsNullOrEmpty(attribute.Key))
                {
                    HtmlDistiller.HtmlAttributeEncode(value, this.writer);
                }
                else
                {
                    HtmlDistiller.HtmlAttributeEncode(attribute.Key, this.writer);
                    this.writer.Write("=\"");
                    HtmlDistiller.HtmlAttributeEncode(value, this.writer);
                    this.writer.Write("\"");
                }
            }
        }
Example #2
0
    public static List <ParseException> Compile(
        string inputFile,
        string inputSource,
        TextWriter output,
        string copyright,
        string timeStamp,
        bool prettyPrint)
    {
        if (output == null)
        {
            throw new NullReferenceException("Output TextWriter was null.");
        }

        if (String.IsNullOrEmpty(inputSource))
        {
            if (String.IsNullOrEmpty(inputFile))
            {
                throw new NullReferenceException("Input file path was empty.");
            }

            inputSource = File.ReadAllText(inputFile);
        }

        // write out header with copyright and timestamp
        WriteHeader(output, copyright, timeStamp);

        // verify, compact and write out results
        // parse JBST markup
        JbstWriter writer = new JbstWriter(inputFile);

        List <ParseException> errors = new List <ParseException>();

        try
        {
            HtmlDistiller parser = new HtmlDistiller();
            parser.EncodeNonAscii      = false;
            parser.BalanceTags         = false;
            parser.NormalizeWhitespace = false;
            parser.HtmlWriter          = writer;
            parser.HtmlFilter          = new NullHtmlFilter();
            parser.Parse(inputSource);
        }
        catch (ParseException ex)
        {
            errors.Add(ex);
        }
        catch (Exception ex)
        {
            errors.Add(new ParseError(ex.Message, inputFile, 0, 0, ex));
        }

        // render the pretty-printed version
        writer.Render(output);

        // return any errors
        return(errors);
    }
Example #3
0
 private void SetAttribute(JbstContainerControl target, string name, string value)
 {
     value = HtmlDistiller.DecodeHtmlEntities(value);
     if ("style".Equals(name, StringComparison.OrdinalIgnoreCase))
     {
         this.SetStyle(target, null, value);
     }
     else
     {
         target.Attributes[name] = value;
     }
 }
Example #4
0
        /// <summary>
        /// Renders the style property
        /// </summary>
        /// <param name="output"></param>
        private void WriteStyles(HtmlTag tag)
        {
            this.writer.Write(" style=\"");

            foreach (KeyValuePair <string, string> style in tag.FilteredStyles)
            {
                HtmlDistiller.HtmlAttributeEncode(style.Key, this.writer);
                this.writer.Write(':');
                HtmlDistiller.HtmlAttributeEncode(style.Value, this.writer);
                this.writer.Write(';');
            }

            this.writer.Write('\"');
        }
Example #5
0
        /// <summary>
        /// Compiles the provided input
        /// </summary>
        /// <param name="input"></param>
        /// <param name="filename"></param>
        /// <param name="compilationErrors"></param>
        /// <param name="compactionErrors"></param>
        /// <returns></returns>
        public IOptimizedResult Compile(TextReader input, string filename, List <ParseException> compilationErrors, List <ParseException> compactionErrors)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            // verify, compact and write out results
            // parse JBST markup
            JbstWriter writer = new JbstWriter(filename);

            StringWriter sw = new StringWriter();

            string source = input.ReadToEnd();

            try
            {
                HtmlDistiller parser = new HtmlDistiller();
                parser.EncodeNonAscii      = false;
                parser.BalanceTags         = false;
                parser.NormalizeWhitespace = false;
                parser.HtmlWriter          = writer;
                parser.HtmlFilter          = new NullHtmlFilter();
                parser.Parse(source);

                // render the pretty-printed version
                writer.Render(sw);
            }
            catch (ParseException ex)
            {
                compilationErrors.Add(ex);
            }
            catch (Exception ex)
            {
                compilationErrors.Add(new ParseError(ex.Message, filename, 0, 0, ex));
            }

            SimpleJbstBuildResult result = new SimpleJbstBuildResult(writer.JbstName, writer.AutoMarkup);

            result.Source        = source;
            result.PrettyPrinted = sw.GetStringBuilder().ToString();
            result.Compacted     = this.Compact(result.PrettyPrinted, filename, compactionErrors);
            result.ContentType   = "text/javascript";
            result.FileExtension = ".jbst.js";
            result.Hash          = this.ComputeHash(result.Source);

            // return any errors
            return(result);
        }
Example #6
0
        protected override void ProcessResource(
            IResourceBuildHelper helper,
            string virtualPath,
            string sourceText,
            out string resource,
            out string compacted,
            List <ParseException> errors)
        {
            // parse JBST markup
            this.jbstWriter = new JbstWriter(virtualPath);

            try
            {
                HtmlDistiller parser = new HtmlDistiller();
                parser.EncodeNonAscii      = false;
                parser.BalanceTags         = false;
                parser.NormalizeWhitespace = false;
                parser.HtmlWriter          = this.jbstWriter;
                parser.HtmlFilter          = new NullHtmlFilter();
                parser.Parse(sourceText);

                // determine which globalization keys were used
                JbstCodeProvider.ExtractGlobalizationKeys(this.jbstWriter.JbstParseTree, this.GlobalizationKeys);
            }
            catch (ParseException ex)
            {
                errors.Add(ex);
            }
            catch (Exception ex)
            {
                errors.Add(new ParseError(ex.Message, virtualPath, 0, 0, ex));
            }

            string renderedTemplate;

            using (StringWriter sw = new StringWriter())
            {
                // render the pretty-printed version
                this.jbstWriter.Render(sw);
                sw.Flush();
                renderedTemplate = sw.ToString();

                resource = ScriptResourceCodeProvider.FirewallScript(virtualPath, renderedTemplate, false);
            }

            // min the output for better compaction
            compacted = ScriptCompactionAdapter.Compact(virtualPath, renderedTemplate, errors);
            compacted = ScriptResourceCodeProvider.FirewallScript(virtualPath, compacted, true);
        }
Example #7
0
        private void AppendChild(string text)
        {
            if (String.IsNullOrEmpty(text))
            {
                return;
            }

            if (this.current == null)
            {
                this.current = this.document;
            }

            // this allows HTML entities to be encoded as unicode
            text = HtmlDistiller.DecodeHtmlEntities(text);

            JbstLiteral literal = new JbstLiteral(text, true);

            this.current.ChildControls.Add(literal);
        }
Example #8
0
        /// <summary>
        /// Optionally allows breaking of long words
        /// </summary>
        /// <param name="source"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="replacement"></param>
        /// <returns>true if literal was changed</returns>
        public virtual bool FilterLiteral(string source, int start, int end, out string replacement)
        {
            replacement = null;
            if (this.MaxWordLength <= 0 || source == null)
            {
                return(false);
            }

            bool addedBreak = false;
            int  last       = start,
                 sinceSpace = 0;

            for (int i = start; i < end; i++)
            {
                if (Char.IsWhiteSpace(source[i]))
                {
                    sinceSpace = 0;
                    continue;
                }

                if (sinceSpace >= this.MaxWordLength)
                {
                    if (this.HtmlWriter == null)
                    {
                        throw new NullReferenceException("HtmlWriter was never set on the HtmlFilter.");
                    }

                    addedBreak = true;
                    // append all before break
                    this.HtmlWriter.WriteLiteral(source.Substring(last, i - last));
                    this.HtmlWriter.WriteTag(new HtmlTag(WordBreakTag, this));
                    this.HtmlWriter.WriteLiteral(SoftHyphenEntity);
                    last       = i;
                    sinceSpace = 0;
                }
                else
                {
                    sinceSpace++;
                }

                if (source[i] == HtmlDistiller.EntityStartChar)
                {
                    char entityChar;
                    int  entityLength = HtmlDistiller.DecodeHtmlEntity(source, i, out entityChar);
                    if (entityLength > 1)
                    {
                        // compensating for the length of this entity
                        // so that doesn't break within the entity symbol
                        i += entityLength - 1;
                    }
                }
            }

            if (!addedBreak)
            {
                // don't replace since didn't need to insert break
                return(false);
            }

            if (last < end)
            {
                // replace with remaining string
                replacement = source.Substring(last, end - last);
            }

            // a word break was inserted
            return(true);
        }