First commits be like

This commit is contained in:
2020-04-21 00:03:26 +02:00
parent f0e071212f
commit 93eea321a8
12 changed files with 440 additions and 0 deletions

26
sunpy/node/node.js Normal file
View File

@@ -0,0 +1,26 @@
const handlers = {};
var onloadOld = onload || (() => {});
onload = () => {
onloadOld();
init();
}
function init() {
for(const k in handlers) {
for (const n of getNodes(k)) {
handlers[k](n);
}
}
}
function getNodes(scriptType) {
return Array.from( document.querySelectorAll(`script[type="orep/${scriptType}"]`) ).filter(n => !n.dataset.handled);
}
export function register(scriptType, handler) {
if (scriptType in handlers)
throw new Error(`'${scriptType}' is already registered`);
handlers[scriptType] = handler;
}

View File

@@ -0,0 +1,5 @@
export class BaseNode {
constructor(node) {
this.node = node;
}
}

View File

@@ -0,0 +1,22 @@
import { SwapNode } from "./swapnode.js";
import { PropertyReader } from "../propertyreader.js";
export class PropertyNode extends SwapNode {
constructor(node, swapNode, propertyReader) {
super(node, swapNode);
if (!(propertyReader instanceof PropertyReader))
throw TypeError("propertyReader must be an instance of PropertyReader");
this.propertyReader = propertyReader;
super.replaceNodes();
}
/**
* @override
*/
linker(node, swapNode) {
this.properties = this.propertyReader.read(node.innerText);
}
}

View File

@@ -0,0 +1,35 @@
import { BaseNode } from "./basenode.js";
export class SwapNode extends BaseNode {
constructor(node, swapNode) {
super(node);
if (!(swapNode instanceof Node))
throw TypeError("swapNode must be instance of Node");
this.swapNode = swapNode;
this.hasReplaceNodes = false;
}
replaceNodes() {
if (this.hasReplaceNodes)
return false;
this.linker(this.node, this.swapNode);
this.node.parentNode.replaceChild(this.swapNode, this.node);
this.node = this.swapNode;
delete this.swapNode;
return this.hasReplaceNodes = true;
}
/**
* Override this to have access to the script tag that gets converted to the new node inplace
* @param {Node} node Node of the original script tag
* @param {Node} swapNode Node of the new element that replaces the script tag inline
*/
linker(node, swapNode) {}
}

View File

@@ -0,0 +1,69 @@
class PropertyList {
constructor(...properties) {
for (const property of properties) {
if (!(property instanceof Property))
throw TypeError("properties has to be instances of Property");
}
this.properties = [];
for (const property of properties) {
this.add(property);
}
}
add(property) {
Object.defineProperty(this, property.key, {
get() { return property.value },
set(value) { property.parse(value) }
});
this.properties.push(property);
}
}
export class Property {
constructor(key, defaultValue = null, parser = (v) => v) {
this.key = key;
this.defaultValue = defaultValue;
this.parser = parser;
this.parse(this.defaultValue);
}
parse(v) {
this.value = this.parser(v);
return this.value;
}
clone() {
return new Property(this.key, this.defaultValue, this.parser);
}
}
/** Regex attempts (first to final)
* ^\s*({0})\s*(=|:)\s*(.*)$
* ^\s*({0})\s*(=|:)\s*([^#\n]*).*$
* ^\s*({0})\s*(=|:)\s*(.*?)((#|\/\/).*)?$
*/
export class PropertyReader {
constructor(...properties) {
this.propertyList = new PropertyList(...properties);
this.regex = new RegExp(`^\\s*(${this.propertyList.properties.map(p => p.key).join("|")})\\s*(=|:)\\s*(.*?)((#|\\/\\/).*)?$`, "mg");
}
read(str) {
let match = null;
while ((match = this.regex.exec(str)) !== null) {
if (match.index === this.regex.lastIndex) // Infinite loop fix
this.regex.lastIndex++;
const key = match[1];
const value = match[3].trim();
this.propertyList[key] = value;
}
return this.propertyList;
}
}