Skip to content
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

[Tip] Workaround for incorrectly removed attributes #219

Open
red-meadow opened this issue Feb 7, 2025 · 0 comments
Open

[Tip] Workaround for incorrectly removed attributes #219

red-meadow opened this issue Feb 7, 2025 · 0 comments

Comments

@red-meadow
Copy link

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.

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

No branches or pull requests

1 participant