Color with css,javascript in html

hello to all,
how to colorize css and javascript in html ?
I can’t do it
example =

import wx
import wx.stc as stc
class htmlSTC(stc.StyledTextCtrl):
	def __init__(self, parent):
		stc.StyledTextCtrl.__init__(self, parent)
		self.SetLexer(stc.STC_LEX_HTML)
		self.StyleSetSpec(stc.STC_P_STRING,"back:#000000,fore:#888800")
		self.StyleSetSpec(stc.STC_HJA_KEYWORD,"back:#000000,fore:#ff0000")
		self.StyleSetSpec(stc.STC_CSS_CLASS,"back:#ffff00,fore:#ff00ff")
class frame(wx.Frame):
	def __init__(self):
		wx.Frame.__init__(self, None,title='HTML View',size=(800,400))
		self.view=htmlSTC(self)
		txt="""<!DOCTYPE html>
<html lang='fr'>
	<head>
		<meta charset='utf-8'>
		<title>titre de la page</title>
		<style>
			body {
				background-color:#0a000a;
			}
		</style>
		<script>
			function pageChargee() {
				alert('page chargée');
			}
			if ( window.attachEvent )
	      			window.attachEvent('onload',pageChargee);
			else	window.addEventListener('load',pageChargee,false);
		</script>
	</head>
	<body>
		<div>exemple</div>
	</body>
</html>"""
		self.view.SetText(txt)
		sizer=wx.BoxSizer(wx.VERTICAL)
		sizer.Add(self.view,1,wx.EXPAND)
		self.SetSizer(sizer)
		self.Show()
app=wx.App(False)
frm=frame()
app.MainLoop()

it works in html not in css and javascript included
thank you in advance
( sorry, French translation with deepl.com !! )

no one has the slightest idea, the slightest lead ?

Hi bul,

According to this document: wxStyledTextCtrl - Lexing

Some lexers have embedded-language support, controlled by the keywordSet parameter. For example, HTML can have embedded languages like VBScript, Javascript, and a few others.Some lexers have embedded-language support, controlled by the keywordSet parameter. For example, HTML can have embedded languages like VBScript, Javascript, and a few others.

So, if you want to colorize JavaScript embedded in HTML, you should write like this:

        self.SetKeyWords(1, "if else function alert")
        self.StyleSetSpec(stc.STC_HJ_KEYWORD,"back:#000000,fore:#ff0000")

where SetKeyWords argument keywordSet=1 is for JavaScript.

Some more points.

STC_P_STRING is for Python lexer. Use STC_H_SINGLESTRING or STC_H_DOUBLESTRING for HTML lexer instead.
I don’t think HTML lexer can colorize CSS correctly because the flag numbers STC_H_xxx(0 -- 31) and STC_CSS_xxx(0 -- 23) are duplicated.

1 Like

thank you very much
it works perfectly
in fact, it’s quite simple to do, but not to find the documentation
I still have to refine it