Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
Loading items

Target

Select target project
  • oss/libraries/javascript/monster
1 result
Select Git revision
Loading items
Show changes
Commits on Source (5)
Showing
with 730 additions and 165 deletions
<a name="v3.34.0"></a>
## [v3.34.0] - 2023-03-27
### Add Features
- new function detectRuntimeEnvironment and convertToPixels
### Changes
- update packages
<a name="v3.33.0"></a>
## [v3.33.0] - 2023-03-26
### Add Features
......@@ -479,6 +488,7 @@
<a name="1.8.0"></a>
## 1.8.0 - 2021-08-15
[v3.34.0]: https://gitlab.schukai.com/oss/libraries/javascript/monster/compare/v3.33.0...v3.34.0
[v3.33.0]: https://gitlab.schukai.com/oss/libraries/javascript/monster/compare/v3.32.0...v3.33.0
[v3.32.0]: https://gitlab.schukai.com/oss/libraries/javascript/monster/compare/v3.31.1...v3.32.0
[v3.31.1]: https://gitlab.schukai.com/oss/libraries/javascript/monster/compare/v3.31.0...v3.31.1
......
{
"name": "@schukai/monster",
"version": "3.32.0",
"version": "3.33.0",
"description": "Monster is a simple library for creating fast, robust and lightweight websites.",
"keywords": [
"framework",
......
/**
* Copyright schukai GmbH and contributors 2023. All Rights Reserved.
* Node module: @schukai/monster
* This file is licensed under the AGPLv3 License.
* License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
*/
import {getWindow} from './util.mjs';
export {convertToPixels, getDeviceDPI}
/**
* Stores the DPI of the device.
*
* @returns {number}
* @type {number}
*/
let CURRENT_DEVICE_DPI = function () {
let i = 0;
for (i = 56; i < 2000; i++) {
if (getWindow().matchMedia("(max-resolution: " + i + "dpi)").matches === true) {
return i;
}
}
return i;
};
/**
* Returns the DPI of the device.
*
* @returns {number}
*/
function getDeviceDPI() {
// only call the function once
if (typeof CURRENT_DEVICE_DPI === 'function') {
CURRENT_DEVICE_DPI = CURRENT_DEVICE_DPI();
}
return getWindow().devicePixelRatio * CURRENT_DEVICE_DPI;
}
/**
* Converts a CSS value to pixels.
*
* As Example:
*
* ```js
* convertToPixels('1em') // returns the current font size in pixels
* convertToPixels('1rem') // returns the current root font size in pixels
* convertToPixels('1px') // returns 1
* convertToPixels('100%') // returns the current width of the parent element in pixels
* ```
*
* Following units are supported:
* - px
* - em
* - rem
* - %
*
* @param value
* @param parentElement
* @param fontSizeElement
* @returns {number}
* @license AGPLv3
* @since 1.6.0
* @copyright schukai GmbH
* @memberOf Monster.DOM
* @throws {Error} Unsupported unit
*/
function convertToPixels(value, parentElement = document.documentElement, fontSizeElement = document.documentElement) {
const regex = /^([\d.]+)(.*)$/;
const [, num, unit] = value.match(regex);
const number = parseFloat(num);
const dpi = getDeviceDPI();
if (unit === 'px') {
return number;
} else if (unit === 'em') {
const fontSize = parseFloat(window.getComputedStyle(fontSizeElement).fontSize);
return number * fontSize;
} else if (unit === 'rem') {
const rootFontSize = parseFloat(window.getComputedStyle(parentElement).fontSize);
return number * rootFontSize;
} else if (unit === '%') {
const parentWidth = parseFloat(window.getComputedStyle(parentElement).width);
return (number * parentWidth) / 100;
} else if (unit === 'in') {
return number * dpi;
} else if (unit === 'cm') {
return (number * dpi) / 2.54;
} else if (unit === 'mm') {
return (number * dpi) / 25.4;
} else if (unit === 'pt') {
return (number * dpi) / 72;
} else if (unit === 'pc') {
return (number * dpi) / 6;
} else {
throw new Error(`Unsupported unit: ${unit}`);
}
}
......@@ -142,7 +142,7 @@ function getMonsterVersion() {
}
/** don't touch, replaced by make with package.json version */
monsterVersion = new Version("3.32.0");
monsterVersion = new Version("3.33.0");
return monsterVersion;
}
/**
* Copyright schukai GmbH and contributors 2023. All Rights Reserved.
* Node module: @schukai/monster
* This file is licensed under the AGPLv3 License.
* License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
*/
const ENV_AWS_LAMBDA = 'aws-lambda';
const ENV_GOOGLE_FUNCTIONS = 'google-functions';
const ENV_ELECTRON = 'electron';
const ENV_NODE = 'node';
const ENV_BROWSER = 'browser';
const ENV_WEB_WORKER = 'web-worker';
const ENV_DENO = 'deno';
const ENV_UNKNOWN = 'unknown';
/**
* Detects and returns the current runtime environment.
*
* - 'aws-lambda': AWS Lambda environment
* - 'google-functions': Google Cloud Functions environment
* - 'electron': Electron environment
* - 'node': Node.js environment
* - 'browser': Browser environment
* - 'web-worker': Web Worker environment
* - 'deno': Deno environment
* - 'react-native': React Native environment
* - 'unknown': Unknown environment
*
* @returns {string} The detected runtime environment. Possible values are:
*/
function detectRuntimeEnvironment() {
// AWS Lambda environment
if (
typeof process !== 'undefined' &&
process.env != null &&
process.env.AWS_LAMBDA_FUNCTION_NAME
) {
return ENV_AWS_LAMBDA;
}
// Google Cloud Functions environment
if (
typeof process !== 'undefined' &&
process.env != null &&
process.env.FUNCTION_NAME
) {
return ENV_GOOGLE_FUNCTIONS;
}
// Node.js environment
if (
typeof process !== 'undefined' &&
process.versions != null &&
process.versions.node != null
) {
// Electron environment
if (process.versions.electron != null) {
return ENV_ELECTRON;
}
return ENV_NODE;
}
// Browser environment
if (
typeof window !== 'undefined' &&
typeof window.document !== 'undefined' &&
typeof navigator !== 'undefined' &&
typeof navigator.userAgent === 'string'
) {
// Web Worker environment
if (typeof self === 'object' && typeof importScripts === 'function') {
return ENV_WEB_WORKER;
}
return ENV_BROWSER;
}
// Deno environment
if (typeof Deno !== 'undefined') {
return ENV_DENO;
}
// Unknown environment
return ENV_UNKNOWN;
}
export {
ENV_AWS_LAMBDA,
ENV_GOOGLE_FUNCTIONS,
ENV_ELECTRON,
ENV_NODE,
ENV_BROWSER,
ENV_WEB_WORKER,
ENV_DENO,
ENV_UNKNOWN,
detectRuntimeEnvironment}
\ No newline at end of file
{
"name": "monster",
"version": "3.32.0",
"version": "3.33.0",
"description": "monster",
"repository": {
"type": "git",
......@@ -21,7 +21,7 @@
"license": "see LICENSE file",
"devDependencies": {
"@oss/web-components-build-tools": "^1.2.8",
"@peculiar/webcrypto": "^1.4.2",
"@peculiar/webcrypto": "^1.4.3",
"autoprefixer": "^10.4.14",
"browserslist": "^4.21.5",
"btoa": "^1.2.1",
......@@ -32,8 +32,8 @@
"create-polyfill-service-url": "^2.2.6",
"crypt": "^0.0.2",
"cssnano": "^5.1.15",
"esbuild": "^0.17.12",
"flow-bin": "^0.202.0",
"esbuild": "^0.17.14",
"flow-bin": "^0.202.1",
"fs": "0.0.1-security",
"glob": "^9.3.2",
"graphviz": "^0.0.9",
......@@ -51,13 +51,13 @@
"postcss-load-config": "^4.0.1",
"postcss-mixins": "^9.0.4",
"postcss-nested": "^6.0.1",
"postcss-nesting": "^11.2.1",
"postcss-nesting": "^11.2.2",
"postcss-normalize": "^10.0.1",
"postcss-responsive-type": "^1.0.0",
"postcss-rtlcss": "^4.0.3",
"postcss-strip-units": "^2.0.1",
"rome": "^11.0.0",
"sinon": "^15.0.2",
"sinon": "^15.0.3",
"url": "^0.11.0",
"url-exist": "3.0.1",
"util": "^0.12.5",
......
This diff is collapsed.
'use strict';
import {expect} from "chai"
import {ATTRIBUTEPREFIX, Assembler} from "../../../../application/source/dom/assembler.mjs";
......
'use strict';
import {expect} from "chai"
......
import {expect} from 'chai';
import {convertToPixels, getDeviceDPI} from "../../../../application/source/dom/dimension.mjs";
import {getWindow} from "../../../../application/source/dom/util.mjs";
import {initJSDOM, isBrowser, JSDOMExport as JSDOM} from "../../util/jsdom.mjs";
import {getGlobal} from "../../../../application/source/types/global.mjs";
import {detectRuntimeEnvironment} from "../../../../application/source/util/runtime.mjs";
function getMockWindow(dpi) {
if(detectRuntimeEnvironment() === 'browser') {
return getWindow();
}
const dom = new JSDOM('', {
pretendToBeVisual: true,
resources: 'usable',
});
dom.window.matchMedia = (query) => {
const dpiRegex = /\(max-resolution: (\d+)dpi\)/;
const match = query.match(dpiRegex);
if (match) {
const maxDpi = parseInt(match[1], 10);
return {matches: dpi <= maxDpi};
}
return {matches: false};
};
return dom.window;
}
describe('dimension', () => {
let currentEnvironment;
before(function (done) {
initJSDOM().then(() => {
//chaiDom(getDocument());
done();
});
})
beforeEach(() => {
const testDpi = 96;
const testWindow = getMockWindow(testDpi);
getGlobal().window = testWindow;
});
afterEach(() => {
delete getGlobal().window;
});
describe('convertToPixels', () => {
it('should correctly convert px values', () => {
const result = convertToPixels('100px');
expect(result).to.equal(100);
});
it('should correctly convert em values', () => {
const testElement = document.createElement('div');
testElement.style.fontSize = '16px';
document.body.appendChild(testElement);
const result = convertToPixels('2em', testElement, testElement);
expect(result).to.equal(32);
document.body.removeChild(testElement);
});
it('should correctly convert rem values', () => {
const testElement = document.createElement('div');
testElement.style.fontSize = '16px';
document.documentElement.appendChild(testElement);
const result = convertToPixels('2rem', testElement);
expect(result).to.equal(32);
document.documentElement.removeChild(testElement);
});
it('should correctly convert percentage values', () => {
const testElement = document.createElement('div');
testElement.style.width = '500px';
document.body.appendChild(testElement);
const result = convertToPixels('50%', testElement);
expect(result).to.equal(250);
document.body.removeChild(testElement);
});
it('should throw an error for unsupported units', () => {
expect(() => convertToPixels('10unsupportedUnit')).to.throw('Unsupported unit: unsupportedUnit');
});
});
describe('getDeviceDPI', () => {
it('should return the correct device DPI', () => {
const testDpi = 96;
const testWindow = getMockWindow(testDpi);
getGlobal().window = testWindow;
const deviceDpi = getDeviceDPI();
expect(deviceDpi).to.equal(testDpi * testWindow.devicePixelRatio);
delete getGlobal().window;
});
it('should cache the result and return the same value', () => {
const testDpi = 96;
const testWindow = getMockWindow(testDpi);
getGlobal().window = testWindow;
const deviceDpi1 = getDeviceDPI();
const deviceDpi2 = getDeviceDPI();
expect(deviceDpi1).to.equal(deviceDpi2);
delete getGlobal().window;
});
});
});
\ No newline at end of file
......@@ -7,7 +7,7 @@ describe('Monster', function () {
let monsterVersion
/** don´t touch, replaced by make with package.json version */
monsterVersion = new Version("3.32.0")
monsterVersion = new Version("3.33.0")
let m = getMonsterVersion();
......
......@@ -6,6 +6,7 @@ import {getGlobal} from "../../../application/source/types/global.mjs";
export const isBrowser = new Function("try {return this===window;}catch(e){ return false;}");
export const isNode = new Function("try {return this===global;}catch(e){return false;}");
let JSDOMExport = null;
/**
* this helper function creates the dom stack in the node environment
......@@ -27,6 +28,7 @@ function initJSDOM(options) {
}, options || {})
return import("jsdom").then(({JSDOM}) => {
JSDOMExport = JSDOM;
const {window} = new JSDOM(`<html>
<head>
</head>
......@@ -85,5 +87,5 @@ function initJSDOM(options) {
});
}
export {initJSDOM}
export {initJSDOM,JSDOMExport}
......@@ -20,6 +20,7 @@ import "../cases/dom/resource.mjs";
import "../cases/dom/resourcemanager.mjs";
import "../cases/dom/util.mjs";
import "../cases/dom/find.mjs";
import "../cases/dom/dimension.mjs";
import "../cases/dom/customelement.mjs";
import "../cases/dom/attributes.mjs";
import "../cases/dom/events.mjs";
......
......@@ -14,8 +14,8 @@
</head>
<body>
<div id="headline" style="display: flex;align-items: center;justify-content: center;flex-direction: column;">
<h1 style='margin-bottom: 0.1em;'>Monster 3.32.0</h1>
<div id="lastupdate" style='font-size:0.7em'>last update So 26. Mär 17:02:25 CEST 2023</div>
<h1 style='margin-bottom: 0.1em;'>Monster 3.33.0</h1>
<div id="lastupdate" style='font-size:0.7em'>last update Mo 27. Mär 18:10:51 CEST 2023</div>
</div>
<div id="mocks"></div>
<div id="mocha"></div>
......
This diff is collapsed.
{"version":"3.33.0"}
{"version":"3.34.0"}