public static HtmlDocumentFacade ProcessDifferences(HtmlDocumentFacade doc) { var urlSource = HttpContext.Current.Request["tsp-src"]; if (urlSource != null) { return null; } HtmlDocumentFacade diffDoc = doc; doc.Trace("begindocInherits"); var docInherits = doc.querySelectorAll("html>head>link[rel='export']"); doc.Trace("enddocInherits"); if (docInherits.Count == 0) return null; if (docInherits.Count > 1) { throw new ArgumentException("Cannot inherit from more than one page"); } var metaEl = docInherits[0]; var baseDocRelativeURL = metaEl.getAttribute("href"); if (string.IsNullOrEmpty(baseDocRelativeURL)) throw new ArgumentException("No Content Attribute found"); var inheritedContentFilePath = doc.GetHostContentFilePath(baseDocRelativeURL); var superHandler = new tspHandler(inheritedContentFilePath); HtmlDocumentFacade superDoc = superHandler.ProcessFile(); //var nodeHierarchy = new Stack<HtmlNodeFacade>(); //nodeHierarchy.Push(diffDoc.html); //var differenceStack = new Stack<NodeDifference>(); //var differences = new List<NodeDifference>(); //ProcessNode(nodeHierarchy, differenceStack, differences); //MergeDifferences(superDoc, differences); //return superDoc; ProcessTransform(superDoc.html, doc.html); return superDoc; }
public static void searchForNode(Func<HtmlNode, bool> test, HtmlNode node, List<HtmlNodeFacade> nodes, HtmlDocumentFacade owner) { if (test(node)) { nodes.Add( new HtmlNodeFacade(node, owner) ); } foreach (var child in node.ChildNodes) { searchForNode(test, child, nodes, owner); } }
public HtmlNodeFacade(HtmlNode node, HtmlDocumentFacade owner) { if (node == null) throw new Exception(); this._node = node; this._ownerDoc = owner; }
public HtmlDocumentFacade ProcessFile() { var doc = new HtmlDocumentFacade(this); var req = HttpContext.Current.Request; var src = req["DBS.src"]; if (src == "hif") { doc.IDSelector = req["DBS.id"]; } return doc.Process(); }
public static HtmlDocumentFacade ProcessResourceDependencies(this HtmlDocumentFacade doc) { if (doc.head == null) return doc; var resourceDependencies = doc.head.querySelectorAll("link[rel='import']").ToList(); var isLocal = HttpContext.Current.Request.IsLocal; Dictionary<string, string> CustomAppMapping = null; var staticPathMappings = ConfigurationManager.AppSettings["StaticPathMappings"]; if (!string.IsNullOrEmpty(staticPathMappings)) { CustomAppMapping = new Dictionary<string, string>(); var mappings = staticPathMappings.Split(','); foreach (string mapping in mappings) { var LHS = mapping.SubstringBefore("=>"); var RHS = mapping.SubstringAfter("=>"); CustomAppMapping[LHS] = RHS; } } resourceDependencies.ForEach(rd => { var relPath = rd.getAttribute("href"); string mode = rd.getAttribute("data-mode"); string content = doc.GetHostContent(relPath); var depDoc = new HtmlDocumentFacade(content); var metaProcessor = depDoc.head.querySelectorAll("meta[name='importProcessor'][content='DBS.mergeAssets']"); if (metaProcessor.Count == 0) return; var depDocFilePath = doc.GetHostContentFilePath(relPath); DateTime latestTimeStamp = new DateTime(0); if(isLocal){ var fi = new FileInfo(depDocFilePath); if(fi.LastWriteTime > latestTimeStamp){ latestTimeStamp = fi.LastWriteTime; } var fi2 = new FileInfo(doc.Host.GetDocumentFilePath()); if (fi2.LastWriteTime > latestTimeStamp) { latestTimeStamp = fi2.LastWriteTime; } } var header = depDoc.head; #region find type def mappings var typeDefsToImplementationMappings = depDoc.ProcessTypeScriptMappingFile(depDocFilePath); #region process scripts var scriptRefs = new Dictionary<string, bool>(); var scripts = header .querySelectorAll("script") .ToList(); scripts.ForEach(s => { var src = s.getAttribute("src"); var srcLC = src.ToLower(); scriptRefs[src] = true; }); var scriptFiles = scriptRefs.Select(typescriptRef => { var src = typescriptRef.Key; var srcFilePath = depDocFilePath.NavigateTo(src); return new ScriptFile(srcFilePath); }).ToList(); var alreadyAdded = new Dictionary<string, bool>(); var fileList = new List<ScriptFile>(); foreach(var tsFile in scriptFiles){ checkScriptFileForDependencies(alreadyAdded, fileList, tsFile); } fileList.Sort(); var rdID = rd.id; var previousScriptTags = doc.head.querySelectorAll("script[data-genID='" + rdID + "']"); previousScriptTags.ForEach(pst => pst.delete()); foreach (var scriptFile in fileList) { #region map script files var scriptFileAbsPaths = new List<string>{ scriptFile.DocumentFilePath, }; if (typeDefsToImplementationMappings.ContainsKey(scriptFileAbsPaths[0])) { scriptFileAbsPaths = typeDefsToImplementationMappings[scriptFileAbsPaths[0]]; } foreach (var tsFileAbsPath in scriptFileAbsPaths) { var scriptImport = doc.createElement("script"); if (isLocal) { var fi = new FileInfo(tsFileAbsPath); if (fi.LastWriteTime > latestTimeStamp) { latestTimeStamp = fi.LastWriteTime; } } var src = doc.GetHostRelativePath(tsFileAbsPath); if (src.EndsWith(".ts")) { src = src.ReplaceLast(".ts").With(".js"); } if (CustomAppMapping != null) { foreach (var kvp in CustomAppMapping) { src = src.Replace(kvp.Key, kvp.Value); } } scriptImport.setAttribute("src", src); if (!string.IsNullOrEmpty(mode)) { scriptImport.setAttribute("data-mode", mode); } scriptImport.setAttribute("data-genID", rdID); //doc.head.appendChild(sc); doc.head.insertBefore(scriptImport, rd); } #endregion } #endregion #endregion #region process css var links = header .querySelectorAll("link[rel='stylesheet']") .Select(l => { var href = l.getAttribute("href"); var hrefFilePath = depDocFilePath.NavigateTo(href); return hrefFilePath; }) .ToList(); var previousLinkTags = doc.head.querySelectorAll("link[data-genID='" + rdID + "']"); previousLinkTags.ForEach(plt => plt.delete()); links.ForEach(linkAbsPath => { var linkImport = doc.createElement("link"); var fi = new FileInfo(linkAbsPath); if (fi.LastWriteTime > latestTimeStamp) latestTimeStamp = fi.LastWriteTime; var href = doc.GetHostRelativePath(linkAbsPath); linkImport.setAttribute("href", href); linkImport.setAttribute("data-genID", rdID); linkImport.setAttribute("rel", "stylesheet"); doc.head.insertBefore(linkImport, rd); }); #endregion rd.delete(); var newHtml = doc.html; if (isLocal) { string destFilePath = doc.Host.GetDocumentFilePath().ReplaceLast(".tsp").With(".d.tsp"); var destFI = new FileInfo(destFilePath); if (destFI.LastWriteTime < latestTimeStamp) { File.WriteAllText(destFilePath, newHtml.outerHTML); } } }); return doc; }
public JQueryFacade(HtmlDocumentFacade doc) { _doc = doc; }