@node-projects/web-component-designer 0.0.173 → 0.0.175

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.
@@ -2,6 +2,8 @@
2
2
  const units = ['px', 'cm', 'mm', 'q', 'in', 'pc', 'pt', 'rem', 'em', 'vw', 'vh', 'vmin', 'vmax', 'lh', 'rlh', '%', 'ms', 's', 'deg', 'rad', 'grad', 'turn', 'cqw', 'cqh', 'cqi', 'cqb', 'cqmin', 'cqmax', 'fr'];
3
3
  const pattern = new RegExp(`^([\-\+]?(?:\\d+(?:\\.\\d+)?))(${units.join('|')})$`, 'i');
4
4
  export function convertCssUnitToPixel(cssValue, target, percentTarget) {
5
+ if (!cssValue)
6
+ return null;
5
7
  const supportedUnits = {
6
8
  // Absolute sizes
7
9
  'px': value => value,
@@ -55,9 +57,13 @@ export function convertCssUnitToPixel(cssValue, target, percentTarget) {
55
57
  }
56
58
  export function getCssUnit(cssValue) {
57
59
  const matches = cssValue.trim().match(pattern);
58
- return matches[2].toLowerCase();
60
+ if (matches)
61
+ return matches[2].toLowerCase();
62
+ return null;
59
63
  }
60
64
  export function convertCssUnit(cssValue, target, percentTarget, unit) {
65
+ if (!cssValue)
66
+ return null;
61
67
  const supportedUnits = {
62
68
  // Absolute sizes
63
69
  'px': value => value,
@@ -20,9 +20,16 @@ export class DesignItem {
20
20
  appliedDesignerExtensions = new Map();
21
21
  shouldAppliedDesignerExtensions = new Map();
22
22
  async clone() {
23
- const html = DomConverter.ConvertToString([this], null, false);
24
- const parsed = await this.serviceContainer.htmlParserService.parse(html, this.serviceContainer, this.instanceServiceContainer);
25
- return parsed[0];
23
+ try {
24
+ const html = DomConverter.ConvertToString([this], null, false);
25
+ const parsed = await this.serviceContainer.htmlParserService.parse(html, this.serviceContainer, this.instanceServiceContainer);
26
+ return parsed[0];
27
+ }
28
+ catch (err) {
29
+ //TODO: clone service for design item, maybe refactor copy&paste to use this also...
30
+ console.warn("could not clone design item.", this);
31
+ }
32
+ return null;
26
33
  }
27
34
  replaceNode(newNode) {
28
35
  DesignItem._designItemMap.delete(this.node);
@@ -92,8 +92,8 @@ export class CssTreeStylesheetService {
92
92
  let sourceNode = this._stylesheets.get(declaration.parent.stylesheetName);
93
93
  declaration.ast.value = window.csstree.toPlainObject(window.csstree.parse(declaration.name + ": " + value + (important ? " !important" : ""), { context: 'declaration', parseValue: false })).value;
94
94
  sourceNode.stylesheet.content = window.csstree.generate(window.csstree.fromPlainObject(sourceNode.ast));
95
- // After generating the stylesheet, the AST has to be transformed back into a plain object
96
- sourceNode.ast = window.csstree.toPlainObject(sourceNode.ast);
95
+ // After generating the stylesheet, parse again (so line numbers are correct)
96
+ sourceNode.ast = window.csstree.toPlainObject((window.csstree.parse(sourceNode.stylesheet.content, { positions: true, parseValue: false })));
97
97
  this.stylesheetChanged.emit({ stylesheet: sourceNode.stylesheet });
98
98
  return true;
99
99
  }
@@ -101,8 +101,8 @@ export class CssTreeStylesheetService {
101
101
  let sourceNode = this._stylesheets.get(rule.stylesheetName);
102
102
  rule.ast.block.children.push(window.csstree.toPlainObject(window.csstree.parse(declaration.name + ": " + declaration.value + (declaration.important ? " !important" : ""), { context: 'declaration', parseValue: false })));
103
103
  sourceNode.stylesheet.content = window.csstree.generate(window.csstree.fromPlainObject(sourceNode.ast));
104
- // After generating the stylesheet, the AST has to be transformed back into a plain object
105
- sourceNode.ast = window.csstree.toPlainObject(sourceNode.ast);
104
+ // After generating the stylesheet, parse again (so line numbers are correct)
105
+ sourceNode.ast = window.csstree.toPlainObject((window.csstree.parse(sourceNode.stylesheet.content, { positions: true, parseValue: false })));
106
106
  this.stylesheetChanged.emit({ stylesheet: sourceNode.stylesheet });
107
107
  return true;
108
108
  }
@@ -112,7 +112,8 @@ export class CssTreeStylesheetService {
112
112
  return false;
113
113
  rule.ast.block.children.splice(index, 1);
114
114
  this._stylesheets.get(rule.stylesheetName).stylesheet.content = window.csstree.generate(window.csstree.fromPlainObject(this._stylesheets.get(rule.stylesheetName).ast));
115
- this._stylesheets.get(rule.stylesheetName).ast = window.csstree.toPlainObject(window.csstree.fromPlainObject(this._stylesheets.get(rule.stylesheetName).ast));
115
+ // After generating the stylesheet, parse again (so line numbers are correct)
116
+ this._stylesheets.get(rule.stylesheetName).ast = window.csstree.toPlainObject((window.csstree.parse(this._stylesheets.get(rule.stylesheetName).stylesheet.content, { positions: true, parseValue: false })));
116
117
  this.stylesheetChanged.emit({ stylesheet: this._stylesheets.get(rule.stylesheetName).stylesheet });
117
118
  return true;
118
119
  }
@@ -129,19 +130,19 @@ export class CssTreeStylesheetService {
129
130
  let ruleCollection = this.rulesFromAST(child, stylesheet, source, designItem, previousCheck + currentCheck.type + " " + currentCheck.sel + "\n");
130
131
  if (ruleCollection) {
131
132
  for (const r of ruleCollection) {
132
- if (!this.elementMatchesASelector(designItem, this.buildSelectorString(r.ast.prelude)))
133
+ if (!this.elementMatchesASelector(designItem, this.buildSelectorString(r.ast.prelude, stylesheet)))
133
134
  continue;
134
135
  yield r;
135
136
  }
136
137
  }
137
138
  }
138
139
  if (child.type == "Rule") {
139
- let selectors = this.buildSelectorString(child.prelude);
140
+ let selectors = this.buildSelectorString(child.prelude, stylesheet);
140
141
  if (!this.elementMatchesASelector(designItem, selectors))
141
142
  continue;
142
143
  yield ({
143
144
  ast: child,
144
- selector: previousCheck + this.buildSelectorString(child.prelude).join(", "),
145
+ selector: previousCheck + this.buildSelectorString(child.prelude, stylesheet).join(", "),
145
146
  specificity: this.getSpecificity(child.prelude),
146
147
  stylesheetName: source,
147
148
  declarations: null,
@@ -154,22 +155,13 @@ export class CssTreeStylesheetService {
154
155
  astHasChildren(ast) {
155
156
  return ast != null && ast["children"] != null && ast["children"].length > 0;
156
157
  }
157
- buildSelectorString(selectorsAST) {
158
- let selectors = [];
159
- for (let selector of selectorsAST.children) {
160
- let sel = "";
161
- for (let fraction of selector.children) {
162
- if (fraction.type == "IdSelector")
163
- sel += "#" + fraction.name;
164
- else if (fraction.type == "ClassSelector")
165
- sel += "." + fraction.name;
166
- else
167
- sel += fraction.name;
168
- }
169
- selectors.push(sel);
158
+ buildSelectorString(selectorsAST, stylesheet) {
159
+ if (selectorsAST.type == 'SelectorList') {
160
+ return [...selectorsAST.children.map(x => this.buildSelectorString(x, stylesheet)[0])];
161
+ }
162
+ else {
163
+ return [stylesheet.substring(selectorsAST.loc.start.offset, selectorsAST.loc.end.offset)];
170
164
  }
171
- ;
172
- return selectors;
173
165
  }
174
166
  getSpecificity(selector) {
175
167
  const specificities = calculateSpecifity(selector);
@@ -200,7 +200,7 @@ export class PointerTool {
200
200
  this._clonedItems = [];
201
201
  for (let d of this._actionStartedDesignItems) {
202
202
  const clone = await d.clone();
203
- if (this._clonedItems)
203
+ if (this._clonedItems && clone)
204
204
  this._clonedItems.push(clone);
205
205
  }
206
206
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "description": "A UI designer for Polymer apps",
3
3
  "name": "@node-projects/web-component-designer",
4
- "version": "0.0.173",
4
+ "version": "0.0.175",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "author": "",