Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

text: Skip setting HTML text when it hasn't changed #19012

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions core/src/display_object/edit_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,13 @@ impl<'gc> EditText<'gc> {
}

pub fn set_text(self, text: &WStr, context: &mut UpdateContext<'gc>) {
if self.text() == text {
// Note: this check not only prevents text relayout,
// but it also has observable effects, because text
// format is not being reset to the default format.
return;
}

let mut edit_text = self.0.write(context.gc_context);
let default_format = edit_text.text_spans.default_format().clone();
edit_text.text_spans = FormatSpans::from_text(text.into(), default_format);
Expand All @@ -414,6 +421,16 @@ impl<'gc> EditText<'gc> {
}

pub fn set_html_text(self, text: &WStr, context: &mut UpdateContext<'gc>) {
if self.html_text() == text {
// Note: this check not only prevents text relayout,
// but it also has observable effects, because not
// every set of spans is representable as HTML.
//
// For instance, a paragraph may not end with a newline,
// but its HTML representation will always infer one.
return;
}

if self.is_html() {
let mut write = self.0.write(context.gc_context);
let default_format = write.text_spans.default_format().clone();
Expand Down
1 change: 1 addition & 0 deletions core/src/html/text_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,7 @@ impl FormatSpans {
b"p" => {
p_open = true;
if let Some(align) = attribute(b"align") {
let align = align.to_ascii_lowercase();
if align == WStr::from_units(b"left") {
format.align = Some(swf::TextAlign::Left)
} else if align == WStr::from_units(b"center") {
Expand Down
4 changes: 4 additions & 0 deletions tests/tests/swfs/avm2/edittext_html/Test.as
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ public class Test extends Sprite {
runTest(text, '<p align="justify">text</p>');
runTest(text, '<p align="center">text</p>');
runTest(text, '<p align="unknown">text</p>');
runTest(text, '<p align="Left">text</p>');
runTest(text, '<p ALIGN="LEFT">text</p>');
runTest(text, '<p ALIGN="RiGHT">text</p>');
runTest(text, '<p ALIGN="CENtEr">text</p>');
runTest(text, '<a href="http://example.com"><p align="right"></p></a>');
runTest(text, '<a href="http://example.com"><p align="right">text</p></a>');
runTest(text, '<textformat leading="1"><p align="right"></p></textformat>');
Expand Down
40 changes: 40 additions & 0 deletions tests/tests/swfs/avm2/edittext_html/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2007,6 +2007,46 @@ Default multiline: false
Text runs (1):
from 0 to 5: size=12, blockIndent=0, font=Times, align=left, leading=0, display=block, kerning=false, leftMargin=0, rightMargin=0, color=0, bold=false, italic=false, bullet=false, underline=false
===============
HTML set: <p align="Left">text</p>
HTML get: <P ALIGN="LEFT"><FONT FACE="Times" SIZE="12" COLOR="#000000" LETTERSPACING="0" KERNING="0">text</FONT></P>
Text get: text
Text runs (1):
from 0 to 4: size=12, blockIndent=0, font=Times, align=left, leading=0, display=block, kerning=false, leftMargin=0, rightMargin=0, color=0, bold=false, italic=false, bullet=false, underline=false
HTML get ml: <!-- the same -->
Text get: text\r
Text runs (1):
from 0 to 5: size=12, blockIndent=0, font=Times, align=left, leading=0, display=block, kerning=false, leftMargin=0, rightMargin=0, color=0, bold=false, italic=false, bullet=false, underline=false
===============
HTML set: <p ALIGN="LEFT">text</p>
HTML get: <P ALIGN="LEFT"><FONT FACE="Times" SIZE="12" COLOR="#000000" LETTERSPACING="0" KERNING="0">text</FONT></P>
Text get: text
Text runs (1):
from 0 to 4: size=12, blockIndent=0, font=Times, align=left, leading=0, display=block, kerning=false, leftMargin=0, rightMargin=0, color=0, bold=false, italic=false, bullet=false, underline=false
HTML get ml: <!-- the same -->
Text get: text\r
Text runs (1):
from 0 to 5: size=12, blockIndent=0, font=Times, align=left, leading=0, display=block, kerning=false, leftMargin=0, rightMargin=0, color=0, bold=false, italic=false, bullet=false, underline=false
===============
HTML set: <p ALIGN="RiGHT">text</p>
HTML get: <P ALIGN="RIGHT"><FONT FACE="Times" SIZE="12" COLOR="#000000" LETTERSPACING="0" KERNING="0">text</FONT></P>
Text get: text
Text runs (1):
from 0 to 4: size=12, blockIndent=0, font=Times, align=right, leading=0, display=block, kerning=false, leftMargin=0, rightMargin=0, color=0, bold=false, italic=false, bullet=false, underline=false
HTML get ml: <!-- the same -->
Text get: text\r
Text runs (1):
from 0 to 5: size=12, blockIndent=0, font=Times, align=right, leading=0, display=block, kerning=false, leftMargin=0, rightMargin=0, color=0, bold=false, italic=false, bullet=false, underline=false
===============
HTML set: <p ALIGN="CENtEr">text</p>
HTML get: <P ALIGN="CENTER"><FONT FACE="Times" SIZE="12" COLOR="#000000" LETTERSPACING="0" KERNING="0">text</FONT></P>
Text get: text
Text runs (1):
from 0 to 4: size=12, blockIndent=0, font=Times, align=center, leading=0, display=block, kerning=false, leftMargin=0, rightMargin=0, color=0, bold=false, italic=false, bullet=false, underline=false
HTML get ml: <!-- the same -->
Text get: text\r
Text runs (1):
from 0 to 5: size=12, blockIndent=0, font=Times, align=center, leading=0, display=block, kerning=false, leftMargin=0, rightMargin=0, color=0, bold=false, italic=false, bullet=false, underline=false
===============
HTML set: <a href="http://example.com"><p align="right"></p></a>
HTML get:
Text get:
Expand Down
Binary file modified tests/tests/swfs/avm2/edittext_html/test.swf
Binary file not shown.
48 changes: 48 additions & 0 deletions tests/tests/swfs/avm2/edittext_set_html_same/Test.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package {
import flash.display.*;
import flash.text.*;
import flash.events.*;
import flash.geom.*;

public class Test extends MovieClip {
public function Test() {
var tf = new TextField();
tf.multiline = true;
tf.defaultTextFormat = new TextFormat("Unknown Font");
tf.htmlText = "<p align=\"center\">x</p>";

trace("Before newline removal:");
trace(" " + tf.text.length);

tf.replaceText(1, 2, "");

trace("After newline removal:");
trace(" " + tf.text.length);
trace(" " + tf.htmlText);

tf.htmlText = "<P ALIGN=\"CENTER\"><FONT FACE=\"Unknown Font\" SIZE=\"12\" COLOR=\"#000000\" LETTERSPACING=\"0\" KERNING=\"0\">x</FONT></P>";

trace("After setting to the same value:");
trace(" " + tf.text.length);
trace(" " + tf.htmlText);

tf.htmlText = "<P ALIGN=\"CENTER\"><FONT FACE=\"Unknown Font\" SIZE=\"12\" COLOR=\"#000000\" LETTERSPACING=\"0\" KERNING=\"0\">x</FONT></P>";

trace("After setting to a slightly different value:");
trace(" " + tf.text.length);
trace(" " + tf.htmlText);

tf.htmlText = "<font face='Unknown Font 2'><b>x</b></font>";

trace("After setting to HTML x:");
trace(" " + tf.text.length);
trace(" " + tf.htmlText);

tf.text = "x";

trace("After setting text to x:");
trace(" " + tf.text.length);
trace(" " + tf.htmlText);
}
}
}
17 changes: 17 additions & 0 deletions tests/tests/swfs/avm2/edittext_set_html_same/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Before newline removal:
2
After newline removal:
1
<P ALIGN="CENTER"><FONT FACE="Unknown Font" SIZE="12" COLOR="#000000" LETTERSPACING="0" KERNING="0">x</FONT></P>
After setting to the same value:
1
<P ALIGN="CENTER"><FONT FACE="Unknown Font" SIZE="12" COLOR="#000000" LETTERSPACING="0" KERNING="0">x</FONT></P>
After setting to a slightly different value:
2
<P ALIGN="CENTER"><FONT FACE="Unknown Font" SIZE="12" COLOR="#000000" LETTERSPACING="0" KERNING="0">x</FONT></P>
After setting to HTML x:
1
<P ALIGN="LEFT"><FONT FACE="Unknown Font 2" SIZE="12" COLOR="#000000" LETTERSPACING="0" KERNING="0"><B>x</B></FONT></P>
After setting text to x:
1
<P ALIGN="LEFT"><FONT FACE="Unknown Font 2" SIZE="12" COLOR="#000000" LETTERSPACING="0" KERNING="0"><B>x</B></FONT></P>
Binary file not shown.
1 change: 1 addition & 0 deletions tests/tests/swfs/avm2/edittext_set_html_same/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
num_ticks = 1