Giter Site home page Giter Site logo

Attachment DOM is messy about trix HOT 8 CLOSED

basecamp avatar basecamp commented on May 17, 2024
Attachment DOM is messy

from trix.

Comments (8)

javan avatar javan commented on May 17, 2024

Can you explain what issue you're having other than considering the DOM structure messy?

Keep in mind that the <trix-editor>'s HTML isn't the same as its <input>s HTML. Trix cleans up much of the "mess" when it serializes.

For example, looking at this document:
screen shot 2015-11-01 at 9 07 36 pm
This is the <trix-editor>'s HTML (document.querySelector("trix-editor").innerHTML):

<div>
  <!--block-->Here's the logo:
  <br>
  <br>
  <span data-trix-cursor-target="true" data-trix-serialize="false"></span>
  <a href="https://d13txem1unpe48.cloudfront.net/tmp/2015-11-02/1446429236735-basecamp-logo.png" data-trix-attachment="{&quot;contentType&quot;:&quot;image/png&quot;,&quot;filename&quot;:&quot;basecamp-logo.png&quot;,&quot;filesize&quot;:2287,&quot;height&quot;:84,&quot;href&quot;:&quot;https://d13txem1unpe48.cloudfront.net/tmp/2015-11-02/1446429236735-basecamp-logo.png&quot;,&quot;url&quot;:&quot;https://d13txem1unpe48.cloudfront.net/tmp/2015-11-02/1446429236735-basecamp-logo.png&quot;,&quot;width&quot;:100}"
  data-trix-content-type="image/png" data-trix-id="1275" contenteditable="false">
    <figure class="attachment attachment-preview png">
      <img src="https://d13txem1unpe48.cloudfront.net/tmp/2015-11-02/1446429236735-basecamp-logo.png" data-trix-mutable="true" data-trix-store-key="imageElement/1275/1415/https://d13txem1unpe48.cloudfront.net/tmp/2015-11-02/1446429236735-basecamp-logo.png"
      width="100" height="84">
      <figcaption class="caption">basecamp-logo.png
        <span class="size">2.23 KB</span>
      </figcaption>
    </figure>
  </a>
  <span data-trix-cursor-target="true" data-trix-serialize="false"></span>
</div>

And this is its <input>'s HTML that you'd be saving to your database (document.querySelector("input[type=hidden]").value):

<div>Here's the logo:
  <br>
  <br>
  <a href="https://d13txem1unpe48.cloudfront.net/tmp/2015-11-02/1446429236735-basecamp-logo.png" data-trix-attachment="{&quot;contentType&quot;:&quot;image/png&quot;,&quot;filename&quot;:&quot;basecamp-logo.png&quot;,&quot;filesize&quot;:2287,&quot;height&quot;:84,&quot;href&quot;:&quot;https://d13txem1unpe48.cloudfront.net/tmp/2015-11-02/1446429236735-basecamp…tmp/2015-11-02/1446429236735-basecamp-logo.png&quot;,&quot;width&quot;:100}"
  data-trix-content-type="image/png">
    <figure class="attachment attachment-preview png">
      <img src="https://d13txem1unpe48.cloudfront.net/tmp/2015-11-02/1446429236735-basecamp-logo.png" width="100" height="84">
      <figcaption class="caption">basecamp-logo.png
        <span class="size">2.23 KB</span>
      </figcaption>
    </figure>
  </a>
</div>

The data-trix-* attributes aren't absolutely necessary to survive a round trip through your database unless you're adding custom attributes to the Trix.Attachment objects (which is very convenient) so you could sanitize them on your server without breaking anything. You can also configure the CSS class names if you'd like: https://github.com/basecamp/trix/blob/master/src/trix/config/css.coffee

but I would also store JSON serialization of the editor into the database. So when a user wants to edit content again, I load the JSON serialization of the editor.

That can be done by serializing the editor as JSON yourself and then loading it back in.

element = document.querySelector("trix-editor")

// Serialize
myDocument = JSON.stringify(element.editor)

// Load
element.editor.loadJSON(JSON.parse(myDocument))

from trix.

mitar avatar mitar commented on May 17, 2024

Trix cleans up much of the "mess" when it serializes.

