protected override void OnBeginTag(BeginTag tag) { if (tag.NameEquals(HTMLTokens.Title) && !tag.Complete) { _inTitle = true; } if (TagsToPreserve.Contains(tag.Name.ToUpper(CultureInfo.InvariantCulture))) { EmitTagAndAttributes(tag.Name, tag); } else if (ReplaceTags.ContainsKey(tag.Name.ToUpper(CultureInfo.InvariantCulture))) { EmitTagAndAttributes((string)ReplaceTags[tag.Name.ToUpper(CultureInfo.InvariantCulture)], tag); } }
protected override void OnEndTag(EndTag tag) { if (tag.Implicit) { return; } if (tag.NameEquals(HTMLTokens.Title)) { _inTitle = false; } if (TagsToPreserve.Contains(tag.Name.ToUpper(CultureInfo.InvariantCulture))) { EmitTagAndAttributes(tag.Name, tag); } else if (ReplaceTags.ContainsKey(tag.Name.ToUpper(CultureInfo.InvariantCulture))) { EmitTagAndAttributes((string)ReplaceTags[tag.Name.ToUpper(CultureInfo.InvariantCulture)], tag); } }
private void HandleNextNode(Queue <HtmlNode> nodes, IDataMap map, string itemPath, Item importRow) { var node = nodes.Dequeue(); var nodeName = node.Name.ToLower(); var parentNode = node.ParentNode; var childNodes = node.SelectNodes("./*|./text()"); if (childNodes != null) { foreach (var child in childNodes) { nodes.Enqueue(child); } } if (UnwantedTags.Any(tag => tag == nodeName)) { // if this node is one to remove if (childNodes != null) { // make sure children are added back foreach (var child in childNodes) { parentNode.InsertBefore(child, node); } } parentNode.RemoveChild(node); } else if (node.HasAttributes) { // if it's not being removed foreach (string s in UnwantedAttributes) // remove unwanted attributes { node.Attributes.Remove(s); } //replace images if (nodeName.Equals("img")) { // see if it exists string imgSrc = node.Attributes["src"].Value; DynamicLink dynamicLink; if (!DynamicLink.TryParse(imgSrc, out dynamicLink)) { return; } MediaItem mediaItem = importRow.Database.GetItem(dynamicLink.ItemId, dynamicLink.Language ?? map.ImportToLanguage); var mediaParentItem = BuildMediaPath(map.ToDB, mediaItem.InnerItem.Paths.ParentPath); MediaItem newImg = HandleMediaItem(map, mediaParentItem, itemPath, mediaItem); if (newImg != null) { string newSrc = string.Format("-/media/{0}.ashx", newImg.ID.ToShortID().ToString()); // replace the node with sitecore tag node.SetAttributeValue("src", newSrc); } } } else if (ReplaceTags.ContainsKey(nodeName)) { // Replace tag node.Name = ReplaceTags[nodeName]; } else { //Keep node as is } }