about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/css/cascade.nim5
-rw-r--r--src/html/dom.nim15
-rw-r--r--src/io/buffer.nim24
3 files changed, 31 insertions, 13 deletions
diff --git a/src/css/cascade.nim b/src/css/cascade.nim
index 0606a31c..73ec093b 100644
--- a/src/css/cascade.nim
+++ b/src/css/cascade.nim
@@ -126,7 +126,7 @@ proc applyRules(document: Document, ua, user: CSSStylesheet, cachedTree: StyledN
 
   if document.head != nil:
     for sheet in document.head.sheets:
-      author.add(sheet)
+      author.add(sheet.applyMediaQuery())
 
   var lenstack = newSeqOfCap[int](256)
   var styledStack: seq[(StyledNode, Node, PseudoElem, StyledNode)]
@@ -246,7 +246,8 @@ proc applyRules(document: Document, ua, user: CSSStylesheet, cachedTree: StyledN
       # stylesheets
       let sheets = elem.sheets()
       if sheets.len > 0:
-        author.add(sheets)
+	for sheet in sheets:
+          author.add(sheet.applyMediaQuery())
         lenstack.add(sheets.len)
         styledStack.add((nil, nil, PSEUDO_NONE, nil))
 
diff --git a/src/html/dom.nim b/src/html/dom.nim
index acfe2a22..51a4058b 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -164,6 +164,10 @@ type
     ctype*: bool
     #TODO result
 
+  HTMLBaseElement* = ref object of HTMLElement
+
+  HTMLAreaElement* = ref object of HTMLElement
+
 proc tostr(ftype: enum): string =
   return ($ftype).split('_')[1..^1].join("-").tolower()
 
@@ -643,6 +647,8 @@ func newHTMLElement*(document: Document, tagType: TagType, namespace = Namespace
   of TAG_SCRIPT:
     result = new(HTMLScriptElement)
     HTMLScriptElement(result).forceAsync = true
+  of TAG_BASE:
+    result = new(HTMLBaseElement)
   else:
     result = new(HTMLElement)
 
@@ -727,18 +733,19 @@ func baseUrl*(document: Document): Url =
     return document.location
   return url.get
 
-func href*(element: Element): string =
-  assert element.tagType in {TAG_A, TAG_LINK, TAG_BASE}
+func href*[T: HTMLAnchorElement|HTMLLinkElement|HTMLBaseElement](element: T): string =
   if element.attrb("href"):
     let url = parseUrl(element.attr("href"), some(element.document.location))
     if url.issome:
       return $url.get
   return ""
 
-func rel*(element: Element): string =
-  assert element.tagType in {TAG_A, TAG_LINK, TAG_AREA}
+func rel*[T: HTMLAnchorElement|HTMLLinkElement|HTMLAreaElement](element: T): string =
   return element.attr("rel")
 
+func media*[T: HTMLLinkElement|HTMLStyleElement](element: T): string =
+  return element.attr("media")
+
 func title*(document: Document): string =
   for title in document.elements(TAG_TITLE):
     return title.childTextContent.stripAndCollapse()
diff --git a/src/io/buffer.nim b/src/io/buffer.nim
index 1e0a1c29..0443f514 100644
--- a/src/io/buffer.nim
+++ b/src/io/buffer.nim
@@ -6,6 +6,9 @@ import tables
 import terminal
 import unicode
 
+import css/cascade
+import css/cssparser
+import css/mediaquery
 import css/sheet
 import css/stylednode
 import config/config
@@ -831,6 +834,19 @@ proc updateHover(buffer: Buffer) =
 
   buffer.prevnode = thisnode
 
+proc loadResource(buffer: Buffer, document: Document, elem: HTMLLinkElement) =
+  let url = parseUrl(elem.href, document.location.some)
+  if url.isSome:
+    let url = url.get
+    if url.scheme == buffer.location.scheme:
+      let media = elem.media
+      if media != "":
+        let media = parseMediaQueryList(parseListOfComponentValues(newStringStream(media)))
+        if not media.applies(): return
+      let fs = buffer.loader.doRequest(newRequest(url))
+      if fs.body != nil and fs.contenttype == "text/css":
+        elem.sheet = parseStylesheet(fs.body)
+
 proc loadResources(buffer: Buffer, document: Document) =
   var stack: seq[Element]
   if document.html != nil:
@@ -841,13 +857,7 @@ proc loadResources(buffer: Buffer, document: Document) =
     if elem.tagType == TAG_LINK:
       let elem = HTMLLinkElement(elem)
       if elem.rel == "stylesheet":
-        let url = parseUrl(elem.href, document.location.some)
-        if url.issome:
-          let url = url.get
-          if url.scheme == buffer.location.scheme:
-            let fs = buffer.loader.doRequest(newRequest(url))
-            if fs.body != nil and fs.contenttype == "text/css":
-              elem.sheet = parseStylesheet(fs.body)
+	buffer.loadResource(document, elem)
 
     for child in elem.children_rev:
       stack.add(child)