question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question/Bug] grapesjs throws a DOMException if you import HTML contents with invalid (numbered) attributes

See original GitHub issue

Hi @artf ,

We got a report that when you import some HTML code with incorrect attributes, such as

<td class="cell" 01234="0" >Hello world! </td>

grapes.js throws the following Uncaught DOMException: Failed to execute ‘setAttribute’ on ‘Element’: ‘01234’ is not a valid attribute name. at HTMLTableCellElement.<anonymous> (https://grapesjs.com/js/grapes.min.js?v0.14.61:3:3229) at x (https://grapesjs.com/js/grapes.min.js?v0.14.61:3:1245) at m.each (https://grapesjs.com/js/grapes.min.js?v0.14.61:3:4121) at m.attr (https://grapesjs.com/js/grapes.min.js?v0.14.61:3:3195) at m.attr (https://grapesjs.com/js/grapes.min.js?v0.14.61:3:3276) at i.updateAttributes (https://grapesjs.com/js/grapes.min.js?v0.14.61:2:60111) at i.renderAttributes (https://grapesjs.com/js/grapes.min.js?v0.14.61:2:60919) at i.render (https://grapesjs.com/js/grapes.min.js?v0.14.61:2:61020)

Numbered invalid attribute

Unless that’s an explicit choice from you, do you think you could do something to catch this exception on the parser, or at least give us a way to handle it on our own? Like some option to explicitely ignore such attributes?

Sure, the user should not import such content, but we can’t always ensure that they won’t do anything like this 😉

Thanks for your help.

-Maxime

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
arachnosoftcommented, Aug 14, 2019

Just to keep you informed, I applied the try/catch fix suggested by @ankx06 and @artf to our code, but in a generic way.

I wanted to ensure that the exception would be catched in all cases when setComponents() is being called. Either from our code, or, more importantly (and not covered by @ankx06 's example, as far as I know) from the Import Code window.

So, I used a JavaScript closure technique to wrap the original setComponents() function within a new one, in which I put the try/catch: https://stackoverflow.com/a/10427757

var myGrapesJsObject = grapesjs.init(...);
myGrapesJsObject.setComponents = (function (originalFct) {
		  return function (components) {
			try {
				originalFct(components);
			}
			catch (ex) {
                                window.alert('Parse error: ' + ex);
				}
		  }
		})(myGrapesJsObject.setComponents);

And it works perfectly. No more blocking DOMExceptions.

Thank you all for your suggestions!

1reaction
ankx06commented, Aug 7, 2019

@jmchaves this is not called from any event. This is the function to update component’s inner components/html.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Trait Manager - GrapesJS
Add Traits to Components · Built-in trait types. Text; Number; Checkbox; Select; Color; Button · Updating traits at run-time · Define new Trait ......
Read more >
Component API - GrapesJS
The Component object represents a single node of our template structure, so when you update its properties the changes are immediately ...
Read more >
Component Manager - GrapesJS
To understand what type should be assigned, for each parsed HTML Element, the editor iterates over all the defined components, called Component ...
Read more >
Components & JS - GrapesJS
Basic scripts; Important caveat; Passing properties to scripts; Dependencies ... If now you check the generated HTML code in the editor (via ...
Read more >
Use Custom CSS Parser - GrapesJS
On the other hand, if you import templates from already defined HTML/CSS or let the user embed custom codes (eg. using the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found