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
  • 1.31
  • master
  • 1.10.0
  • 1.30.1
  • 1.31.0
  • 1.8.0
  • 1.9.0
  • 3.100.0
  • 3.100.1
  • 3.100.10
  • 3.100.11
  • 3.100.12
  • 3.100.13
  • 3.100.14
  • 3.100.15
  • 3.100.16
  • 3.100.17
  • 3.100.18
  • 3.100.19
  • 3.100.2
  • 3.100.20
  • 3.100.3
  • 3.100.4
  • 3.100.5
  • 3.100.6
  • 3.100.7
  • 3.100.8
  • 3.100.9
  • 3.101.0
  • 3.101.1
  • 3.101.2
  • 3.101.3
  • 3.102.0
  • 3.102.1
  • 3.102.2
  • 3.102.3
  • 3.102.4
  • 3.102.5
  • 3.102.6
  • 3.103.0
  • 3.103.1
  • 3.104.0
  • 3.104.1
  • 3.105.0
  • 3.105.1
  • 3.105.2
  • 3.106.0
  • 3.106.1
  • 3.107.0
  • 3.108.0
  • 3.108.1
  • 3.108.2
  • 3.108.3
  • 3.108.4
  • 3.108.5
  • 3.109.0
  • 3.110.0
  • 3.110.1
  • 3.110.2
  • 3.110.3
  • 3.110.4
  • 3.111.0
  • 3.112.0
  • 3.112.1
  • 3.112.2
  • 3.112.3
  • 3.112.4
  • 3.113.0
  • 3.114.0
  • 3.114.1
  • 3.114.2
  • 3.114.3
  • 3.114.4
  • 3.114.5
  • 3.114.6
  • 3.114.7
  • 3.115.0
  • 3.115.1
  • 3.115.2
  • 3.115.3
  • 3.115.4
  • 3.116.0
  • 3.116.1
  • 3.117.0
  • 3.117.1
  • 3.117.2
  • 3.117.3
  • 3.118.0
  • 3.118.1
  • 3.119.0
  • 3.120.0
  • 3.121.0
  • 3.51.5
  • 3.52.0
  • 3.52.1
  • 3.53.0
  • 3.54.0
  • 3.55.0
  • 3.55.1
  • 3.55.2
  • 3.55.3
  • 3.55.4
102 results

Target

Select target project
  • oss/libraries/javascript/monster
1 result
Select Git revision
  • 1.31
  • master
  • 1.10.0
  • 1.30.1
  • 1.31.0
  • 1.8.0
  • 1.9.0
  • 3.100.0
  • 3.100.1
  • 3.100.10
  • 3.100.11
  • 3.100.12
  • 3.100.13
  • 3.100.14
  • 3.100.15
  • 3.100.16
  • 3.100.17
  • 3.100.18
  • 3.100.19
  • 3.100.2
  • 3.100.20
  • 3.100.3
  • 3.100.4
  • 3.100.5
  • 3.100.6
  • 3.100.7
  • 3.100.8
  • 3.100.9
  • 3.101.0
  • 3.101.1
  • 3.101.2
  • 3.101.3
  • 3.102.0
  • 3.102.1
  • 3.102.2
  • 3.102.3
  • 3.102.4
  • 3.102.5
  • 3.102.6
  • 3.103.0
  • 3.103.1
  • 3.104.0
  • 3.104.1
  • 3.105.0
  • 3.105.1
  • 3.105.2
  • 3.106.0
  • 3.106.1
  • 3.107.0
  • 3.108.0
  • 3.108.1
  • 3.108.2
  • 3.108.3
  • 3.108.4
  • 3.108.5
  • 3.109.0
  • 3.110.0
  • 3.110.1
  • 3.110.2
  • 3.110.3
  • 3.110.4
  • 3.111.0
  • 3.112.0
  • 3.112.1
  • 3.112.2
  • 3.112.3
  • 3.112.4
  • 3.113.0
  • 3.114.0
  • 3.114.1
  • 3.114.2
  • 3.114.3
  • 3.114.4
  • 3.114.5
  • 3.114.6
  • 3.114.7
  • 3.115.0
  • 3.115.1
  • 3.115.2
  • 3.115.3
  • 3.115.4
  • 3.116.0
  • 3.116.1
  • 3.117.0
  • 3.117.1
  • 3.117.2
  • 3.117.3
  • 3.118.0
  • 3.118.1
  • 3.119.0
  • 3.120.0
  • 3.121.0
  • 3.51.5
  • 3.52.0
  • 3.52.1
  • 3.53.0
  • 3.54.0
  • 3.55.0
  • 3.55.1
  • 3.55.2
  • 3.55.3
  • 3.55.4
