mirror of
https://github.com/google/blockly.git
synced 2026-01-10 18:37:09 +01:00
Perf: Move multiline xml text encoding out of domToText (#4046)
* Move multiline xml text encoding out of domToText
This commit is contained in:
@@ -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
|
||||
|
||||
11
core/xml.js
11
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. <foo>hello\nworld</foo> -> <foo>hello 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 $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>');
|
||||
|
||||
Reference in New Issue
Block a user