When TinyMCE is used to insert media code, the original HTML is replaced with the img tag, and the original data is converted to json data. For example:
Original HTML clip:
<p><object width="320" height="240" data="http://www.sin.com/sss.swf" type="application/x-shockwave-flash"><param name="src" value="http://www.sin.com/sss.swf" /></object></p>
HTML snippets formatted by TinyMCE:
I went to the official website and stackoverflow for a long time. The solution to this problem seems to be to configure the valid_elements attribute of TinyMCE, but I have tried none of them.
The final solution is to adopt a curve to save the country. The general idea is as follows:
HtmlAgilityPack is used to parse the obtained editor content.
Extract matching video elements
Replace this element with formatted HTML media elements, such as <object>
Key points:
1. XPATH expression // img [@ class = "mceItemMedia mceItemFlash"]
2. The conversion from a json string to A. net object does not need to be defined separately. I tried the json to dynamic type and it turned out to be OK. It seems that the json. Net class library is very powerful.
Paste the conversion code. When multiple matching elements exist, traverse the HtmlAgilityPack. the HtmlNode method has never been successful. I don't know whether it is correct or the problem of HtmlAgilityPack. In the end, the string replacement method is used.
Private string HandleMediaContent (string content)
{
Var reStr = string. Empty;
ReStr = content;
String template = "<object width =" {0} "height =" {1} "align =" middle "data =" {2} "type =" application/x-shockwave -flash "> <param name =" src "value =" {3} "/> <param name =" allowfullscreen "value =" true "/> <param name =" quality "value =" high "/> <param name =" allowscriptaccess "value =" always "/> </object> ";
HtmlAgilityPack. HtmlDocument doc = new HtmlAgilityPack. HtmlDocument ();
Doc. LoadHtml (content );
Var vNodes = doc. DocumentNode. SelectNodes ("// img [@ class =" mceItemMedia mceItemFlash "]");
If (vNodes = null)
Return content;
For (var I = 0; I <vNodes. Count; I ++)
{
// Var parent = vNodes [I]. ParentNode;
Var attr = vNodes [I]. Attributes. FirstOrDefault (t => t. Name = "data-mce-json ");
Var width = vNodes [I]. Attributes. FirstOrDefault (t => t. Name = "width ");
Var height = vNodes [I]. Attributes. FirstOrDefault (t => t. Name = "height ");
Var obj = Newtonsoft. Json. JsonConvert. DeserializeObject <dynamic> (attr. Value );
Var newNodeDocument = new HtmlAgilityPack. HtmlDocument ();
NewNodeDocument. LoadHtml (string. Format (template, width. Value, height. Value, obj.@params.src, obj.@params.src ));
// Parent. InsertBefore (newNodeDocument. DocumentNode, vNodes [I]);
// Parent. RemoveChild (vNodes [I]);
ReStr = reStr. Replace (vNodes [I]. OuterHtml, newNodeDocument. DocumentNode. OuterHtml );
}
// Return doc. DocumentNode. OuterHtml;
Return reStr;
}