102 results
Show changes
Commits on Source (4)
<a name="v3.38.0"></a>
## [v3.38.0] - 2023-04-03
### Add Features
- new function initOptionsFromAttributes
<a name="v3.37.0"></a>
## [v3.37.0] - 2023-04-02
### Add Features
......@@ -530,6 +536,7 @@
<a name="1.8.0"></a>
## 1.8.0 - 2021-08-15
[v3.38.0]: https://gitlab.schukai.com/oss/libraries/javascript/monster/compare/v3.37.0...v3.38.0
[v3.37.0]: https://gitlab.schukai.com/oss/libraries/javascript/monster/compare/v3.36.0...v3.37.0
[v3.36.0]: https://gitlab.schukai.com/oss/libraries/javascript/monster/compare/v3.35.4...v3.36.0
[v3.35.4]: https://gitlab.schukai.com/oss/libraries/javascript/monster/compare/v3.35.3...v3.35.4
......
{
"name": "@schukai/monster",
"version": "3.36.0",
"version": "3.37.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 {Pathfinder} from '../../data/pathfinder.mjs';
import {isFunction} from '../../types/is.mjs';
export {initOptionsFromAttributes };
/**
* Initializes the given options object based on the attributes of the current DOM element.
* The function looks for attributes with the prefix 'data-monster-option-', and maps them to
* properties in the options object. It replaces the dashes with dots to form the property path.
* For example, the attribute 'data-monster-option-url' maps to the 'url' property in the options object.
*
* With the mapping parameter, the attribute value can be mapped to a different value.
* For example, the attribute 'data-monster-option-foo' maps to the 'bar' property in the options object.
*
* The mapping object would look like this:
* {
* 'foo': (value) => value + 'bar'
* // the value of the attribute 'data-monster-option-foo' is appended with 'bar'
* // and assigned to the 'bar' property in the options object.
* // e.g. <div data-monster-option-foo="foo"></div>
* 'bar.baz': (value) => value + 'bar'
* // the value of the attribute 'data-monster-option-bar-baz' is appended with 'bar'
* // and assigned to the 'bar.baz' property in the options object.
* // e.g. <div data-monster-option-bar-baz="foo"></div>
* }
*
* @param {HTMLElement} element - The DOM element to be used as the source of the attributes.
* @param {Object} options - The options object to be initialized.
* @param {Object} mapping - A mapping between the attribute value and the property value.
* @param {string} prefix - The prefix of the attributes to be considered.
* @returns {Object} - The initialized options object.
* @this HTMLElement - The context of the DOM element.
*/
function initOptionsFromAttributes(element, options, mapping={},prefix = 'data-monster-option-') {
if (!(element instanceof HTMLElement)) return options;
if (!element.hasAttributes()) return options;
const finder = new Pathfinder(options);
element.getAttributeNames().forEach((name) => {
if (!name.startsWith(prefix)) return;
// check if the attribute name is a valid option.
// the mapping between the attribute is simple. The dash is replaced by a dot.
// e.g. data-monster-url => url
const optionName = name.replace(prefix, '').replace(/-/g, '.');
if (!finder.exists(optionName)) return;
if (element.hasAttribute(name)) {
let value = element.getAttribute(name);
if (mapping.hasOwnProperty(optionName)&&isFunction(mapping[optionName])) {
value = mapping[optionName](value);
}
const typeOfOptionValue = typeof finder.getVia(optionName);
if (typeOfOptionValue === 'boolean') {
value = value === 'true';
} else if (typeOfOptionValue === 'number') {
value = Number(value);
} else if (typeOfOptionValue === 'string') {
value = String(value);
} else if (typeOfOptionValue === 'object') {
value = JSON.parse(value);
}
finder.setVia(optionName, value);
}
})
return options;
}
\ No newline at end of file
......@@ -142,7 +142,7 @@ function getMonsterVersion() {
}
/** don't touch, replaced by make with package.json version */
monsterVersion = new Version("3.36.0");
monsterVersion = new Version("3.37.0");
return monsterVersion;
}
{
"name": "monster",
"version": "3.36.0",
"version": "3.37.0",
"description": "monster",
"repository": {
"type": "git",
......
......@@ -1611,7 +1611,7 @@ packages:
execa: 4.1.0
polyfill-library: 3.111.0
semver: 7.3.8
snyk: 1.1133.0
snyk: 1.1134.0
yargs: 15.4.1
transitivePeerDependencies:
- supports-color
......@@ -4091,8 +4091,8 @@ packages:
supports-color: 7.2.0
dev: true
/snyk@1.1133.0:
resolution: {integrity: sha512-op/OCcfZZcR1nZDwKS4sVYB51Dqc327/t47a0nyzVTtB8qVQJyujnIcsWaOeY4uEoaJnUNxp1Mp5kgN6jhwJFA==}
/snyk@1.1134.0:
resolution: {integrity: sha512-gy+Aas1F10AEkAH40f8ewPavFCVjUW/izTv1RJNRwuOt52wqFgb4mkW3F0U6xq4HCNiEzmWCkz8BN21uzdMPUA==}
engines: {node: '>=12'}
hasBin: true
requiresBuild: true
......
......@@ -8,7 +8,7 @@ import {initJSDOM} from "../../util/jsdom.mjs";
describe('Events', function () {
before(async function () {
initJSDOM();
await initJSDOM();
})
describe('findTargetElementFromEvent()', function () {
......
import {expect} from "chai"
import {initOptionsFromAttributes} from "../../../../..//application/source/dom/util/init-options-from-attributes.mjs";
import {initJSDOM} from "../../../util/jsdom.mjs";
describe('initOptionsFromAttributes', () => {
let element;
let options;
before(async function () {
await initJSDOM();
})
beforeEach(() => {
options = { url: "", key: { subkey: "" } };
element = document.createElement('div');
});
it('should initialize options with matching attributes', () => {
element.setAttribute('data-monster-option-url', 'https://example.com');
element.setAttribute('data-monster-option-key.subkey', 'test');
const result = initOptionsFromAttributes(element, options);
expect(result.url).to.equal('https://example.com');
expect(result.key.subkey).to.equal('test');
});
it('should not modify options without matching attributes', () => {
const result = initOptionsFromAttributes(element, options);
expect(result.url).to.equal('');
expect(result.key.subkey).to.equal('');
});
it('should ignore attributes without the correct prefix', () => {
element.setAttribute('data-some-option-url', 'https://example.com');
const result = initOptionsFromAttributes(element, options);
expect(result.url).to.equal('');
});
it('should ignore attributes with invalid option paths', () => {
element.setAttribute('data-monster-option-nonexistent', 'value');
const result = initOptionsFromAttributes(element, options);
expect(result).to.deep.equal(options);
});
it('should apply mapping for a single attribute', () => {
element.setAttribute('data-monster-option-url', 'example');
const mapping = {
'url': (value) => 'https://' + value + '.com'
};
const result = initOptionsFromAttributes(element, options, mapping);
expect(result.url).to.equal('https://example.com');
});
it('should apply mapping for a nested attribute', () => {
element.setAttribute('data-monster-option-key-subkey', '123');
const mapping = {
'key.subkey': (value) => parseInt(value, 10) * 2
};
const result = initOptionsFromAttributes(element, options, mapping);
expect(result.key.subkey).to.equal("246");
});
it('should apply multiple mappings', () => {
element.setAttribute('data-monster-option-url', 'example');
element.setAttribute('data-monster-option-key.subkey', '123');
const mapping = {
'url': (value) => 'https://' + value + '.com',
'key.subkey': (value) => parseInt(value, 10) * 2
};
const result = initOptionsFromAttributes(element, options, mapping);
expect(result.url).to.equal('https://example.com');
expect(result.key.subkey).to.equal("246");
});
it('should ignore mappings for non-existing attributes', () => {
const mapping = {
'url': (value) => 'https://' + value + '.com'
};
const result = initOptionsFromAttributes(element, options, mapping);
expect(result.url).to.equal('');
});
it('should ignore mappings for invalid option paths', () => {
element.setAttribute('data-monster-option-nonexistent', 'value');
const mapping = {
'nonexistent': (value) => value + 'bar'
};
const result = initOptionsFromAttributes(element, options, mapping);
expect(result).to.deep.equal(options);
});
it('should apply mapping only to specified attributes', () => {
element.setAttribute('data-monster-option-url', 'example');
element.setAttribute('data-monster-option-key.subkey', '123');
const mapping = {
'url': (value) => 'https://' + value + '.com'
};
const result = initOptionsFromAttributes(element, options, mapping);
expect(result.url).to.equal('https://example.com');
expect(result.key.subkey).to.equal('123');
});
it('should not apply mapping if not a function', () => {
element.setAttribute('data-monster-option-url', 'example');
const mapping = {
'url': 'https://example.com'
};
const result = initOptionsFromAttributes(element, options, mapping);
expect(result.url).to.equal('example');
});
it('should apply mapping with custom prefix', () => {
element.setAttribute('data-custom-option-url', 'example');
const mapping = {
'url': (value) => 'https://' + value + '.com'
};
const result = initOptionsFromAttributes(element, options, mapping, 'data-custom-option-');
expect(result.url).to.equal('https://example.com');
});
it('should not apply mapping with incorrect custom prefix', () => {
element.setAttribute('data-custom-option-url', 'example');
const mapping = {
'url': (value) => 'https://' + value + '.com'
};
const result = initOptionsFromAttributes(element, options, mapping);
expect(result.url).to.equal('');
});
});
......@@ -7,7 +7,7 @@ describe('Monster', function () {
let monsterVersion
/** don´t touch, replaced by make with package.json version */
monsterVersion = new Version("3.36.0")
monsterVersion = new Version("3.37.0")
let m = getMonsterVersion();
......
{"version":"3.37.0"}
{"version":"3.38.0"}