Skip to content

Commit

Permalink
Fix compressor.
Browse files Browse the repository at this point in the history
  • Loading branch information
Natim committed Apr 10, 2020
1 parent 37a7c15 commit 407e6e7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 39 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ serve:
./testtinymce/manage.py migrate
./testtinymce/manage.py createsuperuser --username admin --email "[email protected]" --no-input 2>/dev/null || exit 0
./testtinymce/manage.py runserver

passwd:
./testtinymce/manage.py changepassword admin
7 changes: 6 additions & 1 deletion testtinymce/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@
MEDIA_ROOT = join(ROOT_PATH, "media")
MEDIA_URL = "/media/"
ADMIN_MEDIA_PREFIX = "/static/admin/"
STATIC_ROOT = join(ROOT_PATH, "static")

STATICFINDERS = [
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
]

STATIC_URL = "/static/"

MIDDLEWARE = [
Expand Down
36 changes: 17 additions & 19 deletions tinymce/compressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@

from datetime import datetime
import json
import logging
import os
import re

from django.conf import settings
from django.contrib.staticfiles import finders
from django.core.cache import cache
from django.http import HttpResponse
from django.template.loader import render_to_string
Expand All @@ -23,19 +24,16 @@

import tinymce.settings

logger = logging.getLogger(__name__)

safe_filename_re = re.compile("^[a-zA-Z][a-zA-Z0-9_/-]*$")


def get_file_contents(filename):
if (
"staticfiles" in settings.INSTALLED_APPS
or "django.contrib.staticfiles" in settings.INSTALLED_APPS
):
from django.contrib.staticfiles import finders
def get_file_contents(filename, source=False):

file_path = finders.find(os.path.join("tiny_mce", filename))
else:
file_path = os.path.join(tinymce.settings.JS_ROOT, filename)
file_path = finders.find(os.path.join("tinymce", f"{filename}.js"))
if not file_path:
file_path = finders.find(os.path.join("tinymce", f"{filename}.min.js"))

try:
f = open(file_path)
Expand All @@ -44,6 +42,7 @@ def get_file_contents(filename):
finally:
f.close()
except (IOError, TypeError):
logger.error(f"Couldn't load file: {file_path} for {filename}")
return ""


Expand All @@ -57,9 +56,10 @@ def gzip_compressor(request):
plugins = split_commas(request.GET.get("plugins", ""))
languages = split_commas(request.GET.get("languages", ""))
themes = split_commas(request.GET.get("themes", ""))
files = split_commas(request.GET.get("files", ""))
source = request.GET.get("src", "") == "true"
isJS = request.GET.get("js", "") == "true"
compress = request.GET.get("compress", "true") == "true"
suffix = request.GET.get("suffix", "") == "_src" and "_src" or ""
content = []

response = HttpResponse()
Expand Down Expand Up @@ -105,22 +105,22 @@ def gzip_compressor(request):
content.append(f"var tinyMCEPreInit={json.dumps(tinyMCEPreInit)};")

# Add core
files = ["tiny_mce"]
files = ["tinymce"]

# Add core languages
for lang in languages:
files.append(f"langs/{lang}")

# Add plugins
for plugin in plugins:
files.append(f"plugins/{plugin}/editor_plugin{suffix}")
files.append(f"plugins/{plugin}/plugin")

for lang in languages:
files.append(f"plugins/{plugin}/langs/{lang}")

# Add themes
for theme in themes:
files.append(f"themes/{theme}/editor_template{suffix}")
files.append(f"themes/{theme}/theme")

for lang in languages:
files.append(f"themes/{theme}/langs/{lang}")
Expand All @@ -129,13 +129,12 @@ def gzip_compressor(request):
# Check for unsafe characters
if not safe_filename_re.match(f):
continue
content.append(get_file_contents(f"{f}.js"))
content.append(get_file_contents(f, source=source))

# Restore loading functions
content.append(
f'tinymce.each("{",".join(files)}".split(","), function(f){{'
"tinymce.ScriptLoader.markDone(tinyMCE.baseURL+"
'"/"+f+".js");}});'
'tinymce.each("{}".split(",")'.format(",".join(files))
+ ', function(f){tinymce.ScriptLoader.markDone(tinyMCE.baseURL+"/"+f+".js");});'
)

unicode_content = []
Expand All @@ -162,7 +161,6 @@ def gzip_compressor(request):
timeout = 3600 * 24 * 10
patch_response_headers(response, timeout)
if not response.has_header("Last-Modified"):
# Last-Modified not set since Django 1.11
response["Last-Modified"] = http_date()
cache.set(
cacheKey, {"Last-Modified": response["Last-Modified"], "ETag": response.get("ETag", "")},
Expand Down
27 changes: 9 additions & 18 deletions tinymce/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,14 @@
settings, "TINYMCE_FILEBROWSER", "filebrowser" in settings.INSTALLED_APPS
)

if (
"staticfiles" in settings.INSTALLED_APPS
or "django.contrib.staticfiles" in settings.INSTALLED_APPS
):
JS_URL = getattr(
settings, "TINYMCE_JS_URL", os.path.join(settings.STATIC_URL, "tinymce/tinymce.min.js"),
)
try:
from django.contrib.staticfiles import finders

JS_ROOT = getattr(settings, "TINYMCE_JS_ROOT", finders.find("tinymce", all=False))
except AppRegistryNotReady:
JS_ROOT = getattr(
settings, "TINYMCE_JS_ROOT", os.path.join(settings.STATIC_ROOT, "tinymce")
)
else:
JS_URL = getattr(settings, "TINYMCE_JS_URL", f"{settings.MEDIA_URL}js/tinymce/tinymce.min.js")
JS_ROOT = getattr(settings, "TINYMCE_JS_ROOT", os.path.join(settings.MEDIA_ROOT, "js/tinymce"))
JS_URL = getattr(
settings, "TINYMCE_JS_URL", os.path.join(settings.STATIC_URL, "tinymce/tinymce.min.js"),
)
try:
from django.contrib.staticfiles import finders

JS_ROOT = getattr(settings, "TINYMCE_JS_ROOT", finders.find("tinymce", all=False))
except AppRegistryNotReady:
JS_ROOT = getattr(settings, "TINYMCE_JS_ROOT", os.path.join(settings.STATIC_ROOT, "tinymce"))

JS_BASE_URL = JS_URL[: JS_URL.rfind("/")]
6 changes: 5 additions & 1 deletion tinymce/templates/tinymce/tiny_mce_gzip.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,11 @@ var tinyMCE_GZ = {
se.text = co;

// Add it to evaluate it and remove it
(document.getElementsByTagName('head')[0] || document.documentElement).appendChild(se);
try {
(document.getElementsByTagName('head')[0] || document.documentElement).appendChild(se);
} catch (error) {
console.error(error, "Tried to eval:", se);
}
se.parentNode.removeChild(se);
}
};

0 comments on commit 407e6e7

Please sign in to comment.