TinyMCE cannot insert media player Solution

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.