Skip to content

Commit

Permalink
Use Jinja2 autoescaping
Browse files Browse the repository at this point in the history
  • Loading branch information
haxtibal committed Jul 20, 2024
1 parent 842d4e4 commit d1a7be8
Show file tree
Hide file tree
Showing 28 changed files with 90 additions and 137 deletions.
4 changes: 0 additions & 4 deletions strictdoc/backend/sdoc/models/free_text.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import html
from typing import Any, List, Optional

from strictdoc.backend.sdoc.models.anchor import Anchor
Expand Down Expand Up @@ -55,9 +54,6 @@ def get_parts_as_text(self) -> str:
raise NotImplementedError(part)
return text

def get_parts_as_text_escaped(self) -> str:
return html.escape(self.get_parts_as_text())


class FreeTextContainer(FreeText):
def __init__(self, parts: List[Any]) -> None:
Expand Down
11 changes: 0 additions & 11 deletions strictdoc/backend/sdoc/models/node.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# mypy: disable-error-code="union-attr"
import html
from collections import OrderedDict
from typing import Any, Generator, List, Optional, Tuple, Union

Expand Down Expand Up @@ -92,9 +91,6 @@ def get_text_value(self) -> str:
raise NotImplementedError(part)
return text

def get_text_value_escaped(self) -> str:
return html.escape(self.get_text_value())


@auto_described
class SDocNode(SDocObject):
Expand Down Expand Up @@ -456,13 +452,6 @@ def enumerate_all_fields(
meta_field_value = field.get_text_value()
yield field, field.field_name, meta_field_value

def enumerate_all_fields_escaped(
self,
) -> Generator[Tuple[SDocNodeField, str, str], None, None]:
for field in self.enumerate_fields():
meta_field_value = field.get_text_value_escaped()
yield field, field.field_name, meta_field_value

def enumerate_meta_fields(
self, skip_single_lines: bool = False, skip_multi_lines: bool = False
) -> Generator[Tuple[str, SDocNodeField], None, None]:
Expand Down
21 changes: 17 additions & 4 deletions strictdoc/core/document_iterator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# mypy: disable-error-code="arg-type,attr-defined,no-any-return,no-untyped-call,no-untyped-def,operator,type-arg"
from typing import Optional, Tuple
from typing import NewType, Optional, Tuple, Union

from strictdoc.backend.sdoc.models.document import SDocDocument
from strictdoc.backend.sdoc.models.document_from_file import DocumentFromFile
Expand All @@ -9,6 +9,19 @@
)
from strictdoc.backend.sdoc.models.section import FreeText, SDocSection

DocumentAny = NewType(
"DocumentAny",
Union[
DocumentFromFile,
FreeText,
SDocSection,
SDocCompositeNode,
SDocNode,
SDocDocument,
str,
],
)


class DocumentCachingIterator:
def __init__(self, document):
Expand Down Expand Up @@ -37,7 +50,7 @@ def all_content(
self,
print_fragments: bool = False,
print_fragments_from_files: bool = False,
):
) -> DocumentAny:
root_node = self.document

