xzzuf/language.py

143 lines
4.3 KiB
Python
Raw Normal View History

2023-10-27 10:13:05 +00:00
from language_utils import gen_boolean, gen_color, gen_date, gen_email, \
gen_javascript, gen_number, gen_style, gen_text, gen_url, gen_name, \
2023-11-27 14:20:19 +00:00
gen_target, gen_drop, gen_dir, gen_flag, gen_wtarget, gen_access_key, \
gen_duration
2023-10-27 10:13:05 +00:00
from utils import rndstr, choice
from mutations import Mutations
2023-10-24 15:49:07 +00:00
class HTMLTag:
def __init__(self, name, self_closing=False):
self.name = name
self.self_closing = self_closing
self.attributes = []
self.children = []
2023-10-27 10:13:05 +00:00
self.id = rndstr(8)
self.nameattr = rndstr(10)
self.rndchrpos = {}
self.mutator: Mutations = None
2023-11-27 14:20:19 +00:00
self.content = rndstr(10)
self.ids = []
self.names = []
2023-10-27 10:13:05 +00:00
def set_mutator(self, mutator):
self.mutator = mutator
def add_attribute(self, attr):
attr.root = self
self.attributes.append(attr)
2023-10-24 15:49:07 +00:00
2023-10-25 09:25:41 +00:00
def __str__(self) -> str:
2023-10-27 10:13:05 +00:00
# self mutate
if self.mutator:
# mutate attributes
for attr in self.attributes:
2023-11-27 14:20:19 +00:00
attr.__str__()
if attr.attr == AttrType.ATTR_TAG_SPECIFIC and attr.kind != HTMLTagAttributeType.TypeFlag:
2023-10-27 10:13:05 +00:00
cases = [
2023-11-27 14:20:19 +00:00
lambda x: self.mutator.mutate_attr(x),
lambda x: self.mutator.mutate_value(x),
2023-10-27 10:13:05 +00:00
]
choice(cases)(attr)
elif attr.attr == AttrType.ATTR_EVENT:
self.mutator.mutate_js(attr.value)
self.mutator.mutate_tag(tag=self)
2023-10-25 09:25:41 +00:00
tag = ""
if self.self_closing:
2023-10-27 10:13:05 +00:00
tag = f"<{self.name}"
tag += f" name=\"{self.nameattr}\" "
tag += f" id=\"{self.id}\" "
2023-10-25 09:25:41 +00:00
for attr in self.attributes:
tag += f"{attr} "
tag += "/>"
else:
tag = f"<{self.name}"
2023-10-27 10:13:05 +00:00
tag += f" name=\"{self.nameattr}\" "
tag += f" id=\"{self.id}\" "
2023-10-25 09:25:41 +00:00
for attr in self.attributes:
tag += f"{attr} "
tag += ">"
2023-11-27 14:20:19 +00:00
if len(self.children) == 0:
tag += f"{self.content}"
2023-10-25 09:25:41 +00:00
for child in self.children:
tag += f"{child}"
tag += f"</{self.name}>"
2023-10-27 10:13:05 +00:00
for pos in self.rndchrpos:
tag = tag[:pos] + self.rndchrpos[pos] + tag[pos:]
2023-10-25 09:25:41 +00:00
return tag
2023-10-24 15:49:07 +00:00
class HTMLTagAttributeType:
TypeText = 0
TypeBoolean = 1
TypeNumber = 2
TypeColor = 3
2023-10-27 10:13:05 +00:00
TypeJS = 4
2023-10-24 15:49:07 +00:00
TypeStlye = 5
TypeURL = 6
TypeEmail = 7
TypeDate = 8
2023-10-27 10:13:05 +00:00
TypeTarget = 9
TypeName = 10
TypeFlag = 11
TypeDrop = 12
TypeDir = 13
TypeWindowTarget = 14
TypeAccessKey = 15
2023-11-27 14:20:19 +00:00
TypeDuration = 16
2023-10-24 15:49:07 +00:00
Generators = {
HTMLTagAttributeType.TypeText: gen_text,
HTMLTagAttributeType.TypeBoolean: gen_boolean,
HTMLTagAttributeType.TypeNumber: gen_number,
HTMLTagAttributeType.TypeColor: gen_color,
2023-10-27 10:13:05 +00:00
HTMLTagAttributeType.TypeJS: gen_javascript,
2023-10-24 15:49:07 +00:00
HTMLTagAttributeType.TypeStlye: gen_style,
HTMLTagAttributeType.TypeURL: gen_url,
HTMLTagAttributeType.TypeEmail: gen_email,
HTMLTagAttributeType.TypeDate: gen_date,
2023-10-27 10:13:05 +00:00
HTMLTagAttributeType.TypeTarget: gen_target,
HTMLTagAttributeType.TypeName: gen_name,
HTMLTagAttributeType.TypeFlag: gen_flag,
HTMLTagAttributeType.TypeDrop: gen_drop,
HTMLTagAttributeType.TypeDir: gen_dir,
HTMLTagAttributeType.TypeWindowTarget: gen_wtarget,
HTMLTagAttributeType.TypeAccessKey: gen_access_key,
2023-11-27 14:20:19 +00:00
HTMLTagAttributeType.TypeDuration: gen_duration,
2023-10-24 15:49:07 +00:00
}
2023-10-27 10:13:05 +00:00
class AttrType:
ATTR_NONE = -1
ATTR_GLOBAL = 0
ATTR_EVENT = 1
ATTR_TAG_SPECIFIC = 2
2023-10-24 15:49:07 +00:00
class HTMLAttribute:
2023-10-27 10:13:05 +00:00
def __init__(self, name, value_type, glob=True, root=None):
self.root = root
2023-10-24 15:49:07 +00:00
self.name = name
self.kind = value_type
2023-10-27 10:13:05 +00:00
if self.kind == HTMLTagAttributeType.TypeJS:
self.attr = AttrType.ATTR_EVENT
else:
if glob:
self.attr = AttrType.ATTR_GLOBAL
else:
self.attr = AttrType.ATTR_TAG_SPECIFIC
2023-10-24 15:49:07 +00:00
def __str__(self) -> str:
2023-10-27 10:13:05 +00:00
if self.kind == HTMLTagAttributeType.TypeTarget or self.kind == HTMLTagAttributeType.TypeName:
self.value = Generators[self.kind](self.root)
else:
self.value = Generators[self.kind]()
2023-10-24 15:49:07 +00:00
if not self.value:
return self.name
else:
return f'{self.name}="{self.value}"'