From 72b14856d4b73de996265a9ebe43172bbeb25dac Mon Sep 17 00:00:00 2001 From: Sam El-Husseini <16690124+samelhusseini@users.noreply.github.com> Date: Fri, 2 Aug 2019 13:07:32 -0700 Subject: [PATCH] Cherry pick to master: Fix node build (#2756) --- blockly_compressed.js | 21 +++++++------ blockly_uncompressed.js | 20 +++--------- core/utils.js | 6 +--- core/utils/base.js | 38 ++++++++++++++++++++++ core/utils/dom.js | 14 ++++++++- core/utils/useragent.js | 3 +- core/xml.js | 10 +++--- gulpfile.js | 10 ++---- package.json | 2 ++ tests/node/.eslintrc.json | 14 +++++++++ tests/node/mocha.opts | 3 ++ tests/node/run_node_test.js | 63 +++++++++++++++++++++++++++++++++++++ tests/run_all_tests.sh | 3 ++ 13 files changed, 163 insertions(+), 44 deletions(-) create mode 100644 core/utils/base.js create mode 100644 tests/node/.eslintrc.json create mode 100644 tests/node/mocha.opts create mode 100644 tests/node/run_node_test.js diff --git a/blockly_compressed.js b/blockly_compressed.js index a5f2b1aec..cc916f572 100644 --- a/blockly_compressed.js +++ b/blockly_compressed.js @@ -879,7 +879,7 @@ goog.ui.tree.TreeControl.prototype.handleMouseEvent_=function(a){goog.log.fine(t goog.ui.tree.TreeControl.prototype.handleKeyEvent=function(a){var b;(b=this.typeAhead_.handleNavigation(a)||this.selectedItem_&&this.selectedItem_.onKeyDown(a)||this.typeAhead_.handleTypeAheadChar(a))&&a.preventDefault();return b};goog.ui.tree.TreeControl.prototype.getNodeFromEvent_=function(a){for(var b=a.target;null!=b;){if(a=goog.ui.tree.BaseNode.allNodes[b.id])return a;if(b==this.getElement())break;b=b.parentNode}return null}; goog.ui.tree.TreeControl.prototype.createNode=function(a){return new goog.ui.tree.TreeNode(a||goog.html.SafeHtml.EMPTY,this.getConfig(),this.getDomHelper())};goog.ui.tree.TreeControl.prototype.setNode=function(a){this.typeAhead_.setNodeInMap(a)};goog.ui.tree.TreeControl.prototype.removeNode=function(a){this.typeAhead_.removeNodeFromMap(a)};goog.ui.tree.TreeControl.prototype.clearTypeAhead=function(){this.typeAhead_.clear()};goog.ui.tree.TreeControl.defaultConfig=goog.ui.tree.BaseNode.defaultConfig; var Blockly={};Blockly.Blocks=Object.create(null);Blockly.Msg={};goog.getMsgOrig=goog.getMsg;goog.getMsg=function(a,b){var c=goog.getMsg.blocklyMsgMap[a];c&&(a=Blockly.Msg[c]);return goog.getMsgOrig(a,b)};goog.getMsg.blocklyMsgMap={Today:"TODAY"}; -Blockly.utils={};Blockly.utils.Coordinate=function(a,b){this.x=a;this.y=b};Blockly.utils.Coordinate.equals=function(a,b){return a==b?!0:a&&b?a.x==b.x&&a.y==b.y:!1};Blockly.utils.Coordinate.distance=function(a,b){var c=a.x-b.x,d=a.y-b.y;return Math.sqrt(c*c+d*d)};Blockly.utils.Coordinate.magnitude=function(a){return Math.sqrt(a.x*a.x+a.y*a.y)};Blockly.utils.Coordinate.difference=function(a,b){return new Blockly.utils.Coordinate(a.x-b.x,a.y-b.y)}; +Blockly.utils={};Blockly.utils.base={};Blockly.utils.global=this||self;Blockly.utils.Coordinate=function(a,b){this.x=a;this.y=b};Blockly.utils.Coordinate.equals=function(a,b){return a==b?!0:a&&b?a.x==b.x&&a.y==b.y:!1};Blockly.utils.Coordinate.distance=function(a,b){var c=a.x-b.x,d=a.y-b.y;return Math.sqrt(c*c+d*d)};Blockly.utils.Coordinate.magnitude=function(a){return Math.sqrt(a.x*a.x+a.y*a.y)};Blockly.utils.Coordinate.difference=function(a,b){return new Blockly.utils.Coordinate(a.x-b.x,a.y-b.y)}; Blockly.utils.Coordinate.sum=function(a,b){return new Blockly.utils.Coordinate(a.x+b.x,a.y+b.y)};Blockly.utils.Coordinate.prototype.scale=function(a){this.x*=a;this.y*=a;return this};Blockly.utils.Coordinate.prototype.translate=function(a,b){this.x+=a;this.y+=b;return this};Blockly.utils.string={};Blockly.utils.string.startsWith=function(a,b){return 0==a.lastIndexOf(b,0)};Blockly.utils.string.shortestStringLength=function(a){return a.length?a.reduce(function(a,c){return a.lengthd&&(d=h,e=g)}return e?Blockly.utils.string.wrapMutate_(a,e,c):b};Blockly.utils.string.wrapToText_=function(a,b){for(var c=[],d=0;d"!=d.slice(-2)&&(b+=" ")}a=a.join("\n");a=a.replace(/(<(\w+)\b[^>]*>[^\n]*)\n *<\/\2>/g,"$1");return a.replace(/^\n/,"")}; Blockly.Xml.textToDom=function(a){var b=Blockly.utils.xml.textToDomDocument(a);if(!b||!b.documentElement||b.getElementsByTagName("parsererror").length)throw Error("textToDom was unable to parse: "+a);return b.documentElement};Blockly.Xml.clearWorkspaceAndLoadFromXml=function(a,b){b.setResizesEnabled(!1);b.clear();var c=Blockly.Xml.domToWorkspace(a,b);b.setResizesEnabled(!0);return c}; Blockly.Xml.domToWorkspace=function(a,b){if(a instanceof Blockly.Workspace){var c=a;a=b;b=c;console.warn("Deprecated call to Blockly.Xml.domToWorkspace, swap the arguments.")}var d;b.RTL&&(d=b.getWidth());c=[];Blockly.Field.startCache();var e=a.childNodes.length,f=Blockly.Events.getGroup();f||Blockly.Events.setGroup(!0);b.setResizesEnabled&&b.setResizesEnabled(!1);var g=!0;try{for(var h=0;h level. It's all whitespace anyway. continue; } @@ -615,7 +615,7 @@ Blockly.Xml.domToBlockHeadless_ = function(xmlBlock, workspace) { var childBlockElement = null; var childShadowElement = null; for (var j = 0, grandchild; grandchild = xmlChild.childNodes[j]; j++) { - if (grandchild.nodeType == Node.ELEMENT_NODE) { + if (grandchild.nodeType == Blockly.utils.dom.Node.ELEMENT_NODE) { if (grandchild.nodeName.toLowerCase() == 'block') { childBlockElement = /** @type {!Element} */ (grandchild); } else if (grandchild.nodeName.toLowerCase() == 'shadow') { diff --git a/gulpfile.js b/gulpfile.js index c870e1b77..7f9eab613 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -62,18 +62,14 @@ gulp.task('blockly_javascript_en', function() { .pipe(insert.append(` if (typeof DOMParser !== 'function') { var JSDOM = require('jsdom').JSDOM; + var window = (new JSDOM()).window; + var document = window.document; + var Element = window.Element; Blockly.utils.xml.textToDomDocument = function(text) { var jsdom = new JSDOM(text, { contentType: 'text/xml' }); return jsdom.window.document; }; } -if (typeof Node !== 'function') { - var Node = { - ELEMENT_NODE: 1, - TEXT_NODE: 3, - DOCUMENT_POSITION_CONTAINED_BY: 16 - }; -} if (typeof module === 'object') { module.exports = Blockly; } if (typeof window === 'object') { window.Blockly = Blockly; }\n`)) .pipe(gulp.dest('.')); diff --git a/package.json b/package.json index 053c4c239..17948ff04 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "license": "Apache-2.0", "private": true, "devDependencies": { + "chai": "^4.2.0", "eslint": "^5.13.0", "google-closure-compiler": "^20190618.0.0", "google-closure-library": "^20190618.0.0", @@ -34,6 +35,7 @@ "gulp-series": "^1.0.2", "gulp-shell": "^0.7.1", "jshint": "^2.10.2", + "mocha": "^6.1.4", "webdriverio": "^5.11.5" }, "jshintConfig": { diff --git a/tests/node/.eslintrc.json b/tests/node/.eslintrc.json new file mode 100644 index 000000000..50215619b --- /dev/null +++ b/tests/node/.eslintrc.json @@ -0,0 +1,14 @@ +{ + "parserOptions": { + "ecmaVersion": 6 + }, + "env": { + "browser": false, + "mocha": true + }, + "globals": { + "console": true, + "require": true + }, + "extends": "../../.eslintrc.json" +} diff --git a/tests/node/mocha.opts b/tests/node/mocha.opts new file mode 100644 index 000000000..240bd1610 --- /dev/null +++ b/tests/node/mocha.opts @@ -0,0 +1,3 @@ +# mocha.opts +--ui tdd +--reporter landing diff --git a/tests/node/run_node_test.js b/tests/node/run_node_test.js new file mode 100644 index 000000000..d934f8355 --- /dev/null +++ b/tests/node/run_node_test.js @@ -0,0 +1,63 @@ +/** + * @license + * Visual Blocks Editor + * + * Copyright 2019 Google Inc. + * https://developers.google.com/blockly/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @fileoverview Mocha tests that test Blockly in Node. + */ + +var assert = require('chai').assert; +var Blockly = require('../../blockly_node_javascript_en.js'); + +var xmlText = ` + + + + Hello from Blockly! + + + +`; + +suite('Test Node.js', function() { + test('Import XML', function() { + assert.doesNotThrow(function() { + const xml = Blockly.Xml.textToDom(xmlText); + + // Create workspace and import the XML + const workspace = new Blockly.Workspace(); + Blockly.Xml.domToWorkspace(xml, workspace); + + }, "Failed to import XML"); + }); + test('Generate Code', function() { + const xml = Blockly.Xml.textToDom(xmlText); + + // Create workspace and import the XML + const workspace = new Blockly.Workspace(); + Blockly.Xml.domToWorkspace(xml, workspace); + + // Convert code + const code = Blockly.JavaScript.workspaceToCode(workspace); + + // Check output + assert.equal(`window.alert('Hello from Blockly!');`, code.trim(), 'equal'); + }); +}); + diff --git a/tests/run_all_tests.sh b/tests/run_all_tests.sh index f0d4695d7..c73a4cfd1 100755 --- a/tests/run_all_tests.sh +++ b/tests/run_all_tests.sh @@ -61,6 +61,9 @@ run_test_command "jsunit" "node tests/jsunit/run_jsunit_tests_in_browser.js" # Run Mocha tests inside a browser. run_test_command "mocha" "node tests/mocha/run_mocha_tests_in_browser.js" +# Run Node tests. +run_test_command "node" "./node_modules/.bin/mocha tests/node --opts tests/node/mocha.opts" + # Run generator tests inside a browser and check the results. run_test_command "generators" "tests/scripts/run_generators.sh"