How to use right method in Testcafe

Best JavaScript code snippet using testcafe

typing.test.js

Source:typing.test.js Github

copy

Full Screen

1suite('typing with auto-replaces', function() {2 var mq;3 setup(function() {4 mq = MQ.MathField($('<span></span>').appendTo('#mock')[0]);5 });6 function prayWellFormedPoint(pt) { prayWellFormed(pt.parent, pt[L], pt[R]); }7 function assertLatex(latex) {8 prayWellFormedPoint(mq.__controller.cursor);9 assert.equal(mq.latex(), latex);10 }11 suite('LiveFraction', function() {12 test('full MathQuill', function() {13 mq.typedText('1/2').keystroke('Tab').typedText('+sinx/');14 assertLatex('\\frac{1}{2}+\\frac{\\sin x}{ }');15 mq.latex('').typedText('1+/2');16 assertLatex('1+\\frac{2}{ }');17 mq.latex('').typedText('1 2/3');18 assertLatex('1\\ \\frac{2}{3}');19 });20 test('mathquill-basic', function() {21 var mq_basic = MQBasic.MathField($('<span></span>').appendTo('#mock')[0]);22 mq_basic.typedText('1/2');23 assert.equal(mq_basic.latex(), '\\frac{1}{2}');24 });25 });26 suite('LatexCommandInput', function() {27 test('basic', function() {28 mq.typedText('\\sqrt-x');29 assertLatex('\\sqrt{-x}');30 });31 test('they\'re passed their name', function() {32 mq.cmd('\\alpha');33 assert.equal(mq.latex(), '\\alpha');34 });35 test('replaces selection', function() {36 mq.typedText('49').select().typedText('\\sqrt').keystroke('Enter');37 assertLatex('\\sqrt{49}');38 });39 test('auto-operator names', function() {40 mq.typedText('\\sin^2');41 assertLatex('\\sin^2');42 });43 test('nonexistent LaTeX command', function() {44 mq.typedText('\\asdf+');45 assertLatex('\\text{asdf}+');46 });47 test('dollar sign', function() {48 mq.typedText('$');49 assertLatex('\\$');50 });51 test('\\text followed by command', function() {52 mq.typedText('\\text{');53 assertLatex('\\text{\\{}');54 });55 });56 suite('auto-expanding parens', function() {57 suite('simple', function() {58 test('empty parens ()', function() {59 mq.typedText('(');60 assertLatex('\\left(\\right)');61 mq.typedText(')');62 assertLatex('\\left(\\right)');63 });64 test('straight typing 1+(2+3)+4', function() {65 mq.typedText('1+(2+3)+4');66 assertLatex('1+\\left(2+3\\right)+4');67 });68 test('basic command \\sin(', function () {69 mq.typedText('\\sin(');70 assertLatex('\\sin\\left(\\right)');71 });72 test('wrapping things in parens 1+(2+3)+4', function() {73 mq.typedText('1+2+3+4');74 assertLatex('1+2+3+4');75 mq.keystroke('Left Left').typedText(')');76 assertLatex('\\left(1+2+3\\right)+4');77 mq.keystroke('Left Left Left Left').typedText('(');78 assertLatex('1+\\left(2+3\\right)+4');79 });80 test('nested parens 1+(2+(3+4)+5)+6', function() {81 mq.typedText('1+(2+(3+4)+5)+6');82 assertLatex('1+\\left(2+\\left(3+4\\right)+5\\right)+6');83 });84 });85 suite('mismatched brackets', function() {86 test('empty mismatched brackets (] and [}', function() {87 mq.typedText('(');88 assertLatex('\\left(\\right)');89 mq.typedText(']');90 assertLatex('\\left(\\right]');91 mq.typedText('[');92 assertLatex('\\left(\\right]\\left[\\right]');93 mq.typedText('}');94 assertLatex('\\left(\\right]\\left[\\right\\}');95 });96 test('typing mismatched brackets 1+(2+3]+4', function() {97 mq.typedText('1+');98 assertLatex('1+');99 mq.typedText('(');100 assertLatex('1+\\left(\\right)');101 mq.typedText('2+3');102 assertLatex('1+\\left(2+3\\right)');103 mq.typedText(']+4');104 assertLatex('1+\\left(2+3\\right]+4');105 });106 test('wrapping things in mismatched brackets 1+(2+3]+4', function() {107 mq.typedText('1+2+3+4');108 assertLatex('1+2+3+4');109 mq.keystroke('Left Left').typedText(']');110 assertLatex('\\left[1+2+3\\right]+4');111 mq.keystroke('Left Left Left Left').typedText('(');112 assertLatex('1+\\left(2+3\\right]+4');113 });114 test('nested mismatched brackets 1+(2+[3+4)+5]+6', function() {115 mq.typedText('1+(2+[3+4)+5]+6');116 assertLatex('1+\\left(2+\\left[3+4\\right)+5\\right]+6');117 });118 suite('restrictMismatchedBrackets', function() {119 setup(function() {120 mq.config({ restrictMismatchedBrackets: true });121 });122 test('typing (|x|+1) works', function() {123 mq.typedText('(|x|+1)');124 assertLatex('\\left(\\left|x\\right|+1\\right)');125 });126 test('typing [x} becomes [{x}]', function() {127 mq.typedText('[x}');128 assertLatex('\\left[\\left\\{x\\right\\}\\right]');129 });130 test('normal matching pairs {f(n), [a,b]} work', function() {131 mq.typedText('{f(n), [a,b]}');132 assertLatex('\\left\\{f\\left(n\\right),\\ \\left[a,b\\right]\\right\\}');133 });134 test('[a,b) and (a,b] still work', function() {135 mq.typedText('[a,b) + (a,b]');136 assertLatex('\\left[a,b\\right)\\ +\\ \\left(a,b\\right]');137 });138 });139 });140 suite('pipes', function() {141 test('empty pipes ||', function() {142 mq.typedText('|');143 assertLatex('\\left|\\right|');144 mq.typedText('|');145 assertLatex('\\left|\\right|');146 });147 test('straight typing 1+|2+3|+4', function() {148 mq.typedText('1+|2+3|+4');149 assertLatex('1+\\left|2+3\\right|+4');150 });151 test('wrapping things in pipes 1+|2+3|+4', function() {152 mq.typedText('1+2+3+4');153 assertLatex('1+2+3+4');154 mq.keystroke('Home Right Right').typedText('|');155 assertLatex('1+\\left|2+3+4\\right|');156 mq.keystroke('Right Right Right').typedText('|');157 assertLatex('1+\\left|2+3\\right|+4');158 });159 suite('can type mismatched paren/pipe group from any side', function() {160 suite('straight typing', function() {161 test('|)', function() {162 mq.typedText('|)');163 assertLatex('\\left|\\right)');164 });165 test('(|', function() {166 mq.typedText('(|');167 assertLatex('\\left(\\right|');168 });169 });170 suite('the other direction', function() {171 test('|)', function() {172 mq.typedText(')');173 assertLatex('\\left(\\right)');174 mq.keystroke('Left').typedText('|');175 assertLatex('\\left|\\right)');176 });177 test('(|', function() {178 mq.typedText('||');179 assertLatex('\\left|\\right|');180 mq.keystroke('Left Left Del');181 assertLatex('\\left|\\right|');182 mq.typedText('(');183 assertLatex('\\left(\\right|');184 });185 });186 });187 });188 suite('backspacing', backspacingTests);189 suite('backspacing with restrictMismatchedBrackets', function() {190 setup(function() {191 mq.config({ restrictMismatchedBrackets: true });192 });193 backspacingTests();194 });195 function backspacingTests() {196 test('typing then backspacing a close-paren in the middle of 1+2+3+4', function() {197 mq.typedText('1+2+3+4');198 assertLatex('1+2+3+4');199 mq.keystroke('Left Left').typedText(')');200 assertLatex('\\left(1+2+3\\right)+4');201 mq.keystroke('Backspace');202 assertLatex('1+2+3+4');203 });204 test('backspacing close-paren then open-paren of 1+(2+3)+4', function() {205 mq.typedText('1+(2+3)+4');206 assertLatex('1+\\left(2+3\\right)+4');207 mq.keystroke('Left Left Backspace');208 assertLatex('1+\\left(2+3+4\\right)');209 mq.keystroke('Left Left Left Backspace');210 assertLatex('1+2+3+4');211 });212 test('backspacing open-paren of 1+(2+3)+4', function() {213 mq.typedText('1+(2+3)+4');214 assertLatex('1+\\left(2+3\\right)+4');215 mq.keystroke('Left Left Left Left Left Left Backspace');216 assertLatex('1+2+3+4');217 });218 test('backspacing close-bracket then open-paren of 1+(2+3]+4', function() {219 mq.typedText('1+(2+3]+4');220 assertLatex('1+\\left(2+3\\right]+4');221 mq.keystroke('Left Left Backspace');222 assertLatex('1+\\left(2+3+4\\right)');223 mq.keystroke('Left Left Left Backspace');224 assertLatex('1+2+3+4');225 });226 test('backspacing open-paren of 1+(2+3]+4', function() {227 mq.typedText('1+(2+3]+4');228 assertLatex('1+\\left(2+3\\right]+4');229 mq.keystroke('Left Left Left Left Left Left Backspace');230 assertLatex('1+2+3+4');231 });232 test('backspacing close-bracket then open-paren of 1+(2+3] (nothing after paren group)', function() {233 mq.typedText('1+(2+3]');234 assertLatex('1+\\left(2+3\\right]');235 mq.keystroke('Backspace');236 assertLatex('1+\\left(2+3\\right)');237 mq.keystroke('Left Left Left Backspace');238 assertLatex('1+2+3');239 });240 test('backspacing open-paren of 1+(2+3] (nothing after paren group)', function() {241 mq.typedText('1+(2+3]');242 assertLatex('1+\\left(2+3\\right]');243 mq.keystroke('Left Left Left Left Backspace');244 assertLatex('1+2+3');245 });246 test('backspacing close-bracket then open-paren of (2+3]+4 (nothing before paren group)', function() {247 mq.typedText('(2+3]+4');248 assertLatex('\\left(2+3\\right]+4');249 mq.keystroke('Left Left Backspace');250 assertLatex('\\left(2+3+4\\right)');251 mq.keystroke('Left Left Left Backspace');252 assertLatex('2+3+4');253 });254 test('backspacing open-paren of (2+3]+4 (nothing before paren group)', function() {255 mq.typedText('(2+3]+4');256 assertLatex('\\left(2+3\\right]+4');257 mq.keystroke('Left Left Left Left Left Left Backspace');258 assertLatex('2+3+4');259 });260 function assertParenBlockNonEmpty() {261 var parenBlock = $(mq.el()).find('.mq-paren+span');262 assert.equal(parenBlock.length, 1, 'exactly 1 paren block');263 assert.ok(!parenBlock.hasClass('mq-empty'),264 'paren block auto-expanded, should no longer be gray');265 }266 test('backspacing close-bracket then open-paren of 1+(]+4 (empty paren group)', function() {267 mq.typedText('1+(]+4');268 assertLatex('1+\\left(\\right]+4');269 mq.keystroke('Left Left Backspace');270 assertLatex('1+\\left(+4\\right)');271 assertParenBlockNonEmpty();272 mq.keystroke('Backspace');273 assertLatex('1++4');274 });275 test('backspacing open-paren of 1+(]+4 (empty paren group)', function() {276 mq.typedText('1+(]+4');277 assertLatex('1+\\left(\\right]+4');278 mq.keystroke('Left Left Left Backspace');279 assertLatex('1++4');280 });281 test('backspacing close-bracket then open-paren of 1+(] (empty paren group, nothing after)', function() {282 mq.typedText('1+(]');283 assertLatex('1+\\left(\\right]');284 mq.keystroke('Backspace');285 assertLatex('1+\\left(\\right)');286 mq.keystroke('Backspace');287 assertLatex('1+');288 });289 test('backspacing open-paren of 1+(] (empty paren group, nothing after)', function() {290 mq.typedText('1+(]');291 assertLatex('1+\\left(\\right]');292 mq.keystroke('Left Backspace');293 assertLatex('1+');294 });295 test('backspacing close-bracket then open-paren of (]+4 (empty paren group, nothing before)', function() {296 mq.typedText('(]+4');297 assertLatex('\\left(\\right]+4');298 mq.keystroke('Left Left Backspace');299 assertLatex('\\left(+4\\right)');300 assertParenBlockNonEmpty();301 mq.keystroke('Backspace');302 assertLatex('+4');303 });304 test('backspacing open-paren of (]+4 (empty paren group, nothing before)', function() {305 mq.typedText('(]+4');306 assertLatex('\\left(\\right]+4');307 mq.keystroke('Left Left Left Backspace');308 assertLatex('+4');309 });310 test('rendering mismatched brackets 1+(2+3]+4 from LaTeX then backspacing close-bracket then open-paren', function() {311 mq.latex('1+\\left(2+3\\right]+4');312 assertLatex('1+\\left(2+3\\right]+4');313 mq.keystroke('Left Left Backspace');314 assertLatex('1+\\left(2+3+4\\right)');315 mq.keystroke('Left Left Left Backspace');316 assertLatex('1+2+3+4');317 });318 test('rendering mismatched brackets 1+(2+3]+4 from LaTeX then backspacing open-paren', function() {319 mq.latex('1+\\left(2+3\\right]+4');320 assertLatex('1+\\left(2+3\\right]+4');321 mq.keystroke('Left Left Left Left Left Left Backspace');322 assertLatex('1+2+3+4');323 });324 test('rendering paren group 1+(2+3)+4 from LaTeX then backspacing close-paren then open-paren', function() {325 mq.latex('1+\\left(2+3\\right)+4');326 assertLatex('1+\\left(2+3\\right)+4');327 mq.keystroke('Left Left Backspace');328 assertLatex('1+\\left(2+3+4\\right)');329 mq.keystroke('Left Left Left Backspace');330 assertLatex('1+2+3+4');331 });332 test('rendering paren group 1+(2+3)+4 from LaTeX then backspacing open-paren', function() {333 mq.latex('1+\\left(2+3\\right)+4');334 assertLatex('1+\\left(2+3\\right)+4');335 mq.keystroke('Left Left Left Left Left Left Backspace');336 assertLatex('1+2+3+4');337 });338 test('wrapping selection in parens 1+(2+3)+4 then backspacing close-paren then open-paren', function() {339 mq.typedText('1+2+3+4');340 assertLatex('1+2+3+4');341 mq.keystroke('Left Left Shift-Left Shift-Left Shift-Left').typedText(')');342 assertLatex('1+\\left(2+3\\right)+4');343 mq.keystroke('Backspace');344 assertLatex('1+\\left(2+3+4\\right)');345 mq.keystroke('Left Left Left Backspace');346 assertLatex('1+2+3+4');347 });348 test('wrapping selection in parens 1+(2+3)+4 then backspacing open-paren', function() {349 mq.typedText('1+2+3+4');350 assertLatex('1+2+3+4');351 mq.keystroke('Left Left Shift-Left Shift-Left Shift-Left').typedText('(');352 assertLatex('1+\\left(2+3\\right)+4');353 mq.keystroke('Backspace');354 assertLatex('1+2+3+4');355 });356 test('backspacing close-bracket of 1+(2+3] (nothing after) then typing', function() {357 mq.typedText('1+(2+3]');358 assertLatex('1+\\left(2+3\\right]');359 mq.keystroke('Backspace');360 assertLatex('1+\\left(2+3\\right)');361 mq.typedText('+4');362 assertLatex('1+\\left(2+3+4\\right)');363 });364 test('backspacing open-paren of (2+3]+4 (nothing before) then typing', function() {365 mq.typedText('(2+3]+4');366 assertLatex('\\left(2+3\\right]+4');367 mq.keystroke('Home Right Backspace');368 assertLatex('2+3+4');369 mq.typedText('1+');370 assertLatex('1+2+3+4');371 });372 test('backspacing paren containing a one-sided paren 0+[(1+2)+3]+4', function() {373 mq.typedText('0+[1+2+3]+4');374 assertLatex('0+\\left[1+2+3\\right]+4');375 mq.keystroke('Left Left Left Left Left').typedText(')');376 assertLatex('0+\\left[\\left(1+2\\right)+3\\right]+4');377 mq.keystroke('Right Right Right Backspace');378 assertLatex('0+\\left[1+2\\right)+3+4');379 });380 test('backspacing paren inside a one-sided paren (0+[1+2]+3)+4', function() {381 mq.typedText('0+[1+2]+3)+4');382 assertLatex('\\left(0+\\left[1+2\\right]+3\\right)+4');383 mq.keystroke('Left Left Left Left Left Backspace');384 assertLatex('0+\\left[1+2+3\\right)+4');385 });386 test('backspacing paren containing and inside a one-sided paren (([1+2]))', function() {387 mq.typedText('(1+2))');388 assertLatex('\\left(\\left(1+2\\right)\\right)');389 mq.keystroke('Left Left').typedText(']');390 assertLatex('\\left(\\left(\\left[1+2\\right]\\right)\\right)');391 mq.keystroke('Right Backspace');392 assertLatex('\\left(\\left(1+2\\right]\\right)');393 mq.keystroke('Backspace');394 assertLatex('\\left(1+2\\right)');395 });396 test('auto-expanding calls .siblingCreated() on new siblings 1+((2+3))', function() {397 mq.typedText('1+((2+3))');398 assertLatex('1+\\left(\\left(2+3\\right)\\right)');399 mq.keystroke('Left Left Left Left Left Left Del');400 assertLatex('1+\\left(\\left(2+3\\right)\\right)');401 mq.keystroke('Left Left Del');402 assertLatex('\\left(1+\\left(2+3\\right)\\right)');403 // now check that the inner open-paren isn't still a ghost404 mq.keystroke('Right Right Right Right Del');405 assertLatex('1+\\left(2+3\\right)');406 });407 test('that unwrapping calls .siblingCreated() on new siblings ((1+2)+(3+4))+5', function() {408 mq.typedText('(1+2+3+4)+5');409 assertLatex('\\left(1+2+3+4\\right)+5');410 mq.keystroke('Home Right Right Right Right').typedText(')');411 assertLatex('\\left(\\left(1+2\\right)+3+4\\right)+5');412 mq.keystroke('Right').typedText('(');413 assertLatex('\\left(\\left(1+2\\right)+\\left(3+4\\right)\\right)+5');414 mq.keystroke('Right Right Right Right Right Backspace');415 assertLatex('\\left(1+2\\right)+\\left(3+4\\right)+5');416 mq.keystroke('Left Left Left Left Backspace');417 assertLatex('\\left(1+2\\right)+3+4+5');418 });419 test('typing Ctrl-Backspace deletes everything to the left of the cursor', function () {420 mq.typedText('12345');421 assertLatex('12345');422 mq.keystroke('Left Left');423 mq.keystroke('Ctrl-Backspace');424 assertLatex('45');425 mq.keystroke('Ctrl-Backspace');426 assertLatex('45');427 });428 test('typing Ctrl-Del deletes everything to the right of the cursor', function () {429 mq.typedText('12345');430 assertLatex('12345');431 mq.keystroke('Left Left');432 mq.keystroke('Ctrl-Del');433 assertLatex('123');434 mq.keystroke('Ctrl-Del');435 assertLatex('123');436 });437 suite('pipes', function() {438 test('typing then backspacing a pipe in the middle of 1+2+3+4', function() {439 mq.typedText('1+2+3+4');440 assertLatex('1+2+3+4');441 mq.keystroke('Left Left Left').typedText('|');442 assertLatex('1+2+\\left|3+4\\right|');443 mq.keystroke('Backspace');444 assertLatex('1+2+3+4');445 });446 test('backspacing close-pipe then open-pipe of 1+|2+3|+4', function() {447 mq.typedText('1+|2+3|+4');448 assertLatex('1+\\left|2+3\\right|+4');449 mq.keystroke('Left Left Backspace');450 assertLatex('1+\\left|2+3+4\\right|');451 mq.keystroke('Left Left Left Backspace');452 assertLatex('1+2+3+4');453 });454 test('backspacing open-pipe of 1+|2+3|+4', function() {455 mq.typedText('1+|2+3|+4');456 assertLatex('1+\\left|2+3\\right|+4');457 mq.keystroke('Left Left Left Left Left Left Backspace');458 assertLatex('1+2+3+4');459 });460 test('backspacing close-pipe then open-pipe of 1+|2+3| (nothing after pipe pair)', function() {461 mq.typedText('1+|2+3|');462 assertLatex('1+\\left|2+3\\right|');463 mq.keystroke('Backspace');464 assertLatex('1+\\left|2+3\\right|');465 mq.keystroke('Left Left Left Backspace');466 assertLatex('1+2+3');467 });468 test('backspacing open-pipe of 1+|2+3| (nothing after pipe pair)', function() {469 mq.typedText('1+|2+3|');470 assertLatex('1+\\left|2+3\\right|');471 mq.keystroke('Left Left Left Left Backspace');472 assertLatex('1+2+3');473 });474 test('backspacing close-pipe then open-pipe of |2+3|+4 (nothing before pipe pair)', function() {475 mq.typedText('|2+3|+4');476 assertLatex('\\left|2+3\\right|+4');477 mq.keystroke('Left Left Backspace');478 assertLatex('\\left|2+3+4\\right|');479 mq.keystroke('Left Left Left Backspace');480 assertLatex('2+3+4');481 });482 test('backspacing open-pipe of |2+3|+4 (nothing before pipe pair)', function() {483 mq.typedText('|2+3|+4');484 assertLatex('\\left|2+3\\right|+4');485 mq.keystroke('Left Left Left Left Left Left Backspace');486 assertLatex('2+3+4');487 });488 function assertParenBlockNonEmpty() {489 var parenBlock = $(mq.el()).find('.mq-paren+span');490 assert.equal(parenBlock.length, 1, 'exactly 1 paren block');491 assert.ok(!parenBlock.hasClass('mq-empty'),492 'paren block auto-expanded, should no longer be gray');493 }494 test('backspacing close-pipe then open-pipe of 1+||+4 (empty pipe pair)', function() {495 mq.typedText('1+||+4');496 assertLatex('1+\\left|\\right|+4');497 mq.keystroke('Left Left Backspace');498 assertLatex('1+\\left|+4\\right|');499 assertParenBlockNonEmpty();500 mq.keystroke('Backspace');501 assertLatex('1++4');502 });503 test('backspacing open-pipe of 1+||+4 (empty pipe pair)', function() {504 mq.typedText('1+||+4');505 assertLatex('1+\\left|\\right|+4');506 mq.keystroke('Left Left Left Backspace');507 assertLatex('1++4');508 });509 test('backspacing close-pipe then open-pipe of 1+|| (empty pipe pair, nothing after)', function() {510 mq.typedText('1+||');511 assertLatex('1+\\left|\\right|');512 mq.keystroke('Backspace');513 assertLatex('1+\\left|\\right|');514 mq.keystroke('Backspace');515 assertLatex('1+');516 });517 test('backspacing open-pipe of 1+|| (empty pipe pair, nothing after)', function() {518 mq.typedText('1+||');519 assertLatex('1+\\left|\\right|');520 mq.keystroke('Left Backspace');521 assertLatex('1+');522 });523 test('backspacing close-pipe then open-pipe of ||+4 (empty pipe pair, nothing before)', function() {524 mq.typedText('||+4');525 assertLatex('\\left|\\right|+4');526 mq.keystroke('Left Left Backspace');527 assertLatex('\\left|+4\\right|');528 assertParenBlockNonEmpty();529 mq.keystroke('Backspace');530 assertLatex('+4');531 });532 test('backspacing open-pipe of ||+4 (empty pipe pair, nothing before)', function() {533 mq.typedText('||+4');534 assertLatex('\\left|\\right|+4');535 mq.keystroke('Left Left Left Backspace');536 assertLatex('+4');537 });538 test('rendering pipe pair 1+|2+3|+4 from LaTeX then backspacing close-pipe then open-pipe', function() {539 mq.latex('1+\\left|2+3\\right|+4');540 assertLatex('1+\\left|2+3\\right|+4');541 mq.keystroke('Left Left Backspace');542 assertLatex('1+\\left|2+3+4\\right|');543 mq.keystroke('Left Left Left Backspace');544 assertLatex('1+2+3+4');545 });546 test('rendering pipe pair 1+|2+3|+4 from LaTeX then backspacing open-pipe', function() {547 mq.latex('1+\\left|2+3\\right|+4');548 assertLatex('1+\\left|2+3\\right|+4');549 mq.keystroke('Left Left Left Left Left Left Backspace');550 assertLatex('1+2+3+4');551 });552 test('rendering mismatched paren/pipe group 1+|2+3)+4 from LaTeX then backspacing close-paren then open-pipe', function() {553 mq.latex('1+\\left|2+3\\right)+4');554 assertLatex('1+\\left|2+3\\right)+4');555 mq.keystroke('Left Left Backspace');556 assertLatex('1+\\left|2+3+4\\right|');557 mq.keystroke('Left Left Left Backspace');558 assertLatex('1+2+3+4');559 });560 test('rendering mismatched paren/pipe group 1+|2+3)+4 from LaTeX then backspacing open-pipe', function() {561 mq.latex('1+\\left|2+3\\right)+4');562 assertLatex('1+\\left|2+3\\right)+4');563 mq.keystroke('Left Left Left Left Left Left Backspace');564 assertLatex('1+2+3+4');565 });566 test('rendering mismatched paren/pipe group 1+(2+3|+4 from LaTeX then backspacing close-pipe then open-paren', function() {567 mq.latex('1+\\left(2+3\\right|+4');568 assertLatex('1+\\left(2+3\\right|+4');569 mq.keystroke('Left Left Backspace');570 assertLatex('1+\\left(2+3+4\\right)');571 mq.keystroke('Left Left Left Backspace');572 assertLatex('1+2+3+4');573 });574 test('rendering mismatched paren/pipe group 1+(2+3|+4 from LaTeX then backspacing open-paren', function() {575 mq.latex('1+\\left(2+3\\right|+4');576 assertLatex('1+\\left(2+3\\right|+4');577 mq.keystroke('Left Left Left Left Left Left Backspace');578 assertLatex('1+2+3+4');579 });580 test('wrapping selection in pipes 1+|2+3|+4 then backspacing open-pipe', function() {581 mq.typedText('1+2+3+4');582 assertLatex('1+2+3+4');583 mq.keystroke('Left Left Shift-Left Shift-Left Shift-Left').typedText('|');584 assertLatex('1+\\left|2+3\\right|+4');585 mq.keystroke('Backspace');586 assertLatex('1+2+3+4');587 });588 test('wrapping selection in pipes 1+|2+3|+4 then backspacing close-pipe then open-pipe', function() {589 mq.typedText('1+2+3+4');590 assertLatex('1+2+3+4');591 mq.keystroke('Left Left Shift-Left Shift-Left Shift-Left').typedText('|');592 assertLatex('1+\\left|2+3\\right|+4');593 mq.keystroke('Tab Backspace');594 assertLatex('1+\\left|2+3+4\\right|');595 mq.keystroke('Left Left Left Backspace');596 assertLatex('1+2+3+4');597 });598 test('backspacing close-pipe of 1+|2+3| (nothing after) then typing', function() {599 mq.typedText('1+|2+3|');600 assertLatex('1+\\left|2+3\\right|');601 mq.keystroke('Backspace');602 assertLatex('1+\\left|2+3\\right|');603 mq.typedText('+4');604 assertLatex('1+\\left|2+3+4\\right|');605 });606 test('backspacing open-pipe of |2+3|+4 (nothing before) then typing', function() {607 mq.typedText('|2+3|+4');608 assertLatex('\\left|2+3\\right|+4');609 mq.keystroke('Home Right Backspace');610 assertLatex('2+3+4');611 mq.typedText('1+');612 assertLatex('1+2+3+4');613 });614 test('backspacing pipe containing a one-sided pipe 0+|1+|2+3||+4', function() {615 mq.typedText('0+|1+2+3|+4');616 assertLatex('0+\\left|1+2+3\\right|+4');617 mq.keystroke('Left Left Left Left Left Left').typedText('|');618 assertLatex('0+\\left|1+\\left|2+3\\right|\\right|+4');619 mq.keystroke('Shift-Tab Shift-Tab Del');620 assertLatex('0+1+\\left|2+3\\right|+4');621 });622 test('backspacing pipe inside a one-sided pipe 0+|1+|2+3|+4|', function() {623 mq.typedText('0+1+|2+3|+4');624 assertLatex('0+1+\\left|2+3\\right|+4');625 mq.keystroke('Home Right Right').typedText('|');626 assertLatex('0+\\left|1+\\left|2+3\\right|+4\\right|');627 mq.keystroke('Right Right Del');628 assertLatex('0+\\left|1+2+3\\right|+4');629 });630 test('backspacing pipe containing and inside a one-sided pipe |0+|1+|2+3||+4|', function() {631 mq.typedText('0+|1+2+3|+4');632 assertLatex('0+\\left|1+2+3\\right|+4');633 mq.keystroke('Home').typedText('|');634 assertLatex('\\left|0+\\left|1+2+3\\right|+4\\right|');635 mq.keystroke('Right Right Right Right Right').typedText('|');636 assertLatex('\\left|0+\\left|1+\\left|2+3\\right|\\right|+4\\right|');637 mq.keystroke('Left Left Left Backspace');638 assertLatex('\\left|0+1+\\left|2+3\\right|+4\\right|');639 });640 test('backspacing pipe containing a one-sided pipe facing same way 0+||1+2||+3', function() {641 mq.typedText('0+|1+2|+3');642 assertLatex('0+\\left|1+2\\right|+3');643 mq.keystroke('Home Right Right Right').typedText('|');644 assertLatex('0+\\left|\\left|1+2\\right|\\right|+3');645 mq.keystroke('Tab Tab Backspace');646 assertLatex('0+\\left|\\left|1+2\\right|+3\\right|');647 });648 test('backspacing pipe inside a one-sided pipe facing same way 0+|1+|2+3|+4|', function() {649 mq.typedText('0+1+|2+3|+4');650 assertLatex('0+1+\\left|2+3\\right|+4');651 mq.keystroke('Home Right Right').typedText('|');652 assertLatex('0+\\left|1+\\left|2+3\\right|+4\\right|');653 mq.keystroke('Right Right Right Right Right Right Right Backspace');654 assertLatex('0+\\left|1+\\left|2+3+4\\right|\\right|');655 });656 test('backspacing open-paren of mismatched paren/pipe group containing a one-sided pipe 0+(1+|2+3||+4', function() {657 mq.latex('0+\\left(1+2+3\\right|+4');658 assertLatex('0+\\left(1+2+3\\right|+4');659 mq.keystroke('Left Left Left Left Left Left').typedText('|');660 assertLatex('0+\\left(1+\\left|2+3\\right|\\right|+4');661 mq.keystroke('Shift-Tab Shift-Tab Del');662 assertLatex('0+1+\\left|2+3\\right|+4');663 });664 test('backspacing open-paren of mismatched paren/pipe group inside a one-sided pipe 0+|1+(2+3|+4|', function() {665 mq.latex('0+1+\\left(2+3\\right|+4');666 assertLatex('0+1+\\left(2+3\\right|+4');667 mq.keystroke('Home Right Right').typedText('|');668 assertLatex('0+\\left|1+\\left(2+3\\right|+4\\right|');669 mq.keystroke('Right Right Del');670 assertLatex('0+\\left|1+2+3\\right|+4');671 });672 });673 }674 suite('typing outside ghost paren', function() {675 test('typing outside ghost paren solidifies ghost 1+(2+3)', function() {676 mq.typedText('1+(2+3');677 assertLatex('1+\\left(2+3\\right)');678 mq.keystroke('Right').typedText('+4');679 assertLatex('1+\\left(2+3\\right)+4');680 mq.keystroke('Left Left Left Left Left Left Left Del');681 assertLatex('\\left(1+2+3\\right)+4');682 });683 test('selected and replaced by LiveFraction solidifies ghosts (1+2)/( )', function() {684 mq.typedText('1+2)/');685 assertLatex('\\frac{\\left(1+2\\right)}{ }');686 mq.keystroke('Left Backspace');687 assertLatex('\\frac{\\left(1+2\\right)}{ }');688 });689 test('close paren group by typing close-bracket outside ghost paren (1+2]', function() {690 mq.typedText('(1+2');691 assertLatex('\\left(1+2\\right)');692 mq.keystroke('Right').typedText(']');693 assertLatex('\\left(1+2\\right]');694 });695 test('close adjacent paren group before containing paren group (1+(2+3])', function() {696 mq.typedText('(1+(2+3');697 assertLatex('\\left(1+\\left(2+3\\right)\\right)');698 mq.keystroke('Right').typedText(']');699 assertLatex('\\left(1+\\left(2+3\\right]\\right)');700 mq.typedText(']');701 assertLatex('\\left(1+\\left(2+3\\right]\\right]');702 });703 test('can type close-bracket on solid side of one-sided paren [](1+2)', function() {704 mq.typedText('(1+2');705 assertLatex('\\left(1+2\\right)');706 mq.moveToLeftEnd().typedText(']');707 assertLatex('\\left[\\right]\\left(1+2\\right)');708 });709 suite('pipes', function() {710 test('close pipe pair from outside to the right |1+2|', function() {711 mq.typedText('|1+2');712 assertLatex('\\left|1+2\\right|');713 mq.keystroke('Right').typedText('|');714 assertLatex('\\left|1+2\\right|');715 mq.keystroke('Home Del');716 assertLatex('\\left|1+2\\right|');717 });718 test('close pipe pair from outside to the left |1+2|', function() {719 mq.typedText('|1+2|');720 assertLatex('\\left|1+2\\right|');721 mq.keystroke('Home Del');722 assertLatex('\\left|1+2\\right|');723 mq.keystroke('Left').typedText('|');724 assertLatex('\\left|1+2\\right|');725 mq.keystroke('Ctrl-End Backspace');726 assertLatex('\\left|1+2\\right|');727 });728 test('can type pipe on solid side of one-sided pipe ||||', function() {729 mq.typedText('|');730 assertLatex('\\left|\\right|');731 mq.moveToLeftEnd().typedText('|');732 assertLatex('\\left|\\left|\\right|\\right|');733 });734 });735 });736 });737 suite('autoCommands', function() {738 setup(function() {739 mq.config({740 autoOperatorNames: 'sin pp',741 autoCommands: 'pi tau phi theta Gamma sum prod sqrt nthroot'742 });743 });744 test('individual commands', function(){745 mq.typedText('sum' + 'n=0');746 mq.keystroke('Up').typedText('100').keystroke('Right');747 assertLatex('\\sum_{n=0}^{100}');748 mq.keystroke('Ctrl-Backspace');749 mq.typedText('prod');750 mq.typedText('n=0').keystroke('Up').typedText('100').keystroke('Right');751 assertLatex('\\prod_{n=0}^{100}');752 mq.keystroke('Ctrl-Backspace');753 mq.typedText('sqrt');754 mq.typedText('100').keystroke('Right');755 assertLatex('\\sqrt{100}');756 mq.keystroke('Ctrl-Backspace');757 mq.typedText('nthroot');758 mq.typedText('n').keystroke('Right').typedText('100').keystroke('Right');759 assertLatex('\\sqrt[n]{100}');760 mq.keystroke('Ctrl-Backspace');761 mq.typedText('pi');762 assertLatex('\\pi');763 mq.keystroke('Backspace');764 mq.typedText('tau');765 assertLatex('\\tau');766 mq.keystroke('Backspace');767 mq.typedText('phi');768 assertLatex('\\phi');769 mq.keystroke('Backspace');770 mq.typedText('theta');771 assertLatex('\\theta');772 mq.keystroke('Backspace');773 mq.typedText('Gamma');774 assertLatex('\\Gamma');775 mq.keystroke('Backspace');776 });777 test('sequences of auto-commands and other assorted characters', function() {778 mq.typedText('sin' + 'pi');779 assertLatex('\\sin\\pi');780 mq.keystroke('Left Backspace');781 assertLatex('si\\pi');782 mq.keystroke('Left').typedText('p');783 assertLatex('spi\\pi');784 mq.typedText('i');785 assertLatex('s\\pi i\\pi');786 mq.typedText('p');787 assertLatex('s\\pi pi\\pi');788 mq.keystroke('Right').typedText('n');789 assertLatex('s\\pi pin\\pi');790 mq.keystroke('Left Left Left').typedText('s');791 assertLatex('s\\pi spin\\pi');792 mq.keystroke('Backspace');793 assertLatex('s\\pi pin\\pi');794 mq.keystroke('Del').keystroke('Backspace');795 assertLatex('\\sin\\pi');796 });797 test('has lower "precedence" than operator names', function() {798 mq.typedText('ppi');799 assertLatex('\\operatorname{pp}i');800 mq.keystroke('Left Left').typedText('i');801 assertLatex('\\pi pi');802 });803 test('command contains non-letters', function() {804 assert.throws(function() { MQ.config({ autoCommands: 'e1' }); });805 });806 test('command length less than 2', function() {807 assert.throws(function() { MQ.config({ autoCommands: 'e' }); });808 });809 test('command is a built-in operator name', function() {810 var cmds = ('Pr arg deg det dim exp gcd hom inf ker lg lim ln log max min sup'811 + ' limsup liminf injlim projlim Pr').split(' ');812 for (var i = 0; i < cmds.length; i += 1) {813 assert.throws(function() { MQ.config({ autoCommands: cmds[i] }) },814 'MQ.config({ autoCommands: "'+cmds[i]+'" })');815 }816 });817 test('built-in operator names even after auto-operator names overridden', function() {818 MQ.config({ autoOperatorNames: 'sin inf arcosh cosh cos cosec csc' });819 // ^ happen to be the ones required by autoOperatorNames.test.js820 var cmds = 'Pr arg deg det exp gcd inf lg lim ln log max min sup'.split(' ');821 for (var i = 0; i < cmds.length; i += 1) {822 assert.throws(function() { MQ.config({ autoCommands: cmds[i] }) },823 'MQ.config({ autoCommands: "'+cmds[i]+'" })');824 }825 });826 suite('command list not perfectly space-delimited', function() {827 test('double space', function() {828 assert.throws(function() { MQ.config({ autoCommands: 'pi theta' }); });829 });830 test('leading space', function() {831 assert.throws(function() { MQ.config({ autoCommands: ' pi' }); });832 });833 test('trailing space', function() {834 assert.throws(function() { MQ.config({ autoCommands: 'pi ' }); });835 });836 });837 });838 suite('inequalities', function() {839 // assertFullyFunctioningInequality() checks not only that the inequality840 // has the right LaTeX and when you backspace it has the right LaTeX,841 // but also that when you backspace you get the right state such that842 // you can either type = again to get the non-strict inequality again,843 // or backspace again and it'll delete correctly.844 function assertFullyFunctioningInequality(nonStrict, strict) {845 assertLatex(nonStrict);846 mq.keystroke('Backspace');847 assertLatex(strict);848 mq.typedText('=');849 assertLatex(nonStrict);850 mq.keystroke('Backspace');851 assertLatex(strict);852 mq.keystroke('Backspace');853 assertLatex('');854 }855 test('typing and backspacing <= and >=', function() {856 mq.typedText('<');857 assertLatex('<');858 mq.typedText('=');859 assertFullyFunctioningInequality('\\le', '<');860 mq.typedText('>');861 assertLatex('>');862 mq.typedText('=');863 assertFullyFunctioningInequality('\\ge', '>');864 mq.typedText('<<>>==>><<==');865 assertLatex('<<>\\ge=>><\\le=');866 });867 test('typing ≤ and ≥ chars directly', function() {868 mq.typedText('≤');869 assertFullyFunctioningInequality('\\le', '<');870 mq.typedText('≥');871 assertFullyFunctioningInequality('\\ge', '>');872 });873 suite('rendered from LaTeX', function() {874 test('control sequences', function() {875 mq.latex('\\le');876 assertFullyFunctioningInequality('\\le', '<');877 mq.latex('\\ge');878 assertFullyFunctioningInequality('\\ge', '>');879 });880 test('≤ and ≥ chars', function() {881 mq.latex('≤');882 assertFullyFunctioningInequality('\\le', '<');883 mq.latex('≥');884 assertFullyFunctioningInequality('\\ge', '>');885 });886 });887 });888 suite('SupSub behavior options', function() {889 test('charsThatBreakOutOfSupSub', function() {890 assert.equal(mq.typedText('x^2n+y').latex(), 'x^{2n+y}');891 mq.latex('');892 assert.equal(mq.typedText('x^+2n').latex(), 'x^{+2n}');893 mq.latex('');894 assert.equal(mq.typedText('x^-2n').latex(), 'x^{-2n}');895 mq.latex('');896 assert.equal(mq.typedText('x^=2n').latex(), 'x^{=2n}');897 mq.latex('');898 MQ.config({ charsThatBreakOutOfSupSub: '+-=<>' });899 assert.equal(mq.typedText('x^2n+y').latex(), 'x^{2n}+y');900 mq.latex('');901 // Unary operators never break out of exponents.902 assert.equal(mq.typedText('x^+2n').latex(), 'x^{+2n}');903 mq.latex('');904 assert.equal(mq.typedText('x^-2n').latex(), 'x^{-2n}');905 mq.latex('');906 assert.equal(mq.typedText('x^=2n').latex(), 'x^{=2n}');907 mq.latex('');908 // Only break out of exponents if cursor at the end, don't909 // jump from the middle of the exponent out to the right.910 assert.equal(mq.typedText('x^ab').latex(), 'x^{ab}');911 assert.equal(mq.keystroke('Left').typedText('+').latex(), 'x^{a+b}');912 mq.latex('');913 });914 test('supSubsRequireOperand', function() {915 assert.equal(mq.typedText('^').latex(), '^{ }');916 assert.equal(mq.typedText('2').latex(), '^2');917 assert.equal(mq.typedText('n').latex(), '^{2n}');918 mq.latex('');919 assert.equal(mq.typedText('x').latex(), 'x');920 assert.equal(mq.typedText('^').latex(), 'x^{ }');921 assert.equal(mq.typedText('2').latex(), 'x^2');922 assert.equal(mq.typedText('n').latex(), 'x^{2n}');923 mq.latex('');924 assert.equal(mq.typedText('x').latex(), 'x');925 assert.equal(mq.typedText('^').latex(), 'x^{ }');926 assert.equal(mq.typedText('^').latex(), 'x^{^{ }}');927 assert.equal(mq.typedText('2').latex(), 'x^{^2}');928 assert.equal(mq.typedText('n').latex(), 'x^{^{2n}}');929 mq.latex('');930 assert.equal(mq.typedText('2').latex(), '2');931 assert.equal(mq.keystroke('Shift-Left').typedText('^').latex(), '^2');932 mq.latex('');933 MQ.config({ supSubsRequireOperand: true });934 assert.equal(mq.typedText('^').latex(), '');935 assert.equal(mq.typedText('2').latex(), '2');936 assert.equal(mq.typedText('n').latex(), '2n');937 mq.latex('');938 assert.equal(mq.typedText('x').latex(), 'x');939 assert.equal(mq.typedText('^').latex(), 'x^{ }');940 assert.equal(mq.typedText('2').latex(), 'x^2');941 assert.equal(mq.typedText('n').latex(), 'x^{2n}');942 mq.latex('');943 assert.equal(mq.typedText('x').latex(), 'x');944 assert.equal(mq.typedText('^').latex(), 'x^{ }');945 assert.equal(mq.typedText('^').latex(), 'x^{ }');946 assert.equal(mq.typedText('2').latex(), 'x^2');947 assert.equal(mq.typedText('n').latex(), 'x^{2n}');948 mq.latex('');949 assert.equal(mq.typedText('2').latex(), '2');950 assert.equal(mq.keystroke('Shift-Left').typedText('^').latex(), '^2');951 });952 });953 suite('alternative symbols when typing / and *', function() {954 test('typingSlashWritesDivisionSymbol', function() {955 mq.typedText('/');956 assertLatex('\\frac{ }{ }');957 mq.config({ typingSlashWritesDivisionSymbol: true });958 mq.keystroke('Backspace').typedText('/');959 assertLatex('\\div');960 });961 test('typingAsteriskWritesTimesSymbol', function() {962 mq.typedText('*');963 assertLatex('\\cdot');964 mq.config({ typingAsteriskWritesTimesSymbol: true });965 mq.keystroke('Backspace').typedText('*');966 assertLatex('\\times');967 });968 });...

Full Screen

Full Screen

calc.js

Source:calc.js Github

copy

Full Screen

1$(function() {2 damage_result();3 // わざ選択のモーダル部分4 function move_ajax() {5 var input = $('#move__search').val();6 $.ajax({7 type: "GET",8 url: "move/search",9 data: {keyword: input},10 })11 .fail(function() {12 alert("わざの検索時にエラーが発生しました");13 });14 }15 $('.moves').on('click', '.move__text_form', function() {16 var target_move_id = $(event.target).attr('id');17 $('#move__search').val("");18 move_ajax();19 $('.move__modal').fadeIn(100);20 $('#move__search').on('keyup', function() {21 move_ajax()22 })23 $('.move__modal').on('click', '.modal__move', function() {24 var move_id = $(event.target).closest('.modal__move').attr('val');25 $.ajax({26 type: "GET",27 url: "move/result",28 data: {move_id: move_id, target_move: target_move_id},29 })30 .done(function() {31 damage_result();32 })33 .fail(function() {34 alert("わざの決定時にエラーが発生しました");35 });36 $('.move__modal').off('click');37 $('.move__modal').fadeOut(100);38 });39 $('.move__modal--background').on('click', function() {40 $('.move__modal').fadeOut(100);41 });42 })43 // インクリメンタルサーチを実行する部分を定義44 function search_ajax(input, target, side) {45 $.ajax({46 type: "GET",47 url: "/calc/search",48 data: {keyword: input},49 dataType: "json"50 })51 .done(function(pokemons) {52 $(target).empty();53 pokemons.forEach(function(pokemon){54 var HTML = `<div class="pokemon__result_${side}">${pokemon.name}</div>`55 $(target).append(HTML);56 });57 })58 .fail(function(){59 alert("エラーが発生しました");60 });61 };62 function result_ajax(input, side) {63 $.ajax({64 type: "GET",65 url: "/calc/result",66 data: {keyword: input},67 dataType: "json"68 })69 .done(function(pokemon) {70 $('#search_'+side).val(pokemon.name);71 $('#name_'+side).text(pokemon.name);72 $('#hp_base_status_'+side).text(pokemon.hp);73 $('#attack_base_status_'+side).text(pokemon.attack);74 $('#defence_base_status_'+side).text(pokemon.defence);75 $('#sp_atk_base_status_'+side).text(pokemon.sp_atk);76 $('#sp_def_base_status_'+side).text(pokemon.sp_def);77 $('#speed_base_status_'+side).text(pokemon.speed);78 hp_ev_calc(side);79 status_ev_calc(side, "attack");80 status_ev_calc(side, "defence");81 status_ev_calc(side, "sp_atk");82 status_ev_calc(side, "sp_def");83 status_ev_calc(side, "speed");84 $('#abilities_'+side).empty();85 pokemon.abilities.forEach(function(ability) {86 var HTML = `<option value="${ability.id}">${ability.name}</option>`87 $('#abilities_'+side).append(HTML);88 });89 $('#type_'+side).empty();90 pokemon.types.forEach(function(type) {91 var HTML = `<div class="pokemon__type--${type.id}">${type.name}</div>92 <div class="body__hidden" id="pokemon_type1_${side}">${type.id}</div>`;93 $('#type_'+side).append(HTML);94 damage_result();95 })96 })97 .fail(function(){98 alert("エラーが発生しました");99 });100 }101// ポケモンのステータスを計算する関数を定義102 function status_acquire(side, stts) {103 var base_status = Number($('#'+stts+'_base_status_'+side).text());104 var iv = Number($('#'+stts+'_iv_'+side).val());105 var ev = Number($('#'+stts+'_ev_'+side).val());106 var level = Number($('#level_'+side).val());107 var value = Number($('#'+stts+'_value_'+side).val());108 nature_effect = nature_effect_calc(side, stts)109 return {base_status, iv, ev, level, value, nature_effect}110 };111 function nature_effect_calc(side, stts) {112 var nature = Number($('#nature_'+side).val());113 up_stts_id = Math.floor(nature / 10);114 down_stts_id = nature % 10;115 if (up_stts_id === down_stts_id) {116 return 1;117 } else {118 if (stts === "attack") {stts_id = 1}119 else if (stts === "defence") {stts_id = 2}120 else if (stts === "sp_atk") {stts_id = 3}121 else if (stts === "sp_def") {stts_id = 4}122 else if (stts === "speed") {stts_id = 5}123 else {return 1};124 if (stts_id === up_stts_id) {return 1.1}125 else if (stts_id === down_stts_id) {return 0.9}126 else {return 1};127 };128 }129 function hp_iv_calc(side) {130 var {base_status, iv, ev, level, value, nature_effect} = status_acquire(side, "hp");131 $('.table__hp_iv_'+side).text(iv);132 var value = Math.floor(((base_status*2) + iv + (ev*0.25))/100*level + 10 + level);133 $('#hp_value_'+side).val(value);134 $(event.target).blur();135 };136 function hp_ev_calc(side) {137 var {base_status, iv, ev, level, value, nature_effect} = status_acquire(side, "hp");138 $('.table__hp_ev_'+side).text(ev);139 var value = Math.floor(((base_status*2) + iv + (ev*0.25))/100*level + 10 + level);140 $('#hp_value_'+side).val(value);141 $(event.target).blur();142 };143 function hp_value_calc(side) {144 var {base_status, iv, ev, level, value, nature_effect} = status_acquire(side, "hp");145 var ev = Math.floor(((value - 10 - level)*100/level - (base_status*2) - iv)*4);146 if (ev <= -8 || 252 < ev) {147 var ev = "";148 } else if (-8 < ev && ev < 0) {149 var ev = 0;150 };151 $('#hp_ev_'+side).val(ev);152 $('.table__hp_ev_'+side).text(ev);153 $(event.target).blur();154 };155 function status_iv_calc(side, stts) {156 var {base_status, iv, ev, level, value, nature_effect} = status_acquire(side, stts);157 $('.table__'+stts+'_iv_'+side).text(iv);158 var value = Math.floor(Math.floor(((base_status*2) + iv + (ev*0.25))/100*level + 5)*nature_effect);159 $('#'+stts+'_value_'+side).val(value);160 $(event.target).blur();161 };162 function status_ev_calc(side, stts) {163 var {base_status, iv, ev, level, value, nature_effect} = status_acquire(side, stts);164 $('.table__'+stts+'_ev_'+side).text(ev);165 var value = Math.floor(Math.floor(((base_status*2) + iv + (ev*0.25))/100*level + 5)*nature_effect);166 $('#'+stts+'_value_'+side).val(value);167 $(event.target).blur();168 };169 function status_value_calc(side, stts) {170 var {base_status, iv, ev, level, value, nature_effect} = status_acquire(side, stts);171 var ev = Math.floor(((value - 5)*100/level - (base_status*2) - iv)*4);172 if (ev <= -8 || 252 < ev) {173 var ev = "";174 } else if (-8 < ev && ev < 0) {175 var ev = 0;176 };177 $('#'+stts+'_ev_'+side).val(ev);178 $('.table__'+stts+'_ev_'+side).text(ev);179 $(event.target).blur();180 };181 function nature_correction(side, nature_id) {182 var up_status_id = Math.floor(nature_id / 10);183 var down_status_id = nature_id % 10;184 $('.table_'+side+' .table__correction--up').removeClass("table__correction--up");185 $('.table_'+side+' .table__correction--down').removeClass("table__correction--down");186 if (up_status_id === down_status_id) {return};187 $('tr[val=status_'+up_status_id+'_'+side+']').addClass("table__correction--up");188 $('tr[val=status_'+down_status_id+'_'+side+']').addClass("table__correction--down");189 };190 function damage_result() {191 $.ajax({192 type: "GET",193 url: "/calc/damage",194 data: {195 pokemon_left: $('#name_left').text(),196 move_left: $('#move1_left').text(),197 ability_left: $('#ability_left').val(),198 item_left: $('#item_left').val(),199 status_ailment_left: $('#status_ailment_left').val(),200 hp_value_left: $('#hp_value_left').val(),201 attack_value_left: $('#attack_value_left').val(),202 defence_value_left: $('#defence_value_left').val(),203 sp_atk_value_left: $('#sp_atk_value_left').val(),204 sp_def_value_left: $('#sp_def_value_left').val(),205 speed_value_left: $('#speed_value_left').val(),206 attack_rank_left: $('#attack_rank_left').val(),207 defence_rank_left: $('#defence_rank_left').val(),208 sp_atk_rank_left: $('#sp_atk_rank_left').val(),209 sp_def_rank_left: $('#sp_def_rank_left').val(),210 speed_rank_left: $('#speed_rank_left').val(),211 accuracy_rank_left: $('#accuracy_rank_left').val(),212 evasion_rank_left: $('#evasion_rank_left').val(),213 critical_rank_left: $('#critical_rank_left').val(),214 level_left: $('#level_left').val(),215 pokemon_right: $('#name_right').text(),216 move_right: $('#move1_right').text(),217 ability_right: $('#ability_right').val(),218 item_right: $('#item_right').val(),219 status_ailment_right: $('#status_ailment_right').val(),220 hp_value_right: $('#hp_value_right').val(),221 attack_value_right: $('#attack_value_right').val(),222 defence_value_right: $('#defence_value_right').val(),223 sp_atk_value_right: $('#sp_atk_value_right').val(),224 sp_def_value_right: $('#sp_def_value_right').val(),225 speed_value_right: $('#speed_value_right').val(),226 attack_rank_right: $('#attack_rank_right').val(),227 defence_rank_right: $('#defence_rank_right').val(),228 sp_atk_rank_right: $('#sp_atk_rank_right').val(),229 sp_def_rank_right: $('#sp_def_rank_right').val(),230 speed_rank_right: $('#speed_rank_right').val(),231 accuracy_rank_right: $('#accuracy_rank_right').val(),232 evasion_rank_right: $('#evasion_rank_right').val(),233 critical_rank_right: $('#critical_rank_right').val(),234 level_right: $('#level_right').val(),235 weather: $('#weather').val(),236 field: $('#field').val(),237 trick_room: $('#trick_room'). val(),238 wall_physical_left: $('#wall_physical_left'). val(),239 wall_physical_right: $('#wall_physical_right'). val(),240 wall_special_left: $('#wall_special_left'). val(),241 wall_special_right: $('#wall_special_right'). val(),242 war_shot_left: $('#war_shot_left'). val(),243 war_shot_right: $('#war_shot_right'). val(),244 }245 })246 .done(function(event) {247 $('.result').html(event);248 })249 .fail(function() {250 alert("エラーが発生しました")251 })252 }253 $('.body').on('change', function() {254 damage_result();255 })256 // 文字入力もしくはフォームをクリックで検索結果が表示される257 $('#search_left').on('keyup click', function(){258 var input = $('#search_left').val();259 search_ajax(input, "#search_list_left", "left");260 })261 $('.left').on('click', '.pokemon__result_left', function() {262 var input = $(event.target).text();263 result_ajax(input, "left");264 })265 $('#search_right').on('keyup click', function(){266 var input = $('#search_right').val();267 search_ajax(input, "#search_list_right", "right");268 });269 $('.right').on('click', '.pokemon__result_right', function() {270 var input = $(event.target).text();271 result_ajax(input, "right");272 });273// どこかをクリックすると検索結果が削除される274 $(document).on("click", function(){275 $('.pokemon__search_result').empty();276 });277// ステータス部分を変更した際に再計算278 $('.left').on('change', '#hp_iv_left', function() {279 hp_iv_calc("left");280 });281 $('.left').on('change', '#hp_ev_left', function() {282 hp_ev_calc("left");283 });284 $('.left').on('change', '#hp_value_left', function() {285 hp_value_calc("left");286 });287 $('.left').on('change', '#attack_iv_left', function() {288 status_iv_calc("left", "attack");289 });290 $('.left').on('change', '#attack_ev_left', function() {291 status_ev_calc("left", "attack");292 });293 $('.left').on('change', '#attack_value_left', function() {294 status_value_calc("left", "attack");295 });296 $('.left').on('change', '#defence_iv_left', function() {297 status_iv_calc("left", "defence");298 });299 $('.left').on('change', '#defence_ev_left', function() {300 status_ev_calc("left", "defence");301 });302 $('.left').on('change', '#defence_value_left', function() {303 status_value_calc("left", "defence");304 });305 $('.left').on('change', '#sp_atk_iv_left', function() {306 status_iv_calc("left", "sp_atk");307 });308 $('.left').on('change', '#sp_atk_ev_left', function() {309 status_ev_calc("left", "sp_atk");310 });311 $('.left').on('change', '#sp_atk_value_left', function() {312 status_value_calc("left", "sp_atk");313 });314 $('.left').on('change', '#sp_def_iv_left', function() {315 status_iv_calc("left", "sp_def");316 });317 $('.left').on('change', '#sp_def_ev_left', function() {318 status_ev_calc("left", "sp_def");319 });320 $('.left').on('change', '#sp_def_value_left', function() {321 status_value_calc("left", "sp_def");322 });323 $('.left').on('change', '#speed_iv_left', function() {324 status_iv_calc("left", "speed");325 });326 $('.left').on('change', '#speed_ev_left', function() {327 status_ev_calc("left", "speed");328 });329 $('.left').on('change', '#speed_value_left', function() {330 status_value_calc("left", "speed");331 });332 $('.left').on('change', '#level_left', function() {333 status_ev_calc("left", "attack");334 status_ev_calc("left", "defence");335 status_ev_calc("left", "sp_atk");336 status_ev_calc("left", "sp_def");337 status_ev_calc("left", "speed");338 });339 $('.left').on('change', '#nature_left', function() {340 status_ev_calc("left", "attack");341 status_ev_calc("left", "defence");342 status_ev_calc("left", "sp_atk");343 status_ev_calc("left", "sp_def");344 status_ev_calc("left", "speed");345 });346 $('#nature_left').on('click', function() {347 var original_nature_id = $("#nature_left").val();348 $('.select_box_left').show();349 $('#nature_left').val();350 $('.select_box__line').on('mouseenter', function() {351 var nature_id = $(event.target).attr("val");352 $('#nature_left').val(nature_id);353 nature_correction("left", nature_id);354 status_ev_calc("left", "attack");355 status_ev_calc("left", "defence");356 status_ev_calc("left", "sp_atk");357 status_ev_calc("left", "sp_def");358 status_ev_calc("left", "speed");359 });360 $('.select_box_left .select_box__line').on('click', function() {361 var nature_name = $(event.target).text();362 var nature_id = $(event.target).attr("val");363 $('#nature_left').text(nature_name);364 $('#nature_left').val(nature_id);365 $('.select_box_left .select_box__line--selected').addClass("select_box__line").removeClass("select_box__line--selected");366 $(event.target).addClass("select_box__line--selected").removeClass("select_box__line");367 $('.select_box__line').off('mouseenter');368 damage_result();369 });370 $('.select_box__modal').on('click', function() {371 $('.select_box__line').off('mouseenter');372 $('#nature_left').val(original_nature_id);373 nature_correction("left", original_nature_id);374 status_ev_calc("left", "attack");375 status_ev_calc("left", "defence");376 status_ev_calc("left", "sp_atk");377 status_ev_calc("left", "sp_def");378 status_ev_calc("left", "speed");379 })380 });381 $('.select_box_left').on('click', function() {382 $('.select_box_left').fadeOut(200);383 });384 $('.right').on('change', '#hp_iv_right', function() {385 hp_iv_calc("right");386 });387 $('.right').on('change', '#hp_ev_right', function() {388 hp_ev_calc("right");389 });390 $('.right').on('change', '#hp_value_right', function() {391 hp_value_calc("right");392 });393 $('.right').on('change', '#attack_iv_right', function() {394 status_iv_calc("right", "attack");395 });396 $('.right').on('change', '#attack_ev_right', function() {397 status_ev_calc("right", "attack");398 });399 $('.right').on('change', '#attack_value_right', function() {400 status_value_calc("right", "attack");401 });402 $('.right').on('change', '#defence_iv_right', function() {403 status_iv_calc("right", "defence");404 });405 $('.right').on('change', '#defence_ev_right', function() {406 status_ev_calc("right", "defence");407 });408 $('.right').on('change', '#defence_value_right', function() {409 status_value_calc("right", "defence");410 });411 $('.right').on('change', '#sp_atk_iv_right', function() {412 status_iv_calc("right", "sp_atk");413 });414 $('.right').on('change', '#sp_atk_ev_right', function() {415 status_ev_calc("right", "sp_atk");416 });417 $('.right').on('change', '#sp_atk_value_right', function() {418 status_value_calc("right", "sp_atk");419 });420 $('.right').on('change', '#sp_def_iv_right', function() {421 status_iv_calc("right", "sp_def");422 });423 $('.right').on('change', '#sp_def_ev_right', function() {424 status_ev_calc("right", "sp_def");425 });426 $('.right').on('change', '#sp_def_value_right', function() {427 status_value_calc("right", "sp_def");428 });429 $('.right').on('change', '#speed_iv_right', function() {430 status_iv_calc("right", "speed");431 });432 $('.right').on('change', '#speed_ev_right', function() {433 status_ev_calc("right", "speed");434 });435 $('.right').on('change', '#speed_value_right', function() {436 status_value_calc("right", "speed");437 });438 $('.right').on('change', '#level_right', function() {439 status_ev_calc("right", "attack");440 status_ev_calc("right", "defence");441 status_ev_calc("right", "sp_atk");442 status_ev_calc("right", "sp_def");443 status_ev_calc("right", "speed");444 });445 $('.right').on('change', '#nature_right', function() {446 status_ev_calc("right", "attack");447 status_ev_calc("right", "defence");448 status_ev_calc("right", "sp_atk");449 status_ev_calc("right", "sp_def");450 status_ev_calc("right", "speed");451 });452 $('#nature_right').on('click', function() {453 var original_nature_id = $("#nature_right").val();454 $('.select_box_right').show();455 $('#nature_right').val();456 $('.select_box__line').on('mouseenter', function() {457 var nature_id = $(event.target).attr("val");458 $('#nature_right').val(nature_id);459 nature_correction("right", nature_id);460 status_ev_calc("right", "attack");461 status_ev_calc("right", "defence");462 status_ev_calc("right", "sp_atk");463 status_ev_calc("right", "sp_def");464 status_ev_calc("right", "speed");465 });466 $('.select_box_right .select_box__line').on('click', function() {467 var nature_name = $(event.target).text();468 var nature_id = $(event.target).attr("val");469 $('#nature_right').text(nature_name);470 $('#nature_right').val(nature_id);471 $('.select_box_right .select_box__line--selected').addClass("select_box__line").removeClass("select_box__line--selected");472 $(event.target).addClass("select_box__line--selected").removeClass("select_box__line");473 $('.select_box__line').off('mouseenter');474 damage_result();475 });476 $('.select_box__modal').on('click', function() {477 $('.select_box__line').off('mouseenter');478 $('#nature_right').val(original_nature_id);479 nature_correction("right", original_nature_id);480 status_ev_calc("right", "attack");481 status_ev_calc("right", "defence");482 status_ev_calc("right", "sp_atk");483 status_ev_calc("right", "sp_def");484 status_ev_calc("right", "speed");485 })486 });487 $('.select_box_right').on('click', function() {488 $('.select_box_right').fadeOut(200);489 });...

Full Screen

Full Screen

flexible-admin.js

Source:flexible-admin.js Github

copy

Full Screen

1// $Id: flexible-admin.js,v 1.1.2.5 2010/06/23 00:35:40 merlinofchaos Exp $2Drupal.flexible = Drupal.flexible || {};3Drupal.flexible.splitters = [];4/**5 * Fix the height of all splitters to be the same as the items they are6 * splitting.7 */8Drupal.flexible.fixHeight = function() {9 for (i in Drupal.flexible.splitters) {10 Drupal.flexible.splitters[i].fixHeight();11 }12}13Drupal.behaviors.flexibleAdmin = function(context) {14 // Show/hide layout manager button15 $('input#panels-flexible-toggle-layout:not(.panels-flexible-processed)', context)16 .addClass('panels-flexible-processed')17 .click(function() {18 $('.panel-flexible-admin')19 .toggleClass('panel-flexible-no-edit-layout')20 .toggleClass('panel-flexible-edit-layout');21 if ($('.panel-flexible-admin').hasClass('panel-flexible-edit-layout')) {22 $(this).val(Drupal.t('Hide layout designer'));23 Drupal.flexible.fixHeight();24 }25 else {26 $(this).val(Drupal.t('Show layout designer'));27 }28 return false;29 });30 // Window splitter behavior.31 $('div.panels-flexible-splitter:not(.panels-splitter-processed)', context)32 .addClass('panels-splitter-processed')33 .each(function() {34 Drupal.flexible.splitters.push(new Drupal.flexible.splitter($(this)));35 });36 // Sometimes the splitter IS the context and the above syntax won't37 // catch that.38 if ($(context).hasClass('panels-flexible-splitter')) {39 $(context)40 .addClass('panels-splitter-processed')41 .each(function() {42 Drupal.flexible.splitters.push(new Drupal.flexible.splitter($(this)));43 });44 }45 Drupal.flexible.fixHeight();46};47Drupal.flexible.splitter = function($splitter) {48 var splitter = this;49 this.fixHeight = function() {50 // Set the splitter height to the shorter of the two:51 $splitter.height(Math.max(this.left.outerHeight(), this.right.outerHeight()));52 }53 function splitterStart(event) {54 // Show splitting classes.55// splitter.left.addClass('flexible-splitting'); // Safari selects A/B text on a move56// splitter.right.addClass('flexible-splitting'); // Safari selects A/B text on a move57// splitter.splitter.addClass('flexible-splitter-splitting');58 // Bind motion events.59 $(document)60 .bind("mousemove", splitterMove)61 .bind("mouseup", splitterEnd);62 // Calculate some data about our split regions:63 splitter.getSizes();64 // The X coordinate where we clicked.65 splitter.startX = event.pageX;66 // The current sizes of the left/right panes.67 splitter.currentLeft = parseFloat(splitter.left_width) * parseFloat(splitter.left_scale);68 splitter.currentRight = parseFloat(splitter.right_width) * parseFloat(splitter.right_scale);69 // The starting sizes of the left right panes.70 splitter.startLeft = splitter.currentLeft;71 splitter.startRight = splitter.currentRight;72 if (splitter.left_width_type == splitter.right_width_type) {73 // If they're the same type, add the two together so we know how74 // much space we have for splitting.75 splitter.max = splitter.startLeft + splitter.startRight;76 // calculate unit size and min/max width.77 if (splitter.left_width_type == '%') {78 splitter.left_total = splitter.left.width() / (splitter.left_width / 100);79 // One pixel is equivalent to what percentage of the total?80 splitter.left_unit = (1 / splitter.left_total) * 100;81 splitter.left_min = 5; // minimum % we'll use.82 }83 else {84 splitter.left_unit = 1;85 splitter.left_min = 25; // minimum pixels we'll use.86 }87 if (splitter.right_width_type == '%') {88 splitter.right_total = splitter.right.width() / (splitter.right_width / 100);89 // One pixel is equivalent to what percentage of the total?90 splitter.right_unit = (1 / splitter.right_total) * 100;91 splitter.right_min = 5; // minimum % we'll use.92 }93 else {94 splitter.right_unit = 1;95 splitter.right_min = 25; // minimum pixels we'll use.96 }97 }98 else {99 // Figure out the parent blob's width and set the max to that100 splitter.parent = $splitter.parent().parent();101 if (splitter.left_width_type != 'px') {102 // Only the 'px' side can resize.103 splitter.left_unit = 0;104 splitter.right_unit = 1;105 splitter.right_min = 25;106 splitter.right_padding = parseInt(splitter.parent.css('padding-right'));107 splitter.right_parent = parseInt(splitter.right.parent().css('margin-right'));108 splitter.max = splitter.right.width() + splitter.left.parent().width() -109 (splitter.left.siblings(':not(.panels-flexible-splitter)').length * 25) - 25;110 }111 else {112 splitter.right_unit = 0;113 splitter.left_unit = 1;114 splitter.left_min = 25;115 splitter.left_padding = parseInt(splitter.parent.css('padding-left'));116 splitter.left_parent = parseInt(splitter.left.parent().css('margin-left'));117 if (splitter.right_id) {118 splitter.max = splitter.left.width() + splitter.right.parent().width() -119 (splitter.right.siblings(':not(.panels-flexible-splitter)').length * 25) - 25;120 }121 else {122 var subtract = 0;123 splitter.left.siblings(':not(.panels-flexible-splitter)').each(function() { subtract += $(this).width()});124 splitter.max = splitter.left.parent().width() - subtract;125 }126 }127 }128 var offset = $(splitter.splitter).offset();129 // Create boxes to display widths left and right of the mouse pointer.130 // Create left box only if left box is mobile.131 if (splitter.left_unit) {132 splitter.left_box = $('<div class="flexible-splitter-hover-box">&nbsp;</div>');133 $('body').append(splitter.left_box);134 splitter.left_box.css('top', offset.top);135 splitter.left_box.css('left', event.pageX - 65);136 if (splitter.left_width_type == '%') {137 var left = splitter.currentLeft / splitter.left_scale;138 splitter.left_box.html(left.toFixed(2) + splitter.left_width_type);139 }140 else {141 // make sure pixel values are always whole integers.142 splitter.currentLeft = parseInt(splitter.currentLeft);143 splitter.left_box.html(splitter.currentLeft + splitter.left_width_type);144 }145 }146 // Create the right box if the right side is mobile.147 if (splitter.right_unit) {148 splitter.right_box = $('<div class="flexible-splitter-hover-box"></div>');149 $('body').append(splitter.right_box);150 splitter.right_box.css('top', offset.top);151 splitter.right_box.css('left', event.pageX + 5);152 if (splitter.right_width_type == '%') {153 var right = splitter.currentRight / splitter.right_scale;154 splitter.right_box.html(right.toFixed(2) + splitter.right_width_type);155 }156 else {157 // make sure pixel values are always whole integers.158 splitter.currentRight = parseInt(splitter.currentRight);159 splitter.right_box.html(splitter.currentRight + splitter.right_width_type);160 }161 }162 return false;163 };164 function splitterMove(event) {165 var diff = splitter.startX - event.pageX;166 var moved = 0;167 // Bah, javascript has no logical xor operator168 if ((splitter.left_unit && !splitter.right_unit) ||169 (!splitter.left_unit && splitter.right_unit)) {170 // This happens when one side is fixed and the other side is fluid. The171 // fixed side actually adjusts while the fluid side does not. However,172 // in order to move the fluid side we have to adjust the padding173 // on our parent object.174 if (splitter.left_unit) {175 // Only the left box is allowed to move.176 splitter.currentLeft = splitter.startLeft - diff;177 if (splitter.currentLeft < splitter.left_min) {178 splitter.currentLeft = splitter.left_min;179 }180 if (splitter.currentLeft > splitter.max) {181 splitter.currentLeft = splitter.max;182 }183 // If the shift key is pressed, go with 1% or 10px boundaries.184 if (event.shiftKey) {185 splitter.currentLeft = parseInt(splitter.currentLeft / 10) * 10;186 }187 moved = (splitter.startLeft - splitter.currentLeft);188 }189 else {190 // Only the left box is allowed to move.191 splitter.currentRight = splitter.startRight + diff;192 if (splitter.currentRight < splitter.right_min) {193 splitter.currentRight = splitter.right_min;194 }195 if (splitter.currentRight > splitter.max) {196 splitter.currentRight = splitter.max;197 }198 // If the shift key is pressed, go with 1% or 10px boundaries.199 if (event.shiftKey) {200 splitter.currentRight = parseInt(splitter.currentRight / 10) * 10;201 }202 moved = (splitter.currentRight - splitter.startRight);203 }204 }205 else {206 // If they are both the same type, do this..207 // Adjust the left side by the amount we moved.208 var left = -1 * diff * splitter.left_unit;209 splitter.currentLeft = splitter.startLeft + left;210 if (splitter.currentLeft < splitter.left_min) {211 splitter.currentLeft = splitter.left_min;212 }213 if (splitter.currentLeft > splitter.max - splitter.right_min) {214 splitter.currentLeft = splitter.max - splitter.right_min;215 }216 // If the shift key is pressed, go with 1% or 10px boundaries.217 if (event.shiftKey) {218 if (splitter.left_width_type == '%') {219 splitter.currentLeft = parseInt(splitter.currentLeft / splitter.left_scale) * splitter.left_scale;220 }221 else {222 splitter.currentLeft = parseInt(splitter.currentLeft / 10) * 10;223 }224 }225 // Now automatically make the right side to be the other half.226 splitter.currentRight = splitter.max - splitter.currentLeft;227 // recalculate how far we've moved into pixels so we can adjust our visible228 // boxes.229 moved = (splitter.startLeft - splitter.currentLeft) / splitter.left_unit;230 }231 if (splitter.left_unit) {232 splitter.left_box.css('left', splitter.startX - 65 - moved);233 if (splitter.left_width_type == '%') {234 var left = splitter.currentLeft / splitter.left_scale;235 splitter.left_box.html(left.toFixed(2) + splitter.left_width_type);236 }237 else {238 splitter.left_box.html(parseInt(splitter.currentLeft) + splitter.left_width_type);239 }240 // Finally actually move the left side241 splitter.left.css('width', splitter.currentLeft + splitter.left_width_type);242 }243 else {244 // if not moving the left side, adjust the parent padding instead.245 splitter.parent.css('padding-right', (splitter.right_padding + moved) + 'px');246 splitter.right.parent().css('margin-right', (splitter.right_parent - moved) + 'px');247 }248 if (splitter.right_unit) {249 splitter.right_box.css('left', splitter.startX + 5 - moved);250 if (splitter.right_width_type == '%') {251 var right = splitter.currentRight / splitter.right_scale;252 splitter.right_box.html(right.toFixed(2) + splitter.right_width_type);253 }254 else {255 splitter.right_box.html(parseInt(splitter.currentRight) + splitter.right_width_type);256 }257 // Finally actually move the right side258 splitter.right.css('width', splitter.currentRight + splitter.right_width_type);259 }260 else {261 // if not moving the right side, adjust the parent padding instead.262 splitter.parent.css('padding-left', (splitter.left_padding - moved) + 'px');263 splitter.left.parent().css('margin-left', (splitter.left_parent + moved) + 'px');264 if (jQuery.browser.msie) {265 splitter.left.parent().css('left', splitter.currentLeft);266 }267 }268 return false;269 };270 function splitterEnd(event) {271 if (splitter.left_unit) {272 splitter.left_box.remove();273 }274 if (splitter.right_unit) {275 splitter.right_box.remove();276 }277 splitter.left.removeClass("flexible-splitting"); // Safari selects A/B text on a move278 splitter.right.removeClass("flexible-splitting"); // Safari selects A/B text on a move279 splitter.splitter.removeClass("flexible-splitter-splitting"); // Safari selects A/B text on a move280 splitter.left.css("-webkit-user-select", "text"); // let Safari select text again281 splitter.right.css("-webkit-user-select", "text"); // let Safari select text again282 if (splitter.left_unit) {283 splitter.left_width = splitter.currentLeft / parseFloat(splitter.left_scale);284 }285 if (splitter.right_unit) {286 splitter.right_width = splitter.currentRight / parseFloat(splitter.right_scale);287 }288 splitter.putSizes();289 Drupal.flexible.fixHeight();290 $(document)291 .unbind("mousemove", splitterMove)292 .unbind("mouseup", splitterEnd);293 // Store the data on the server.294 $.ajax({295 type: "POST",296 url: Drupal.settings.flexible.resize,297 data: {298 'left': splitter.left_id,299 'left_width': splitter.left_width,300 'right': splitter.right_id,301 'right_width': splitter.right_width302 },303 global: true,304 success: Drupal.CTools.AJAX.respond,305 error: function() {306 alert("An error occurred while attempting to process " + Drupal.settings.flexible.resize);307 },308 dataType: 'json'309 });310 };311 this.getSizes = function() {312 splitter.left_width = $splitter.children('.panels-flexible-splitter-left-width').html();313 splitter.left_scale = $splitter.children('.panels-flexible-splitter-left-scale').html();314 splitter.left_width_type = $splitter.children('.panels-flexible-splitter-left-width-type').html();315 splitter.right_width = $splitter.children('.panels-flexible-splitter-right-width').html();316 splitter.right_scale = $splitter.children('.panels-flexible-splitter-right-scale').html();317 splitter.right_width_type = $splitter.children('.panels-flexible-splitter-right-width-type').html();318 };319 this.putSizes = function() {320 $(splitter.left_class + '-width').html(splitter.left_width);321 if (splitter.left_class != splitter.right_class) {322 $(splitter.right_class + '-width').html(splitter.right_width);323 }324 }325 splitter.splitter = $splitter;326 splitter.left_class = $splitter.children('.panels-flexible-splitter-left').html();327 splitter.left_id = $splitter.children('.panels-flexible-splitter-left-id').html();328 splitter.left = $(splitter.left_class);329 splitter.right_class = $splitter.children('.panels-flexible-splitter-right').html();330 splitter.right_id = $splitter.children('.panels-flexible-splitter-right-id').html();331 splitter.right = $(splitter.right_class);332 $splitter333 .bind("mousedown", splitterStart);334};335/**336 * Provide an AJAX response command to allow the server to request337 * height fixing.338 */339Drupal.CTools.AJAX.commands.flexible_fix_height = function() {340 Drupal.flexible.fixHeight();341};342/**343 * Provide an AJAX response command to fix the first/last bits of a344 * group.345 */346Drupal.CTools.AJAX.commands.flexible_fix_firstlast = function(data) {347 $(data.selector + ' > div > .' + data.base)348 .removeClass(data.base + '-first')349 .removeClass(data.base + '-last');350 $(data.selector + ' > div > .' + data.base + ':first')351 .addClass(data.base + '-first');352 $(data.selector + ' > div > .' + data.base + ':last')353 .addClass(data.base + '-last');...

Full Screen

Full Screen

exercises.js

Source:exercises.js Github

copy

Full Screen

1import populateTransitionTable from './populateTransitionTable'2import MachineStep from './MachineStep.js'3import {4 DIR_LEFT,5 DIR_RIGHT,6 BLANK7} from './consts.js'8/*9 * This file contains different Turing Machines based on exercises from B4M36TAL class.10 */11let set301 = (transitionTable, machineState, reset, input) => {12 transitionTable = {13 0: {14 "a": new MachineStep(1, "a", DIR_RIGHT)15 },16 1: {17 "a": new MachineStep(1, "a", DIR_RIGHT),18 "b": new MachineStep(2, "b", DIR_RIGHT)19 },20 2: {21 "b": new MachineStep(2, "b", DIR_RIGHT),22 "c": new MachineStep(3, "c", DIR_RIGHT)23 },24 3: {25 "c": new MachineStep(3, "c", DIR_RIGHT),26 "B": new MachineStep(4, BLANK, DIR_LEFT)27 },28 4: {}29 }30 machineState.transitionFunction = transitionTable31 machineState.states = [0, 1, 2, 3, 4]32 machineState.finalStates = [4]33 document.querySelector(".description").innerHTML = "This Turing Machine accepts <br/> L = {w | w = a<sup>i</sup>b<sup>j</sup>c<sup>k</sup>; i,j,k > 0}";34 input = ["a", "a", "b", "b", "c"]35 reset(input)36 populateTransitionTable(transitionTable, ["a", "b", "c", BLANK], machineState)37 return input38}39let set302 = (transitionTable, machineState, reset, input) => {40 transitionTable = {41 0: {42 "a": new MachineStep(1, BLANK, DIR_RIGHT)43 },44 1: {45 "a": new MachineStep(1, "a", DIR_RIGHT),46 "b": new MachineStep(1, "b", DIR_RIGHT),47 "c": new MachineStep(1, "c", DIR_RIGHT),48 "B": new MachineStep(2, BLANK, DIR_LEFT)49 },50 2: {51 "c": new MachineStep(3, BLANK, DIR_LEFT)52 },53 3: {54 "a": new MachineStep(3, "a", DIR_LEFT),55 "b": new MachineStep(3, "b", DIR_LEFT),56 "c": new MachineStep(3, "c", DIR_LEFT),57 "B": new MachineStep(4, BLANK, DIR_RIGHT)58 },59 4: {60 "a": new MachineStep(1, BLANK, DIR_RIGHT),61 "b": new MachineStep(5, BLANK, DIR_RIGHT)62 },63 5: {64 "b": new MachineStep(5, "b", DIR_RIGHT),65 "c": new MachineStep(5, "c", DIR_RIGHT),66 "B": new MachineStep(6, BLANK, DIR_LEFT)67 },68 6: {69 "c": new MachineStep(7, BLANK, DIR_LEFT)70 },71 7: {72 "b": new MachineStep(7, "b", DIR_LEFT),73 "c": new MachineStep(7, "c", DIR_LEFT),74 "B": new MachineStep(8, BLANK, DIR_RIGHT)75 },76 8: {77 "b": new MachineStep(5, BLANK, DIR_RIGHT),78 "B": new MachineStep(9, BLANK, DIR_RIGHT)79 },80 9: {}81 }82 machineState.transitionFunction = transitionTable83 machineState.states = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]84 machineState.finalStates = [9]85 document.querySelector(".description").innerHTML = "This Turing Machine accepts <br/> L = {w | w = a<sup>i</sup>b<sup>j</sup>c<sup>k</sup>; i,j,k > 0, i+j=k}";86 input = ["a", "a", "b", "b", "c", "c", "c", "c"]87 reset(input)88 populateTransitionTable(transitionTable, ["a", "b", "c", BLANK], machineState)89 return input90}91let set303 = (transitionTable, machineState, reset, input) => {92 transitionTable = {93 0: {94 "a": new MachineStep(1, "B", DIR_RIGHT),95 "b": new MachineStep(0, "B", DIR_RIGHT),96 "B": new MachineStep(6, "B", DIR_RIGHT)97 },98 1: {99 "a": new MachineStep(1, "a", DIR_RIGHT),100 "b": new MachineStep(2, "X", DIR_RIGHT)101 },102 2: {103 "b": new MachineStep(2, "b", DIR_RIGHT),104 "c": new MachineStep(2, "c", DIR_RIGHT),105 "B": new MachineStep(3, "B", DIR_LEFT)106 },107 3: {108 "c": new MachineStep(4, "B", DIR_LEFT)109 },110 4: {111 "b": new MachineStep(5, "b", DIR_LEFT),112 "c": new MachineStep(4, "c", DIR_LEFT),113 "X": new MachineStep(5, "b", DIR_LEFT)114 },115 5: {116 "a": new MachineStep(5, "a", DIR_LEFT),117 "b": new MachineStep(5, "b", DIR_LEFT),118 "X": new MachineStep(1, "b", DIR_RIGHT),119 "B": new MachineStep(0, "B", DIR_RIGHT)120 },121 6: {}122 }123 machineState.transitionFunction = transitionTable124 machineState.states = [0, 1, 2, 3, 4, 5, 6]125 machineState.finalStates = [6]126 document.querySelector(".description").innerHTML = "This Turing Machine accepts <br/> L = {w | w = a<sup>i</sup>b<sup>j</sup>c<sup>k</sup>; i,j,k > 0, i*j=k}";127 input = ["a", "a", "b", "b", "c", "c", "c", "c"]128 reset(input)129 populateTransitionTable(transitionTable, ["a", "b", "c", "X", BLANK], machineState)130 return input131}132let set31 = (transitionTable, machineState, reset, input) => {133 transitionTable = {134 0: {135 "0": new MachineStep(1, "0", DIR_LEFT),136 "1": new MachineStep(1, "1", DIR_LEFT),137 "B": new MachineStep(7, "0", DIR_RIGHT)138 },139 1: {140 "B": new MachineStep(2, "X", DIR_RIGHT)141 },142 2: {143 "0": new MachineStep(3, "Y", DIR_LEFT),144 "1": new MachineStep(3, "Z", DIR_LEFT)145 },146 3: {147 "0": new MachineStep(3, "0", DIR_LEFT),148 "1": new MachineStep(3, "1", DIR_LEFT),149 "X": new MachineStep(3, "X", DIR_LEFT),150 "B": new MachineStep(4, "0", DIR_RIGHT)151 },152 4: {153 "0": new MachineStep(4, "0", DIR_RIGHT),154 "1": new MachineStep(4, "1", DIR_RIGHT),155 "X": new MachineStep(4, "X", DIR_RIGHT),156 "Y": new MachineStep(5, "0", DIR_RIGHT),157 "Z": new MachineStep(5, "1", DIR_RIGHT)158 },159 5: {160 "0": new MachineStep(3, "Y", DIR_LEFT),161 "1": new MachineStep(3, "Z", DIR_LEFT),162 "B": new MachineStep(6, "B", DIR_LEFT)163 },164 6: {165 "0": new MachineStep(6, "0", DIR_LEFT),166 "1": new MachineStep(6, "1", DIR_LEFT),167 "X": new MachineStep(7, "0", DIR_RIGHT)168 },169 7: {}170 }171 machineState.transitionFunction = transitionTable172 machineState.states = [0, 1, 2, 3, 4, 5, 6, 7]173 machineState.finalStates = [7]174 document.querySelector(".description").innerHTML = "This Turing Machine realizes function<br/> f(w) = 0<sup>k+1</sup>w, k = |w|";175 input = ["0", "1", "1"]176 reset(input)177 populateTransitionTable(transitionTable, ["0", "1", "X", "Y", "Z", BLANK], machineState)178 return input179}180let set32 = (transitionTable, machineState, reset, input) => {181 transitionTable = {182 0: {183 "0": new MachineStep(1, "a", DIR_RIGHT),184 "1": new MachineStep(1, "b", DIR_RIGHT),185 "X": new MachineStep(4, "X", DIR_RIGHT),186 "Y": new MachineStep(4, "Y", DIR_RIGHT),187 "B": new MachineStep(6, "B", DIR_RIGHT)188 },189 1: {190 "0": new MachineStep(1, "0", DIR_RIGHT),191 "1": new MachineStep(1, "1", DIR_RIGHT),192 "X": new MachineStep(2, "X", DIR_LEFT),193 "Y": new MachineStep(2, "Y", DIR_LEFT),194 "B": new MachineStep(2, "B", DIR_LEFT)195 },196 2: {197 "0": new MachineStep(3, "X", DIR_LEFT),198 "1": new MachineStep(3, "Y", DIR_LEFT),199 "a": new MachineStep(4, "X", DIR_RIGHT),200 "b": new MachineStep(4, "Y", DIR_RIGHT),201 },202 3: {203 "0": new MachineStep(3, "0", DIR_LEFT),204 "1": new MachineStep(3, "1", DIR_LEFT),205 "a": new MachineStep(0, "0", DIR_RIGHT),206 "b": new MachineStep(0, "1", DIR_RIGHT),207 },208 4: {209 "X": new MachineStep(4, "X", DIR_RIGHT),210 "Y": new MachineStep(4, "X", DIR_RIGHT),211 "B": new MachineStep(5, "B", DIR_LEFT)212 },213 5: {214 "0": new MachineStep(6, "0", DIR_LEFT),215 "1": new MachineStep(6, "1", DIR_LEFT),216 "a": new MachineStep(6, "0", DIR_LEFT),217 "b": new MachineStep(6, "1", DIR_LEFT),218 "X": new MachineStep(5, "B", DIR_LEFT),219 "Y": new MachineStep(5, "B", DIR_LEFT)220 },221 6: {}222 }223 machineState.transitionFunction = transitionTable224 machineState.states = [0, 1, 2, 3, 4, 5, 6]225 machineState.finalStates = [6]226 document.querySelector(".description").innerHTML = "This Turing Machine realizes function<br/> f(a<sub>1</sub>a<sub>1</sub>..a<sub>n</sub>) = a<sub>1</sub>a<sub>1</sub>..a<sub>k</sub>, k = floor(n/2)";227 input = ["0", "1", "1", "0", "1"]228 reset(input)229 populateTransitionTable(transitionTable, ["0", "1", "a", "b", "X", "Y", BLANK], machineState)230 return input231}232let set33 = (transitionTable, machineState, reset, input) => {233 transitionTable = {234 0: {235 "a": new MachineStep(0, "a", DIR_RIGHT),236 "b": new MachineStep(0, "b", DIR_RIGHT),237 "c": new MachineStep(1, "Z", DIR_RIGHT)238 },239 1: {240 "a": new MachineStep(1, "a", DIR_RIGHT),241 "b": new MachineStep(2, "c", DIR_LEFT),242 "c": new MachineStep(1, "c", DIR_RIGHT),243 "B": new MachineStep(3, "B", DIR_LEFT)244 },245 2: {246 "a": new MachineStep(2, "a", DIR_LEFT),247 "b": new MachineStep(2, "b", DIR_LEFT),248 "c": new MachineStep(2, "c", DIR_LEFT),249 "Z": new MachineStep(0, "b", DIR_RIGHT)250 },251 3: {252 "a": new MachineStep(3, "a", DIR_LEFT),253 "b": new MachineStep(3, "b", DIR_LEFT),254 "c": new MachineStep(3, "c", DIR_LEFT),255 "Z": new MachineStep(3, "c", DIR_LEFT),256 "B": new MachineStep(4, "B", DIR_RIGHT),257 },258 4: {259 "a": new MachineStep(4, "a", DIR_RIGHT),260 "b": new MachineStep(5, "Y", DIR_RIGHT),261 "c": new MachineStep(4, "c", DIR_RIGHT)262 },263 5: {264 "a": new MachineStep(6, "b", DIR_LEFT),265 "b": new MachineStep(5, "b", DIR_RIGHT),266 "c": new MachineStep(5, "c", DIR_RIGHT),267 "B": new MachineStep(7, "B", DIR_LEFT)268 },269 6: {270 "a": new MachineStep(6, "a", DIR_LEFT),271 "b": new MachineStep(6, "b", DIR_LEFT),272 "c": new MachineStep(6, "c", DIR_LEFT),273 "Y": new MachineStep(4, "a", DIR_RIGHT)274 },275 7: {276 "a": new MachineStep(7, "a", DIR_LEFT),277 "b": new MachineStep(7, "b", DIR_LEFT),278 "c": new MachineStep(7, "c", DIR_LEFT),279 "Y": new MachineStep(7, "b", DIR_LEFT),280 "B": new MachineStep(8, "B", DIR_RIGHT)281 },282 8: {283 "a": new MachineStep(8, "a", DIR_RIGHT),284 "b": new MachineStep(8, "b", DIR_RIGHT),285 "c": new MachineStep(9, "Z", DIR_RIGHT)286 },287 9: {288 "a": new MachineStep(9, "a", DIR_RIGHT),289 "b": new MachineStep(10, "c", DIR_LEFT),290 "c": new MachineStep(9, "c", DIR_RIGHT),291 "B": new MachineStep(11, "B", DIR_LEFT)292 },293 10: {294 "a": new MachineStep(10, "a", DIR_LEFT),295 "b": new MachineStep(10, "b", DIR_LEFT),296 "c": new MachineStep(10, "c", DIR_LEFT),297 "Z": new MachineStep(8, "b", DIR_RIGHT)298 },299 11: {300 "a": new MachineStep(11, "a", DIR_LEFT),301 "b": new MachineStep(11, "b", DIR_LEFT),302 "c": new MachineStep(11, "c", DIR_LEFT),303 "Z": new MachineStep(11, "c", DIR_LEFT),304 "B": new MachineStep(12, "B", DIR_RIGHT),305 },306 12: {}307 }308 machineState.transitionFunction = transitionTable309 machineState.states = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]310 machineState.finalStates = [12]311 document.querySelector(".description").innerHTML = "This Turing Machine realizes function<br/> f(w) = a<sub>k</sub>b<sub>l</sub>c<sub>m</sub>, k = |w|<sub>a</sub>, L = |w|<sub>l</sub>, m = |w|<sub>m</sub>";312 input = ["a", "c", "b", "a", "c", "a", "b", "c"]313 reset(input)314 populateTransitionTable(transitionTable, ["a", "b", "c", "X", "Y", "Z", BLANK], machineState)315 return input316}317let set34 = (transitionTable, machineState, reset, input) => {318 transitionTable = {319 0: {320 "0": new MachineStep(0, "0", DIR_RIGHT),321 "1": new MachineStep(0, "1", DIR_RIGHT),322 "B": new MachineStep(1, "S", DIR_LEFT)323 },324 1: {325 "0": new MachineStep(1, "0", DIR_LEFT),326 "1": new MachineStep(1, "1", DIR_LEFT),327 "B": new MachineStep(2, "B", DIR_RIGHT)328 },329 2: {330 "0": new MachineStep(3, "B", DIR_RIGHT),331 "1": new MachineStep(9, "B", DIR_RIGHT),332 },333 3: {334 "0": new MachineStep(3, "0", DIR_RIGHT),335 "1": new MachineStep(4, "1", DIR_RIGHT)336 },337 4: {338 "0": new MachineStep(5, "X", DIR_RIGHT),339 "1": new MachineStep(8, "1", DIR_LEFT),340 "S": new MachineStep(8, "S", DIR_LEFT),341 },342 5: {343 "0": new MachineStep(5, "0", DIR_RIGHT),344 "1": new MachineStep(5, "1", DIR_RIGHT),345 "S": new MachineStep(6, "S", DIR_RIGHT)346 },347 6: {348 "0": new MachineStep(6, "0", DIR_RIGHT),349 "B": new MachineStep(7, "0", DIR_LEFT)350 },351 7: {352 "0": new MachineStep(7, "0", DIR_LEFT),353 "1": new MachineStep(7, "1", DIR_LEFT),354 "S": new MachineStep(7, "S", DIR_LEFT),355 "X": new MachineStep(4, "0", DIR_RIGHT),356 },357 8: {358 "0": new MachineStep(8, "0", DIR_LEFT),359 "1": new MachineStep(8, "1", DIR_LEFT),360 "B": new MachineStep(2, "B", DIR_RIGHT),361 },362 9: {363 "0": new MachineStep(9, "B", DIR_RIGHT),364 "1": new MachineStep(9, "B", DIR_RIGHT),365 "S": new MachineStep(10, "B", DIR_RIGHT),366 },367 10: {}368 }369 machineState.transitionFunction = transitionTable370 machineState.states = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]371 machineState.finalStates = [10]372 document.querySelector(".description").innerHTML = "This Turing Machine realizes function<br/> f(m,n) = m*n, number x is represented as 0<sup>x</sup>1";373 input = ["0", "0", "1", "0", "0", "1"]374 reset(input)375 populateTransitionTable(transitionTable, ["0", "1", "X", "S", BLANK], machineState)376 return input377}378export {379 set301,380 set302,381 set303,382 set31,383 set32,384 set33,385 set34...

Full Screen

Full Screen

override-properties.js

Source:override-properties.js Github

copy

Full Screen

1var hasInherit = require('./has-inherit');2var everyValuesPair = require('./every-values-pair');3var findComponentIn = require('./find-component-in');4var isComponentOf = require('./is-component-of');5var isMergeableShorthand = require('./is-mergeable-shorthand');6var overridesNonComponentShorthand = require('./overrides-non-component-shorthand');7var sameVendorPrefixesIn = require('./vendor-prefixes').same;8var compactable = require('../compactable');9var deepClone = require('../clone').deep;10var restoreWithComponents = require('../restore-with-components');11var shallowClone = require('../clone').shallow;12var restoreFromOptimizing = require('../../restore-from-optimizing');13var Token = require('../../../tokenizer/token');14var Marker = require('../../../tokenizer/marker');15var serializeProperty = require('../../../writer/one-time').property;16function wouldBreakCompatibility(property, validator) {17 for (var i = 0; i < property.components.length; i++) {18 var component = property.components[i];19 var descriptor = compactable[component.name];20 var canOverride = descriptor && descriptor.canOverride || canOverride.sameValue;21 var _component = shallowClone(component);22 _component.value = [[Token.PROPERTY_VALUE, descriptor.defaultValue]];23 if (!everyValuesPair(canOverride.bind(null, validator), _component, component)) {24 return true;25 }26 }27 return false;28}29function overrideIntoMultiplex(property, by) {30 by.unused = true;31 turnIntoMultiplex(by, multiplexSize(property));32 property.value = by.value;33}34function overrideByMultiplex(property, by) {35 by.unused = true;36 property.multiplex = true;37 property.value = by.value;38}39function overrideSimple(property, by) {40 by.unused = true;41 property.value = by.value;42}43function override(property, by) {44 if (by.multiplex)45 overrideByMultiplex(property, by);46 else if (property.multiplex)47 overrideIntoMultiplex(property, by);48 else49 overrideSimple(property, by);50}51function overrideShorthand(property, by) {52 by.unused = true;53 for (var i = 0, l = property.components.length; i < l; i++) {54 override(property.components[i], by.components[i], property.multiplex);55 }56}57function turnIntoMultiplex(property, size) {58 property.multiplex = true;59 if (compactable[property.name].shorthand) {60 turnShorthandValueIntoMultiplex(property, size);61 } else {62 turnLonghandValueIntoMultiplex(property, size);63 }64}65function turnShorthandValueIntoMultiplex(property, size) {66 var component;67 var i, l;68 for (i = 0, l = property.components.length; i < l; i++) {69 component = property.components[i];70 if (!component.multiplex) {71 turnLonghandValueIntoMultiplex(component, size);72 }73 }74}75function turnLonghandValueIntoMultiplex(property, size) {76 var descriptor = compactable[property.name];77 var withRealValue = descriptor.intoMultiplexMode == 'real';78 var withValue = descriptor.intoMultiplexMode == 'real' ?79 property.value.slice(0) :80 (descriptor.intoMultiplexMode == 'placeholder' ? descriptor.placeholderValue : descriptor.defaultValue);81 var i = multiplexSize(property);82 var j;83 var m = withValue.length;84 for (; i < size; i++) {85 property.value.push([Token.PROPERTY_VALUE, Marker.COMMA]);86 if (Array.isArray(withValue)) {87 for (j = 0; j < m; j++) {88 property.value.push(withRealValue ? withValue[j] : [Token.PROPERTY_VALUE, withValue[j]]);89 }90 } else {91 property.value.push(withRealValue ? withValue : [Token.PROPERTY_VALUE, withValue]);92 }93 }94}95function multiplexSize(component) {96 var size = 0;97 for (var i = 0, l = component.value.length; i < l; i++) {98 if (component.value[i][1] == Marker.COMMA)99 size++;100 }101 return size + 1;102}103function lengthOf(property) {104 var fakeAsArray = [105 Token.PROPERTY,106 [Token.PROPERTY_NAME, property.name]107 ].concat(property.value);108 return serializeProperty([fakeAsArray], 0).length;109}110function moreSameShorthands(properties, startAt, name) {111 // Since we run the main loop in `compactOverrides` backwards, at this point some112 // properties may not be marked as unused.113 // We should consider reverting the order if possible114 var count = 0;115 for (var i = startAt; i >= 0; i--) {116 if (properties[i].name == name && !properties[i].unused)117 count++;118 if (count > 1)119 break;120 }121 return count > 1;122}123function overridingFunction(shorthand, validator) {124 for (var i = 0, l = shorthand.components.length; i < l; i++) {125 if (!anyValue(validator.isUrl, shorthand.components[i]) && anyValue(validator.isFunction, shorthand.components[i])) {126 return true;127 }128 }129 return false;130}131function anyValue(fn, property) {132 for (var i = 0, l = property.value.length; i < l; i++) {133 if (property.value[i][1] == Marker.COMMA)134 continue;135 if (fn(property.value[i][1]))136 return true;137 }138 return false;139}140function wouldResultInLongerValue(left, right) {141 if (!left.multiplex && !right.multiplex || left.multiplex && right.multiplex)142 return false;143 var multiplex = left.multiplex ? left : right;144 var simple = left.multiplex ? right : left;145 var component;146 var multiplexClone = deepClone(multiplex);147 restoreFromOptimizing([multiplexClone], restoreWithComponents);148 var simpleClone = deepClone(simple);149 restoreFromOptimizing([simpleClone], restoreWithComponents);150 var lengthBefore = lengthOf(multiplexClone) + 1 + lengthOf(simpleClone);151 if (left.multiplex) {152 component = findComponentIn(multiplexClone, simpleClone);153 overrideIntoMultiplex(component, simpleClone);154 } else {155 component = findComponentIn(simpleClone, multiplexClone);156 turnIntoMultiplex(simpleClone, multiplexSize(multiplexClone));157 overrideByMultiplex(component, multiplexClone);158 }159 restoreFromOptimizing([simpleClone], restoreWithComponents);160 var lengthAfter = lengthOf(simpleClone);161 return lengthBefore <= lengthAfter;162}163function isCompactable(property) {164 return property.name in compactable;165}166function noneOverrideHack(left, right) {167 return !left.multiplex &&168 (left.name == 'background' || left.name == 'background-image') &&169 right.multiplex &&170 (right.name == 'background' || right.name == 'background-image') &&171 anyLayerIsNone(right.value);172}173function anyLayerIsNone(values) {174 var layers = intoLayers(values);175 for (var i = 0, l = layers.length; i < l; i++) {176 if (layers[i].length == 1 && layers[i][0][1] == 'none')177 return true;178 }179 return false;180}181function intoLayers(values) {182 var layers = [];183 for (var i = 0, layer = [], l = values.length; i < l; i++) {184 var value = values[i];185 if (value[1] == Marker.COMMA) {186 layers.push(layer);187 layer = [];188 } else {189 layer.push(value);190 }191 }192 layers.push(layer);193 return layers;194}195function overrideProperties(properties, withMerging, compatibility, validator) {196 var mayOverride, right, left, component;197 var overriddenComponents;198 var overriddenComponent;199 var overridingComponent;200 var overridable;201 var i, j, k;202 propertyLoop:203 for (i = properties.length - 1; i >= 0; i--) {204 right = properties[i];205 if (!isCompactable(right))206 continue;207 if (right.block)208 continue;209 mayOverride = compactable[right.name].canOverride;210 traverseLoop:211 for (j = i - 1; j >= 0; j--) {212 left = properties[j];213 if (!isCompactable(left))214 continue;215 if (left.block)216 continue;217 if (left.unused || right.unused)218 continue;219 if (left.hack && !right.hack && !right.important || !left.hack && !left.important && right.hack)220 continue;221 if (left.important == right.important && left.hack[0] != right.hack[0])222 continue;223 if (left.important == right.important && (left.hack[0] != right.hack[0] || (left.hack[1] && left.hack[1] != right.hack[1])))224 continue;225 if (hasInherit(right))226 continue;227 if (noneOverrideHack(left, right))228 continue;229 if (right.shorthand && isComponentOf(right, left)) {230 // maybe `left` can be overridden by `right` which is a shorthand?231 if (!right.important && left.important)232 continue;233 if (!sameVendorPrefixesIn([left], right.components))234 continue;235 if (!anyValue(validator.isFunction, left) && overridingFunction(right, validator))236 continue;237 if (!isMergeableShorthand(right)) {238 left.unused = true;239 continue;240 }241 component = findComponentIn(right, left);242 mayOverride = compactable[left.name].canOverride;243 if (everyValuesPair(mayOverride.bind(null, validator), left, component)) {244 left.unused = true;245 }246 } else if (right.shorthand && overridesNonComponentShorthand(right, left)) {247 // `right` is a shorthand while `left` can be overriden by it, think `border` and `border-top`248 if (!right.important && left.important) {249 continue;250 }251 if (!sameVendorPrefixesIn([left], right.components)) {252 continue;253 }254 if (!anyValue(validator.isFunction, left) && overridingFunction(right, validator)) {255 continue;256 }257 overriddenComponents = left.shorthand ?258 left.components:259 [left];260 for (k = overriddenComponents.length - 1; k >= 0; k--) {261 overriddenComponent = overriddenComponents[k];262 overridingComponent = findComponentIn(right, overriddenComponent);263 mayOverride = compactable[overriddenComponent.name].canOverride;264 if (!everyValuesPair(mayOverride.bind(null, validator), left, overridingComponent)) {265 continue traverseLoop;266 }267 }268 left.unused = true;269 } else if (withMerging && left.shorthand && !right.shorthand && isComponentOf(left, right, true)) {270 // maybe `right` can be pulled into `left` which is a shorthand?271 if (right.important && !left.important)272 continue;273 if (!right.important && left.important) {274 right.unused = true;275 continue;276 }277 // Pending more clever algorithm in #527278 if (moreSameShorthands(properties, i - 1, left.name))279 continue;280 if (overridingFunction(left, validator))281 continue;282 if (!isMergeableShorthand(left))283 continue;284 component = findComponentIn(left, right);285 if (everyValuesPair(mayOverride.bind(null, validator), component, right)) {286 var disabledBackgroundMerging =287 !compatibility.properties.backgroundClipMerging && component.name.indexOf('background-clip') > -1 ||288 !compatibility.properties.backgroundOriginMerging && component.name.indexOf('background-origin') > -1 ||289 !compatibility.properties.backgroundSizeMerging && component.name.indexOf('background-size') > -1;290 var nonMergeableValue = compactable[right.name].nonMergeableValue === right.value[0][1];291 if (disabledBackgroundMerging || nonMergeableValue)292 continue;293 if (!compatibility.properties.merging && wouldBreakCompatibility(left, validator))294 continue;295 if (component.value[0][1] != right.value[0][1] && (hasInherit(left) || hasInherit(right)))296 continue;297 if (wouldResultInLongerValue(left, right))298 continue;299 if (!left.multiplex && right.multiplex)300 turnIntoMultiplex(left, multiplexSize(right));301 override(component, right);302 left.dirty = true;303 }304 } else if (withMerging && left.shorthand && right.shorthand && left.name == right.name) {305 // merge if all components can be merged306 if (!left.multiplex && right.multiplex)307 continue;308 if (!right.important && left.important) {309 right.unused = true;310 continue propertyLoop;311 }312 if (right.important && !left.important) {313 left.unused = true;314 continue;315 }316 if (!isMergeableShorthand(right)) {317 left.unused = true;318 continue;319 }320 for (k = left.components.length - 1; k >= 0; k--) {321 var leftComponent = left.components[k];322 var rightComponent = right.components[k];323 mayOverride = compactable[leftComponent.name].canOverride;324 if (!everyValuesPair(mayOverride.bind(null, validator), leftComponent, rightComponent))325 continue propertyLoop;326 }327 overrideShorthand(left, right);328 left.dirty = true;329 } else if (withMerging && left.shorthand && right.shorthand && isComponentOf(left, right)) {330 // border is a shorthand but any of its components is a shorthand too331 if (!left.important && right.important)332 continue;333 component = findComponentIn(left, right);334 mayOverride = compactable[right.name].canOverride;335 if (!everyValuesPair(mayOverride.bind(null, validator), component, right))336 continue;337 if (left.important && !right.important) {338 right.unused = true;339 continue;340 }341 var rightRestored = compactable[right.name].restore(right, compactable);342 if (rightRestored.length > 1)343 continue;344 component = findComponentIn(left, right);345 override(component, right);346 right.dirty = true;347 } else if (left.name == right.name) {348 // two non-shorthands should be merged based on understandability349 overridable = true;350 if (right.shorthand) {351 for (k = right.components.length - 1; k >= 0 && overridable; k--) {352 overriddenComponent = left.components[k];353 overridingComponent = right.components[k];354 mayOverride = compactable[overridingComponent.name].canOverride;355 overridable = overridable && everyValuesPair(mayOverride.bind(null, validator), overriddenComponent, overridingComponent);356 }357 } else {358 mayOverride = compactable[right.name].canOverride;359 overridable = everyValuesPair(mayOverride.bind(null, validator), left, right);360 }361 if (left.important && !right.important && overridable) {362 right.unused = true;363 continue;364 }365 if (!left.important && right.important && overridable) {366 left.unused = true;367 continue;368 }369 if (!overridable) {370 continue;371 }372 left.unused = true;373 }374 }375 }376}...

Full Screen

Full Screen

override-compactor.js

Source:override-compactor.js Github

copy

Full Screen

1/* */ 2var canOverride = require('./can-override');3var compactable = require('./compactable');4var deepClone = require('./clone').deep;5var shallowClone = require('./clone').shallow;6var hasInherit = require('./has-inherit');7var restoreFromOptimizing = require('./restore-from-optimizing');8var everyCombination = require('./every-combination');9var sameVendorPrefixesIn = require('./vendor-prefixes').same;10var stringifyProperty = require('../stringifier/one-time').property;11var MULTIPLEX_SEPARATOR = ',';12function nameMatchFilter(to) {13 return function(property) {14 return to.name === property.name;15 };16}17function wouldBreakCompatibility(property, validator) {18 for (var i = 0; i < property.components.length; i++) {19 var component = property.components[i];20 var descriptor = compactable[component.name];21 var canOverride = descriptor && descriptor.canOverride || canOverride.sameValue;22 var _component = shallowClone(component);23 _component.value = [[descriptor.defaultValue]];24 if (!canOverride(_component, component, validator))25 return true;26 }27 return false;28}29function isComponentOf(shorthand, longhand) {30 return compactable[shorthand.name].components.indexOf(longhand.name) > -1;31}32function overrideIntoMultiplex(property, by) {33 by.unused = true;34 turnIntoMultiplex(by, multiplexSize(property));35 property.value = by.value;36}37function overrideByMultiplex(property, by) {38 by.unused = true;39 property.multiplex = true;40 property.value = by.value;41}42function overrideSimple(property, by) {43 by.unused = true;44 property.value = by.value;45}46function override(property, by) {47 if (by.multiplex)48 overrideByMultiplex(property, by);49 else if (property.multiplex)50 overrideIntoMultiplex(property, by);51 else52 overrideSimple(property, by);53}54function overrideShorthand(property, by) {55 by.unused = true;56 for (var i = 0,57 l = property.components.length; i < l; i++) {58 override(property.components[i], by.components[i], property.multiplex);59 }60}61function turnIntoMultiplex(property, size) {62 property.multiplex = true;63 for (var i = 0,64 l = property.components.length; i < l; i++) {65 var component = property.components[i];66 if (component.multiplex)67 continue;68 var value = component.value.slice(0);69 for (var j = 1; j < size; j++) {70 component.value.push([MULTIPLEX_SEPARATOR]);71 Array.prototype.push.apply(component.value, value);72 }73 }74}75function multiplexSize(component) {76 var size = 0;77 for (var i = 0,78 l = component.value.length; i < l; i++) {79 if (component.value[i][0] == MULTIPLEX_SEPARATOR)80 size++;81 }82 return size + 1;83}84function lengthOf(property) {85 var fakeAsArray = [[property.name]].concat(property.value);86 return stringifyProperty([fakeAsArray], 0).length;87}88function moreSameShorthands(properties, startAt, name) {89 var count = 0;90 for (var i = startAt; i >= 0; i--) {91 if (properties[i].name == name && !properties[i].unused)92 count++;93 if (count > 1)94 break;95 }96 return count > 1;97}98function overridingFunction(shorthand, validator) {99 for (var i = 0,100 l = shorthand.components.length; i < l; i++) {101 if (anyValue(validator.isValidFunction, shorthand.components[i]))102 return true;103 }104 return false;105}106function anyValue(fn, property) {107 for (var i = 0,108 l = property.value.length; i < l; i++) {109 if (property.value[i][0] == MULTIPLEX_SEPARATOR)110 continue;111 if (fn(property.value[i][0]))112 return true;113 }114 return false;115}116function wouldResultInLongerValue(left, right) {117 if (!left.multiplex && !right.multiplex || left.multiplex && right.multiplex)118 return false;119 var multiplex = left.multiplex ? left : right;120 var simple = left.multiplex ? right : left;121 var component;122 var multiplexClone = deepClone(multiplex);123 restoreFromOptimizing([multiplexClone]);124 var simpleClone = deepClone(simple);125 restoreFromOptimizing([simpleClone]);126 var lengthBefore = lengthOf(multiplexClone) + 1 + lengthOf(simpleClone);127 if (left.multiplex) {128 component = multiplexClone.components.filter(nameMatchFilter(simpleClone))[0];129 overrideIntoMultiplex(component, simpleClone);130 } else {131 component = simpleClone.components.filter(nameMatchFilter(multiplexClone))[0];132 turnIntoMultiplex(simpleClone, multiplexSize(multiplexClone));133 overrideByMultiplex(component, multiplexClone);134 }135 restoreFromOptimizing([simpleClone]);136 var lengthAfter = lengthOf(simpleClone);137 return lengthBefore < lengthAfter;138}139function isCompactable(property) {140 return property.name in compactable;141}142function noneOverrideHack(left, right) {143 return !left.multiplex && (left.name == 'background' || left.name == 'background-image') && right.multiplex && (right.name == 'background' || right.name == 'background-image') && anyLayerIsNone(right.value);144}145function anyLayerIsNone(values) {146 var layers = intoLayers(values);147 for (var i = 0,148 l = layers.length; i < l; i++) {149 if (layers[i].length == 1 && layers[i][0][0] == 'none')150 return true;151 }152 return false;153}154function intoLayers(values) {155 var layers = [];156 for (var i = 0,157 layer = [],158 l = values.length; i < l; i++) {159 var value = values[i];160 if (value[0] == MULTIPLEX_SEPARATOR) {161 layers.push(layer);162 layer = [];163 } else {164 layer.push(value);165 }166 }167 layers.push(layer);168 return layers;169}170function compactOverrides(properties, compatibility, validator) {171 var mayOverride,172 right,173 left,174 component;175 var i,176 j,177 k;178 propertyLoop: for (i = properties.length - 1; i >= 0; i--) {179 right = properties[i];180 if (!isCompactable(right))181 continue;182 if (right.variable)183 continue;184 mayOverride = compactable[right.name].canOverride || canOverride.sameValue;185 for (j = i - 1; j >= 0; j--) {186 left = properties[j];187 if (!isCompactable(left))188 continue;189 if (left.variable)190 continue;191 if (left.unused || right.unused)192 continue;193 if (left.hack && !right.hack || !left.hack && right.hack)194 continue;195 if (hasInherit(right))196 continue;197 if (noneOverrideHack(left, right))198 continue;199 if (!left.shorthand && right.shorthand && isComponentOf(right, left)) {200 if (!right.important && left.important)201 continue;202 if (!sameVendorPrefixesIn([left], right.components))203 continue;204 if (!anyValue(validator.isValidFunction, left) && overridingFunction(right, validator))205 continue;206 component = right.components.filter(nameMatchFilter(left))[0];207 mayOverride = (compactable[left.name] && compactable[left.name].canOverride) || canOverride.sameValue;208 if (everyCombination(mayOverride, left, component, validator)) {209 left.unused = true;210 }211 } else if (left.shorthand && !right.shorthand && isComponentOf(left, right)) {212 if (right.important && !left.important)213 continue;214 if (moreSameShorthands(properties, i - 1, left.name))215 continue;216 if (overridingFunction(left, validator))217 continue;218 component = left.components.filter(nameMatchFilter(right))[0];219 if (everyCombination(mayOverride, component, right, validator)) {220 var disabledBackgroundMerging = !compatibility.properties.backgroundClipMerging && component.name.indexOf('background-clip') > -1 || !compatibility.properties.backgroundOriginMerging && component.name.indexOf('background-origin') > -1 || !compatibility.properties.backgroundSizeMerging && component.name.indexOf('background-size') > -1;221 var nonMergeableValue = compactable[right.name].nonMergeableValue === right.value[0][0];222 if (disabledBackgroundMerging || nonMergeableValue)223 continue;224 if (!compatibility.properties.merging && wouldBreakCompatibility(left, validator))225 continue;226 if (component.value[0][0] != right.value[0][0] && (hasInherit(left) || hasInherit(right)))227 continue;228 if (wouldResultInLongerValue(left, right))229 continue;230 if (!left.multiplex && right.multiplex)231 turnIntoMultiplex(left, multiplexSize(right));232 override(component, right);233 left.dirty = true;234 }235 } else if (left.shorthand && right.shorthand && left.name == right.name) {236 if (!left.multiplex && right.multiplex)237 continue;238 if (!right.important && left.important) {239 right.unused = true;240 continue propertyLoop;241 }242 if (right.important && !left.important) {243 left.unused = true;244 continue;245 }246 for (k = left.components.length - 1; k >= 0; k--) {247 var leftComponent = left.components[k];248 var rightComponent = right.components[k];249 mayOverride = compactable[leftComponent.name].canOverride || canOverride.sameValue;250 if (!everyCombination(mayOverride, leftComponent, rightComponent, validator))251 continue propertyLoop;252 if (!everyCombination(canOverride.twoOptionalFunctions, leftComponent, rightComponent, validator) && validator.isValidFunction(rightComponent))253 continue propertyLoop;254 }255 overrideShorthand(left, right);256 left.dirty = true;257 } else if (left.shorthand && right.shorthand && isComponentOf(left, right)) {258 if (!left.important && right.important)259 continue;260 component = left.components.filter(nameMatchFilter(right))[0];261 mayOverride = compactable[right.name].canOverride || canOverride.sameValue;262 if (!everyCombination(mayOverride, component, right, validator))263 continue;264 if (left.important && !right.important) {265 right.unused = true;266 continue;267 }268 var rightRestored = compactable[right.name].restore(right, compactable);269 if (rightRestored.length > 1)270 continue;271 component = left.components.filter(nameMatchFilter(right))[0];272 override(component, right);273 right.dirty = true;274 } else if (left.name == right.name) {275 if (left.important && !right.important) {276 right.unused = true;277 continue;278 }279 mayOverride = compactable[right.name].canOverride || canOverride.sameValue;280 if (!everyCombination(mayOverride, left, right, validator))281 continue;282 left.unused = true;283 }284 }285 }286}...

Full Screen

Full Screen

reducer.js

Source:reducer.js Github

copy

Full Screen

1"use strict";2Object.defineProperty(exports, "__esModule", {3 value: true4});5exports.default = void 0;6var _convert = _interopRequireDefault(require("./convert"));7function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }8function reduce(node, precision) {9 if (node.type === "MathExpression") return reduceMathExpression(node, precision);10 return node;11}12function isEqual(left, right) {13 return left.type === right.type && left.value === right.value;14}15function isValueType(type) {16 switch (type) {17 case 'LengthValue':18 case 'AngleValue':19 case 'TimeValue':20 case 'FrequencyValue':21 case 'ResolutionValue':22 case 'EmValue':23 case 'ExValue':24 case 'ChValue':25 case 'RemValue':26 case 'VhValue':27 case 'VwValue':28 case 'VminValue':29 case 'VmaxValue':30 case 'PercentageValue':31 case 'Value':32 return true;33 }34 return false;35}36function convertMathExpression(node, precision) {37 var nodes = (0, _convert.default)(node.left, node.right, precision);38 var left = reduce(nodes.left, precision);39 var right = reduce(nodes.right, precision);40 if (left.type === "MathExpression" && right.type === "MathExpression") {41 if (left.operator === '/' && right.operator === '*' || left.operator === '-' && right.operator === '+' || left.operator === '*' && right.operator === '/' || left.operator === '+' && right.operator === '-') {42 if (isEqual(left.right, right.right)) nodes = (0, _convert.default)(left.left, right.left, precision);else if (isEqual(left.right, right.left)) nodes = (0, _convert.default)(left.left, right.right, precision);43 left = reduce(nodes.left, precision);44 right = reduce(nodes.right, precision);45 }46 }47 node.left = left;48 node.right = right;49 return node;50}51function flip(operator) {52 return operator === '+' ? '-' : '+';53}54function flipValue(node) {55 if (isValueType(node.type)) node.value = -node.value;else if (node.type == 'MathExpression') {56 node.left = flipValue(node.left);57 node.right = flipValue(node.right);58 }59 return node;60}61function reduceAddSubExpression(node, precision) {62 var _node = node,63 left = _node.left,64 right = _node.right,65 op = _node.operator;66 if (left.type === 'Function' || right.type === 'Function') return node; // something + 0 => something67 // something - 0 => something68 if (right.value === 0) return left; // 0 + something => something69 if (left.value === 0 && op === "+") return right; // 0 - something => -something70 if (left.value === 0 && op === "-") return flipValue(right); // value + value71 // value - value72 if (left.type === right.type && isValueType(left.type)) {73 node = Object.assign({}, left);74 if (op === "+") node.value = left.value + right.value;else node.value = left.value - right.value;75 } // value <op> (expr)76 if (isValueType(left.type) && (right.operator === '+' || right.operator === '-') && right.type === 'MathExpression') {77 // value + (value + something) => (value + value) + something78 // value + (value - something) => (value + value) - something79 // value - (value + something) => (value - value) - something80 // value - (value - something) => (value - value) + something81 if (left.type === right.left.type) {82 node = Object.assign({}, node);83 node.left = reduce({84 type: 'MathExpression',85 operator: op,86 left: left,87 right: right.left88 }, precision);89 node.right = right.right;90 node.operator = op === '-' ? flip(right.operator) : right.operator;91 return reduce(node, precision);92 } // value + (something + value) => (value + value) + something93 // value + (something - value) => (value - value) + something94 // value - (something + value) => (value - value) - something95 // value - (something - value) => (value + value) - something96 else if (left.type === right.right.type) {97 node = Object.assign({}, node);98 node.left = reduce({99 type: 'MathExpression',100 operator: op === '-' ? flip(right.operator) : right.operator,101 left: left,102 right: right.right103 }, precision);104 node.right = right.left;105 return reduce(node, precision);106 } // value - (something + something) => value - something - something107 else if (op === '-' && right.operator === '+') {108 node = Object.assign({}, node);109 node.right.operator = '-';110 return reduce(node, precision);111 }112 } // (expr) <op> value113 if (left.type === 'MathExpression' && (left.operator === '+' || left.operator === '-') && isValueType(right.type)) {114 // (value + something) + value => (value + value) + something115 // (value - something) + value => (value + value) - something116 // (value + something) - value => (value - value) + something117 // (value - something) - value => (value - value) - something118 if (right.type === left.left.type) {119 node = Object.assign({}, left);120 node.left = reduce({121 type: 'MathExpression',122 operator: op,123 left: left.left,124 right: right125 }, precision);126 return reduce(node, precision);127 } // (something + value) + value => something + (value + value)128 // (something - value1) + value2 => something - (value2 - value1)129 // (something + value) - value => something + (value - value)130 // (something - value) - value => something - (value + value)131 else if (right.type === left.right.type) {132 node = Object.assign({}, left);133 if (left.operator === '-') {134 node.right = reduce({135 type: 'MathExpression',136 operator: flip(op),137 left: left.right,138 right: right139 }, precision);140 if (node.right.value && node.right.value < 0) {141 node.right.value = Math.abs(node.right.value);142 node.operator = '+';143 } else {144 node.operator = left.operator;145 }146 } else {147 node.right = reduce({148 type: 'MathExpression',149 operator: op,150 left: left.right,151 right: right152 }, precision);153 }154 if (node.right.value < 0) {155 node.right.value *= -1;156 node.operator = node.operator === '-' ? '+' : '-';157 }158 return reduce(node, precision);159 }160 }161 if (left.type === 'MathExpression' && right.type === 'MathExpression' && op === '-' && right.operator === '-') {162 node.right.operator = flip(node.right.operator);163 }164 return node;165}166function reduceDivisionExpression(node) {167 if (!isValueType(node.right.type)) return node;168 if (node.right.type !== 'Value') throw new Error(`Cannot divide by "${node.right.unit}", number expected`);169 if (node.right.value === 0) throw new Error('Cannot divide by zero'); // something / value170 if (isValueType(node.left.type)) {171 node.left.value /= node.right.value;172 return node.left;173 }174 return node;175}176function reduceMultiplicationExpression(node) {177 // (expr) * value178 if (node.left.type === 'MathExpression' && node.right.type === 'Value') {179 if (isValueType(node.left.left.type) && isValueType(node.left.right.type)) {180 node.left.left.value *= node.right.value;181 node.left.right.value *= node.right.value;182 return node.left;183 }184 } // something * value185 else if (isValueType(node.left.type) && node.right.type === 'Value') {186 node.left.value *= node.right.value;187 return node.left;188 } // value * (expr)189 else if (node.left.type === 'Value' && node.right.type === 'MathExpression') {190 if (isValueType(node.right.left.type) && isValueType(node.right.right.type)) {191 node.right.left.value *= node.left.value;192 node.right.right.value *= node.left.value;193 return node.right;194 }195 } // value * something196 else if (node.left.type === 'Value' && isValueType(node.right.type)) {197 node.right.value *= node.left.value;198 return node.right;199 }200 return node;201}202function reduceMathExpression(node, precision) {203 node = convertMathExpression(node, precision);204 switch (node.operator) {205 case "+":206 case "-":207 return reduceAddSubExpression(node, precision);208 case "/":209 return reduceDivisionExpression(node, precision);210 case "*":211 return reduceMultiplicationExpression(node);212 }213 return node;214}215var _default = reduce;216exports.default = _default;...

Full Screen

Full Screen

reorderable.js

Source:reorderable.js Github

copy

Full Screen

1// TODO: it'd be great to merge it with the other canReorder functionality2var rulesOverlap = require('./rules-overlap');3var specificitiesOverlap = require('./specificities-overlap');4var FLEX_PROPERTIES = /align\-items|box\-align|box\-pack|flex|justify/;5var BORDER_PROPERTIES = /^border\-(top|right|bottom|left|color|style|width|radius)/;6function canReorder(left, right, cache) {7 for (var i = right.length - 1; i >= 0; i--) {8 for (var j = left.length - 1; j >= 0; j--) {9 if (!canReorderSingle(left[j], right[i], cache))10 return false;11 }12 }13 return true;14}15function canReorderSingle(left, right, cache) {16 var leftName = left[0];17 var leftValue = left[1];18 var leftNameRoot = left[2];19 var leftSelector = left[5];20 var leftInSpecificSelector = left[6];21 var rightName = right[0];22 var rightValue = right[1];23 var rightNameRoot = right[2];24 var rightSelector = right[5];25 var rightInSpecificSelector = right[6];26 if (leftName == 'font' && rightName == 'line-height' || rightName == 'font' && leftName == 'line-height')27 return false;28 if (FLEX_PROPERTIES.test(leftName) && FLEX_PROPERTIES.test(rightName))29 return false;30 if (leftNameRoot == rightNameRoot && unprefixed(leftName) == unprefixed(rightName) && (vendorPrefixed(leftName) ^ vendorPrefixed(rightName)))31 return false;32 if (leftNameRoot == 'border' && BORDER_PROPERTIES.test(rightNameRoot) && (leftName == 'border' || leftName == rightNameRoot || (leftValue != rightValue && sameBorderComponent(leftName, rightName))))33 return false;34 if (rightNameRoot == 'border' && BORDER_PROPERTIES.test(leftNameRoot) && (rightName == 'border' || rightName == leftNameRoot || (leftValue != rightValue && sameBorderComponent(leftName, rightName))))35 return false;36 if (leftNameRoot == 'border' && rightNameRoot == 'border' && leftName != rightName && (isSideBorder(leftName) && isStyleBorder(rightName) || isStyleBorder(leftName) && isSideBorder(rightName)))37 return false;38 if (leftNameRoot != rightNameRoot)39 return true;40 if (leftName == rightName && leftNameRoot == rightNameRoot && (leftValue == rightValue || withDifferentVendorPrefix(leftValue, rightValue)))41 return true;42 if (leftName != rightName && leftNameRoot == rightNameRoot && leftName != leftNameRoot && rightName != rightNameRoot)43 return true;44 if (leftName != rightName && leftNameRoot == rightNameRoot && leftValue == rightValue)45 return true;46 if (rightInSpecificSelector && leftInSpecificSelector && !inheritable(leftNameRoot) && !inheritable(rightNameRoot) && !rulesOverlap(rightSelector, leftSelector, false))47 return true;48 if (!specificitiesOverlap(leftSelector, rightSelector, cache))49 return true;50 return false;51}52function vendorPrefixed(name) {53 return /^\-(?:moz|webkit|ms|o)\-/.test(name);54}55function unprefixed(name) {56 return name.replace(/^\-(?:moz|webkit|ms|o)\-/, '');57}58function sameBorderComponent(name1, name2) {59 return name1.split('-').pop() == name2.split('-').pop();60}61function isSideBorder(name) {62 return name == 'border-top' || name == 'border-right' || name == 'border-bottom' || name == 'border-left';63}64function isStyleBorder(name) {65 return name == 'border-color' || name == 'border-style' || name == 'border-width';66}67function withDifferentVendorPrefix(value1, value2) {68 return vendorPrefixed(value1) && vendorPrefixed(value2) && value1.split('-')[1] != value2.split('-')[2];69}70function inheritable(name) {71 // According to http://www.w3.org/TR/CSS21/propidx.html72 // Others will be catched by other, preceeding rules73 return name == 'font' || name == 'line-height' || name == 'list-style';74}75module.exports = {76 canReorder: canReorder,77 canReorderSingle: canReorderSingle...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import { Selector } from 'testcafe';2test('My first test', async t => {3 .typeText('#developer-name', 'John Smith')4 .click('#submit-button')5 .expect(Selector('#article-header').innerText).eql('Thank you, John Smith!');6});7import { AzurePipelinesReporter } from 'testcafe-reporter-azure-pipelines';8export default {9 reporter: new AzurePipelinesReporter(),10};11{12 "scripts": {13 },14 "devDependencies": {15 }16}

Full Screen

Using AI Code Generation

copy

Full Screen

1import { Selector } from 'testcafe';2test('My first test', async t => {3 .typeText('#developer-name', 'John Smith')4 .click('#submit-button')5 .expect(Selector('#article-header').innerText).eql('Thank you, John Smith!');6});

Full Screen

Using AI Code Generation

copy

Full Screen

1import { Selector } from 'testcafe';2test('My first test', async t => {3 .typeText('#developer-name', 'John Smith')4 .click('#submit-button');5 const articleHeader = await Selector('.result-content').find('h1');6 await t.expect(articleHeader.innerText).eql('Thank you, John Smith!');7});8import { Selector } from 'testcafe';9test('My first test', async t => {10 .typeText('#developer-name', 'John Smith')11 .click('#submit-button');12 const articleHeader = await Selector('.result-content').find('h1');13 await t.expect(articleHeader.innerText).eql('Thank you, John Smith!');14});15import { Selector } from 'testcafe';16test('My first test', async t => {17 .typeText('#developer-name', 'John Smith')18 .click('#submit-button');19 const articleHeader = await Selector('.result-content').find('h1');20 await t.expect(articleHeader.innerText).eql('Thank you, John Smith!');21});22import { Selector } from 'testcafe';23test('My first test', async t => {

Full Screen

Using AI Code Generation

copy

Full Screen

1const { Selector } = require('testcafe');2const { ClientFunction } = require('testcafe');3test('My first test', async t => {4 .typeText('#developer-name', 'John Smith')5 .click('#submit-button');6});7const { Selector } = require('testcafe');8const { ClientFunction } = require('testcafe');9test('My first test', async t => {10 .typeText('#developer-name', 'John Smith')11 .click('#submit-button');12});13{

Full Screen

Using AI Code Generation

copy

Full Screen

1import { Selector } from 'testcafe';2import { ClientFunction } from 'testcafe';3const getWindowLocation = ClientFunction(() => window.location);4test('My first test', async t => {5 .typeText('#lst-ib', 'testcafe')6 .click('#tsbb')7 .click('#rso > div:nth-child(1) > div > div > div > div > h3 > a')8 .expect(getWindowLocation()).contains('testcafe');9});10import { Selector } from 'testcafe';11import { ClientFunction } from 'testcafe';12const getWindowLocation = ClientFunction(() => window.location);13test('My first test', async t => {14 .typeText('#lst-ib', 'testcafe')15 .click('#tsbb')16 .click('#rso > div:nth-child(1) > div > div > div > div > h3 > a')17 .expect(getWindowLocation()).contains('testcafe');18});19import { Selector } from 'testcafe';20import { ClientFunction } from 'testcafe';21const getWindowLocation = ClientFunction(() => window.location);22test('My first test', async t => {23 .typeText('#lst-ib', 'testcafe')24 .click('#tsbb')25 .click('#rso > div:nth-child(1) > div > div > div > div > h3 > a')26 .expect(getWindowLocation()).contains('testcafe');27});28import { Selector } from 'testcafe';29import { ClientFunction } from 'testcafe';30const getWindowLocation = ClientFunction(() => window.location);31test('My first test', async t => {32 .typeText('#lst-ib', 'testcafe')33 .click('#

Full Screen

Using AI Code Generation

copy

Full Screen

1import { Selector } from 'testcafe';2import { t } from 'testcafe';3import { ClientFunction } from 'testcafe';4const fs = require('fs');5const path = require('path');6const dotenv = require('dotenv');7const dotenvExpand = require('dotenv-expand');8const myEnv = dotenv.config();9dotenvExpand(myEnv);10const { setTestSpeed, setScreenshots, setVideo } = require('testcafe-browser-provider-electron');11const electronPath = require('electron');12const getElectronPath = ClientFunction(() => {13 return process.env.ELECTRON_PATH;14});15const getElectronVersion = ClientFunction(() => {16 return process.env.ELECTRON_VERSION;17});18test('test', async t => {19 const electronPath = await getElectronPath();20 const electronVersion = await getElectronVersion();21 console.log('Electron path: ', electronPath);22 console.log('Electron version: ', electronVersion);23 .click(Selector('button').withText('Open Window'))24 .expect(Selector('h1').withText('Hello Electron').exists).ok()25 .click(Selector('button').withText('Close'))26 .expect(Selector('h1').withText('Hello Electron').exists).notOk();27});28const electronPath = process.env.ELECTRON_PATH;

Full Screen

Using AI Code Generation

copy

Full Screen

1await t.click(Selector('button').withText('Click me!'));2await t.click(Selector('button').withText('Click me!'));3await t.click(Selector('button').withText('Click me!'));4await t.click(Selector('button').withText('Click me!'));5await t.click(Selector('button').withText('Click me!'));6await t.click(Selector('button').withText('Click me!'));7await t.click(Selector('button').withText('Click me!'));8await t.click(Selector('button').withText('Click me!'));9await t.click(Selector('button').withText('Click me!'));10await t.click(Selector('button').withText('Click me!'));11await t.click(Selector('button').withText('Click me!'));12await t.click(Selector('button').withText('Click me!'));13await t.click(Selector('button').withText('Click me!'));14await t.click(Selector('button').withText('Click me!'));

Full Screen

Using AI Code Generation

copy

Full Screen

1import {t} from 'testcafe';2import {Selector} from 'testcafe';3export default class HomePage {4 constructor() {5 this.nameInput = Selector('#developer-name');6 this.submitButton = Selector('#submit-button');7 }8}9import {t} from 'testcafe';10import HomePage from './pages/homePage';11const homePage = new HomePage();12test('My Test', async () => {13 .typeText(homePage.nameInput, 'Peter')14 .click(homePage.submitButton);15});16export function getUserName() {17 return 'Peter Parker';18}19import {t} from 'testcafe';20import {getUserName} from './helpers/utils';21test('My Test', async () => {22 .typeText('#developer-name', getUserName())23 .click('#submit-button');24});25import { Selector } from 'testcafe';26test('My Test', async t => {27 const numberOfInputs = await Selector('input').count;28 await t.expect(numberOfInputs).eql(5);29});30import { Selector } from

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testcafe automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful