Skip to content

Commit a177a2c

Browse files
committed
Allow pre/code element styled to be overriden for prism/hljs (documented only in docstrings)
1 parent fca52ff commit a177a2c

File tree

5 files changed

+23
-17
lines changed

5 files changed

+23
-17
lines changed

code-input.css

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ code-input:not(.code-input_pre-element-styled) pre code, code-input.code-input_p
5555

5656
code-input:not(.code-input_pre-element-styled) pre, code-input.code-input_pre-element-styled pre code {
5757
/* Remove all margin and padding from others */
58-
margin: 0px!important;
59-
padding: 0px!important;
58+
margin: 0!important;
59+
padding: 0!important;
60+
border: 0!important;
61+
6062
width: 100%;
6163
height: 100%;
6264
}

code-input.d.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ export class Template {
293293
* Constructor to create a custom template instance. Pass this into `codeInput.registerTemplate` to use it.
294294
* I would strongly recommend using the built-in simpler template `codeInput.templates.prism` or `codeInput.templates.hljs`.
295295
* @param {(codeElement: HTMLElement) => void} highlight - a callback to highlight the code, that takes an HTML `<code>` element inside a `<pre>` element as a parameter
296-
* @param {boolean} preElementStyled - is the `<pre>` element CSS-styled as well as the `<code>` element? If true, `<pre>` element's scrolling is synchronised; if false, `<code>` element's scrolling is synchronised.
296+
* @param {boolean} preElementStyled - is the `<pre>` element CSS-styled (if so set to true), or the `<code>` element (false)?
297297
* @param {boolean} isCode - is this for writing code? If true, the code-input's lang HTML attribute can be used, and the `<code>` element will be given the class name 'language-[lang attribute's value]'.
298298
* @param {false} includeCodeInputInHighlightFunc - Setting this to true passes the `<code-input>` element as a second argument to the highlight function.
299299
* @param {boolean} autoDisableDuplicateSearching - Leaving this as true uses code-input's default fix for preventing duplicate results in Ctrl+F searching from the input and result elements, and setting this to false indicates your highlighting function implements its own fix. The default fix works by moving text content from elements to CSS `::before` pseudo-elements after highlighting.
@@ -307,7 +307,7 @@ export class Template {
307307
* Constructor to create a custom template instance. Pass this into `codeInput.registerTemplate` to use it.
308308
* I would strongly recommend using the built-in simpler template `codeInput.templates.prism` or `codeInput.templates.hljs`.
309309
* @param {(codeElement: HTMLElement, codeInput: CodeInput) => void} highlight - a callback to highlight the code, that takes an HTML `<code>` element inside a `<pre>` element as a parameter
310-
* @param {boolean} preElementStyled - is the `<pre>` element CSS-styled as well as the `<code>` element? If true, `<pre>` element's scrolling is synchronised; if false, `<code>` element's scrolling is synchronised.
310+
* @param {boolean} preElementStyled - is the `<pre>` element CSS-styled (if so set to true), or the `<code>` element (false)?
311311
* @param {boolean} isCode - is this for writing code? If true, the code-input's lang HTML attribute can be used, and the `<code>` element will be given the class name 'language-[lang attribute's value]'.
312312
* @param {true} includeCodeInputInHighlightFunc - Setting this to true passes the `<code-input>` element as a second argument to the highlight function.
313313
* @param {boolean} autoDisableDuplicateSearching - Leaving this as true uses code-input's default fix for preventing duplicate results in Ctrl+F searching from the input and result elements, and setting this to false indicates your highlighting function implements its own fix. The default fix works by moving text content from elements to CSS `::before` pseudo-elements after highlighting.
@@ -355,9 +355,10 @@ export namespace templates {
355355
* Constructor to create a template that uses Prism.js syntax highlighting (https://prismjs.com/)
356356
* @param {Object} prism Import Prism.js, then after that import pass the `Prism` object as this parameter.
357357
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.plugins`
358+
* @param {boolean} preElementStyled - Defaults to true, which should be right for most themes. If the styling is broken, change to false. (See `codeInput.Template` constructor's definition.)
358359
* @returns template object
359360
*/
360-
constructor(prism: Object, plugins?: Plugin[])
361+
constructor(prism: Object, plugins?: Plugin[], preElementStyled?: boolean)
361362
}
362363
// ESM-SUPPORT-END-TEMPLATE-prism Do not (re)move this - it's needed for ESM generation
363364
/**
@@ -373,9 +374,10 @@ export namespace templates {
373374
* Constructor to create a template that uses highlight.js syntax highlighting (https://highlightjs.org/)
374375
* @param {Object} hljs Import highlight.js, then after that import pass the `hljs` object as this parameter.
375376
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.plugins`
377+
* @param {boolean} preElementStyled - Defaults to false, which should be right for most themes. If the styling is broken, change to true. (See `codeInput.Template` constructor's definition.)
376378
* @returns template object
377379
*/
378-
constructor(hljs: Object, plugins?: Plugin[])
380+
constructor(hljs: Object, plugins?: Plugin[], preElementStyled?: boolean)
379381
}
380382
// ESM-SUPPORT-END-TEMPLATE-hljs Do not (re)move this - it's needed for ESM generation
381383
/**

code-input.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ var codeInput = {
162162
* Constructor to create a custom template instance. Pass this into `codeInput.registerTemplate` to use it.
163163
* I would strongly recommend using the built-in simpler template `codeInput.templates.prism` or `codeInput.templates.hljs`.
164164
* @param {(codeElement: HTMLCodeElement, codeInput?: codeInput.CodeInput) => void} highlight - a callback to highlight the code, that takes an HTML `<code>` element inside a `<pre>` element as a parameter
165-
* @param {boolean} preElementStyled - is the `<pre>` element CSS-styled as well as the `<code>` element? If true, `<pre>` element's scrolling is synchronised; if false, `<code>` element's scrolling is synchronised.
165+
* @param {boolean} preElementStyled - is the `<pre>` element CSS-styled (if so set to true), or the `<code>` element (false)?
166166
* @param {boolean} isCode - is this for writing code? If true, the code-input's lang HTML attribute can be used, and the `<code>` element will be given the class name 'language-[lang attribute's value]'.
167167
* @param {boolean} includeCodeInputInHighlightFunc - Setting this to true passes the `<code-input>` element as a second argument to the highlight function.
168168
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.Plugin`
@@ -1031,12 +1031,13 @@ var codeInput = {
10311031
* Constructor to create a template that uses Prism.js syntax highlighting (https://prismjs.com/)
10321032
* @param {Object} prism Import Prism.js, then after that import pass the `Prism` object as this parameter.
10331033
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.plugins`
1034+
* @param {boolean} preElementStyled - Defaults to true, which should be right for most themes. If the styling is broken, change to false. (See `codeInput.Template` constructor's definition.)
10341035
* @returns {codeInput.Template} template object
10351036
*/
1036-
constructor(prism, plugins = []) {
1037+
constructor(prism, plugins = [], preElementStyled = true) {
10371038
super(
10381039
prism.highlightElement, // highlight
1039-
true, // preElementStyled
1040+
preElementStyled, // preElementStyled
10401041
true, // isCode
10411042
false, // includeCodeInputInHighlightFunc
10421043
plugins
@@ -1055,15 +1056,16 @@ var codeInput = {
10551056
* Constructor to create a template that uses highlight.js syntax highlighting (https://highlightjs.org/)
10561057
* @param {Object} hljs Import highlight.js, then after that import pass the `hljs` object as this parameter.
10571058
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.plugins`
1059+
* @param {boolean} preElementStyled - Defaults to false, which should be right for most themes. If the styling is broken, change to true. (See `codeInput.Template` constructor's definition.)
10581060
* @returns {codeInput.Template} template object
10591061
*/
1060-
constructor(hljs, plugins = []) {
1062+
constructor(hljs, plugins = [], preElementStyled = false) {
10611063
super(
10621064
function(codeElement) {
10631065
codeElement.removeAttribute("data-highlighted");
10641066
hljs.highlightElement(codeElement);
10651067
}, // highlight
1066-
false, // preElementStyled
1068+
preElementStyled, // preElementStyled
10671069
true, // isCode
10681070
false, // includeCodeInputInHighlightFunc
10691071
plugins

plugins/prism-line-numbers.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* Files: prism-line-numbers.css
66
*/
77
/* Update padding to match line-numbers plugin */
8-
code-input.line-numbers textarea, code-input.line-numbers.code-input_pre-element-styled pre,
9-
.line-numbers code-input textarea, .line-numbers code-input.code-input_pre-element-styled pre {
8+
code-input.line-numbers textarea, code-input.line-numbers.code-input_pre-element-styled pre, code-input.line-numbers:not(.code-input_pre-element-styled) pre code,
9+
.line-numbers code-input textarea, .line-numbers code-input.code-input_pre-element-styled pre, .line-numbers code-input:not(.code-input_pre-element-styled) pre code {
1010
padding-left: max(3.8em, var(--padding, 16px))!important;
1111
}
1212

tests/i18n.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@
5858
<code-input dir="ltr" template="hljs+" language="markdown" placeholder="hljs ltr"></code-input>
5959

6060
<script>
61-
codeInput.registerTemplate("hljs", codeInput.templates.hljs(hljs, []));
62-
codeInput.registerTemplate("prism", codeInput.templates.prism(Prism, []));
61+
codeInput.registerTemplate("hljs", new codeInput.templates.Hljs(hljs, []));
62+
codeInput.registerTemplate("prism", new codeInput.templates.Prism(Prism, []));
6363

6464
// Attribution: Translated by Oliver Geer with some help from English Wiktionary
6565
let findAndReplaceTranslations = {
@@ -91,7 +91,7 @@
9191
tabForNavigation: "Tabulador y Mayús-Tabulador actualmente para la navegación por el teclado. Tecla para activar la indentación.",
9292
};
9393

94-
codeInput.registerTemplate("hljs+", codeInput.templates.hljs(hljs, [
94+
codeInput.registerTemplate("hljs+", new codeInput.templates.Hljs(hljs, [
9595
new codeInput.plugins.AutoCloseBrackets(),
9696
new codeInput.plugins.Autocomplete(function(popupElem, textarea, selectionEnd) {
9797
if(textarea.value.substring(selectionEnd-5, selectionEnd) == "popup") {
@@ -109,7 +109,7 @@
109109
new codeInput.plugins.SelectTokenCallbacks(codeInput.plugins.SelectTokenCallbacks.TokenSelectorCallbacks.createClassSynchronisation("in-selection"), false, true, true, true, true, false),
110110
//new codeInput.plugins.SpecialChars(true),
111111
]));
112-
codeInput.registerTemplate("prism+", codeInput.templates.prism(Prism, [
112+
codeInput.registerTemplate("prism+", new codeInput.templates.Prism(Prism, [
113113
new codeInput.plugins.AutoCloseBrackets(),
114114
new codeInput.plugins.Autocomplete(function(popupElem, textarea, selectionEnd) {
115115
if(textarea.value.substring(selectionEnd-5, selectionEnd) == "popup") {

0 commit comments

Comments
 (0)