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

logic_compare: Type-verification creates top level shadow block as a side effect #1408

Closed
3 of 10 tasks
samelhusseini opened this issue Oct 31, 2017 · 8 comments · Fixed by #1782
Closed
3 of 10 tasks
Labels
issue: bug Describes why the code or behaviour is wrong

Comments

@samelhusseini
Copy link
Contributor

samelhusseini commented Oct 31, 2017

Problem statement

Dragging a block that's an incompatible type bumps the block and then creates a top level shadow block as a sideeffect.

  • Bug report
  • Feature request

Expected Behavior

The text block should be bumped, with no new (top level) shadow block created.

Actual Behavior

loneshadow

The text block is bumped and a new (top level) shadow block appears in the workspace. This is consistent, and happens for every bump.

Also happens when inserting a Color block into a Number field, but doesn't happen when inserting a number block into a String field.

Steps to Reproduce

Take this block XML and try to insert a text block inside of the LHS of the number comparator.

<xml xmlns="http://www.w3.org/1999/xhtml">
  <variables></variables>
  <block type="controls_if" id="(j:IsX.#.-!m#~^cjkq0" x="159" y="333">
    <mutation else="1"></mutation>
    <value name="IF0">
      <block type="logic_compare" id="aNPP`-9t{F%Nj47Xg=kV">
        <field name="OP">EQ</field>
        <value name="A">
          <shadow type="math_number" id="0VN(._,,t6|xuWU(JI+U">
            <field name="NUM">10</field>
          </shadow>
        </value>
        <value name="B">
          <shadow type="math_number" id="6%e5FECiW9q9{P53ny6E">
            <field name="NUM">10</field>
          </shadow>
        </value>
      </block>
    </value>
  </block>
</xml>

Stack Traces

Here's the generated XML as a result of this:

<xml xmlns="http://www.w3.org/1999/xhtml">
  <variables></variables>
  <shadow type="math_number" id="6%e5FECiW9q9{P53ny6E" x="372" y="339">
    <field name="NUM">10</field>
  </shadow>
  <block type="text" id=")-3pKY(#Zk_m%uMs$7Xv" x="213" y="363">
    <field name="TEXT"></field>
  </block>
  <block type="controls_if" id="(j:IsX.#.-!m#~^cjkq0" x="138" y="388">
    <mutation else="1"></mutation>
    <value name="IF0">
      <block type="logic_compare" id="aNPP`-9t{F%Nj47Xg=kV">
        <field name="OP">EQ</field>
        <value name="A">
          <shadow type="math_number" id="0VN(._,,t6|xuWU(JI+U">
            <field name="NUM">10</field>
          </shadow>
        </value>
        <value name="B">
          <shadow type="math_number" id="=-.oL;GbFGo;omj9Fldg">
            <field name="NUM">10</field>
          </shadow>
        </value>
      </block>
    </value>
  </block>
</xml>

Operating System and Browser

  • Desktop:
    • Chrome
    • Firefox
    • Safari
    • Opera
    • IE 10+
    • IE 11
    • EDGE

Affects both develop and master

@rachel-fenichel rachel-fenichel added the issue: bug Describes why the code or behaviour is wrong label Nov 1, 2017
@picklesrus
Copy link
Contributor

Oooh. Neat bug! The fact that the shadow gets bumped onto the workspace is definitely a problem.

Beyond that part, this is tricky. When you say you expect the text block to be bumped, do you mean it should never have connected in the first place?

What types do you want to be able to compare using the == block? Do you want to be able to compare a number and a string and use a cast in the generator?

I suspect the == block in the playground doesn't have shadows at all to avoid dealing with the complexity of switching types like this :)

@samelhusseini
Copy link
Contributor Author

samelhusseini commented Nov 2, 2017

Yeah I don't think there's any issue with the connecting logic. The bug is just that it bumps the shadow out onto the workspace.

Although I do agree, it would be nice to have a union type for the equality block since you should be able to compare two strings, or a string and a number (false?), but that's a different story. (And perhaps this isn't a Blockly thing, and can be solved by having a union (string | number) block).

@NeilFraser
Copy link
Contributor

I suspect the == block in the playground doesn't have shadows at all to avoid dealing with the complexity of switching types like this

Actually, this (awesome) bug exists because it never occurred to me to test the intersection of =='s custom connection logic and shadow blocks. :)

I'd vote to refuse connect a non-compatible block if the conflicting block is a shadow or unmovable. After all, if the developer places a shadow block in a == block, then I feel they have strongly defined the type. This bug may also affect the ?: ternary block as well.

@AnmAtAnm AnmAtAnm changed the title Bump logic creates top level shadow block as a sideeffect logic_compare: Type-verification creates top level shadow block as a side effect Apr 11, 2018
@AnmAtAnm
Copy link
Contributor

Neil's solution implies the toolbox would need to either remove shadow blocks, or have a comparison operator for every type (number, string, list, etc).

This toolbox change is going to affect anyone who has copied a demo toolbox since we introduced shadows. If we implement Neil's suggestion, and a developer does not update their toolbox shadows, their users will not be able to compare strings.

A more compatible approach would be to delete the shadow if it is incompatible.

A more user-friendly variant (but much more complicated) might replace include special handling of the number and string type cases. When the new connecting block is a text and the old is a math_number shadow, replace the shadow with a text block with the string form of the number. When a math_number attempts to compare to a text shadow, coerce the text into a number (or 0) shadow.

In this case, for other unrecognized types we could implement either shadow deletion or connection invalidation.

In terms of the demo toolboxes, the separation of number comparisons and string comparisons makes me feel the blocks belong in the Math and Text categories, instead of the logic category. (Or remove the shadows, as I previously mentioned.)

@AnmAtAnm
Copy link
Contributor

Correction. Blockly toolbox does not (and presumably has not) used shadows in logic_compare.

@AnmAtAnm
Copy link
Contributor

AnmAtAnm commented Apr 12, 2018

More weirdness:

Before

screen shot 2018-04-12 at 3 40 18 pm

After

screen shot 2018-04-12 at 3 40 32 pm

The behavior depends upon other logic_compare blocks on the workspace.

AnmAtAnm added a commit to AnmAtAnm/blockly that referenced this issue Apr 13, 2018
 * Create prevBlock_ upon first call to onchange.
 * Revert state upon an incompatible combination, bumping the new incompatible
   block, instead of the old block. Thus, the shadow is never the bumped block.

Bug:
 * The undo stack get caught in a loop, and will never undo back to a state
   equivalent to the previous action.
rachel-fenichel added a commit that referenced this issue Apr 14, 2018
Rewrote LOGIC_COMPARE_ONCHANGE_MIXIN to fix #1408.
@plh97
Copy link

plh97 commented Jun 5, 2020

why you did not support === or !==

@plh97
Copy link

plh97 commented Jun 5, 2020

image
in javascript syntax it's need to support

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue: bug Describes why the code or behaviour is wrong
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants