internal override void FromHtml(IElement element) { base.FromHtml(element); var div = element.GetElementsByTagName("div").Where(a => a.HasAttribute(TextRteAttribute)).FirstOrDefault(); if (div != null) { Rte = div.GetAttribute(TextRteAttribute); } else { // supporting updated rendering of Text controls, no nested DIV tag with the data-sp-rte attribute...so HTML content is embedded at the root Rte = ""; div = element; } // By default simple plain text is wrapped in a Paragraph, need to drop it to avoid getting multiple paragraphs on page edits. // Only drop the paragraph tag when there's only one Paragraph element underneath the DIV tag if ((div.FirstChild != null && (div.FirstChild as IElement) != null && (div.FirstChild as IElement).TagName.Equals("P", StringComparison.InvariantCultureIgnoreCase)) && (div.ChildElementCount == 1)) { Text = (div.FirstChild as IElement).InnerHtml; } else { Text = div.InnerHtml; } SpControlData = JsonSerializer.Deserialize <TextControlData>(element.GetAttribute(CanvasControl.ControlDataAttribute), new JsonSerializerOptions() { IgnoreNullValues = true }); controlType = SpControlData.ControlType; }
/// <summary> /// Converts this <see cref="PageText"/> control to it's html representation /// </summary> /// <param name="controlIndex">The sequence of the control inside the section</param> /// <returns>Html representation of this <see cref="PageText"/> control</returns> public override string ToHtml(float controlIndex) { // Can this control be hosted in this section type? if (Section.Type == CanvasSectionTemplate.OneColumnFullWidth) { throw new ClientException(ErrorType.Unsupported, PnPCoreResources.Exception_Page_ControlNotAllowedInFullWidthSection); } // Obtain the json data TextControlData controlData = new TextControlData() { ControlType = ControlType, Id = InstanceId.ToString("D"), Position = new CanvasControlPosition() { ZoneIndex = Section.Order, SectionIndex = Column.Order, SectionFactor = Column.ColumnFactor, LayoutIndex = Column.LayoutIndex, ControlIndex = controlIndex, }, Emphasis = new SectionEmphasis() { ZoneEmphasis = Column.VerticalSectionEmphasis ?? Section.ZoneEmphasis, }, EditorType = "CKEditor" }; // Persist the collapsible section settings if (Section.Collapsible) { controlData.ZoneGroupMetadata = new SectionZoneGroupMetadata() { // Set section type to 1 if it was not set (when new sections are added via code) Type = (Section as CanvasSection).SectionType == 0 ? 1 : (Section as CanvasSection).SectionType, DisplayName = Section.DisplayName, IsExpanded = Section.IsExpanded, ShowDividerLine = Section.ShowDividerLine, }; if (Section.IconAlignment.HasValue) { controlData.ZoneGroupMetadata.IconAlignment = Section.IconAlignment.Value.ToString().ToLower(); } else { controlData.ZoneGroupMetadata.IconAlignment = "true"; } } if (section.Type == CanvasSectionTemplate.OneColumnVerticalSection) { if (section.Columns.First().Equals(Column)) { controlData.Position.SectionFactor = 12; } } jsonControlData = JsonSerializer.Serialize(controlData); try { var nodeList = new HtmlParser().ParseFragment(Text, null); PreviewText = string.Concat(nodeList.Select(x => x.Text())); } catch { } StringBuilder html = new StringBuilder(); html.Append($@"<div {CanvasControlAttribute}=""{CanvasControlData}"" {CanvasDataVersionAttribute}=""{ DataVersion}"" {ControlDataAttribute}=""{jsonControlData.Replace("\"", """)}"">"); html.Append($@"<div {TextRteAttribute}=""{Rte}"">"); if (Text.Trim().StartsWith("<p>", StringComparison.InvariantCultureIgnoreCase) || Text.Trim().StartsWith("<h1>", StringComparison.InvariantCultureIgnoreCase) || Text.Trim().StartsWith("<h2>", StringComparison.InvariantCultureIgnoreCase) || Text.Trim().StartsWith("<h3>", StringComparison.InvariantCultureIgnoreCase) || Text.Trim().StartsWith("<h4>", StringComparison.InvariantCultureIgnoreCase) || Text.Trim().StartsWith("<ul>", StringComparison.InvariantCultureIgnoreCase) || Text.Trim().StartsWith("<blockquote>", StringComparison.InvariantCultureIgnoreCase) || Text.Trim().StartsWith("<pre>", StringComparison.InvariantCultureIgnoreCase)) { html.Append(Text); } else { html.Append($@"<p>{Text}</p>"); } html.Append("</div>"); html.Append("</div>"); return(html.ToString()); }
/// <summary> /// Converts this <see cref="PageText"/> control to it's html representation /// </summary> /// <param name="controlIndex">The sequence of the control inside the section</param> /// <returns>Html representation of this <see cref="PageText"/> control</returns> public override string ToHtml(float controlIndex) { // Can this control be hosted in this section type? if (Section.Type == CanvasSectionTemplate.OneColumnFullWidth) { throw new ClientException(ErrorType.Unsupported, PnPCoreResources.Exception_Page_ControlNotAllowedInFullWidthSection); } // Obtain the json data TextControlData controlData = new TextControlData() { ControlType = ControlType, Id = InstanceId.ToString("D"), Position = new CanvasControlPosition() { ZoneIndex = Section.Order, SectionIndex = Column.Order, SectionFactor = Column.ColumnFactor, LayoutIndex = Column.LayoutIndex, ControlIndex = controlIndex, }, Emphasis = new SectionEmphasis() { ZoneEmphasis = Column.VerticalSectionEmphasis ?? Section.ZoneEmphasis, }, EditorType = "CKEditor" }; if (section.Type == CanvasSectionTemplate.OneColumnVerticalSection) { if (section.Columns.First().Equals(Column)) { controlData.Position.SectionFactor = 12; } } jsonControlData = JsonSerializer.Serialize(controlData); try { var nodeList = new HtmlParser().ParseFragment(Text, null); PreviewText = string.Concat(nodeList.Select(x => x.Text())); } #pragma warning disable CA1031 // Do not catch general exception types catch { } #pragma warning restore CA1031 // Do not catch general exception types StringBuilder html = new StringBuilder(100); html.Append($@"<div {CanvasControlAttribute}=""{CanvasControlData}"" {CanvasDataVersionAttribute}=""{ DataVersion}"" {ControlDataAttribute}=""{jsonControlData.Replace("\"", """)}"">"); html.Append($@"<div {TextRteAttribute}=""{Rte}"">"); if (Text.Trim().StartsWith("<p>", StringComparison.InvariantCultureIgnoreCase) || Text.Trim().StartsWith("<h1>", StringComparison.InvariantCultureIgnoreCase) || Text.Trim().StartsWith("<h2>", StringComparison.InvariantCultureIgnoreCase) || Text.Trim().StartsWith("<h3>", StringComparison.InvariantCultureIgnoreCase) || Text.Trim().StartsWith("<h4>", StringComparison.InvariantCultureIgnoreCase) || Text.Trim().StartsWith("<ul>", StringComparison.InvariantCultureIgnoreCase) || Text.Trim().StartsWith("<blockquote>", StringComparison.InvariantCultureIgnoreCase) || Text.Trim().StartsWith("<pre>", StringComparison.InvariantCultureIgnoreCase)) { html.Append(Text); } else { html.Append($@"<p>{Text}</p>"); } html.Append("</div>"); html.Append("</div>"); return(html.ToString()); }