From 7f1324fa8f537619602e6d7e176b6edcf2f09d2c Mon Sep 17 00:00:00 2001 From: Andrzej Kubaszek Date: Mon, 14 Nov 2022 14:30:36 +0100 Subject: [PATCH] qrcode.js .createTableTagMin - minimize html table cell to '' and ''. Almost 159/6=26.5 times smaller HTML. --- js/README.md | 1 + js/demo/assets/sample.js | 1 + js/qrcode.d.ts | 1 + js/qrcode.js | 36 ++++++++++++++++++++++++++++++++++++ js/sample.js | 1 + js/test/qrcode.js | 40 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 80 insertions(+) diff --git a/js/README.md b/js/README.md index a85af12..818bf69 100644 --- a/js/README.md +++ b/js/README.md @@ -72,6 +72,7 @@ _[Note] call make() before this function._ #### createImgTag(cellSize, margin, alt) => string #### createSvgTag(cellSize, margin) => string #### createTableTag(cellSize, margin) => string +#### createTableTagMin(cellSize, margin) => string #### createASCII(cellSize, margin) => string Helper functions for HTML. _[Note] call make() before these functions._ diff --git a/js/demo/assets/sample.js b/js/demo/assets/sample.js index ce94957..6bc4798 100644 --- a/js/demo/assets/sample.js +++ b/js/demo/assets/sample.js @@ -35,6 +35,7 @@ var create_qrcode = function(text, typeNumber, qr.make(); // return qr.createTableTag(); +// return qr.createTableTagMin(); // return qr.createSvgTag(); return qr.createImgTag(); }; diff --git a/js/qrcode.d.ts b/js/qrcode.d.ts index b34952e..e1d508c 100644 --- a/js/qrcode.d.ts +++ b/js/qrcode.d.ts @@ -46,6 +46,7 @@ interface QRCode { scalable?: boolean }) : string; createDataURL(cellSize?: number, margin?: number) : string; createTableTag(cellSize?: number, margin?: number) : string; + createTableTagMin(cellSize?: number, margin?: number) : string; createASCII(cellSize?: number, margin?: number) : string; renderTo2dContext(context: CanvasRenderingContext2D, cellSize?: number): void; } diff --git a/js/qrcode.js b/js/qrcode.js index 76889b5..6ff6743 100644 --- a/js/qrcode.js +++ b/js/qrcode.js @@ -492,6 +492,42 @@ var qrcode = function() { return qrHtml; }; + + _this.createTableTagMin = function(cellSize, margin) { + //AK 2022-11-14: Minimize html table cell to '' and '' - almost 159/6=26.5 times smaller HTML. + //Auto-conversion in innerHTML= :... ... + // could be reverted with: .replace(/<\/td>/g,'') + + cellSize = cellSize || 2; + margin = (typeof margin == 'undefined')? cellSize * 4 : margin; + + var qrHtml = ''; + + qrHtml += '\n'+ + ''; + + for (var r = 0; r < _this.getModuleCount(); r += 1) { + + qrHtml += '\n'; + + for (var c = 0; c < _this.getModuleCount(); c += 1) { + qrHtml += _this.isDark(r, c)? ''; + } + + qrHtml += '
' : ''; + } + + qrHtml += '
'; + + return qrHtml; + }; + + _this.createSvgTag = function(cellSize, margin, alt, title) { var opts = {}; diff --git a/js/sample.js b/js/sample.js index d351b05..11c13aa 100644 --- a/js/sample.js +++ b/js/sample.js @@ -32,6 +32,7 @@ var create_qrcode = function(text, typeNumber, qr.make(); // return qr.createTableTag(); +// return qr.createTableTagMin(); // return qr.createSvgTag(); return qr.createImgTag(); }; diff --git a/js/test/qrcode.js b/js/test/qrcode.js index 6c40125..2103232 100644 --- a/js/test/qrcode.js +++ b/js/test/qrcode.js @@ -115,4 +115,44 @@ describe('QRCode', function(){ assert.strictEqual(qr.createASCII(1, 0), correctTextData2, 'ASCII QRCode of size 1 without margin is incorrect'); assert.strictEqual(qr.createASCII(2), correctTextData3, 'ASCII QRCode of size 2 is incorrect'); }); + + + it('should generate correct min HTML data', function(){ + var sourceText = 'http://www.example.com/ążśźęćńół'; + var correctTableTagMinData = [ + '7159: ', + '
', + '
', + '...', + '
'].join('\n'); + + var qr = qrcode(-1, 'M'); + qr.addData(unescape(encodeURI(sourceText))); + qr.make(); + var tableTagMin=qr.createTableTagMin(); + var out = tableTagMin.length + ': ' + tableTagMin.substring(0,333) + '\n...\n' + tableTagMin.substring(7102); + + assert.strictEqual(out, correctTableTagMinData, 'TableTag QRCode is incorrect'); + }); + + + + it('should generate correct HTML data', function(){ + var sourceText = 'http://www.example.com/ążśźęćńół'; + var correctTableTagData = [ + '173580:
'].join('\n'); + + var qr = qrcode(-1, 'M'); + qr.addData(unescape(encodeURI(sourceText))); + qr.make(); + var tableTag=qr.createTableTag(); + var out2 = tableTag.length + ': ' + tableTag.substring(0,505) + '\n...\n' + tableTag.substring(172923); + + assert.strictEqual(out2, correctTableTagData, 'TableTag QRCode is incorrect'); + }); + + + }); \ No newline at end of file