I was looking at the input value when I was describing the mess. ;-) So:

<a href="https://d13txem1unpe48.cloudfront.net/tmp/2015-11-02/1446429236735-basecamp-logo.png" data-trix-attachment="{&quot;contentType&quot;:&quot;image/png&quot;,&quot;filename&quot;:&quot;basecamp-logo.png&quot;,&quot;filesize&quot;:2287,&quot;height&quot;:84,&quot;href&quot;:&quot;https://d13txem1unpe48.cloudfront.net/tmp/2015-11-02/1446429236735-basecamp…tmp/2015-11-02/1446429236735-basecamp-logo.png&quot;,&quot;width&quot;:100}" data-trix-content-type="image/png">

Pretty messy. ;-)

The issue is that my simple sanitizer (which tags and which attributes are allowed for each tag) would have issues sanitizing this. Because it is more like, you should clean stuff, except inside attachment element, where you should do something else, based on the structure.

It would be really great if attachment would be just <trix-attachment href="https://d13txem1unpe48.cloudfront.net/tmp/2015-11-02/1446429236735-basecamp-logo.png" src="https://d13txem1unpe48.cloudfront.net/tmp/2015-11-02/1446429236735-basecamp-logo.png" width="100" height="84" />. :-)

necessary to survive a round trip through your database unless you're adding custom attributes to the Trix.Attachment objects

Oh, I am doing that. I was just looking into how to make this work. At the moment I was setting a property to the event.attachment.attachment. You are saying that it would be better to use setAttributes for this?

That can be done by serializing the editor as JSON yourself and then loading it back in.

Yes, that was what I had in mind. Sorry if it was not clear. The question is if this is a good pattern to do? So to remove all custom tags for attachments and instead use serialized editor to pass extra data through editing sessions?

Is there an easy way to make Trix not put those data attributes into input's value in the first place?

from trix.

mitar avatar mitar commented on May 17, 2024

Also, is there way to disable attaching CSS classes altogether for attachments? I am considering styling them through structure-based selectors.

from trix.

javan avatar javan commented on May 17, 2024

Personally, I don't consider putting data in a data attribute that messy.

The issue is that my simple sanitizer (which tags and which attributes are allowed for each tag) would have issues sanitizing this.

The simplest solution here is to whitelist Trix's attributes in your sanitizer.

You are saying that it would be better to use setAttributes for this?

Yes, all of the attributes set that way will be serialized and round trip successfully.

Is there an easy way to make Trix not put those data attributes into input's value in the first place?

No, not currently. Trix's priority is round-tripping and producing consistent output.

Also, is there way to disable attaching CSS classes altogether for attachments? I am considering styling them through structure-based selectors.

No, but you're free to ignore them and specify your CSS as you please.

from trix.

mitar avatar mitar commented on May 17, 2024

The simplest solution here is to whitelist Trix's attributes in your sanitizer.

But then they can be added everywhere.

No, not currently. Trix's priority is round-tripping and producing consistent output.

Can I make maybe a pull request making this somehow optional? Or maybe I can just override this by monkey-patching stuff somehow.

No, but you're free to ignore them and specify your CSS as you please.

Can I make a pull request that if Trix.config.css values are set to null, those classes are not added?

from trix.

javan avatar javan commented on May 17, 2024

Another option might be to set your own text/html serializer. See https://github.com/basecamp/trix/blob/master/src/trix/config/serialization.coffee#L20-L44

Perhaps allowing adding additional serializers so you can add to Trix's rather than replacing it. Is that something you'd like to explore?

from trix.

mitar avatar mitar commented on May 17, 2024

Maybe. But I think it would be even easier if I could simply make attachments be rendered with <trix-attachment> and then I define my own custom attribute. Or I can instruct/configure Trix how to define it. So the default of <trix-attachment> could be what is currently used by Trix, with all serialization and everything.

from trix.

mitar avatar mitar commented on May 17, 2024

I think what I have the biggest issue with is that those data attributes are needed only when editing the content again. But they are provided when rendering the content every time, even when not editing. This is why it is messy. I can now store two copies, one when I want to edit, and when for displaying. But then why to store anything into data and why not just store the state of the editor as JSON into the database?

from trix.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.