~openteachermaintainers/openteacher/3.x

« back to all changes in this revision

Viewing changes to modules/org/openteacher/logic/javaScript/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, 2017, 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
 
 
31
        def isSafeHtml(self, html):
 
32
                """Checks if it's safe to load an untrusted piece of ``html``
 
33
                   into a user's browser. Uses a tag whitelist.
 
34
 
 
35
                """
 
36
                return self._js.global_["ot-safe-html-checker"](html)
 
37
 
 
38
        def enable(self):
 
39
                modules = next(iter(self._mm.mods(type="modules")))
 
40
                self._js = modules.default(type="javaScriptEvaluator").loadModule('ot-safe-html-checker')
 
41
 
 
42
                self.active = True
 
43
 
 
44
        def disable(self):
 
45
                self.active = False
 
46
 
 
47
                del self._js
 
48
 
 
49
def init(moduleManager):
 
50
        return SafeHtmlCheckerModule(moduleManager)