1 /** 2 * fakeObjects for music ,video,flash 3 * @author yiminghe@gmail.com 4 */ 5 KISSY.add("editor/plugin/fakeObjects/index", function (S, Editor) { 6 var Node = S.Node, 7 DOM = S.DOM, 8 Utils = Editor.Utils, 9 SPACER_GIF = Utils.debugUrl('theme/spacer.gif'), 10 HtmlParser = S.require("htmlparser"); 11 12 S.augment(Editor, { 13 //ie6 ,object outHTML error 14 createFakeElement:function (realElement, className, realElementType, isResizable, outerHTML, attrs) { 15 var style = realElement.attr("style") || ''; 16 if (realElement.attr("width")) { 17 style = "width:" + realElement.attr("width") + "px;" + style; 18 } 19 if (realElement.attr("height")) { 20 style = "height:" + realElement.attr("height") + "px;" + style; 21 } 22 var self = this, 23 // add current class to fake element 24 existClass = S.trim(realElement.attr('class')), 25 attributes = { 26 'class':className + " " + existClass, 27 src:SPACER_GIF, 28 _ke_realelement:encodeURIComponent(outerHTML || realElement._4e_outerHtml(undefined)), 29 _ke_real_node_type:realElement[0].nodeType, 30 //align : realElement.attr("align") || '', 31 style:style 32 }; 33 attrs && delete attrs.width; 34 attrs && delete attrs.height; 35 36 attrs && S.mix(attributes, attrs, false); 37 if (realElementType) 38 attributes._ke_real_element_type = realElementType; 39 40 if (isResizable) 41 attributes._ke_resizable = isResizable; 42 return new Node("<img/>", attributes, self.get("document")[0]); 43 }, 44 45 restoreRealElement:function (fakeElement) { 46 if (fakeElement.attr('_ke_real_node_type') != DOM.ELEMENT_NODE) { 47 return null; 48 } 49 50 var html = (decodeURIComponent(fakeElement.attr('_ke_realelement'))); 51 52 var temp = new Node('<div>', null, this.get("document")[0]); 53 temp.html(html); 54 // When returning the node, remove it from its parent to detach it. 55 return temp.first().remove(); 56 } 57 }); 58 59 60 var htmlFilterRules = { 61 tags:{ 62 /** 63 * 生成最终html时,从编辑器html转化把fake替换为真实,并将style的width,height搞到属性上去 64 * @param element 65 */ 66 $:function (element) { 67 var realHtml = element.getAttribute("_ke_realelement"); 68 69 var realFragment; 70 71 if (realHtml) { 72 realFragment = new HtmlParser.Parser(decodeURIComponent(realHtml)).parse(); 73 } 74 75 var realElement = realFragment && realFragment.childNodes[ 0 ]; 76 77 // If we have width/height in the element, we must move it into 78 // the real element. 79 if (realElement) { 80 var style = element.getAttribute("style"); 81 if (style) { 82 // Get the width from the style. 83 var match = /(?:^|\s)width\s*:\s*(\d+)/i.exec(style), 84 width = match && match[1]; 85 86 // Get the height from the style. 87 match = /(?:^|\s)height\s*:\s*(\d+)/i.exec(style); 88 89 var height = match && match[1]; 90 91 if (width) { 92 realElement.setAttribute("width", width); 93 } 94 if (height) { 95 realElement.setAttribute("height", height); 96 } 97 } 98 return realElement; 99 } 100 101 } 102 } 103 }; 104 105 106 return { 107 init:function (editor) { 108 var dataProcessor = editor.htmlDataProcessor, 109 htmlFilter = dataProcessor && dataProcessor.htmlFilter; 110 111 if (dataProcessor.createFakeParserElement) { 112 return; 113 } 114 115 if (htmlFilter) { 116 htmlFilter.addRules(htmlFilterRules); 117 } 118 119 S.mix(dataProcessor, { 120 /** 121 * 从外边真实的html,转为为编辑器代码支持的替换元素 122 * @param realElement 123 * @param className 124 * @param realElementType 125 * @param [isResizable] 126 * @param [attrs] 127 */ 128 createFakeParserElement:function (realElement, className, realElementType, isResizable, attrs) { 129 var html, 130 writer = new HtmlParser.BasicWriter(); 131 realElement.writeHtml(writer); 132 html = writer.getHtml(); 133 var style = realElement.getAttribute("style") || ''; 134 if (realElement.getAttribute("width")) { 135 style = "width:" + realElement.getAttribute("width") + "px;" + style; 136 } 137 if (realElement.getAttribute("height")) { 138 style = "height:" + realElement.getAttribute("height") + "px;" + style; 139 } 140 // add current class to fake element 141 var existClass = S.trim(realElement.getAttribute("class")), 142 attributes = { 143 'class':className + " " + existClass, 144 src:SPACER_GIF, 145 _ke_realelement:encodeURIComponent(html), 146 _ke_real_node_type:realElement.nodeType + "", 147 style:style, 148 align:realElement.getAttribute("align") || '' 149 }; 150 151 attrs && delete attrs.width; 152 attrs && delete attrs.height; 153 154 attrs && S.mix(attributes, attrs, false); 155 156 if (realElementType) { 157 attributes._ke_real_element_type = realElementType; 158 } 159 if (isResizable) { 160 attributes._ke_resizable = "_ke_resizable"; 161 } 162 return new HtmlParser.Tag('img', attributes); 163 } 164 }); 165 } 166 }; 167 }, { 168 requires:["editor"] 169 }); 170