protected override string Replace(Element el) { if (el is BeginTag) { BeginTag beginTag = (BeginTag)el; if (beginTag.NameEquals("a")) { Attr href = beginTag.GetAttribute("href"); if (href != null && href.Value != null) { href.Value = ConvertUrl(href.Value); return(beginTag.ToString()); } } else if (beginTag.NameEquals("img")) { Attr src = beginTag.GetAttribute("src"); if (src != null && src.Value != null) { src.Value = ConvertUrl(src.Value); return(beginTag.ToString()); } } } return(base.Replace(el)); }
private void ModifyMetaDataAsNecessary(BeginTag tag) { if (_metaData == null) { return; } Attr nameAttr = tag.GetAttribute(HTMLTokens.Name); if (nameAttr == null) { nameAttr = tag.GetAttribute(HTMLTokens.HttpEquiv); } if (nameAttr != null) { switch (nameAttr.Value.ToUpper(CultureInfo.InvariantCulture)) { case (HTMLTokens.Author): ModifyMetaDataAttribute(tag, HTMLTokens.Content, _metaData.Author); break; case (HTMLTokens.ContentType): ModifyMetaDataAttribute(tag, HTMLTokens.Content, string.Format(CultureInfo.InvariantCulture, "text/html; {0}", _metaData.Charset)); break; case (HTMLTokens.Charset): ModifyMetaDataAttribute(tag, HTMLTokens.Content, _metaData.Charset); break; case (HTMLTokens.Description): ModifyMetaDataAttribute(tag, HTMLTokens.Content, _metaData.Description); break; case (HTMLTokens.Generator): ModifyMetaDataAttribute(tag, HTMLTokens.Content, _metaData.Generator); break; case (HTMLTokens.CopyRight): ModifyMetaDataAttribute(tag, HTMLTokens.Content, _metaData.CopyRight); break; case (HTMLTokens.Keywords): ModifyMetaDataAttribute(tag, HTMLTokens.Content, _metaData.KeywordString); break; case (HTMLTokens.Pragma): ModifyMetaDataAttribute(tag, HTMLTokens.Content, _metaData.Pragma); break; case (HTMLTokens.Robots): ModifyMetaDataAttribute(tag, HTMLTokens.Content, _metaData.Robots); break; } _emittedMetaData.Add(nameAttr.Value.ToUpper(CultureInfo.InvariantCulture)); } }
private void HandleInput(HtmlForm parentForm, BeginTag inputTag) { string type = inputTag.GetAttributeValue("type"); if (type != null) { type = type.Trim().ToLowerInvariant(); } string name = inputTag.GetAttributeValue("name"); string value = inputTag.GetAttributeValue("value"); switch (type) { case "password": new Textbox(parentForm, name, value); break; case "checkbox": { int dummy; bool isChecked = inputTag.GetAttribute("checked", true, 0, out dummy) != null; new Checkbox(parentForm, name, value, isChecked); break; } case "radio": { int dummy; bool isChecked = inputTag.GetAttribute("checked", true, 0, out dummy) != null; new Radio(parentForm, name, value, isChecked); break; } case "submit": new SubmitButton(parentForm, name, value); break; case "image": new ImageButton(parentForm, name, value, inputTag.GetAttributeValue("src")); break; case "hidden": new Hidden(parentForm, name, value); break; case "text": default: new Textbox(parentForm, name, value); break; } }
public IExtensionData[] CalculateReferencedExtensionData(string content) { Hashtable datas = new Hashtable(); ContentSourceManager.SmartContentPredicate predicate = new ContentSourceManager.SmartContentPredicate(); SimpleHtmlParser p = new SimpleHtmlParser(content); for (Element el; null != (el = p.Next());) { if (predicate.IsMatch(el)) { BeginTag bt = el as BeginTag; Attr idAttr = bt.GetAttribute("id"); if (idAttr != null) //Synchronized WP posts will strip ID attrs (bug 488143) { string smartContentSourceId; string smartContentId; string smartContentElementId = idAttr.Value; ContentSourceManager.ParseContainingElementId(smartContentElementId, out smartContentSourceId, out smartContentId); IExtensionData data = GetExtensionData(smartContentId); if (data != null) { datas[smartContentId] = data; } } } } return((IExtensionData[])ArrayHelper.CollectionToArray(datas.Values, typeof(IExtensionData))); }
private void ModifyMetaDataAttribute(BeginTag tag, string attributeName, string valueToChangeTo) { Attr valueAttr = tag.GetAttribute(attributeName); if (valueAttr != null && valueToChangeTo != null) { valueAttr.Value = valueToChangeTo; } }
protected override void OnBeginTag(BeginTag tag) { if (tag != null && LightWeightHTMLDocument.AllUrlElements.ContainsKey(tag.Name.ToUpper(CultureInfo.InvariantCulture))) { Attr attr = tag.GetAttribute((string)LightWeightHTMLDocument.AllUrlElements[tag.Name.ToUpper(CultureInfo.InvariantCulture)]); if (attr != null) { string url = attr.Value; if (!UrlHelper.IsUrl(url) && ShouldEscapeRelativeUrl(url)) { attr.Value = UrlHelper.EscapeRelativeURL(BaseUrl, url); } } } // Special case params if (tag != null && tag.NameEquals(HTMLTokens.Param)) { // Handle Params foreach (string paramValue in LightWeightHTMLDocument.ParamsUrlElements) { Attr attr = tag.GetAttribute(HTMLTokens.Name); if (attr != null) { if (attr.Value.ToUpper(CultureInfo.InvariantCulture) == paramValue) { Attr valueAttr = tag.GetAttribute(HTMLTokens.Value); if (valueAttr != null) { string url = valueAttr.Value; if (!UrlHelper.IsUrl(url)) { valueAttr.Value = UrlHelper.EscapeRelativeURL(BaseUrl, url); } } } } } } base.OnBeginTag(tag); }
/// <summary> /// Clones active smart content contained in the provided HTML, and disables unknown smart content. /// </summary> public static string PrepareSmartContentHtmlForEditorInsertion(string html, IContentSourceSidebarContext sourceContext) { StringBuilder output = new StringBuilder(); ContentSourceManager.SmartContentPredicate predicate = new ContentSourceManager.SmartContentPredicate(); SimpleHtmlParser p = new SimpleHtmlParser(html); for (Element el; null != (el = p.Next());) { if (predicate.IsMatch(el)) { BeginTag bt = el as BeginTag; Attr idAttr = bt.GetAttribute("id"); String contentSourceId, contentItemId; ContentSourceManager.ParseContainingElementId(idAttr.Value, out contentSourceId, out contentItemId); ISmartContent smartContent = sourceContext.FindSmartContent(contentItemId); if (smartContent != null) { String newId = Guid.NewGuid().ToString(); sourceContext.CloneSmartContent(contentItemId, newId); if (RefreshableContentManager.ContentSourcesWithRefreshableContent.Contains(contentSourceId)) { IExtensionData extensionData = sourceContext.FindExtentsionData(newId); Debug.Assert(extensionData != null); // Since we just made a new id for the smart content just about to be inserted // we want to give it a chance to get a callback because its callback might have happened while // it was on the clipboard(in the event of cut). This means the refreshable content manager doesnt know // to watch out for this smart content on paste, it only knows to look out for who created it. Thus // we just force the callback, and if it didnt need it, nothing will happen. if (extensionData.RefreshCallBack == null) { extensionData.RefreshCallBack = DateTime.UtcNow; } } idAttr.Value = ContentSourceManager.MakeContainingElementId(contentSourceId, newId); } else { ContentSourceManager.RemoveSmartContentAttributes(bt); } } output.Append(el.ToString()); } return(output.ToString()); }
protected override void OnBeginTag(BeginTag tag) { if (tag != null && LightWeightHTMLDocument.AllUrlElements.ContainsKey(tag.Name.ToUpper(CultureInfo.InvariantCulture))) { Attr attr = tag.GetAttribute((string)LightWeightHTMLDocument.AllUrlElements[tag.Name.ToUpper(CultureInfo.InvariantCulture)]); if (attr != null) { string url = attr.Value; if (!UrlHelper.IsUrl(url)) { attr.Value = UrlHelper.EscapeRelativeURL(_baseUrl, url); } } } base.OnBeginTag(tag); }
/// <summary> /// Is the tag a meaningless tag such as <p></p> or <a href="..."></a> or <a href="..."> </a> /// </summary> /// <param name="htmlParser"></param> /// <param name="bt"></param> /// <returns></returns> private static bool RemoveMeaninglessTags(SimpleHtmlParser htmlParser, BeginTag bt) { // Look to see if the tag is a <p> without any attributes if ((bt.NameEquals("p") && bt.Attributes.Length == 0 && !bt.HasResidue)) { Element e = htmlParser.Peek(0); // Look to see if thereis a matching end tag to the element we are looking at if (e != null && e is EndTag && ((EndTag)e).NameEquals("p")) { // eat up the end tag htmlParser.Next(); return(true); } } // Look to see if the tag is an <a> without a style/id/name attribute, but has an href... meaning the link is not useful if ((bt.NameEquals("a") && bt.GetAttribute("name") == null && bt.GetAttributeValue("style") == null && bt.GetAttributeValue("id") == null && bt.GetAttributeValue("href") != null)) { bool hadWhiteSpaceText = false; Element e = htmlParser.Peek(0); // Look to see if the a just has whitespace inside of it if (e is Text && HtmlUtils.UnEscapeEntities(e.RawText, HtmlUtils.UnEscapeMode.NonMarkupText).Trim().Length == 0) { e = htmlParser.Peek(1); hadWhiteSpaceText = true; } // Look to see if thereis a matching end tag to the element we are looking at if (e != null && e is EndTag && ((EndTag)e).NameEquals("a")) { // if this was an <a> with whitespace in the middle eat it up if (hadWhiteSpaceText) { htmlParser.Next(); } // eat up the end tag htmlParser.Next(); return(true); } } return(false); }
private bool IsIllegalTag(BeginTag tag) { if (IsRegexMatch(IllegalTagName, tag.Name)) { return(true); } else if (FlagIsSet(Flag.RemoveStyles) && tag.NameEquals("link")) { //if this link element is a stylesheet, it is illegal Attr relAttr = tag.GetAttribute("rel"); if (relAttr != null && relAttr.Value != null && relAttr.Value.ToUpperInvariant().Trim() == "STYLESHEET") { return(true); } } return(false); }
protected override void OnBeginTag(BeginTag tag) { if (tag != null && LightWeightHTMLDocument.AllUrlElements.ContainsKey(tag.Name.ToUpper(CultureInfo.InvariantCulture))) { Attr attr = tag.GetAttribute((string)LightWeightHTMLDocument.AllUrlElements[tag.Name.ToUpper(CultureInfo.InvariantCulture)]); if (attr != null) { string oldRef = attr.Value; string newRef = _referenceFixer(tag, attr.Value); attr.Value = newRef; if (oldRef != newRef && _referenceFixed != null) { //notify the reference fixed callback that a reference was fixed. _referenceFixed(oldRef, newRef); } } } base.OnBeginTag(tag); }
private void HandleSelect(HtmlForm parentForm, BeginTag selectTag) { string name = selectTag.GetAttributeValue("name"); int dummy; bool multiple = selectTag.GetAttribute("multiple", true, 0, out dummy) != null; ArrayList optionInfos = new ArrayList(); Element el = parser.Next(); while (el != null) { BeginTag tag = el as BeginTag; if (tag != null && tag.NameEquals("option")) { string value = tag.GetAttributeValue("value"); bool isSelected = tag.GetAttribute("selected", true, 0, out dummy) != null; string label = string.Empty; el = parser.Next(); if (el != null && el is Text) { label = HtmlUtils.UnEscapeEntities(el.ToString(), HtmlUtils.UnEscapeMode.NonMarkupText).TrimEnd(' ', '\r', '\n', '\t'); el = parser.Next(); } optionInfos.Add(new OptionInfo(value, label, isSelected)); continue; } if (el is EndTag && ((EndTag)el).NameEquals("select")) { new Select(parentForm, name, multiple, (OptionInfo[])optionInfos.ToArray(typeof(OptionInfo))); return; } el = parser.Next(); } }
protected override void OnBeginTag(BeginTag tag) { if (tag != null && LightWeightHTMLDocument.AllUrlElements.ContainsKey(tag.Name.ToUpper(CultureInfo.InvariantCulture))) { Attr attr = tag.GetAttribute((string)LightWeightHTMLDocument.AllUrlElements[tag.Name.ToUpper(CultureInfo.InvariantCulture)]); if (attr != null && attr.Value != null) { if (UrlHelper.IsUrl(attr.Value)) { Uri reference = new Uri(attr.Value); if (_fileReferenceList.IsReferenced(reference)) { ISupportingFile supportingFile = _fileReferenceList.GetSupportingFileByUri(reference); if (supportingFile.Embedded) { AddFileReference(supportingFile); } } } } } base.OnBeginTag(tag); }
protected override void OnBeginTag(BeginTag tag) { if (tag != null) { // Reset any frame urls // This is done because the HTML that is often in this document may have // incorrect urls for frames. The frames enumeration is accurate, so if the // name from the frames enumeration is the same as this frame, we should fix its // url up. if (tag.NameEquals(HTMLTokens.Frame)) { Attr name = tag.GetAttribute(HTMLTokens.Name); if (name != null && this._frames != null) { LightWeightHTMLDocument frameDoc = GetFrameDocumentByName(name.Value); if (frameDoc != null) { Attr src = tag.GetAttribute(HTMLTokens.Src); if (src != null && src.Value != frameDoc.Url) { Generator.AddSubstitionUrl(new UrlToReplace(src.Value, frameDoc.Url)); } } } } LightWeightTag currentTag = new LightWeightTag(tag); // The key we'll use for the table string key = tag.Name.ToUpper(CultureInfo.InvariantCulture); if (!_tagTable.ContainsKey(key)) { _tagTable[key] = new LightWeightTag[0]; } LightWeightTag[] currentTags = (LightWeightTag[])_tagTable[key]; LightWeightTag[] grownTags = new LightWeightTag[currentTags.Length + 1]; currentTags.CopyTo(grownTags, 0); grownTags[currentTags.Length] = currentTag; _tagTable[key] = grownTags; // Accumulate the title text if (tag.NameEquals(HTMLTokens.Title) && !tag.Complete) { _nextTextIsTitleText = true; } else if (tag.NameEquals(HTMLTokens.A) && !tag.Complete && tag.GetAttribute(HTMLTokens.Href) != null) { if (_collectingForTag != null) { if (tag.NameEquals(HTMLTokens.A)) { _collectingForTagDepth++; } } else { _collectingForTag = currentTag; } } } base.OnBeginTag(tag); }
protected override void OnBeginTag(BeginTag tag) { if (tag == null) { return; } if (_firstTag) { if (!tag.NameEquals(HTMLTokens.Html)) { EmitTag(HTMLTokens.Html); } _firstTag = false; } if (!_seenHead && !TagPermittedAboveBody(tag)) { Emit("<head>"); EmitAdditionalMetaData(); Emit("</head>"); _seenHead = true; } if (tag.NameEquals(HTMLTokens.Script)) { if (!tag.Complete) { _scriptDepth++; } return; } if (tag.NameEquals(HTMLTokens.Head)) { _seenHead = true; } else if (!_seenBody && !tag.NameEquals(HTMLTokens.Body)) { if (!TagPermittedAboveBody(tag)) { EmitTag(HTMLTokens.Body); _seenBody = true; } } else if (!_seenBody && tag.NameEquals(HTMLTokens.Body)) { _seenBody = true; } if (tag.NameEquals(HTMLTokens.Base)) { if (_metaData == null || _metaData.Base == null) { return; } else { Attr href = tag.GetAttribute(HTMLTokens.Href); if (href != null) { href.Value = _metaData.Base; } } _emittedMetaData.Add(HTMLTokens.Base); } if (tag.NameEquals(HTMLTokens.Meta)) { ModifyMetaDataAsNecessary(tag); } foreach (Attr attr in tag.Attributes) { if (attr != null) { if (IsScriptAttribute(attr)) { tag.RemoveAttribute(attr.Name); } else { attr.Value = ReplaceValue(attr.Value); } } } Emit(tag.ToString()); base.OnBeginTag(tag); }
/// <summary> /// Walks the current contents to find smart content areas. When one is found, it calls the operation on the smart content. The operation has a chance /// to return new content. If the content is non-null it will replace the current content. /// </summary> /// <param name="contents">the raw HTML string whose structured blocks will be replaced.</param> /// <param name="operation">Delegate for generating replacement content.</param> /// <param name="editMode">If true, then the element's stylename will be activated for editing</param> /// <param name="continueOnError"> /// true - if the plugin throws an exception, it keeps crawling the DOM /// false - if a plugin throws an exception, it stops processing the DOM and return empty string /// null - if a plugin throws an exception, this function will rethrow it /// </param /// <returns>the contents with structured blocks replaced.</returns> internal static string PerformOperation(string contents, SmartContentOperation operation, bool editMode, IContentSourceSidebarContext sourceContext, bool?continueOnError) { //replace all structured content blocks with their editor HTML //string html = PostBodyPreprocessor.Preprocess(contents); StringBuilder sb = new StringBuilder(); SimpleHtmlParser parser = new SimpleHtmlParser(contents); for (Element e = parser.Next(); e != null; e = parser.Next()) { if (e is BeginTag) { BeginTag beginTag = (BeginTag)e; string elementClassName = beginTag.GetAttributeValue("class"); if (ContentSourceManager.IsSmartContentClass(elementClassName)) { ISmartContent sContent = null; try { string contentSourceId, contentItemId; string blockId = beginTag.GetAttributeValue("id"); if (blockId != null) { ContentSourceManager.ParseContainingElementId(blockId, out contentSourceId, out contentItemId); ContentSourceInfo contentSource = sourceContext.FindContentSource(contentSourceId); if (contentSource != null && contentSource.Instance is SmartContentSource) { SmartContentSource sSource = (SmartContentSource)contentSource.Instance; sContent = sourceContext.FindSmartContent(contentItemId); if (sContent != null) { //write the div with the appropriate className string newClassName = editMode ? ContentSourceManager.EDITABLE_SMART_CONTENT : ContentSourceManager.SMART_CONTENT; beginTag.GetAttribute("class").Value = newClassName; //replace the inner HTML of the div with the source's editor HTML string content = parser.CollectHtmlUntil("div"); sb.Append(e.ToString()); operation(sourceContext, sSource, sContent, ref content); sb.Append(content); sb.Append("</div>"); continue; } } } } catch (Exception ex) { Trace.WriteLine(String.Format(CultureInfo.InvariantCulture, "Error loading smart content item\r\n{0}", ex)); sContent = null; if (continueOnError == null) { throw; } if (!continueOnError.Value) { return(String.Empty); } } if (sContent == null) { //this element references an unknown smart content, so it should not be editable Attr classAttr = beginTag.GetAttribute("class"); classAttr.Value = ContentSourceManager.SMART_CONTENT; } } } sb.Append(e.ToString()); } return(sb.ToString()); }