@burger-editor/frozen-patty 0.10.0 → 3.5.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -27
- package/{lib → dist}/frozen-patty.d.ts +4 -18
- package/dist/frozen-patty.js +43 -0
- package/dist/get-value.d.ts +10 -0
- package/dist/get-value.js +15 -0
- package/dist/get-values.d.ts +10 -0
- package/{lib/getValue.js → dist/get-values.js} +42 -34
- package/dist/get.d.ts +9 -0
- package/dist/get.js +19 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +14 -0
- package/dist/set-value.d.ts +14 -0
- package/{lib/setValue.js → dist/set-value.js} +105 -106
- package/dist/set.d.ts +9 -0
- package/dist/set.js +59 -0
- package/dist/types.d.ts +6 -0
- package/dist/types.js +1 -0
- package/{lib/util.d.ts → dist/utils.d.ts} +2 -2
- package/dist/utils.js +25 -0
- package/package.json +34 -39
- package/LICENSE +0 -21
- package/lib/frozen-patty.d.ts.map +0 -1
- package/lib/frozen-patty.js +0 -47
- package/lib/frozen-patty.js.map +0 -1
- package/lib/get.d.ts +0 -4
- package/lib/get.d.ts.map +0 -1
- package/lib/get.js +0 -20
- package/lib/get.js.map +0 -1
- package/lib/getValue.d.ts +0 -10
- package/lib/getValue.d.ts.map +0 -1
- package/lib/getValue.js.map +0 -1
- package/lib/index.d.ts +0 -35
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js +0 -55
- package/lib/index.js.map +0 -1
- package/lib/polyfill.d.ts +0 -1
- package/lib/polyfill.d.ts.map +0 -1
- package/lib/polyfill.js +0 -20
- package/lib/polyfill.js.map +0 -1
- package/lib/set.d.ts +0 -4
- package/lib/set.d.ts.map +0 -1
- package/lib/set.js +0 -56
- package/lib/set.js.map +0 -1
- package/lib/setValue.d.ts +0 -15
- package/lib/setValue.d.ts.map +0 -1
- package/lib/setValue.js.map +0 -1
- package/lib/util.d.ts.map +0 -1
- package/lib/util.js +0 -30
- package/lib/util.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen-patty
|
|
2
2
|
|
|
3
|
-
[](https://badge.fury.io/js/@burger-editor%2Ffrozen-patty)
|
|
4
4
|
|
|
5
5
|
Pure HTML to JSON converter that not use template engine.
|
|
6
6
|
|
|
@@ -15,53 +15,52 @@ $ npm install -D @burger-editor/frozen-patty
|
|
|
15
15
|
### Extraction
|
|
16
16
|
|
|
17
17
|
```js
|
|
18
|
-
import
|
|
18
|
+
import frozenPatty from '@burger-editor/frozen-patty';
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
frozenPatty('<div data-field="text">value</div>').toJSON(); // => { text: 'value' }
|
|
21
|
+
frozenPatty('<div data-field="field-name">value</div>').toJSON(); // => { 'field-name': 'value' }
|
|
22
|
+
frozenPatty('<a href="http://localhost" data-field="href:href">link</a>').toJSON(); // => { 'href': 'http://localhost' }
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
frozenPatty('<div data-bge="text">value</div>', { attr: 'bge' }).toJSON(); // => { text: 'value' }
|
|
25
25
|
```
|
|
26
26
|
|
|
27
27
|
### Data merge
|
|
28
28
|
|
|
29
29
|
```js
|
|
30
|
-
|
|
30
|
+
frozenPatty('<div data-field="text">value</div>').merge({ text: 'merged' }).toHTML(); // => "<div data-field="text">merged</div>";
|
|
31
31
|
```
|
|
32
32
|
|
|
33
|
-
|
|
34
33
|
## API
|
|
35
34
|
|
|
36
|
-
###
|
|
35
|
+
### `frozenPatty(html[, options])`
|
|
37
36
|
|
|
38
37
|
Extraction data from HTML.
|
|
39
38
|
|
|
40
|
-
####
|
|
39
|
+
#### Parameters
|
|
41
40
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
html
|
|
45
|
-
options
|
|
41
|
+
| parameter | type | required | descriptions |
|
|
42
|
+
| --------- | -------- | -------- | ------------- |
|
|
43
|
+
| html | `string` | required | Original HTML |
|
|
44
|
+
| options | `Object` | optional | † |
|
|
46
45
|
|
|
47
|
-
####
|
|
46
|
+
#### Options†
|
|
48
47
|
|
|
49
|
-
options|type|default|descriptions
|
|
50
|
-
|
|
51
|
-
attr|string
|
|
48
|
+
| options | type | default | descriptions |
|
|
49
|
+
| ------- | ------ | --------- | ------------------------------------------------------------------------------------ |
|
|
50
|
+
| attr | string | `"field"` | **Data attribute** name for specifying the node that FrozenPatty treats as a _field_ |
|
|
52
51
|
|
|
53
|
-
### merge
|
|
52
|
+
### `merge(data)`
|
|
54
53
|
|
|
55
|
-
|
|
54
|
+
Merge data.
|
|
56
55
|
|
|
57
|
-
args|type|required|descriptions
|
|
58
|
-
|
|
59
|
-
data
|
|
56
|
+
| args | type | required | descriptions |
|
|
57
|
+
| ---- | -------- | -------- | ------------ |
|
|
58
|
+
| data | `Object` | required | New data |
|
|
60
59
|
|
|
61
|
-
### toJSON
|
|
60
|
+
### `toJSON`
|
|
62
61
|
|
|
63
|
-
|
|
62
|
+
Convert to JSON.
|
|
64
63
|
|
|
65
|
-
### toHTML
|
|
64
|
+
### `toHTML`
|
|
66
65
|
|
|
67
|
-
|
|
66
|
+
Convert to HTML string.
|
|
@@ -1,11 +1,6 @@
|
|
|
1
|
+
import type { Filter, FrozenPattyData } from './types.js';
|
|
1
2
|
export default class FrozenPatty {
|
|
2
|
-
private
|
|
3
|
-
private _attr;
|
|
4
|
-
private _typeConvert;
|
|
5
|
-
/**
|
|
6
|
-
* Value filter
|
|
7
|
-
*/
|
|
8
|
-
private _filter?;
|
|
3
|
+
#private;
|
|
9
4
|
/**
|
|
10
5
|
*
|
|
11
6
|
* @param html Original HTML
|
|
@@ -13,14 +8,13 @@ export default class FrozenPatty {
|
|
|
13
8
|
*/
|
|
14
9
|
constructor(html: string, options?: FrozenPattyOptions);
|
|
15
10
|
merge(data: FrozenPattyData): this;
|
|
16
|
-
toJSON(filtering?: boolean): FrozenPattyData;
|
|
17
|
-
toHTML(): string;
|
|
18
11
|
toDOM(): Element;
|
|
12
|
+
toHTML(): string;
|
|
13
|
+
toJSON(filtering?: boolean): FrozenPattyData;
|
|
19
14
|
}
|
|
20
15
|
export interface FrozenPattyOptions {
|
|
21
16
|
/**
|
|
22
17
|
* **Data attribute** name for specifying the node that FrozenPatty treats as a _field_
|
|
23
|
-
*
|
|
24
18
|
* @default `"field"`
|
|
25
19
|
*/
|
|
26
20
|
attr?: string;
|
|
@@ -33,7 +27,6 @@ export interface FrozenPattyOptions {
|
|
|
33
27
|
* - `"1"` => `1`
|
|
34
28
|
* - `"1.0"` => `1`
|
|
35
29
|
* - `"0.1"` => `0.1`
|
|
36
|
-
*
|
|
37
30
|
* @default `false`
|
|
38
31
|
*/
|
|
39
32
|
typeConvert?: boolean;
|
|
@@ -42,10 +35,3 @@ export interface FrozenPattyOptions {
|
|
|
42
35
|
*/
|
|
43
36
|
valueFilter?: Filter;
|
|
44
37
|
}
|
|
45
|
-
export interface FrozenPattyData {
|
|
46
|
-
[filed: string]: PrimitiveData;
|
|
47
|
-
}
|
|
48
|
-
export declare type PrimitiveDatum = string | number | boolean | null | undefined;
|
|
49
|
-
export declare type PrimitiveData = PrimitiveDatum | PrimitiveDatum[];
|
|
50
|
-
export declare type Filter = <T>(value: T) => T;
|
|
51
|
-
//# sourceMappingURL=frozen-patty.d.ts.map
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import get from './get.js';
|
|
2
|
+
import set from './set.js';
|
|
3
|
+
export default class FrozenPatty {
|
|
4
|
+
#attr = 'field';
|
|
5
|
+
#dom;
|
|
6
|
+
/**
|
|
7
|
+
* Value filter
|
|
8
|
+
*/
|
|
9
|
+
#filter;
|
|
10
|
+
#typeConvert = false;
|
|
11
|
+
/**
|
|
12
|
+
*
|
|
13
|
+
* @param html Original HTML
|
|
14
|
+
* @param options Options
|
|
15
|
+
*/
|
|
16
|
+
constructor(html, options) {
|
|
17
|
+
this.#dom = document.createElement('fp-placeholer');
|
|
18
|
+
this.#dom.innerHTML = html;
|
|
19
|
+
if (options) {
|
|
20
|
+
if (options.attr) {
|
|
21
|
+
this.#attr = options.attr;
|
|
22
|
+
}
|
|
23
|
+
this.#typeConvert = !!options.typeConvert;
|
|
24
|
+
this.#filter = options.valueFilter;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
merge(data) {
|
|
28
|
+
const currentData = this.toJSON(false);
|
|
29
|
+
const newData = Object.assign(currentData, data);
|
|
30
|
+
this.#dom = set(this.#dom, newData, this.#attr, this.#filter);
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
toDOM() {
|
|
34
|
+
return this.#dom;
|
|
35
|
+
}
|
|
36
|
+
toHTML() {
|
|
37
|
+
return this.#dom.innerHTML;
|
|
38
|
+
}
|
|
39
|
+
toJSON(filtering = true) {
|
|
40
|
+
const filter = filtering ? this.#filter : undefined;
|
|
41
|
+
return get(this.#dom, this.#attr, this.#typeConvert, filter);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Filter, PrimitiveDatum } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Get value from an element
|
|
4
|
+
* @param el A target element
|
|
5
|
+
* @param name A label name
|
|
6
|
+
* @param typeConvert Auto covert type of value
|
|
7
|
+
* @param attr Data attribute name for specifying the node that FrozenPatty treats as a field
|
|
8
|
+
* @param filter
|
|
9
|
+
*/
|
|
10
|
+
export declare function getValue(el: Element, name: string, typeConvert?: boolean, attr?: string, filter?: Filter): PrimitiveDatum | PrimitiveDatum[];
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { getValues } from './get-values.js';
|
|
2
|
+
import { arrayToHash } from './utils.js';
|
|
3
|
+
/**
|
|
4
|
+
* Get value from an element
|
|
5
|
+
* @param el A target element
|
|
6
|
+
* @param name A label name
|
|
7
|
+
* @param typeConvert Auto covert type of value
|
|
8
|
+
* @param attr Data attribute name for specifying the node that FrozenPatty treats as a field
|
|
9
|
+
* @param filter
|
|
10
|
+
*/
|
|
11
|
+
export function getValue(el, name, typeConvert = false, attr = 'field', filter) {
|
|
12
|
+
const value = getValues(el, typeConvert, attr, filter);
|
|
13
|
+
const data = arrayToHash(value);
|
|
14
|
+
return data[name];
|
|
15
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Filter, PrimitiveDatum } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Get value from an element
|
|
4
|
+
* @param el A target element
|
|
5
|
+
* @param name A label name
|
|
6
|
+
* @param typeConvert Auto covert type of value
|
|
7
|
+
* @param attr Data attribute name for specifying the node that FrozenPatty treats as a field
|
|
8
|
+
* @param filter
|
|
9
|
+
*/
|
|
10
|
+
export declare function getValues(el: Element, typeConvert?: boolean, attr?: string, filter?: Filter): [string | number, PrimitiveDatum, boolean][];
|
|
@@ -1,43 +1,41 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
1
|
/**
|
|
4
2
|
* Get value from an element
|
|
5
|
-
*
|
|
6
|
-
* @param
|
|
7
|
-
* @param attr Data attribute name for specifying the node that FrozenPatty treats as a field
|
|
3
|
+
* @param el A target element
|
|
4
|
+
* @param name A label name
|
|
8
5
|
* @param typeConvert Auto covert type of value
|
|
6
|
+
* @param attr Data attribute name for specifying the node that FrozenPatty treats as a field
|
|
7
|
+
* @param filter
|
|
9
8
|
*/
|
|
10
|
-
function
|
|
9
|
+
export function getValues(el, typeConvert = false, attr = 'field', filter) {
|
|
11
10
|
/**
|
|
12
11
|
* [key, value, forceArray]
|
|
13
12
|
*/
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
const result = [];
|
|
14
|
+
const rawValue = el.getAttribute(`data-${attr}`);
|
|
15
|
+
const listRoot = el.closest(`[data-${attr}-list]`);
|
|
16
|
+
const forceArray = !!listRoot;
|
|
18
17
|
if (rawValue == null) {
|
|
19
|
-
throw new Error(
|
|
18
|
+
throw new Error(`data-${attr} attriblute is empty.`);
|
|
20
19
|
}
|
|
21
|
-
|
|
20
|
+
const fieldList = `${rawValue}`.split(/\s*,\s*/);
|
|
22
21
|
// console.log({fieldList, el: el.innerHTML});
|
|
23
|
-
for (
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
var value = void 0;
|
|
22
|
+
for (let fieldName of fieldList) {
|
|
23
|
+
let splitKey;
|
|
24
|
+
let keyAttr = '';
|
|
25
|
+
let value;
|
|
28
26
|
fieldName = fieldName.trim();
|
|
29
|
-
if (/^[
|
|
27
|
+
if (/^[_a-z-][\w-]*:[_a-z-][\w-]*(?:\([a-z-]+\))?/i.test(fieldName)) {
|
|
30
28
|
splitKey = fieldName.split(':');
|
|
31
|
-
fieldName = splitKey[0]
|
|
32
|
-
keyAttr = splitKey[1]
|
|
29
|
+
fieldName = splitKey[0]?.trim() ?? '';
|
|
30
|
+
keyAttr = splitKey[1]?.trim() ?? '';
|
|
33
31
|
}
|
|
34
32
|
// console.log({fieldName, keyAttr, el: el.innerHTML});
|
|
35
33
|
if (keyAttr === 'text') {
|
|
36
34
|
value = el.innerHTML;
|
|
37
35
|
}
|
|
38
36
|
else if (/^style\([a-z-]+\)$/i.test(keyAttr)) {
|
|
39
|
-
|
|
40
|
-
|
|
37
|
+
const css = keyAttr.replace(/^style\(([a-z-]+)\)$/i, '$1');
|
|
38
|
+
let style;
|
|
41
39
|
if (el instanceof HTMLElement) {
|
|
42
40
|
style = el.style;
|
|
43
41
|
}
|
|
@@ -56,7 +54,7 @@ function default_1(el, attr, typeConvert, filter) {
|
|
|
56
54
|
if (el instanceof HTMLInputElement ||
|
|
57
55
|
el instanceof HTMLSelectElement ||
|
|
58
56
|
el instanceof HTMLTextAreaElement) {
|
|
59
|
-
|
|
57
|
+
const val = el.value;
|
|
60
58
|
if (Array.isArray(val)) {
|
|
61
59
|
value = cast(val[0]);
|
|
62
60
|
}
|
|
@@ -77,7 +75,12 @@ function default_1(el, attr, typeConvert, filter) {
|
|
|
77
75
|
// console.log({result});
|
|
78
76
|
return result;
|
|
79
77
|
}
|
|
80
|
-
|
|
78
|
+
/**
|
|
79
|
+
*
|
|
80
|
+
* @param el
|
|
81
|
+
* @param keyAttr
|
|
82
|
+
* @param typeConvert
|
|
83
|
+
*/
|
|
81
84
|
function getAttribute(el, keyAttr, typeConvert) {
|
|
82
85
|
switch (keyAttr) {
|
|
83
86
|
case 'contenteditable': {
|
|
@@ -85,7 +88,7 @@ function getAttribute(el, keyAttr, typeConvert) {
|
|
|
85
88
|
return el.contentEditable === '' || el.contentEditable === 'true';
|
|
86
89
|
}
|
|
87
90
|
else {
|
|
88
|
-
return
|
|
91
|
+
return el.getAttribute(keyAttr) === '' || el.getAttribute(keyAttr) === 'true';
|
|
89
92
|
}
|
|
90
93
|
}
|
|
91
94
|
case 'checked': {
|
|
@@ -104,8 +107,8 @@ function getAttribute(el, keyAttr, typeConvert) {
|
|
|
104
107
|
return el.getAttribute(keyAttr) || ''; // return plain string
|
|
105
108
|
}
|
|
106
109
|
default: {
|
|
107
|
-
if (
|
|
108
|
-
|
|
110
|
+
if (keyAttr.startsWith('data-')) {
|
|
111
|
+
const value = el.getAttribute(keyAttr) || '';
|
|
109
112
|
if (typeConvert) {
|
|
110
113
|
return cast(value);
|
|
111
114
|
}
|
|
@@ -115,28 +118,33 @@ function getAttribute(el, keyAttr, typeConvert) {
|
|
|
115
118
|
}
|
|
116
119
|
}
|
|
117
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
*
|
|
123
|
+
* @param value
|
|
124
|
+
*/
|
|
118
125
|
function cast(value) {
|
|
119
126
|
if (value == null || value === '') {
|
|
120
127
|
return '';
|
|
121
128
|
}
|
|
122
129
|
switch (value) {
|
|
123
|
-
case 'true':
|
|
130
|
+
case 'true': {
|
|
124
131
|
return true;
|
|
125
|
-
|
|
132
|
+
}
|
|
133
|
+
case 'false': {
|
|
126
134
|
return false;
|
|
135
|
+
}
|
|
127
136
|
}
|
|
128
|
-
|
|
129
|
-
if (isFinite(numeric)) {
|
|
137
|
+
const numeric = Number(value);
|
|
138
|
+
if (Number.isFinite(numeric)) {
|
|
130
139
|
return numeric;
|
|
131
140
|
}
|
|
132
141
|
return value;
|
|
133
142
|
}
|
|
134
143
|
/**
|
|
135
144
|
* Get path from value of "background-image"
|
|
136
|
-
*
|
|
145
|
+
* @param value
|
|
137
146
|
*/
|
|
138
147
|
function getBackgroundImagePath(value) {
|
|
139
|
-
|
|
148
|
+
const origin = `${location.protocol}//${location.hostname}${location.port ? `:${location.port}` : ''}`;
|
|
140
149
|
return decodeURI(value.replace(/^url\(["']?([^"']+)["']?\)$/i, '$1').replace(origin, ''));
|
|
141
150
|
}
|
|
142
|
-
//# sourceMappingURL=getValue.js.map
|
package/dist/get.d.ts
ADDED
package/dist/get.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { getValues } from './get-values.js';
|
|
2
|
+
import { arrayToHash } from './utils.js';
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @param el
|
|
6
|
+
* @param attr
|
|
7
|
+
* @param typeConvert
|
|
8
|
+
* @param filter
|
|
9
|
+
*/
|
|
10
|
+
export default function (el, attr, typeConvert, filter) {
|
|
11
|
+
// eslint-disable-next-line unicorn/prefer-spread
|
|
12
|
+
const filedElements = Array.from(el.querySelectorAll(`[data-${attr}]`));
|
|
13
|
+
let values = [];
|
|
14
|
+
for (const _el of filedElements) {
|
|
15
|
+
values = [...values, ...getValues(_el, typeConvert, attr, filter)];
|
|
16
|
+
}
|
|
17
|
+
const result = arrayToHash(values);
|
|
18
|
+
return result;
|
|
19
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { FrozenPattyOptions } from './frozen-patty.js';
|
|
2
|
+
import FrozenPatty from './frozen-patty.js';
|
|
3
|
+
/**
|
|
4
|
+
* Pure HTML to JSON converter that not use template engine.
|
|
5
|
+
* @param html Original HTML
|
|
6
|
+
* @param options Options
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
*
|
|
10
|
+
* @param html
|
|
11
|
+
* @param options
|
|
12
|
+
*/
|
|
13
|
+
export default function frozenPatty(html: string, options?: FrozenPattyOptions): FrozenPatty;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import FrozenPatty from './frozen-patty.js';
|
|
2
|
+
/**
|
|
3
|
+
* Pure HTML to JSON converter that not use template engine.
|
|
4
|
+
* @param html Original HTML
|
|
5
|
+
* @param options Options
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
*
|
|
9
|
+
* @param html
|
|
10
|
+
* @param options
|
|
11
|
+
*/
|
|
12
|
+
export default function frozenPatty(html, options) {
|
|
13
|
+
return new FrozenPatty(html, options);
|
|
14
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Filter, PrimitiveDatum } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Set value to an element
|
|
4
|
+
*
|
|
5
|
+
* ```html
|
|
6
|
+
* <div [target-attribute] data-[attr]="[name]:[target-attribute]"></div>
|
|
7
|
+
* ```
|
|
8
|
+
* @param el A target element
|
|
9
|
+
* @param name A label name
|
|
10
|
+
* @param datum A datum of value
|
|
11
|
+
* @param attr Data attribute name for specifying the node that FrozenPatty treats as a field
|
|
12
|
+
* @param filter
|
|
13
|
+
*/
|
|
14
|
+
export declare function setValue(el: Element, name: string, datum: PrimitiveDatum, attr?: string, filter?: Filter): void;
|