From 2b8208b1e2fa461cbd598791ee85d912b92b894d Mon Sep 17 00:00:00 2001 From: Eric Anderson Date: Tue, 14 Jul 2020 16:32:12 -0700 Subject: [PATCH] Perf: Move multiline xml text encoding out of domToText (#4046) * Move multiline xml text encoding out of domToText --- core/field_multilineinput.js | 27 +++++++++++++++++++++++++++ core/xml.js | 11 ----------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/core/field_multilineinput.js b/core/field_multilineinput.js index 4b5ccd741..d87c3e3ad 100644 --- a/core/field_multilineinput.js +++ b/core/field_multilineinput.js @@ -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 ' '. 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 diff --git a/core/xml.js b/core/xml.js index 45b3e0ca6..81f49fa1b 100644 --- a/core/xml.js +++ b/core/xml.js @@ -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. hello\nworld -> hello world - // Do not replace line breaks between tags. - // E.g. ...\n 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 $2'); - } while (text != oldText); // Unpack self-closing tags. These tags fail when embedded in HTML. // -> return text.replace(/<(\w+)([^<]*)\/>/g, '<$1$2>');