Skip to content

Add parameter in getHtml function to remove css classes #200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
thomasmfinalcad opened this issue Mar 27, 2024 Discussed in #199 · 3 comments · Fixed by #218
Closed

Add parameter in getHtml function to remove css classes #200

thomasmfinalcad opened this issue Mar 27, 2024 Discussed in #199 · 3 comments · Fixed by #218

Comments

@thomasmfinalcad
Copy link

Discussed in #199

Originally posted by thomasmfinalcad March 27, 2024
Hello @stevengharris,
I think it could be a good id to add a parameter into getHtml function to remove extra elements in html and only return html balises with content.
Morehover, when I try to convert the result into NSAttributedString and break line is added.

Actual

<ul>
    <li>
        <p class="editor-paragraph" dir="ltr">NO <u>SYjjjjjjjj<i>​hhryhhghgggjf</i></u></p>
    </li>
</ul>

Expected

<ul>
    <li>
        <p>NO <u>SYjjjjjjjj<i>​hhryhhghgggjf</i></u></p>
    </li>
</ul>
@stevengharris
Copy link
Owner

Hmm, this shouldn't be happening. The default getHtml should be stripping all that stuff out, with options that let you keep it in if it's useful.

@stevengharris
Copy link
Owner

Oh, I see, no, by default getHtml strips out things like style elements, etc, but it leaves in the attributes for all HTML elements if they exist. For example, for an image it might look like: <img src="steve.png" alt="Local image" class="resize-image" tabindex="-1" width="80" height="80">, and for a table header spanning columns, it might look like: <th colspan="2">. So maybe what would be useful would be, by default, to strip all the attributes for what I refer to as "styles" (sorry for the contrary terminology wrt css!): P, H1, H2, H3, H4, H5, and H6. I can also tell you that if you have some attributes in those elements, it probably will be lost on copy/paste anyway. I'm already stripping them off of any HTML you paste-in, so this would be consistent.

So, to summarize what I would do here: Change the default getHtml behavior to strip all attributes from P and H1-H6.

By way of detail, this would be part of the "clean" option in MU.getHTML, which is true by default, so for debugging or other purposes, they could be left in place:

MU.getHTML = function(pretty="true", clean="true", divID) {
    const prettyHTML = pretty === "true";
    const cleanHTML = clean === "true";
    const div = (divID) ? document.getElementById(divID) : MU.editor;
    if (!div) {
        MUError.NoDiv.callback();
        return "";
    }
    let editor, text;
    if (cleanHTML) {
        const template = document.createElement('template');
        template.innerHTML = div.innerHTML;
        editor = template.content;
        _cleanUpDivsWithin(editor);
        _cleanUpSpansWithin(editor);
        _cleanUpEmptyTextNodes(editor);
    } else {
        editor = div;
    };
    if (prettyHTML) {
        text = _allPrettyHTML(editor);
    } else {
        text = MU.editor.innerHTML;
        //text = _isFragment(editor) ? _fragmentString(editor) : editor.innerHTML;
    };
    return text;
};

@stevengharris
Copy link
Owner

This issue (at least as I've interpreted it above to mean stripping attributes from P and H1-H6, should be fixed in #218.

@stevengharris stevengharris linked a pull request Feb 7, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants