Example #1
0
        public void Init(IValueProvider valueProvider, IPathProvider pathProvider)
        {
            var needAttr = htmlElement.htmlAttribute()
                           .ToDictionary(
                key => key.htmlAttributeName().TAG_NAME().GetText().ToUpper(),
                value => new HtmlAttributeManager(value).Value
                );

            if (needAttr["REL"]?.ToUpper() != "STYLESHEET")
            {
                return;
            }

            var fileName = needAttr["HREF"];

            if (fileName == null)
            {
                return;
            }

            string path = pathProvider.GetPathFile(fileName);

            File = valueProvider.GetFile(path);
            if (File == null)
            {
                return;
            }

            htmlElement.AddChild(new BufferValueContext <File>(htmlElement, File));
        }
Example #2
0
            public override object VisitScript([NotNull] HtmlParser.ScriptContext context)
            {
                foreach (var item in context.htmlAttribute())
                {
                    var name = item.htmlAttributeName().TAG_NAME().GetText();
                    if (name.ToUpper() != "SRC")
                    {
                        continue;
                    }
                    var           manager    = new HtmlAttributeManager(item);
                    var           path       = manager.Value;
                    Entities.File searchFile = valueProvider.GetFile(path);
                    if (searchFile != null)
                    {
                        if (searchFile.IsExternal)
                        {
                            manager.Value = searchFile.FileName;
                        }
                        return(null);
                    }
                    try {
                        HttpWebRequest req = WebRequest.CreateHttp(path);
                        req.Method = "GET";
                        var           result   = req.GetResponse();
                        var           fileName = Path.GetRandomFileName() + ".js";
                        Entities.File file     = new ParseFile().ToParse(fileName, result.GetResponseStream(), Entities.FileType.Js);
                        valueProvider.AddFile(file);

                        file = new Entities.File {
                            Type       = file.Type,
                            FileName   = file.FileName,
                            SearchName = path,
                            IsExternal = true,
                        };
                        valueProvider.AddFile(file);

                        manager.Value = fileName;
                    } catch {
                        return(null);
                    }
                }
                return(null);
            }
Example #3
0
        public void Init(IValueProvider valueProvider, IPathProvider pathProvider)
        {
            var curContent = scriptContext.htmlContent();

            foreach (var attr in scriptContext.htmlAttribute())
            {
                if (attr.htmlAttributeName().TAG_NAME().GetText().ToUpper() == "SRC")
                {
                    HtmlAttributeManager manager = new HtmlAttributeManager(attr);
                    string path = pathProvider.GetPathFile(manager.Value);
                    File = valueProvider.GetFile(path);
                    if (File == null)
                    {
                        return;
                    }
                }
            }
            if (curContent == null)
            {
                return;
            }
            HtmlContentContext content = null;

            if (File == null)
            {
                var stream = curContent.GetStream();
                File            = new ParseFile().ToParse(IO.Path.GetRandomFileName(), stream, FileType.Js);
                File.IsInternal = true;
                valueProvider.AddFile(File);

                content = new ContentContext(scriptContext, File);
            }
            else
            {
                BufferValueContext <File> bufferValue = new BufferValueContext <File>(scriptContext, File);
                scriptContext.AddChild(bufferValue);
                content = new HtmlContentContext(scriptContext, 0);
            }
            scriptContext.Replace(curContent, content);
        }