Perf: Move multiline xml text encoding out of domToText (#4046)

* Move multiline xml text encoding out of domToText
This commit is contained in:
Eric Anderson
2020-07-14 16:32:12 -07:00
committed by GitHub
parent 25821c888d
commit 2b8208b1e2
2 changed files with 27 additions and 11 deletions

View File

@@ -69,6 +69,33 @@ Blockly.FieldMultilineInput.fromJson = function(options) {
return new Blockly.FieldMultilineInput(text, undefined, options);
};
/**
* Serializes this field's value to XML. Should only be called by Blockly.Xml.
* @param {!Element} fieldElement The element to populate with info about the
* field's state.
* @return {!Element} The element containing info about the field's state.
* @package
*/
Blockly.FieldMultilineInput.prototype.toXml = function(fieldElement) {
// Replace '\n' characters with html-escaped equivalent '&#10'. This is
// needed so the plain-text representation of the xml produced by
// `Blockly.Xml.domToText` will appear on a single line (this is a limitation
// of the plain-text format).
fieldElement.textContent = this.getValue().replace(/\n/g, '
');
return fieldElement;
};
/**
* Sets the field's value based on the given XML element. Should only be
* called by Blockly.Xml.
* @param {!Element} fieldElement The element containing info about the
* field's state.
* @package
*/
Blockly.FieldMultilineInput.prototype.fromXml = function(fieldElement) {
this.setValue(fieldElement.textContent.replace(/
/g, '\n'));
};
/**
* Create the block UI for this field.
* @package

View File

@@ -311,17 +311,6 @@ Blockly.Xml.cloneShadow_ = function(shadow, opt_noId) {
*/
Blockly.Xml.domToText = function(dom) {
var text = Blockly.utils.xml.domToText(dom);
// Replace line breaks in text content with '
' to make them single line.
// E.g. <foo>hello\nworld</foo> -> <foo>hello&#10;world</foo>
// Do not replace line breaks between tags.
// E.g. ...</foo>\n</bar> is unchanged.
// Can't use global flag on regexp since backtracking is needed.
var regexp = /(<[^/](?:[^>]*[^/])?>[^<]*)\n([^<]*<\/)/;
var oldText;
do {
oldText = text;
text = text.replace(regexp, '$1&#10;$2');
} while (text != oldText);
// Unpack self-closing tags. These tags fail when embedded in HTML.
// <block name="foo"/> -> <block name="foo"></block>
return text.replace(/<(\w+)([^<]*)\/>/g, '<$1$2></$1>');