|  | 
| 17 | 17 |  * - set pattern.jquery_plugin if you want it | 
| 18 | 18 |  */ | 
| 19 | 19 | import $ from "jquery"; | 
| 20 |  | -import _ from "underscore"; | 
|  | 20 | +import dom from "./dom"; | 
| 21 | 21 | import logging from "./logging"; | 
| 22 | 22 | import utils from "./utils"; | 
| 23 | 23 | 
 | 
| 24 |  | -// below here modules that are only loaded | 
| 25 |  | -import "./jquery-ext"; | 
| 26 |  | - | 
| 27 | 24 | const log = logging.getLogger("registry"); | 
| 28 | 25 | const disable_re = /patterns-disable=([^&]+)/g; | 
| 29 | 26 | const dont_catch_re = /patterns-dont-catch/g; | 
| @@ -117,44 +114,49 @@ const registry = { | 
| 117 | 114 |         // sure here, that it appears first. Not sure what would be | 
| 118 | 115 |         // the best solution. Perhaps some kind of way to register | 
| 119 | 116 |         // patterns "before" or "after" other patterns. | 
| 120 |  | -        if ( | 
| 121 |  | -            _.contains(patterns, "validation") && | 
| 122 |  | -            _.contains(patterns, "inject") | 
| 123 |  | -        ) { | 
|  | 117 | +        if (patterns.includes("validation") && patterns.includes("inject")) { | 
| 124 | 118 |             patterns.splice(patterns.indexOf("validation"), 1); | 
| 125 | 119 |             patterns.unshift("validation"); | 
| 126 | 120 |         } | 
| 127 | 121 |         return patterns; | 
| 128 | 122 |     }, | 
| 129 | 123 | 
 | 
| 130 | 124 |     scan(content, patterns, trigger) { | 
|  | 125 | +        if (typeof content === "string") { | 
|  | 126 | +            content = document.querySelector(content); | 
|  | 127 | +        } else if (content.jquery) { | 
|  | 128 | +            content = content[0]; | 
|  | 129 | +        } | 
|  | 130 | + | 
| 131 | 131 |         const selectors = []; | 
| 132 |  | -        let $match; | 
| 133 | 132 |         patterns = this.orderPatterns( | 
| 134 | 133 |             patterns || Object.keys(registry.patterns) | 
| 135 | 134 |         ); | 
| 136 |  | -        patterns.forEach(_.partial(this.transformPattern, _, content)); | 
| 137 |  | -        patterns = _.each(patterns, (name) => { | 
|  | 135 | +        for (const name of patterns) { | 
|  | 136 | +            this.transformPattern(name, content); | 
| 138 | 137 |             const pattern = registry.patterns[name]; | 
| 139 | 138 |             if (pattern.trigger) { | 
| 140 | 139 |                 selectors.unshift(pattern.trigger); | 
| 141 | 140 |             } | 
| 142 |  | -        }); | 
| 143 |  | -        $match = $(content).findInclusive(selectors.join(",")); // Find all DOM elements belonging to a pattern | 
| 144 |  | -        $match = $match.filter(function () { | 
|  | 141 | +        } | 
|  | 142 | + | 
|  | 143 | +        let matches = dom.querySelectorAllAndMe(content, selectors.join(",")); | 
|  | 144 | +        matches = matches.filter((el) => { | 
| 145 | 145 |             // Filter out code examples wrapped in <pre> elements. | 
| 146 |  | -            return $(this).parents("pre").length === 0; | 
|  | 146 | +            // Also filter special class ``.cant-touch-this`` | 
|  | 147 | +            return ( | 
|  | 148 | +                dom.find_parents(el, "pre").length === 0 && | 
|  | 149 | +                !el.matches(".cant-touch-this") | 
|  | 150 | +            ); | 
| 147 | 151 |         }); | 
| 148 |  | -        $match = $match.filter(":not(.cant-touch-this)"); | 
| 149 | 152 | 
 | 
| 150 | 153 |         // walk list backwards and initialize patterns inside-out. | 
| 151 |  | -        $match.toArray().reduceRight( | 
| 152 |  | -            function registryInitPattern(acc, el) { | 
| 153 |  | -                patterns.forEach(_.partial(this.initPattern, _, el, trigger)); | 
| 154 |  | -            }.bind(this), | 
| 155 |  | -            null | 
| 156 |  | -        ); | 
| 157 |  | -        $("body").addClass("patterns-loaded"); | 
|  | 154 | +        for (const el of matches.reverse()) { | 
|  | 155 | +            for (const name of patterns) { | 
|  | 156 | +                this.initPattern(name, el, trigger); | 
|  | 157 | +            } | 
|  | 158 | +        } | 
|  | 159 | +        document.body.classList.add("patterns-loaded"); | 
| 158 | 160 |     }, | 
| 159 | 161 | 
 | 
| 160 | 162 |     register(pattern, name) { | 
|  | 
0 commit comments