Skip to content

Commit

Permalink
fix: fix html converter
Browse files Browse the repository at this point in the history
  • Loading branch information
andycall committed Apr 15, 2024
1 parent 358042a commit fbfc10c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
5 changes: 3 additions & 2 deletions bin/wbc.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ const sourceFileName = source.split('/').slice(-1)[0].split('.')[0];
const sourceCode = fs.readFileSync(source, { encoding: 'utf-8' });

if (options.convertHtml) {
let distPath = path.join(dist, sourceFileName + '.bhtml');
const output = transformInlineScriptToWbc(sourceCode);
fs.writeFileSync(dist, output);
console.log('Quickjs bytecode generated wbc1 at: \n' + dist);
fs.writeFileSync(distPath, output);
console.log('Quickjs bytecode generated at: \n' + distPath);
} else {
if (type == 'kbc1') {
let distPath = path.join(dist, sourceFileName + '.kbc1');
Expand Down
8 changes: 0 additions & 8 deletions demo.html

This file was deleted.

47 changes: 44 additions & 3 deletions html_converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,63 @@ const { JSDOM } = jsdom;
const Qjsc = require('./qjsc');
const { Wbc } = require('./wbc');

const _replaceMap = {};
let _index = 1;

function generateReplaceKey(buffer) {
const newIndex = _index++;
_replaceMap[newIndex] = buffer;
return `%@%${newIndex}%@%`;
}

function parseAndConvertHTML(html, version = '1') {
let dom = new JSDOM(html);
const scripts = dom.window.document.querySelectorAll('script');

[].slice.call(scripts).forEach(script => {
const textContent = script.textContent;
if (textContent.length == 0) return;
script.type = 'application/vnd.webf.bc1';

const qjsc = new Qjsc();
const wbc = new Wbc();
const buffer = qjsc.compile(textContent);
let wbcBytecode = wbc.generateWbcBytecode(buffer);
let code = wbcBytecode.toString('binary');
script.textContent = code;

script.textContent = generateReplaceKey(wbcBytecode);
})

return dom.window.document.documentElement.outerHTML;
const rawHtml = dom.window.document.documentElement.outerHTML;
return replaceIndexWithByteCode(rawHtml);
}

function replaceIndexWithByteCode(html) {
const byteArray = new Uint8Array(Buffer.from(html));
const chunks = [];
let start = 0;

for (let i = 0; i < byteArray.length - 3; i ++) {
if (byteArray[i] == 37 && byteArray[i + 1] == 64 && byteArray[i + 2] == 37) { // %@%
chunks.push(Buffer.from(byteArray.slice(start, i).buffer));
i += 2;
let index;
let amount = 0;
while(byteArray[i + amount] == 48) { // '0
amount++
}
index = amount * 10 + Number(String.fromCharCode(byteArray[i + amount + 1]));
const byteCodeBuffer = _replaceMap[index];
chunks.push(byteCodeBuffer);

i += amount + 2;
start = i + 3;
}
}

chunks.push(byteArray.slice(start));

let totalBuffer = Buffer.concat(chunks);
return totalBuffer;
}


Expand Down

0 comments on commit fbfc10c

Please sign in to comment.