yield from self._all_content(
Expand All @@ -53,7 +66,7 @@ def all_content_from_node(
node,
print_fragments: bool = False,
print_fragments_from_files: bool = False,
):
) -> DocumentAny:
document = node if isinstance(node, SDocDocument) else node.document
yield from self._all_content(
node,
Expand All @@ -70,7 +83,7 @@ def _all_content(
print_fragments_from_files: bool = False,
level_stack: Optional[Tuple] = (),
custom_level: bool = False,
):
) -> DocumentAny:
def get_level_string_(node_) -> str:
if isinstance(node_, SDocNode) and node_.requirement_type == "TEXT":
return ""
Expand Down
8 changes: 4 additions & 4 deletions strictdoc/core/transforms/create_requirement.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ def perform(self):
) = RstToHtmlFragmentWriter(
path_to_output_dir=self.project_config.export_output_dir,
context_document=document,
).write_with_validation(field_.field_unescaped_value)
).write_with_validation(field_.field_value)
if parsed_html is None:
errors[field_.field_name].append(rst_error)
else:
try:
free_text_container: Optional[FreeTextContainer] = (
SDFreeTextReader.read(field_.field_unescaped_value)
if len(field_.field_unescaped_value) > 0
SDFreeTextReader.read(field_.field_value)
if len(field_.field_value) > 0
else None
)
map_form_to_requirement_fields[field_] = (
Expand Down Expand Up @@ -208,7 +208,7 @@ def perform(self):
requirement.set_field_value(
field_name=form_field_name,
form_field_index=form_field_index,
value=form_field.field_unescaped_value,
value=form_field.field_value,
)
continue

Expand Down
8 changes: 4 additions & 4 deletions strictdoc/core/transforms/update_requirement.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ def perform(self):
) = RstToHtmlFragmentWriter(
path_to_output_dir=self.project_config.export_output_dir,
context_document=self.context_document,
).write_with_validation(field_.field_unescaped_value)
).write_with_validation(field_.field_value)
if parsed_html is None:
form_object.add_error(field_.field_name, rst_error)
else:
try:
free_text_container: Optional[FreeTextContainer] = (
SDFreeTextReader.read(field_.field_unescaped_value)
if len(field_.field_unescaped_value) > 0
SDFreeTextReader.read(field_.field_value)
if len(field_.field_value) > 0
else None
)
map_form_to_requirement_fields[field_] = (
Expand Down Expand Up @@ -455,7 +455,7 @@ def populate_node_fields_from_form_object(
node.set_field_value(
field_name=form_field_name,
form_field_index=form_field_index,
value=form_field.field_unescaped_value,
value=form_field.field_value,
)
continue

Expand Down
81 changes: 24 additions & 57 deletions strictdoc/export/html/form_objects/requirement_form_object.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# mypy: disable-error-code="arg-type,attr-defined,no-redef,no-untyped-call,no-untyped-def,union-attr,type-arg"
import html
from collections import defaultdict
from enum import Enum
from typing import Dict, List, Optional, Set, Union
Expand Down Expand Up @@ -57,15 +56,12 @@ def __init__(
field_mid: str,
field_name: str,
field_type: RequirementFormFieldType,
field_unescaped_value: str,
field_escaped_value: str,
field_value: str,
):
assert isinstance(field_unescaped_value, str)
assert isinstance(field_escaped_value, str)
assert isinstance(field_value, str)
self.field_mid: str = field_mid
self.field_name: str = field_name
self.field_unescaped_value: str = field_unescaped_value
self.field_escaped_value: str = field_escaped_value
self.field_value: str = field_value
self.field_type = field_type

def is_singleline(self):
Expand All @@ -85,18 +81,12 @@ def create_from_grammar_field(
*,
grammar_field: GrammarElementField,
multiline: bool,
value_unescaped: str,
value_escaped: str,
value: str,
) -> "RequirementFormField":
assert isinstance(value_unescaped, str), (
grammar_field,
multiline,
value_unescaped,
)
assert isinstance(value_escaped, str), (
assert isinstance(value, str), (
grammar_field,
multiline,
value_escaped,
value,
)
if grammar_field.gef_type in (
RequirementFieldType.STRING,
Expand All @@ -111,8 +101,7 @@ def create_from_grammar_field(
if multiline
else RequirementFormFieldType.SINGLELINE
),
field_unescaped_value=value_unescaped,
field_escaped_value=value_escaped,
field_value=value,
)
raise NotImplementedError(grammar_field)

Expand All @@ -128,7 +117,6 @@ def create_existing_from_grammar_field(
RequirementFieldType.MULTIPLE_CHOICE,
):
field_value = requirement_field.get_text_value()
escaped_field_value = html.escape(field_value)
return RequirementFormField(
field_mid=MID.create(),
field_name=grammar_field.title,
Expand All @@ -137,8 +125,7 @@ def create_existing_from_grammar_field(
if multiline
else RequirementFormFieldType.SINGLELINE
),
field_unescaped_value=field_value,
field_escaped_value=escaped_field_value,
field_value=field_value,
)
raise NotImplementedError(grammar_field)

Expand All @@ -148,8 +135,7 @@ def create_mid_field(mid: MID) -> "RequirementFormField":
field_mid=MID.create(),
field_name="MID",
field_type=RequirementFormFieldType.SINGLELINE,
field_unescaped_value=mid,
field_escaped_value=html.escape(mid),
field_value=mid,
)


Expand Down Expand Up @@ -327,8 +313,7 @@ def create_from_request(
form_field = RequirementFormField.create_from_grammar_field(
grammar_field=field,
multiline=multiline,
value_unescaped=sanitized_field_value,
value_escaped=html.escape(sanitized_field_value),
value=sanitized_field_value,
)
form_fields.append(form_field)

Expand Down Expand Up @@ -381,21 +366,14 @@ def create_new(
RequirementFormField.create_from_grammar_field(
grammar_field=field,
multiline=field_idx >= content_field_idx,
value_unescaped="",
value_escaped="",
value="",
)
)
form_fields.append(form_field)
if form_field.field_name == "UID" and next_uid is not None:
form_field.field_unescaped_value = next_uid
form_field.field_escaped_value = next_uid
form_field.field_value = next_uid
elif form_field.field_name == "MID" and document.config.enable_mid:
form_field.field_unescaped_value = (
new_requirement_mid.get_string_value()
)
form_field.field_escaped_value = (
new_requirement_mid.get_string_value()
)
form_field.field_value = new_requirement_mid.get_string_value()

return RequirementFormObject(
is_new=True,
Expand Down Expand Up @@ -455,8 +433,7 @@ def create_from_requirement(
form_field = RequirementFormField.create_from_grammar_field(
grammar_field=field,
multiline=multiline,
value_unescaped="",
value_escaped="",
value="",
)
form_fields.append(form_field)

Expand Down Expand Up @@ -515,8 +492,7 @@ def clone_from_requirement(
for field_name, fields_ in form_object.fields.items():
if field_name == "UID":
field: RequirementFormField = fields_[0]
field.field_unescaped_value = clone_uid
field.field_escaped_value = clone_uid
field.field_value = clone_uid
form_object.requirement_mid = MID.create()

return form_object
Expand Down Expand Up @@ -613,7 +589,7 @@ def validate(
FIXME: MID uniqueness if a node is updated.
"""
if self.is_new and "MID" in self.fields:
new_node_mid = self.fields["MID"][0].field_unescaped_value
new_node_mid = self.fields["MID"][0].field_value
if len(new_node_mid) > 0:
existing_node_with_this_mid = (
traceability_index.get_node_by_mid_weak(MID(new_node_mid))
Expand All @@ -633,7 +609,7 @@ def validate(
"""
new_node_uid_or_none: Optional[str] = None
if "UID" in self.fields:
new_node_uid = self.fields["UID"][0].field_unescaped_value
new_node_uid = self.fields["UID"][0].field_value
if len(new_node_uid) > 0:
new_node_uid_or_none = new_node_uid

Expand Down Expand Up @@ -689,9 +665,7 @@ def validate(
"""
requirement_element = self.grammar.elements_by_type[self.element_type]
statement_field_name = requirement_element.content_field[0]
requirement_statement = self.fields[statement_field_name][
0
].field_unescaped_value
requirement_statement = self.fields[statement_field_name][0].field_value
if requirement_statement is None or len(requirement_statement) == 0:
self.add_error(
statement_field_name,
Expand All @@ -717,7 +691,7 @@ def validate(
and grammar_element_field_.required
):
for form_field_ in self.fields[grammar_element_field_.title]:
field_value = form_field_.field_unescaped_value
field_value = form_field_.field_value
if field_value is None or len(field_value) == 0:
self.add_error(
grammar_element_field_.title,
Expand All @@ -735,9 +709,7 @@ def validate(
self._validate_choice(grammar_element_field_)

requirement_uid: Optional[str] = (
self.fields["UID"][0].field_unescaped_value
if "UID" in self.fields
else None
self.fields["UID"][0].field_value if "UID" in self.fields else None
)
if len(self.reference_fields) > 0 and (
requirement_uid is None or len(requirement_uid) == 0
Expand Down Expand Up @@ -862,10 +834,7 @@ def child_lambda(requirement_id_) -> List[str]:

def _validate_choice(self, grammar_element_field: GrammarElementField):
field_0 = self.fields[grammar_element_field.title][0]
if (
len(field_0.field_unescaped_value) == 0
and not grammar_element_field.required
):
if len(field_0.field_value) == 0 and not grammar_element_field.required:
# The empty choice fields are allowed if the field is not REQUIRED.
return

Expand All @@ -881,8 +850,7 @@ def _validate_choice(self, grammar_element_field: GrammarElementField):
)
if (
grammar_element_field.gef_type == RequirementFieldType.SINGLE_CHOICE
and field_0.field_unescaped_value
not in choice_grammar_element_field.options
and field_0.field_value not in choice_grammar_element_field.options
):
self.add_error(
grammar_element_field.title,
Expand All @@ -896,14 +864,13 @@ def _validate_choice(self, grammar_element_field: GrammarElementField):
== RequirementFieldType.MULTIPLE_CHOICE
):
choices = [
choice.strip()
for choice in field_0.field_unescaped_value.split(",")
choice.strip() for choice in field_0.field_value.split(",")
]
if all(
choice in choice_grammar_element_field.options
for choice in choices
):
field_0.field_unescaped_value = ", ".join(choices)
field_0.field_value = ", ".join(choices)
else:
self.add_error(
grammar_element_field.title,
Expand Down
Loading

0 comments on commit d1a7be8

Please sign in to comment.