~openteachermaintainers/openteacher/3.x

« back to all changes in this revision

Viewing changes to modules/org/openteacher/logic/safeHtmlChecker/safeHtmlChecker.py

  • Committer: Marten de Vries
  • Date: 2017-06-28 18:05:48 UTC
  • Revision ID: git-v1:b4c406307aa345c58b9904b76580f15c5bff2a4e
Move JS into npm modules

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/bin/env python3
2
 
# -*- coding: utf-8 -*-
3
 
 
4
 
#       Copyright 2013, Marten de Vries
5
 
#
6
 
#       This file is part of OpenTeacher.
7
 
#
8
 
#       OpenTeacher is free software: you can redistribute it and/or modify
9
 
#       it under the terms of the GNU General Public License as published by
10
 
#       the Free Software Foundation, either version 3 of the License, or
11
 
#       (at your option) any later version.
12
 
#
13
 
#       OpenTeacher is distributed in the hope that it will be useful,
14
 
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
#       GNU General Public License for more details.
17
 
#
18
 
#       You should have received a copy of the GNU General Public License
19
 
#       along with OpenTeacher.  If not, see <http://www.gnu.org/licenses/>.
20
 
 
21
 
class SafeHtmlCheckerModule:
22
 
        def __init__(self, moduleManager, *args, **kwargs):
23
 
                super().__init__(*args, **kwargs)
24
 
                self._mm = moduleManager
25
 
 
26
 
                self.type = "safeHtmlChecker"
27
 
                self.requires = (
28
 
                        self._mm.mods(type="javaScriptEvaluator"),
29
 
                )
30
 
                self.javaScriptImplementation = True
31
 
 
32
 
        def isSafeHtml(self, html):
33
 
                """Checks if it's safe to load an untrusted piece of ``html``
34
 
                   into a user's browser. Uses a tag whitelist.
35
 
 
36
 
                """
37
 
                return self._js.global_["isSafeHtml"](html)
38
 
 
39
 
        def enable(self):
40
 
                self._modules = next(iter(self._mm.mods(type="modules")))
41
 
                self._js = self._modules.default(type="javaScriptEvaluator").createEvaluator()
42
 
                with open(self._mm.resourcePath("safeHtmlChecker.js"), encoding='UTF-8') as f:
43
 
                        self.code = f.read()
44
 
                self._js.eval(self.code)
45
 
 
46
 
                self.active = True
47
 
 
48
 
        def disable(self):
49
 
                self.active = False
50
 
 
51
 
                del self._modules
52
 
                del self._js
53
 
                del self.code
54
 
 
55
 
def init(moduleManager):
56
 
        return SafeHtmlCheckerModule(moduleManager)