8000 [Tip] Workaround for incorrectly removed attributes · Issue #219 · wilsonzlin/minify-html · GitHub
[go: up one dir, main page]

Skip to content

[Tip] Workaround for incorrectly removed attributes #219

Open

Description

@red-meadow

Hi!

I've found several issues here caused by incorrectly removed HTML attributes, and I've recently opened a new one. I'd like to share my workaround, hoping it might be useful to someone, until a better solution is available.

The basic idea is to replace incorrectly removed strings with a unique string sequence and restore them after minification. My example is in Python, but it can be easily adapted to other programming languages.

def unique_id():
    # uuid.uuid1() is a function from Python standard
    # library that generates unique ids
    return str(uuid.uuid1())

strings_to_preserve = [
    ['target="_self"', unique_id()],
    ['type="text"', unique_id()] 
]

# Replace strings with unique ids that will be preserved
for string, id in strings_to_preserve:
    html = html.replace(string, id)

minified = minify_html.minify(html)

# Replace preserved ids with original strings
for string, id in strings_to_preserve:
    minified = minified.replace(id, string)

I haven't tested this approach beyond attributes, but it should likely work for other cases as well.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0