Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

qrcode.js .createTableTagMin - minimize html table cell #111

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ _[Note] call make() before this function._
#### createImgTag(cellSize, margin, alt) => <code>string</code>
#### createSvgTag(cellSize, margin) => <code>string</code>
#### createTableTag(cellSize, margin) => <code>string</code>
#### createTableTagMin(cellSize, margin) => <code>string</code>
#### createASCII(cellSize, margin) => <code>string</code>
Helper functions for HTML.
_[Note] call make() before these functions._
Expand Down
1 change: 1 addition & 0 deletions js/demo/assets/sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var create_qrcode = function(text, typeNumber,
qr.make();

// return qr.createTableTag();
// return qr.createTableTagMin();
// return qr.createSvgTag();
return qr.createImgTag();
};
Expand Down
1 change: 1 addition & 0 deletions js/qrcode.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
36 changes: 36 additions & 0 deletions js/qrcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,42 @@ var qrcode = function() {
return qrHtml;
};


_this.createTableTagMin = function(cellSize, margin) {
//AK 2022-11-14: Minimize html table cell to '<td/>' and '<td w/>' - almost 159/6=26.5 times smaller HTML.
//Auto-conversion in innerHTML= :...<td></td> <td w=""></td>...
// could be reverted with: .replace(/<td(( w)="")?><\/td>/g,'<td$2/>')

cellSize = cellSize || 2;
margin = (typeof margin == 'undefined')? cellSize * 4 : margin;

var qrHtml = '';

qrHtml += '<style>'+
' table#qrTbl {border: 0px none; border-collapse: collapse; padding: 0px; background-color: #000000;}'+
' table#qrTbl {table-layout: fixed; margin: ' + margin + 'px;}'+
' table#qrTbl td {padding: 0px; width:' + cellSize + 'px;'+' height:' + cellSize + 'px;}'+
'[w] {background-color: #ffffff !important;}'+
'</style>\n'+
'<table id="qrTbl"><tbody>';

for (var r = 0; r < _this.getModuleCount(); r += 1) {

qrHtml += '\n<tr>';

for (var c = 0; c < _this.getModuleCount(); c += 1) {
qrHtml += _this.isDark(r, c)? '<td/>' : '<td w/>';
}

qrHtml += '</tr>';
}

qrHtml += '</tbody></table>';

return qrHtml;
};


_this.createSvgTag = function(cellSize, margin, alt, title) {

var opts = {};
Expand Down
1 change: 1 addition & 0 deletions js/sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var create_qrcode = function(text, typeNumber,
qr.make();

// return qr.createTableTag();
// return qr.createTableTagMin();
// return qr.createSvgTag();
return qr.createImgTag();
};
Expand Down
40 changes: 40 additions & 0 deletions js/test/qrcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <table> data', function(){
var sourceText = 'http://www.example.com/ążśźęćńół';
var correctTableTagMinData = [
'7159: <style> table#qrTbl {border: 0px none; border-collapse: collapse; padding: 0px; background-color: #000000;} table#qrTbl {table-layout: fixed; margin: 8px;} table#qrTbl td {padding: 0px; width:2px; height:2px;}[w] {background-color: #ffffff !important;}</style>',
'<table id="qrTbl"><tbody>',
'<tr><td/><td/><td/><td/><td/><td/><td/><td w/>',
'...',
'<td w/><td w/><td w/><td/><td/><td/></tr></tbody></table>'].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 <table> data', function(){
var sourceText = 'http://www.example.com/ążśźęćńół';
var correctTableTagData = [
'173580: <table style=" border-width: 0px; border-style: none; border-collapse: collapse; padding: 0px; margin: 8px;"><tbody><tr><td style=" border-width: 0px; border-style: none; border-collapse: collapse; padding: 0px; margin: 0px; width: 2px; height: 2px; background-color: #000000;"/><td style=" border-width: 0px; border-style: none; border-collapse: collapse; padding: 0px; margin: 0px; width: 2px; height: 2px; background-color: #000000;"/><td style=" border-width: 0px; border-style: none; border-collapse:',
'...',
'<td style=" border-width: 0px; border-style: none; border-collapse: collapse; padding: 0px; margin: 0px; width: 2px; height: 2px; background-color: #ffffff;"/><td style=" border-width: 0px; border-style: none; border-collapse: collapse; padding: 0px; margin: 0px; width: 2px; height: 2px; background-color: #000000;"/><td style=" border-width: 0px; border-style: none; border-collapse: collapse; padding: 0px; margin: 0px; width: 2px; height: 2px; background-color: #000000;"/><td style=" border-width: 0px; border-style: none; border-collapse: collapse; padding: 0px; margin: 0px; width: 2px; height: 2px; background-color: #000000;"/></tr></tbody></table>'].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');
});



});