From 356f5ee4ec78e1c538f224acb762c98e75be3d39 Mon Sep 17 00:00:00 2001 From: Alan Date: Fri, 7 Jan 2022 15:58:18 +0800 Subject: [PATCH] sync --- Roo/HtmlEditorCore.js | 21 +++- Roo/htmleditor/Tidy.js | 43 ------- Roo/htmleditor/TidyEntities.js | 39 ++++--- Roo/htmleditor/TidyWriter.js | 74 ++++++------ buildSDK/dependancy_ui.txt | 4 +- docs/src/Roo_HtmlEditorCore.js.html | 19 ++- docs/src/Roo_htmleditor_BlockFigure.js.html | 2 +- docs/src/Roo_htmleditor_Tidy.js.html | 122 ++------------------ examples/form/htmledit.html | 5 +- 9 files changed, 107 insertions(+), 222 deletions(-) delete mode 100644 Roo/htmleditor/Tidy.js diff --git a/Roo/HtmlEditorCore.js b/Roo/HtmlEditorCore.js index 64b7d211fa..59011b8514 100644 --- a/Roo/HtmlEditorCore.js +++ b/Roo/HtmlEditorCore.js @@ -195,14 +195,16 @@ Roo.extend(Roo.HtmlEditorCore, Roo.Component, { st += ''; - - var cls = 'roo-htmleditor-body'; + + st += '' + + var cls = 'notranslate roo-htmleditor-body'; if(this.bodyCls.length){ cls += ' ' + this.bodyCls; } - return '' + st + + return '' + st + //' + @@ -329,7 +331,8 @@ Roo.extend(Roo.HtmlEditorCore, Roo.Component, { * @param {String} html The HTML to be cleaned * return {String} The cleaned HTML */ - cleanHtml : function(html){ + cleanHtml : function(html) + { html = String(html); if(html.length > 5){ if(Roo.isSafari){ // strip safari nonsense @@ -361,15 +364,19 @@ Roo.extend(Roo.HtmlEditorCore, Roo.Component, { var div = document.createElement('div'); div.innerHTML = bd.innerHTML; - + + if (this.enableBlocks) { new Roo.htmleditor.FilterBlock({ node : div }); } //?? tidy? + var tidy = new Roo.htmleditor.TidySerializer({ + inner: true + }); + var html = tidy.serialize(div) - var html = div.innerHTML; if(Roo.isSafari){ var bs = bd.getAttribute('style'); // Safari puts text-align styles on the body element! var m = bs ? bs.match(/text-align:(.*?);/i) : false; @@ -508,6 +515,8 @@ Roo.extend(Roo.HtmlEditorCore, Roo.Component, { //var ss = this.el.getStyles( 'background-image', 'background-repeat'); //ss['background-attachment'] = 'fixed'; // w3c dbody.bgProperties = 'fixed'; // ie + dbody.setAttribute("translate", "no"); + //Roo.DomHelper.applyStyles(dbody, ss); Roo.EventManager.on(this.doc, { diff --git a/Roo/htmleditor/Tidy.js b/Roo/htmleditor/Tidy.js deleted file mode 100644 index 00af29c2f9..0000000000 --- a/Roo/htmleditor/Tidy.js +++ /dev/null @@ -1,43 +0,0 @@ - -/** - * @class Roo.htmleditor.Tidy - * Tidy HTML - * - * @constructor - * Create a new Filter. - * @param {Object} config Configuration options - */ - - -Roo.htmleditor.Tidy = function(cfg) { - Roo.apply(this, cfg); - - this.core.doc.body.innerHTML = this.tidy(this.core.doc.body, ''); - -} - -Roo.htmleditor.Tidy.toString = function(node) -{ - return Roo.htmleditor.Tidy.prototype.tidy(node, ''); -} - -Roo.htmleditor.Tidy.prototype = { - - /** - * - * @cfg {Roo.HtmlEditorCore} core the editor. - */ - core : false, - - - - tidy : function(node, indent) { - var ser = new Roo.htmleditor.TidySerializer({ - indent : 2, - }); - return ser.serialize(node); - - - } - -} \ No newline at end of file diff --git a/Roo/htmleditor/TidyEntities.js b/Roo/htmleditor/TidyEntities.js index 5db0ea6723..fef4683994 100644 --- a/Roo/htmleditor/TidyEntities.js +++ b/Roo/htmleditor/TidyEntities.js @@ -3,6 +3,8 @@ * @class Roo.htmleditor.TidyEntities * @static * https://github.com/thorn0/tinymce.html/blob/master/tinymce.html.js + * + * Not 100% sure this is actually used or needed. */ Roo.htmleditor.TidyEntities = { @@ -11,32 +13,31 @@ Roo.htmleditor.TidyEntities = { * initialize data.. */ init : function (){ - - - - this.namedEntities = buildEntitiesLookup(this.namedEntitiesData.join(','), 32); + + this.namedEntities = this.buildEntitiesLookup(this.namedEntitiesData, 32); }, - buildEntitiesLookup: function(items, radix) { + buildEntitiesLookup: function(items, radix) { var i, chr, entity, lookup = {}; - if (items) { - items = items.split(','); - radix = radix || 10; - // Build entities lookup table - for (i = 0; i < items.length; i += 2) { - chr = String.fromCharCode(parseInt(items[i], radix)); - // Only add non base entities - if (!baseEntities[chr]) { - entity = '&' + items[i + 1] + ';'; - lookup[chr] = entity; - lookup[entity] = chr; - } + if (!items) { + return {}; + } + items = typeof(items) == 'string' ? items.split(',') : items; + radix = radix || 10; + // Build entities lookup table + for (i = 0; i < items.length; i += 2) { + chr = String.fromCharCode(parseInt(items[i], radix)); + // Only add non base entities + if (!this.baseEntities[chr]) { + entity = '&' + items[i + 1] + ';'; + lookup[chr] = entity; + lookup[entity] = chr; } - return lookup; } - return {}; + return lookup; + }, asciiMap : { diff --git a/Roo/htmleditor/TidyWriter.js b/Roo/htmleditor/TidyWriter.js index 4d23dcd90e..dfb1144dd7 100644 --- a/Roo/htmleditor/TidyWriter.js +++ b/Roo/htmleditor/TidyWriter.js @@ -5,7 +5,7 @@ * * Known issues? * - not tested much with 'PRE' formated elements. - * - long text inside of inline can be wrapped and clened? + * * * */ @@ -23,23 +23,7 @@ Roo.htmleditor.TidyWriter = function(settings) } Roo.htmleditor.TidyWriter.prototype = { - - - makeMap : function (items, delim, map) { - var i; - items = items || []; - delim = delim || ','; - if (typeof items == "string") { - items = items.split(delim); - } - map = map || {}; - i = items.length; - while (i--) { - map[items[i]] = {}; - } - return map; - }, - + state : false, indent : ' ', @@ -208,11 +192,17 @@ Roo.htmleditor.TidyWriter.prototype = { if (text.length < 1) { return; } - if (this.in_pre || this.in_inline) { - - if (this.in_inline) { - text = text.replace(/\s/g,' ') // all line breaks to ' ' - .replace(/\s+/,' ') // all white space to single white space + if (this.in_pre) { + this.html[this.html.length] = text; + return; + } + + if (this.in_inline) { + text = text.replace(/\s+/g,' ') // all white space inc line breaks to a slingle' ' + if (text != ' ') { + text = text.replace(/\s+/,' ') // all white space to single white space + + // if next tag is '
', then we can trim right.. if (node.nextSibling && node.nextSibling.nodeType == 1 && @@ -227,35 +217,51 @@ Roo.htmleditor.TidyWriter.prototype = { { text = this.indentstr + text.replace(/^\s+/g,''); } + if (text.match(/\n/)) { + text = text.replace( + /(?![^\n]{1,64}$)([^\n]{1,64})\s/g, '$1\n' + this.indentstr + ); + // remoeve the last whitespace / line break. + text = text.replace(/\n\s+$/,''); + } // repace long lines - text = this.indentstr + text.replace( - /(?![^\n]{1,64}$)([^\n]{1,64})\s/g, '$1\n' + this.indentstr - ); - // remoeve the last whitespace / line break. - text = text.replace(/\s+$/,''); - - } + this.html[this.html.length] = text; return; } - // see if last element was a inline element. + // see if previous element was a inline element. var indentstr = this.indentstr; + + text = text.replace(/\s+/g," "); // all whitespace into single white space. + + // should trim left? if (node.previousSibling && node.previousSibling.nodeType == 1 && Roo.htmleditor.TidyWriter.inline_elements.indexOf(node.previousSibling.nodeName) > -1) { indentstr = ''; + } else { this.addLine(); + text = text.replace(/^\s+/,''); // trim left + } + // should trim right? + if (node.nextSibling && + node.nextSibling.nodeType == 1 && + Roo.htmleditor.TidyWriter.inline_elements.indexOf(node.nextSibling.nodeName) > -1) + { + // noop + } else { + text = text.replace(/\s+$/,''); // trim right + } + + - text = text.replace(/\s/g," ") // all line breaks to ' ' - .replace(/^\s+/,'') // leding white space - .replace(/\s+$/,''); // clean trailing white space if (text.length < 1) { return; diff --git a/buildSDK/dependancy_ui.txt b/buildSDK/dependancy_ui.txt index c969f982ca..33afdcad9f 100644 --- a/buildSDK/dependancy_ui.txt +++ b/buildSDK/dependancy_ui.txt @@ -117,7 +117,9 @@ Roo.htmleditor.FilterWord Roo.htmleditor.FilterStyleToTag Roo.htmleditor.FilterLongBr Roo.htmleditor.FilterBlock -Roo.htmleditor.Tidy +Roo.htmleditor.TidySerializer +Roo.htmleditor.TidyWriter +Roo.htmleditor.TidyEntities Roo.htmleditor.KeyEnter Roo.htmleditor.Block diff --git a/docs/src/Roo_HtmlEditorCore.js.html b/docs/src/Roo_HtmlEditorCore.js.html index 0168750f01..3f466c6a24 100644 --- a/docs/src/Roo_HtmlEditorCore.js.html +++ b/docs/src/Roo_HtmlEditorCore.js.html @@ -196,13 +196,15 @@ 'IMG { cursor: pointer } ' + '</style>'; - var cls = 'roo-htmleditor-body'; + st += '<meta name="google" content="notranslate">' + + var cls = 'notranslate roo-htmleditor-body'; if(this.bodyCls.length){ cls += ' ' + this.bodyCls; } - return '<html><head>' + st + + return '<html class="notranslate" translate="no"><head>' + st + //<style type="text/css">' + //'body{border:0;margin:0;padding:3px;height:98%;cursor:text;}' + //'</style>' + @@ -329,7 +331,8 @@ * @param {String} html The HTML to be cleaned * return {String} The cleaned HTML */ - cleanHtml : function(html){ + cleanHtml : function(html) + { html = String(html); if(html.length > 5){ if(Roo.isSafari){ // strip safari nonsense @@ -363,13 +366,17 @@ div.innerHTML = bd.innerHTML; + if (this.enableBlocks) { new Roo.htmleditor.FilterBlock({ node : div }); } //?? tidy? + var tidy = new Roo.htmleditor.TidySerializer({ + inner: true + }); + var html = tidy.serialize(div) - var html = div.innerHTML; if(Roo.isSafari){ var bs = bd.getAttribute('style'); // Safari puts text-align styles on the body element! var m = bs ? bs.match(/text-align:(.*?);/i) : false; @@ -508,7 +515,9 @@ //var ss = this.el.getStyles( 'background-image', 'background-repeat'); //ss['background-attachment'] = 'fixed'; // w3c dbody.bgProperties = 'fixed'; // ie - //Roo.DomHelper.applyStyles(dbody, ss); + dbody.setAttribute("translate", "no"); + + //Roo.DomHelper.applyStyles(dbody, ss); Roo.EventManager.on(this.doc, { 'mouseup': this.onEditorEvent, diff --git a/docs/src/Roo_htmleditor_BlockFigure.js.html b/docs/src/Roo_htmleditor_BlockFigure.js.html index d4e41d6496..eaf6609c3c 100644 --- a/docs/src/Roo_htmleditor_BlockFigure.js.html +++ b/docs/src/Roo_htmleditor_BlockFigure.js.html @@ -205,7 +205,7 @@ tag : 'img', contenteditable : 'false', src : this.image_src, - alt : d.innerText.replace(/\n/g, " "), // removeHTML.. + alt : d.innerText.replace(/\n/g, " ").replace(/\s+/g, ' ').trim(), // removeHTML and reduce spaces.. style: { width : 'auto', 'max-width': '100%', diff --git a/docs/src/Roo_htmleditor_Tidy.js.html b/docs/src/Roo_htmleditor_Tidy.js.html index 597ca0bb10..9c2f205cf4 100644 --- a/docs/src/Roo_htmleditor_Tidy.js.html +++ b/docs/src/Roo_htmleditor_Tidy.js.html @@ -2,7 +2,7 @@ /** * @class Roo.htmleditor.Tidy * Tidy HTML - * @cfg {Roo.HtmlEditorCore} core the editor. + * * @constructor * Create a new Filter. * @param {Object} config Configuration options @@ -23,123 +23,21 @@ Roo.htmleditor.Tidy.prototype = { + /** + * + * @cfg {Roo.HtmlEditorCore} core the editor. + */ + core : false, - wrap : function(s) { - return s.replace(/\n/g, " ").replace(/(?![^\n]{1,80}$)([^\n]{1,80})\s/g, '$1\n'); - }, tidy : function(node, indent) { + var ser = new Roo.htmleditor.TidySerializer({ + indent : 2, + }); + return ser.serialize(node); - if (node.nodeType == 3) { - // text. - - - return indent === false ? node.nodeValue : this.wrap(node.nodeValue.trim()).split("\n").join("\n" + indent); - - - } - - if (node.nodeType != 1) { - return ''; - } - - - - if (node.tagName == 'BODY') { - - return this.cn(node, ''); - } - - // Prints the node tagName, such as <A>, <IMG>, etc - var ret = "<" + node.tagName + this.attr(node) ; - - // elements with no children.. - if (['IMG', 'BR', 'HR', 'INPUT'].indexOf(node.tagName) > -1) { - return ret + '/>'; - } - ret += '>'; - - - var cindent = indent === false ? '' : (indent + ' '); - // tags where we will not pad the children.. (inline text tags etc..) - if (['PRE', 'TEXTAREA', 'TD', 'A', 'SPAN', 'B', 'I', 'S'].indexOf(node.tagName) > -1) { // or code? - cindent = false; - - - } - - var cn = this.cn(node, cindent ); - - return ret + cn + '</' + node.tagName + '>'; - - }, - cn: function(node, indent) - { - var ret = []; - - var ar = Array.from(node.childNodes); - for (var i = 0 ; i < ar.length ; i++) { - - - - if (indent !== false // indent==false preservies everything - && i > 0 - && ar[i].nodeType == 3 - && ar[i].nodeValue.length > 0 - && ar[i].nodeValue.match(/^\s+/) - ) { - if (ret.length && ret[ret.length-1] == "\n" + indent) { - ret.pop(); // remove line break from last? - } - - ret.push(" "); // add a space if i'm a text item with a space at the front, as tidy will strip spaces. - } - if (indent !== false - && ar[i].nodeType == 1 // element - and indent is not set... - ) { - ret.push("\n" + indent); - } - - ret.push(this.tidy(ar[i], indent)); - // text + trailing indent - if (indent !== false - && ar[i].nodeType == 3 - && ar[i].nodeValue.length > 0 - && ar[i].nodeValue.match(/\s+$/) - ){ - ret.push("\n" + indent); - } - - - - - } - // what if all text? - - - return ret.join(''); - }, - - - - attr : function(node) - { - var attr = []; - for(i = 0; i < node.attributes.length;i++) { - - // skip empty values? - if (!node.attributes.item(i).value.length) { - continue; - } - attr.push( node.attributes.item(i).name + '="' + - Roo.util.Format.htmlEncode(node.attributes.item(i).value) + '"' - ); - } - return attr.length ? (' ' + attr.join(' ') ) : ''; } - - } \ No newline at end of file diff --git a/examples/form/htmledit.html b/examples/form/htmledit.html index 18976775e4..23ebd7d98a 100644 --- a/examples/form/htmledit.html +++ b/examples/form/htmledit.html @@ -77,7 +77,10 @@ - + + + + -- 2.39.2