From 0d4d50a2ac38a66bad5d590291ddaf043d94ada1 Mon Sep 17 00:00:00 2001 From: Lapo Luchini Date: Sun, 12 May 2024 15:47:35 +0000 Subject: [PATCH] Use a code style closer to trunk branch. --- context.js | 21 ++++++++++----------- dom.js | 30 ++++++++---------------------- index.js | 2 +- package.json | 4 ++-- 4 files changed, 21 insertions(+), 36 deletions(-) diff --git a/context.js b/context.js index 05d0b54..ffe6d52 100644 --- a/context.js +++ b/context.js @@ -12,17 +12,16 @@ export function bindContextMenu(node) { const valueEnabled = type != 'SET' && type != 'SEQUENCE'; node.onclick = function (event) { // do not show the menu in case of clicking the icon - if (event.srcElement.nodeName === 'SPAN') { - contextMenu.style.left = event.pageX + 'px'; - contextMenu.style.top = event.pageY + 'px'; - contextMenu.style.visibility = 'visible'; - contextMenu.node = this; - btnHideTree.innerText = (node.className == 'node') ? 'Hide subtree' : 'Show subtree'; - btnHideTree.style.display = node.className.startsWith('node') ? 'block' : 'none'; - btnCopyValue.style.display = valueEnabled ? 'block' : 'none'; - event.preventDefault(); - event.stopPropagation(); - } + if (event.srcElement.nodeName != 'SPAN') return; + contextMenu.style.left = event.pageX + 'px'; + contextMenu.style.top = event.pageY + 'px'; + contextMenu.style.visibility = 'visible'; + contextMenu.node = this; + btnHideTree.innerText = (node.className == 'node') ? 'Hide subtree' : 'Show subtree'; + btnHideTree.style.display = node.className.startsWith('node') ? 'block' : 'none'; + btnCopyValue.style.display = valueEnabled ? 'block' : 'none'; + event.preventDefault(); + event.stopPropagation(); }; } diff --git a/dom.js b/dom.js index bd89805..a4403b1 100644 --- a/dom.js +++ b/dom.js @@ -4,7 +4,7 @@ // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. -// +// // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR @@ -24,7 +24,7 @@ const ellipsis: '\u2026', tag: function (tagName, className, text) { let t = document.createElement(tagName); - t.className = className; + if (className) t.className = className; if (text) t.innerText = text; return t; }, @@ -53,16 +53,11 @@ const export class ASN1DOM extends ASN1 { - buf2hex(buffer) { - return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join(' '); - } - toDOM(spaces) { spaces = spaces || ''; let isOID = (typeof oids === 'object') && (this.tag.isUniversal() && (this.tag.tagNumber == 0x06) || (this.tag.tagNumber == 0x0D)); - let node; - node = document.createElement('li'); - node.asn1 = this; + let node = DOM.tag('li'); + node.asn1 = this; let head = DOM.tag('span', 'head'); const typeName = this.typeName().replace(/_/g, ' '); if (this.def) { @@ -79,8 +74,6 @@ export class ASN1DOM extends ASN1 { head.appendChild(DOM.space()); } } - head.setAttribute('pos', this.posStart()); - head.setAttribute('end', this.posEnd()); head.appendChild(DOM.text(typeName)); let content; try { @@ -131,13 +124,11 @@ export class ASN1DOM extends ASN1 { // summary.setAttribute('class', 'node'); contentNode = summary; childNode = details; - } - else { + } else { contentNode = node; contentNode.setAttribute('class', 'node'); contentNode.appendChild(head); } - this.node = contentNode; this.head = head; let value = DOM.tag('div', 'value'); @@ -162,14 +153,12 @@ export class ASN1DOM extends ASN1 { } value.innerHTML = s; contentNode.appendChild(value); - let sub = DOM.tag('div', 'sub'); if (this.sub !== null) { - let ul = document.createElement('ul'); - childNode.appendChild(ul); - + let sub = DOM.tag('ul'); + childNode.appendChild(sub); spaces += '\xA0 '; for (let i = 0, max = this.sub.length; i < max; ++i) - ul.appendChild(this.sub[i].toDOM(spaces)); + sub.appendChild(this.sub[i].toDOM(spaces)); } bindContextMenu(node); return node; @@ -228,9 +217,6 @@ export class ASN1DOM extends ASN1 { node.appendChild(skip); } } - // set the current start and end position as an attribute at the node to know the selected area - node.setAttribute('pos', this.posStart()); - node.setAttribute('end', this.posEnd()); this.toHexDOM_sub(node, 'tag', this.stream, this.posStart(), this.posLen()); this.toHexDOM_sub(node, (this.length >= 0) ? 'dlen' : 'ulen', this.stream, this.posLen(), this.posContent()); if (this.sub === null) { diff --git a/index.js b/index.js index 3e4385f..caea287 100644 --- a/index.js +++ b/index.js @@ -229,4 +229,4 @@ if (window.devicePixelRatio >= 2 ) { let treecollapse = document.querySelector(':root'); treecollapse.style.setProperty('--zoom-fix', '-1.5px'); } -console.log(window.devicePixelRatio); \ No newline at end of file +console.log(window.devicePixelRatio); diff --git a/package.json b/package.json index 18a9fe1..2f74cc6 100644 --- a/package.json +++ b/package.json @@ -13,9 +13,9 @@ "license": "ISC", "bugs": { "url": "https://github.com/lapo-luchini/asn1js/issues" }, "homepage": "https://lapo.it/asn1js/", - "files": [ "asn1.js", "base64.js", "hex.js", "int10.js", "defs.js", "oids.js", "rfcdef.js", "dumpASN1.js" ], + "files": [ "asn1.js", "base64.js", "hex.js", "int10.js", "dom.js", "defs.js", "oids.js", "rfcdef.js", "dumpASN1.js" ], "scripts": { - "lint": "npx eslint asn1.js base64.js hex.js int10.js defs.js oids.js rfcdef.js tags.js context.js index.js parseRFC.js dumpASN1.js test.js testDefs.js vite.config.js theme.js", + "lint": "npx eslint asn1.js base64.js hex.js int10.js dom.js defs.js oids.js rfcdef.js tags.js context.js index.js parseRFC.js dumpASN1.js test.js testDefs.js vite.config.js theme.js", "lint-action": "npx @action-validator/cli .github/workflows/node.js.yml", "build": "vite build", "serve": "echo 'Connect to http://localhost:3000/' ; npx statik --port 3000 .",